blob: b625a1b95b38aff57cd04a5c1a8c1d06e8d91a65 [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,
218 uint32_t pointerCount, const PointerProperties* pointerProperties,
Prabir Pradhan5beda762021-12-10 09:30:08 +0000219 const 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),
236 pointerCount(pointerCount) {
237 for (uint32_t i = 0; i < pointerCount; i++) {
238 this->pointerProperties[i].copyFrom(pointerProperties[i]);
239 this->pointerCoords[i].copyFrom(pointerCoords[i]);
Garfield Tane84e6f92019-08-29 17:28:41 -0700240 }
241}
242
243MotionEntry::~MotionEntry() {}
244
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500245std::string MotionEntry::getDescription() const {
Prabir Pradhan65613802023-02-22 23:36:58 +0000246 if (!IS_DEBUGGABLE_BUILD) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500247 return "MotionEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700248 }
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500249 std::string msg;
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -0500250 msg += StringPrintf("MotionEvent(deviceId=%d, eventTime=%" PRIu64
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800251 ", source=%s, 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=[",
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800256 deviceId, eventTime, inputEventSourceToString(source).c_str(), displayId,
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500257 MotionEvent::actionToString(action).c_str(), actionButton, flags, metaState,
258 buttonState, motionClassificationToString(classification), edgeFlags,
259 xPrecision, yPrecision, xCursorPosition, yCursorPosition);
Garfield Tane84e6f92019-08-29 17:28:41 -0700260
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);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500269 return msg;
Garfield Tane84e6f92019-08-29 17:28:41 -0700270}
271
Chris Yef59a2f42020-10-16 12:55:26 -0700272// --- SensorEntry ---
273
274SensorEntry::SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
275 uint32_t policyFlags, nsecs_t hwTimestamp,
276 InputDeviceSensorType sensorType, InputDeviceSensorAccuracy accuracy,
277 bool accuracyChanged, std::vector<float> values)
278 : EventEntry(id, Type::SENSOR, eventTime, policyFlags),
279 deviceId(deviceId),
280 source(source),
281 sensorType(sensorType),
282 accuracy(accuracy),
283 accuracyChanged(accuracyChanged),
284 hwTimestamp(hwTimestamp),
285 values(std::move(values)) {}
286
287SensorEntry::~SensorEntry() {}
288
289std::string SensorEntry::getDescription() const {
290 std::string msg;
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800291 msg += StringPrintf("SensorEntry(deviceId=%d, source=%s, sensorType=%s, "
Chris Yef59a2f42020-10-16 12:55:26 -0700292 "accuracy=0x%08x, hwTimestamp=%" PRId64,
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -0800293 deviceId, inputEventSourceToString(source).c_str(),
294 ftl::enum_string(sensorType).c_str(), accuracy, hwTimestamp);
Chris Yef59a2f42020-10-16 12:55:26 -0700295
Prabir Pradhan65613802023-02-22 23:36:58 +0000296 if (IS_DEBUGGABLE_BUILD) {
Chris Yef59a2f42020-10-16 12:55:26 -0700297 for (size_t i = 0; i < values.size(); i++) {
298 if (i > 0) {
299 msg += ", ";
300 }
301 msg += StringPrintf("(%.3f)", values[i]);
302 }
303 }
304 msg += StringPrintf(", policyFlags=0x%08x", policyFlags);
305 return msg;
306}
307
Garfield Tane84e6f92019-08-29 17:28:41 -0700308// --- DispatchEntry ---
309
310volatile int32_t DispatchEntry::sNextSeqAtomic;
311
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800312DispatchEntry::DispatchEntry(std::shared_ptr<EventEntry> eventEntry,
313 ftl::Flags<InputTarget::Flags> targetFlags,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700314 const ui::Transform& transform, const ui::Transform& rawTransform,
315 float globalScaleFactor)
Garfield Tane84e6f92019-08-29 17:28:41 -0700316 : seq(nextSeq()),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700317 eventEntry(std::move(eventEntry)),
Garfield Tane84e6f92019-08-29 17:28:41 -0700318 targetFlags(targetFlags),
chaviw1ff3d1e2020-07-01 15:53:47 -0700319 transform(transform),
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700320 rawTransform(rawTransform),
Garfield Tane84e6f92019-08-29 17:28:41 -0700321 globalScaleFactor(globalScaleFactor),
Garfield Tane84e6f92019-08-29 17:28:41 -0700322 deliveryTime(0),
323 resolvedAction(0),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700324 resolvedFlags(0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -0700325
326uint32_t DispatchEntry::nextSeq() {
327 // Sequence number 0 is reserved and will never be returned.
328 uint32_t seq;
329 do {
330 seq = android_atomic_inc(&sNextSeqAtomic);
331 } while (!seq);
332 return seq;
333}
334
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800335std::ostream& operator<<(std::ostream& out, const DispatchEntry& entry) {
336 out << "DispatchEntry{resolvedAction=";
337 switch (entry.eventEntry->type) {
338 case EventEntry::Type::KEY: {
339 out << KeyEvent::actionToString(entry.resolvedAction);
340 break;
341 }
342 case EventEntry::Type::MOTION: {
343 out << MotionEvent::actionToString(entry.resolvedAction);
344 break;
345 }
346 default: {
347 out << "<invalid, not a key or a motion>";
348 break;
349 }
350 }
351 std::string transform;
352 entry.transform.dump(transform, "transform");
353 out << ", resolvedFlags=" << entry.resolvedFlags
354 << ", targetFlags=" << entry.targetFlags.string() << ", transform=" << transform
355 << "} original =" << entry.eventEntry->getDescription();
356 return out;
357}
358
Garfield Tane84e6f92019-08-29 17:28:41 -0700359} // namespace android::inputdispatcher