blob: 30e68024f9049d9d699f77a0ca297568750d7b15 [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
Prabir Pradhan65613802023-02-22 23:36:58 +000017#define LOG_TAG "InputDispatcher"
18
Garfield Tane84e6f92019-08-29 17:28:41 -070019#include "Entry.h"
20
21#include "Connection.h"
Prabir Pradhan65613802023-02-22 23:36:58 +000022#include "DebugConfig.h"
Garfield Tane84e6f92019-08-29 17:28:41 -070023
24#include <android-base/stringprintf.h>
25#include <cutils/atomic.h>
26#include <inttypes.h>
27
28using android::base::StringPrintf;
29
30namespace android::inputdispatcher {
31
Gang Wange9087892020-01-07 12:17:14 -050032VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry) {
33 return {{VerifiedInputEvent::Type::KEY, entry.deviceId, entry.eventTime, entry.source,
34 entry.displayId},
35 entry.action,
Gang Wange9087892020-01-07 12:17:14 -050036 entry.flags & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -080037 entry.downTime,
Gang Wange9087892020-01-07 12:17:14 -050038 entry.keyCode,
39 entry.scanCode,
40 entry.metaState,
41 entry.repeatCount};
42}
43
Prabir Pradhanb5cb9572021-09-24 06:35:16 -070044VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry,
45 const ui::Transform& rawTransform) {
46 const vec2 rawXY = MotionEvent::calculateTransformedXY(entry.source, rawTransform,
47 entry.pointerCoords[0].getXYValue());
Gang Wange9087892020-01-07 12:17:14 -050048 const int actionMasked = entry.action & AMOTION_EVENT_ACTION_MASK;
49 return {{VerifiedInputEvent::Type::MOTION, entry.deviceId, entry.eventTime, entry.source,
50 entry.displayId},
Prabir Pradhanb5cb9572021-09-24 06:35:16 -070051 rawXY.x,
52 rawXY.y,
Gang Wange9087892020-01-07 12:17:14 -050053 actionMasked,
Gang Wange9087892020-01-07 12:17:14 -050054 entry.flags & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -080055 entry.downTime,
Gang Wange9087892020-01-07 12:17:14 -050056 entry.metaState,
57 entry.buttonState};
58}
Garfield Tane84e6f92019-08-29 17:28:41 -070059
60// --- EventEntry ---
61
Garfield Tanc51d1ba2020-01-28 13:24:04 -080062EventEntry::EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags)
63 : id(id),
Garfield Tane84e6f92019-08-29 17:28:41 -070064 type(type),
65 eventTime(eventTime),
66 policyFlags(policyFlags),
67 injectionState(nullptr),
68 dispatchInProgress(false) {}
69
70EventEntry::~EventEntry() {
71 releaseInjectionState();
72}
73
Garfield Tane84e6f92019-08-29 17:28:41 -070074void EventEntry::releaseInjectionState() {
75 if (injectionState) {
76 injectionState->release();
77 injectionState = nullptr;
78 }
79}
80
81// --- ConfigurationChangedEntry ---
82
Garfield Tanc51d1ba2020-01-28 13:24:04 -080083ConfigurationChangedEntry::ConfigurationChangedEntry(int32_t id, nsecs_t eventTime)
84 : EventEntry(id, Type::CONFIGURATION_CHANGED, eventTime, 0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070085
86ConfigurationChangedEntry::~ConfigurationChangedEntry() {}
87
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050088std::string ConfigurationChangedEntry::getDescription() const {
89 return StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070090}
91
92// --- DeviceResetEntry ---
93
Garfield Tanc51d1ba2020-01-28 13:24:04 -080094DeviceResetEntry::DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId)
95 : EventEntry(id, Type::DEVICE_RESET, eventTime, 0), deviceId(deviceId) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070096
97DeviceResetEntry::~DeviceResetEntry() {}
98
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050099std::string DeviceResetEntry::getDescription() const {
100 return StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -0700101}
102
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100103// --- FocusEntry ---
104
105// Focus notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER for all entries
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700106FocusEntry::FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -0800107 const std::string& reason)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800108 : EventEntry(id, Type::FOCUS, eventTime, POLICY_FLAG_PASS_TO_USER),
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100109 connectionToken(connectionToken),
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700110 hasFocus(hasFocus),
111 reason(reason) {}
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100112
113FocusEntry::~FocusEntry() {}
114
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500115std::string FocusEntry::getDescription() const {
116 return StringPrintf("FocusEvent(hasFocus=%s)", hasFocus ? "true" : "false");
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100117}
118
Prabir Pradhan99987712020-11-10 18:43:05 -0800119// --- PointerCaptureChangedEntry ---
120
121// PointerCaptureChanged notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER
122// for all entries.
123PointerCaptureChangedEntry::PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime,
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000124 const PointerCaptureRequest& request)
Prabir Pradhan99987712020-11-10 18:43:05 -0800125 : EventEntry(id, Type::POINTER_CAPTURE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000126 pointerCaptureRequest(request) {}
Prabir Pradhan99987712020-11-10 18:43:05 -0800127
128PointerCaptureChangedEntry::~PointerCaptureChangedEntry() {}
129
130std::string PointerCaptureChangedEntry::getDescription() const {
131 return StringPrintf("PointerCaptureChangedEvent(pointerCaptureEnabled=%s)",
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000132 pointerCaptureRequest.enable ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800133}
134
arthurhungb89ccb02020-12-30 16:19:01 +0800135// --- DragEntry ---
136
137// Drag notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER for all entries
138DragEntry::DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting,
139 float x, float y)
140 : EventEntry(id, Type::DRAG, eventTime, POLICY_FLAG_PASS_TO_USER),
141 connectionToken(connectionToken),
142 isExiting(isExiting),
143 x(x),
144 y(y) {}
145
146DragEntry::~DragEntry() {}
147
148std::string DragEntry::getDescription() const {
149 return StringPrintf("DragEntry(isExiting=%s, x=%f, y=%f)", isExiting ? "true" : "false", x, y);
150}
151
Garfield Tane84e6f92019-08-29 17:28:41 -0700152// --- KeyEntry ---
153
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800154KeyEntry::KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700155 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
156 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
157 nsecs_t downTime)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800158 : EventEntry(id, Type::KEY, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700159 deviceId(deviceId),
160 source(source),
161 displayId(displayId),
162 action(action),
163 flags(flags),
164 keyCode(keyCode),
165 scanCode(scanCode),
166 metaState(metaState),
167 repeatCount(repeatCount),
168 downTime(downTime),
169 syntheticRepeat(false),
Michael Wright5caf55a2022-11-24 22:31:42 +0000170 interceptKeyResult(KeyEntry::InterceptKeyResult::UNKNOWN),
Garfield Tane84e6f92019-08-29 17:28:41 -0700171 interceptKeyWakeupTime(0) {}
172
173KeyEntry::~KeyEntry() {}
174
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500175std::string KeyEntry::getDescription() const {
Prabir Pradhan65613802023-02-22 23:36:58 +0000176 if (!IS_DEBUGGABLE_BUILD) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500177 return "KeyEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700178 }
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800179 return StringPrintf("KeyEvent(deviceId=%d, eventTime=%" PRIu64 ", source=%s, displayId=%" PRId32
180 ", action=%s, "
Siarhei Vishniakou5b6c96f2021-02-25 00:21:51 -1000181 "flags=0x%08x, keyCode=%s(%d), scanCode=%d, metaState=0x%08x, "
Garfield Tane84e6f92019-08-29 17:28:41 -0700182 "repeatCount=%d), policyFlags=0x%08x",
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800183 deviceId, eventTime, inputEventSourceToString(source).c_str(), displayId,
184 KeyEvent::actionToString(action), flags, KeyEvent::getLabel(keyCode),
185 keyCode, scanCode, metaState, repeatCount, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -0700186}
187
188void KeyEntry::recycle() {
189 releaseInjectionState();
190
191 dispatchInProgress = false;
192 syntheticRepeat = false;
Michael Wright5caf55a2022-11-24 22:31:42 +0000193 interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN;
Garfield Tane84e6f92019-08-29 17:28:41 -0700194 interceptKeyWakeupTime = 0;
195}
196
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700197// --- TouchModeEntry ---
198
Antonio Kantek15beb512022-06-13 22:35:41 +0000199TouchModeEntry::TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode, int displayId)
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700200 : EventEntry(id, Type::TOUCH_MODE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
Antonio Kantek15beb512022-06-13 22:35:41 +0000201 inTouchMode(inTouchMode),
202 displayId(displayId) {}
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700203
204TouchModeEntry::~TouchModeEntry() {}
205
206std::string TouchModeEntry::getDescription() const {
207 return StringPrintf("TouchModeEvent(inTouchMode=%s)", inTouchMode ? "true" : "false");
208}
209
Garfield Tane84e6f92019-08-29 17:28:41 -0700210// --- MotionEntry ---
211
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800212MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700213 int32_t displayId, uint32_t policyFlags, int32_t action,
214 int32_t actionButton, int32_t flags, int32_t metaState,
215 int32_t buttonState, MotionClassification classification,
216 int32_t edgeFlags, float xPrecision, float yPrecision,
217 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700218 const std::vector<PointerProperties>& pointerProperties,
219 const std::vector<PointerCoords>& pointerCoords)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800220 : EventEntry(id, Type::MOTION, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700221 deviceId(deviceId),
222 source(source),
223 displayId(displayId),
224 action(action),
225 actionButton(actionButton),
226 flags(flags),
227 metaState(metaState),
228 buttonState(buttonState),
229 classification(classification),
230 edgeFlags(edgeFlags),
231 xPrecision(xPrecision),
232 yPrecision(yPrecision),
233 xCursorPosition(xCursorPosition),
234 yCursorPosition(yCursorPosition),
235 downTime(downTime),
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700236 pointerProperties(pointerProperties),
237 pointerCoords(pointerCoords) {}
Garfield Tane84e6f92019-08-29 17:28:41 -0700238
239MotionEntry::~MotionEntry() {}
240
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500241std::string MotionEntry::getDescription() const {
Prabir Pradhan65613802023-02-22 23:36:58 +0000242 if (!IS_DEBUGGABLE_BUILD) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500243 return "MotionEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700244 }
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500245 std::string msg;
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -0500246 msg += StringPrintf("MotionEvent(deviceId=%d, eventTime=%" PRIu64
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800247 ", source=%s, displayId=%" PRId32
Garfield Tane84e6f92019-08-29 17:28:41 -0700248 ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, "
249 "buttonState=0x%08x, "
250 "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
251 "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[",
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800252 deviceId, eventTime, inputEventSourceToString(source).c_str(), displayId,
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500253 MotionEvent::actionToString(action).c_str(), actionButton, flags, metaState,
254 buttonState, motionClassificationToString(classification), edgeFlags,
255 xPrecision, yPrecision, xCursorPosition, yCursorPosition);
Garfield Tane84e6f92019-08-29 17:28:41 -0700256
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700257 for (uint32_t i = 0; i < getPointerCount(); i++) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700258 if (i) {
259 msg += ", ";
260 }
261 msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
262 pointerCoords[i].getY());
263 }
264 msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500265 return msg;
Garfield Tane84e6f92019-08-29 17:28:41 -0700266}
267
Siarhei Vishniakou72945a02023-09-18 18:30:25 -0700268std::ostream& operator<<(std::ostream& out, const MotionEntry& motionEntry) {
269 out << motionEntry.getDescription();
270 return out;
271}
272
Chris Yef59a2f42020-10-16 12:55:26 -0700273// --- SensorEntry ---
274
275SensorEntry::SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
276 uint32_t policyFlags, nsecs_t hwTimestamp,
277 InputDeviceSensorType sensorType, InputDeviceSensorAccuracy accuracy,
278 bool accuracyChanged, std::vector<float> values)
279 : EventEntry(id, Type::SENSOR, eventTime, policyFlags),
280 deviceId(deviceId),
281 source(source),
282 sensorType(sensorType),
283 accuracy(accuracy),
284 accuracyChanged(accuracyChanged),
285 hwTimestamp(hwTimestamp),
286 values(std::move(values)) {}
287
288SensorEntry::~SensorEntry() {}
289
290std::string SensorEntry::getDescription() const {
291 std::string msg;
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800292 msg += StringPrintf("SensorEntry(deviceId=%d, source=%s, sensorType=%s, "
Chris Yef59a2f42020-10-16 12:55:26 -0700293 "accuracy=0x%08x, hwTimestamp=%" PRId64,
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -0800294 deviceId, inputEventSourceToString(source).c_str(),
295 ftl::enum_string(sensorType).c_str(), accuracy, hwTimestamp);
Chris Yef59a2f42020-10-16 12:55:26 -0700296
Prabir Pradhan65613802023-02-22 23:36:58 +0000297 if (IS_DEBUGGABLE_BUILD) {
Chris Yef59a2f42020-10-16 12:55:26 -0700298 for (size_t i = 0; i < values.size(); i++) {
299 if (i > 0) {
300 msg += ", ";
301 }
302 msg += StringPrintf("(%.3f)", values[i]);
303 }
304 }
305 msg += StringPrintf(", policyFlags=0x%08x", policyFlags);
306 return msg;
307}
308
Garfield Tane84e6f92019-08-29 17:28:41 -0700309// --- DispatchEntry ---
310
311volatile int32_t DispatchEntry::sNextSeqAtomic;
312
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800313DispatchEntry::DispatchEntry(std::shared_ptr<EventEntry> eventEntry,
314 ftl::Flags<InputTarget::Flags> targetFlags,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700315 const ui::Transform& transform, const ui::Transform& rawTransform,
316 float globalScaleFactor)
Garfield Tane84e6f92019-08-29 17:28:41 -0700317 : seq(nextSeq()),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700318 eventEntry(std::move(eventEntry)),
Garfield Tane84e6f92019-08-29 17:28:41 -0700319 targetFlags(targetFlags),
chaviw1ff3d1e2020-07-01 15:53:47 -0700320 transform(transform),
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700321 rawTransform(rawTransform),
Garfield Tane84e6f92019-08-29 17:28:41 -0700322 globalScaleFactor(globalScaleFactor),
Garfield Tane84e6f92019-08-29 17:28:41 -0700323 deliveryTime(0),
324 resolvedAction(0),
Siarhei Vishniakou2af5b032023-07-10 18:52:17 -0700325 resolvedFlags(0) {
326 switch (this->eventEntry->type) {
327 case EventEntry::Type::KEY: {
328 const KeyEntry& keyEntry = static_cast<KeyEntry&>(*this->eventEntry);
329 resolvedEventId = keyEntry.id;
330 resolvedAction = keyEntry.action;
331 resolvedFlags = keyEntry.flags;
332
333 break;
334 }
335 case EventEntry::Type::MOTION: {
336 const MotionEntry& motionEntry = static_cast<MotionEntry&>(*this->eventEntry);
337 resolvedEventId = motionEntry.id;
338 resolvedAction = motionEntry.action;
339 resolvedFlags = motionEntry.flags;
340 break;
341 }
342 default: {
343 break;
344 }
345 }
346}
Garfield Tane84e6f92019-08-29 17:28:41 -0700347
348uint32_t DispatchEntry::nextSeq() {
349 // Sequence number 0 is reserved and will never be returned.
350 uint32_t seq;
351 do {
352 seq = android_atomic_inc(&sNextSeqAtomic);
353 } while (!seq);
354 return seq;
355}
356
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800357std::ostream& operator<<(std::ostream& out, const DispatchEntry& entry) {
358 out << "DispatchEntry{resolvedAction=";
359 switch (entry.eventEntry->type) {
360 case EventEntry::Type::KEY: {
361 out << KeyEvent::actionToString(entry.resolvedAction);
362 break;
363 }
364 case EventEntry::Type::MOTION: {
365 out << MotionEvent::actionToString(entry.resolvedAction);
366 break;
367 }
368 default: {
369 out << "<invalid, not a key or a motion>";
370 break;
371 }
372 }
373 std::string transform;
374 entry.transform.dump(transform, "transform");
375 out << ", resolvedFlags=" << entry.resolvedFlags
376 << ", targetFlags=" << entry.targetFlags.string() << ", transform=" << transform
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -0700377 << "} original: " << entry.eventEntry->getDescription();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800378 return out;
379}
380
Garfield Tane84e6f92019-08-29 17:28:41 -0700381} // namespace android::inputdispatcher