blob: 21c8ae165d9c61f25d9031e1dd12916ed62a603a [file] [log] [blame]
Garfield Tane84e6f92019-08-29 17:28:41 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Entry.h"
18
19#include "Connection.h"
20
Ashwini Orugantid399a522019-10-10 11:16:45 -070021#include <android-base/properties.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070022#include <android-base/stringprintf.h>
23#include <cutils/atomic.h>
24#include <inttypes.h>
25
Ashwini Orugantid399a522019-10-10 11:16:45 -070026using android::base::GetBoolProperty;
Garfield Tane84e6f92019-08-29 17:28:41 -070027using android::base::StringPrintf;
28
29namespace android::inputdispatcher {
30
31static std::string motionActionToString(int32_t action) {
32 // Convert MotionEvent action to string
33 switch (action & AMOTION_EVENT_ACTION_MASK) {
34 case AMOTION_EVENT_ACTION_DOWN:
35 return "DOWN";
36 case AMOTION_EVENT_ACTION_MOVE:
37 return "MOVE";
38 case AMOTION_EVENT_ACTION_UP:
39 return "UP";
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070040 case AMOTION_EVENT_ACTION_CANCEL:
41 return "CANCEL";
Garfield Tane84e6f92019-08-29 17:28:41 -070042 case AMOTION_EVENT_ACTION_POINTER_DOWN:
43 return "POINTER_DOWN";
44 case AMOTION_EVENT_ACTION_POINTER_UP:
45 return "POINTER_UP";
46 }
47 return StringPrintf("%" PRId32, action);
48}
49
50static std::string keyActionToString(int32_t action) {
51 // Convert KeyEvent action to string
52 switch (action) {
53 case AKEY_EVENT_ACTION_DOWN:
54 return "DOWN";
55 case AKEY_EVENT_ACTION_UP:
56 return "UP";
57 case AKEY_EVENT_ACTION_MULTIPLE:
58 return "MULTIPLE";
59 }
60 return StringPrintf("%" PRId32, action);
61}
Siarhei Vishniakoucd899e82020-05-08 09:24:29 -070062
Gang Wange9087892020-01-07 12:17:14 -050063VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry) {
64 return {{VerifiedInputEvent::Type::KEY, entry.deviceId, entry.eventTime, entry.source,
65 entry.displayId},
66 entry.action,
67 entry.downTime,
68 entry.flags & VERIFIED_KEY_EVENT_FLAGS,
69 entry.keyCode,
70 entry.scanCode,
71 entry.metaState,
72 entry.repeatCount};
73}
74
75VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry) {
76 const float rawX = entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
77 const float rawY = entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
78 const int actionMasked = entry.action & AMOTION_EVENT_ACTION_MASK;
79 return {{VerifiedInputEvent::Type::MOTION, entry.deviceId, entry.eventTime, entry.source,
80 entry.displayId},
81 rawX,
82 rawY,
83 actionMasked,
84 entry.downTime,
85 entry.flags & VERIFIED_MOTION_EVENT_FLAGS,
86 entry.metaState,
87 entry.buttonState};
88}
Garfield Tane84e6f92019-08-29 17:28:41 -070089
90// --- EventEntry ---
91
Garfield Tanc51d1ba2020-01-28 13:24:04 -080092EventEntry::EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags)
93 : id(id),
Garfield Tane84e6f92019-08-29 17:28:41 -070094 refCount(1),
95 type(type),
96 eventTime(eventTime),
97 policyFlags(policyFlags),
98 injectionState(nullptr),
99 dispatchInProgress(false) {}
100
101EventEntry::~EventEntry() {
102 releaseInjectionState();
103}
104
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -0700105std::string EventEntry::getDescription() const {
106 std::string result;
107 appendDescription(result);
108 return result;
109}
110
Garfield Tane84e6f92019-08-29 17:28:41 -0700111void EventEntry::release() {
112 refCount -= 1;
113 if (refCount == 0) {
114 delete this;
115 } else {
116 ALOG_ASSERT(refCount > 0);
117 }
118}
119
120void EventEntry::releaseInjectionState() {
121 if (injectionState) {
122 injectionState->release();
123 injectionState = nullptr;
124 }
125}
126
127// --- ConfigurationChangedEntry ---
128
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800129ConfigurationChangedEntry::ConfigurationChangedEntry(int32_t id, nsecs_t eventTime)
130 : EventEntry(id, Type::CONFIGURATION_CHANGED, eventTime, 0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -0700131
132ConfigurationChangedEntry::~ConfigurationChangedEntry() {}
133
134void ConfigurationChangedEntry::appendDescription(std::string& msg) const {
135 msg += StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags);
136}
137
138// --- DeviceResetEntry ---
139
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800140DeviceResetEntry::DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId)
141 : EventEntry(id, Type::DEVICE_RESET, eventTime, 0), deviceId(deviceId) {}
Garfield Tane84e6f92019-08-29 17:28:41 -0700142
143DeviceResetEntry::~DeviceResetEntry() {}
144
145void DeviceResetEntry::appendDescription(std::string& msg) const {
146 msg += StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags);
147}
148
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100149// --- FocusEntry ---
150
151// Focus notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER for all entries
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800152FocusEntry::FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus)
153 : EventEntry(id, Type::FOCUS, eventTime, POLICY_FLAG_PASS_TO_USER),
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100154 connectionToken(connectionToken),
155 hasFocus(hasFocus) {}
156
157FocusEntry::~FocusEntry() {}
158
159void FocusEntry::appendDescription(std::string& msg) const {
160 msg += StringPrintf("FocusEvent(hasFocus=%s)", hasFocus ? "true" : "false");
161}
162
Garfield Tane84e6f92019-08-29 17:28:41 -0700163// --- KeyEntry ---
164
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800165KeyEntry::KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700166 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
167 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
168 nsecs_t downTime)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800169 : EventEntry(id, Type::KEY, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700170 deviceId(deviceId),
171 source(source),
172 displayId(displayId),
173 action(action),
174 flags(flags),
175 keyCode(keyCode),
176 scanCode(scanCode),
177 metaState(metaState),
178 repeatCount(repeatCount),
179 downTime(downTime),
180 syntheticRepeat(false),
181 interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN),
182 interceptKeyWakeupTime(0) {}
183
184KeyEntry::~KeyEntry() {}
185
186void KeyEntry::appendDescription(std::string& msg) const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700187 msg += StringPrintf("KeyEvent");
188 if (!GetBoolProperty("ro.debuggable", false)) {
189 return;
190 }
191 msg += StringPrintf("(deviceId=%d, source=0x%08x, displayId=%" PRId32 ", action=%s, "
Garfield Tane84e6f92019-08-29 17:28:41 -0700192 "flags=0x%08x, keyCode=%d, scanCode=%d, metaState=0x%08x, "
193 "repeatCount=%d), policyFlags=0x%08x",
194 deviceId, source, displayId, keyActionToString(action).c_str(), flags,
195 keyCode, scanCode, metaState, repeatCount, policyFlags);
196}
197
198void KeyEntry::recycle() {
199 releaseInjectionState();
200
201 dispatchInProgress = false;
202 syntheticRepeat = false;
203 interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
204 interceptKeyWakeupTime = 0;
205}
206
207// --- MotionEntry ---
208
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800209MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700210 int32_t displayId, uint32_t policyFlags, int32_t action,
211 int32_t actionButton, int32_t flags, int32_t metaState,
212 int32_t buttonState, MotionClassification classification,
213 int32_t edgeFlags, float xPrecision, float yPrecision,
214 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
215 uint32_t pointerCount, const PointerProperties* pointerProperties,
216 const PointerCoords* pointerCoords, float xOffset, float yOffset)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800217 : EventEntry(id, Type::MOTION, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700218 eventTime(eventTime),
219 deviceId(deviceId),
220 source(source),
221 displayId(displayId),
222 action(action),
223 actionButton(actionButton),
224 flags(flags),
225 metaState(metaState),
226 buttonState(buttonState),
227 classification(classification),
228 edgeFlags(edgeFlags),
229 xPrecision(xPrecision),
230 yPrecision(yPrecision),
231 xCursorPosition(xCursorPosition),
232 yCursorPosition(yCursorPosition),
233 downTime(downTime),
234 pointerCount(pointerCount) {
235 for (uint32_t i = 0; i < pointerCount; i++) {
236 this->pointerProperties[i].copyFrom(pointerProperties[i]);
237 this->pointerCoords[i].copyFrom(pointerCoords[i]);
238 if (xOffset || yOffset) {
239 this->pointerCoords[i].applyOffset(xOffset, yOffset);
240 }
241 }
242}
243
244MotionEntry::~MotionEntry() {}
245
246void MotionEntry::appendDescription(std::string& msg) const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700247 msg += StringPrintf("MotionEvent");
248 if (!GetBoolProperty("ro.debuggable", false)) {
249 return;
250 }
251 msg += StringPrintf("(deviceId=%d, source=0x%08x, displayId=%" PRId32
Garfield Tane84e6f92019-08-29 17:28:41 -0700252 ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, "
253 "buttonState=0x%08x, "
254 "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
255 "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[",
256 deviceId, source, displayId, motionActionToString(action).c_str(),
257 actionButton, flags, metaState, buttonState,
258 motionClassificationToString(classification), edgeFlags, xPrecision,
259 yPrecision, xCursorPosition, yCursorPosition);
260
261 for (uint32_t i = 0; i < pointerCount; i++) {
262 if (i) {
263 msg += ", ";
264 }
265 msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
266 pointerCoords[i].getY());
267 }
268 msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
269}
270
271// --- DispatchEntry ---
272
273volatile int32_t DispatchEntry::sNextSeqAtomic;
274
275DispatchEntry::DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, float xOffset,
276 float yOffset, float globalScaleFactor, float windowXScale,
277 float windowYScale)
278 : seq(nextSeq()),
279 eventEntry(eventEntry),
280 targetFlags(targetFlags),
281 xOffset(xOffset),
282 yOffset(yOffset),
283 globalScaleFactor(globalScaleFactor),
284 windowXScale(windowXScale),
285 windowYScale(windowYScale),
286 deliveryTime(0),
287 resolvedAction(0),
288 resolvedFlags(0) {
289 eventEntry->refCount += 1;
290}
291
292DispatchEntry::~DispatchEntry() {
293 eventEntry->release();
294}
295
296uint32_t DispatchEntry::nextSeq() {
297 // Sequence number 0 is reserved and will never be returned.
298 uint32_t seq;
299 do {
300 seq = android_atomic_inc(&sNextSeqAtomic);
301 } while (!seq);
302 return seq;
303}
304
305// --- CommandEntry ---
306
307CommandEntry::CommandEntry(Command command)
308 : command(command),
309 eventTime(0),
310 keyEntry(nullptr),
311 userActivityEventType(0),
312 seq(0),
313 handled(false) {}
314
315CommandEntry::~CommandEntry() {}
316
317} // namespace android::inputdispatcher