blob: 621543a1ee3d2d3465d189c56bfe475e53261100 [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
26using android::base::StringPrintf;
27
28namespace android::inputdispatcher {
29
Siarhei Vishniakou16e4fa02023-02-16 17:48:56 -080030static const bool DEBUGGABLE =
31#if defined(__ANDROID__)
32 android::base::GetBoolProperty("ro.debuggable", false);
33#else
34 true;
35#endif
36
Gang Wange9087892020-01-07 12:17:14 -050037VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry) {
38 return {{VerifiedInputEvent::Type::KEY, entry.deviceId, entry.eventTime, entry.source,
39 entry.displayId},
40 entry.action,
Gang Wange9087892020-01-07 12:17:14 -050041 entry.flags & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -080042 entry.downTime,
Gang Wange9087892020-01-07 12:17:14 -050043 entry.keyCode,
44 entry.scanCode,
45 entry.metaState,
46 entry.repeatCount};
47}
48
Prabir Pradhanb5cb9572021-09-24 06:35:16 -070049VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry,
50 const ui::Transform& rawTransform) {
51 const vec2 rawXY = MotionEvent::calculateTransformedXY(entry.source, rawTransform,
52 entry.pointerCoords[0].getXYValue());
Gang Wange9087892020-01-07 12:17:14 -050053 const int actionMasked = entry.action & AMOTION_EVENT_ACTION_MASK;
54 return {{VerifiedInputEvent::Type::MOTION, entry.deviceId, entry.eventTime, entry.source,
55 entry.displayId},
Prabir Pradhanb5cb9572021-09-24 06:35:16 -070056 rawXY.x,
57 rawXY.y,
Gang Wange9087892020-01-07 12:17:14 -050058 actionMasked,
Gang Wange9087892020-01-07 12:17:14 -050059 entry.flags & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -080060 entry.downTime,
Gang Wange9087892020-01-07 12:17:14 -050061 entry.metaState,
62 entry.buttonState};
63}
Garfield Tane84e6f92019-08-29 17:28:41 -070064
65// --- EventEntry ---
66
Garfield Tanc51d1ba2020-01-28 13:24:04 -080067EventEntry::EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags)
68 : id(id),
Garfield Tane84e6f92019-08-29 17:28:41 -070069 type(type),
70 eventTime(eventTime),
71 policyFlags(policyFlags),
72 injectionState(nullptr),
73 dispatchInProgress(false) {}
74
75EventEntry::~EventEntry() {
76 releaseInjectionState();
77}
78
Garfield Tane84e6f92019-08-29 17:28:41 -070079void EventEntry::releaseInjectionState() {
80 if (injectionState) {
81 injectionState->release();
82 injectionState = nullptr;
83 }
84}
85
86// --- ConfigurationChangedEntry ---
87
Garfield Tanc51d1ba2020-01-28 13:24:04 -080088ConfigurationChangedEntry::ConfigurationChangedEntry(int32_t id, nsecs_t eventTime)
89 : EventEntry(id, Type::CONFIGURATION_CHANGED, eventTime, 0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070090
91ConfigurationChangedEntry::~ConfigurationChangedEntry() {}
92
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050093std::string ConfigurationChangedEntry::getDescription() const {
94 return StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070095}
96
97// --- DeviceResetEntry ---
98
Garfield Tanc51d1ba2020-01-28 13:24:04 -080099DeviceResetEntry::DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId)
100 : EventEntry(id, Type::DEVICE_RESET, eventTime, 0), deviceId(deviceId) {}
Garfield Tane84e6f92019-08-29 17:28:41 -0700101
102DeviceResetEntry::~DeviceResetEntry() {}
103
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500104std::string DeviceResetEntry::getDescription() const {
105 return StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -0700106}
107
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100108// --- FocusEntry ---
109
110// 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 -0700111FocusEntry::FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -0800112 const std::string& reason)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800113 : EventEntry(id, Type::FOCUS, eventTime, POLICY_FLAG_PASS_TO_USER),
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100114 connectionToken(connectionToken),
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700115 hasFocus(hasFocus),
116 reason(reason) {}
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100117
118FocusEntry::~FocusEntry() {}
119
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500120std::string FocusEntry::getDescription() const {
121 return StringPrintf("FocusEvent(hasFocus=%s)", hasFocus ? "true" : "false");
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100122}
123
Prabir Pradhan99987712020-11-10 18:43:05 -0800124// --- PointerCaptureChangedEntry ---
125
126// PointerCaptureChanged notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER
127// for all entries.
128PointerCaptureChangedEntry::PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime,
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000129 const PointerCaptureRequest& request)
Prabir Pradhan99987712020-11-10 18:43:05 -0800130 : EventEntry(id, Type::POINTER_CAPTURE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000131 pointerCaptureRequest(request) {}
Prabir Pradhan99987712020-11-10 18:43:05 -0800132
133PointerCaptureChangedEntry::~PointerCaptureChangedEntry() {}
134
135std::string PointerCaptureChangedEntry::getDescription() const {
136 return StringPrintf("PointerCaptureChangedEvent(pointerCaptureEnabled=%s)",
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000137 pointerCaptureRequest.enable ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800138}
139
arthurhungb89ccb02020-12-30 16:19:01 +0800140// --- DragEntry ---
141
142// Drag notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER for all entries
143DragEntry::DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting,
144 float x, float y)
145 : EventEntry(id, Type::DRAG, eventTime, POLICY_FLAG_PASS_TO_USER),
146 connectionToken(connectionToken),
147 isExiting(isExiting),
148 x(x),
149 y(y) {}
150
151DragEntry::~DragEntry() {}
152
153std::string DragEntry::getDescription() const {
154 return StringPrintf("DragEntry(isExiting=%s, x=%f, y=%f)", isExiting ? "true" : "false", x, y);
155}
156
Garfield Tane84e6f92019-08-29 17:28:41 -0700157// --- KeyEntry ---
158
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800159KeyEntry::KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700160 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
161 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
162 nsecs_t downTime)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800163 : EventEntry(id, Type::KEY, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700164 deviceId(deviceId),
165 source(source),
166 displayId(displayId),
167 action(action),
168 flags(flags),
169 keyCode(keyCode),
170 scanCode(scanCode),
171 metaState(metaState),
172 repeatCount(repeatCount),
173 downTime(downTime),
174 syntheticRepeat(false),
Michael Wright5caf55a2022-11-24 22:31:42 +0000175 interceptKeyResult(KeyEntry::InterceptKeyResult::UNKNOWN),
Garfield Tane84e6f92019-08-29 17:28:41 -0700176 interceptKeyWakeupTime(0) {}
177
178KeyEntry::~KeyEntry() {}
179
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500180std::string KeyEntry::getDescription() const {
Siarhei Vishniakou16e4fa02023-02-16 17:48:56 -0800181 if (!DEBUGGABLE) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500182 return "KeyEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700183 }
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800184 return StringPrintf("KeyEvent(deviceId=%d, eventTime=%" PRIu64 ", source=%s, displayId=%" PRId32
185 ", action=%s, "
Siarhei Vishniakou5b6c96f2021-02-25 00:21:51 -1000186 "flags=0x%08x, keyCode=%s(%d), scanCode=%d, metaState=0x%08x, "
Garfield Tane84e6f92019-08-29 17:28:41 -0700187 "repeatCount=%d), policyFlags=0x%08x",
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800188 deviceId, eventTime, inputEventSourceToString(source).c_str(), displayId,
189 KeyEvent::actionToString(action), flags, KeyEvent::getLabel(keyCode),
190 keyCode, scanCode, metaState, repeatCount, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -0700191}
192
193void KeyEntry::recycle() {
194 releaseInjectionState();
195
196 dispatchInProgress = false;
197 syntheticRepeat = false;
Michael Wright5caf55a2022-11-24 22:31:42 +0000198 interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN;
Garfield Tane84e6f92019-08-29 17:28:41 -0700199 interceptKeyWakeupTime = 0;
200}
201
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700202// --- TouchModeEntry ---
203
Antonio Kantek15beb512022-06-13 22:35:41 +0000204TouchModeEntry::TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode, int displayId)
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700205 : EventEntry(id, Type::TOUCH_MODE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
Antonio Kantek15beb512022-06-13 22:35:41 +0000206 inTouchMode(inTouchMode),
207 displayId(displayId) {}
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700208
209TouchModeEntry::~TouchModeEntry() {}
210
211std::string TouchModeEntry::getDescription() const {
212 return StringPrintf("TouchModeEvent(inTouchMode=%s)", inTouchMode ? "true" : "false");
213}
214
Garfield Tane84e6f92019-08-29 17:28:41 -0700215// --- MotionEntry ---
216
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800217MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700218 int32_t displayId, uint32_t policyFlags, int32_t action,
219 int32_t actionButton, int32_t flags, int32_t metaState,
220 int32_t buttonState, MotionClassification classification,
221 int32_t edgeFlags, float xPrecision, float yPrecision,
222 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
223 uint32_t pointerCount, const PointerProperties* pointerProperties,
Prabir Pradhan5beda762021-12-10 09:30:08 +0000224 const PointerCoords* pointerCoords)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800225 : EventEntry(id, Type::MOTION, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700226 deviceId(deviceId),
227 source(source),
228 displayId(displayId),
229 action(action),
230 actionButton(actionButton),
231 flags(flags),
232 metaState(metaState),
233 buttonState(buttonState),
234 classification(classification),
235 edgeFlags(edgeFlags),
236 xPrecision(xPrecision),
237 yPrecision(yPrecision),
238 xCursorPosition(xCursorPosition),
239 yCursorPosition(yCursorPosition),
240 downTime(downTime),
241 pointerCount(pointerCount) {
242 for (uint32_t i = 0; i < pointerCount; i++) {
243 this->pointerProperties[i].copyFrom(pointerProperties[i]);
244 this->pointerCoords[i].copyFrom(pointerCoords[i]);
Garfield Tane84e6f92019-08-29 17:28:41 -0700245 }
246}
247
248MotionEntry::~MotionEntry() {}
249
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500250std::string MotionEntry::getDescription() const {
Siarhei Vishniakou16e4fa02023-02-16 17:48:56 -0800251 if (!DEBUGGABLE) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500252 return "MotionEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700253 }
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500254 std::string msg;
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -0500255 msg += StringPrintf("MotionEvent(deviceId=%d, eventTime=%" PRIu64
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800256 ", source=%s, displayId=%" PRId32
Garfield Tane84e6f92019-08-29 17:28:41 -0700257 ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, "
258 "buttonState=0x%08x, "
259 "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
260 "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[",
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800261 deviceId, eventTime, inputEventSourceToString(source).c_str(), displayId,
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500262 MotionEvent::actionToString(action).c_str(), actionButton, flags, metaState,
263 buttonState, motionClassificationToString(classification), edgeFlags,
264 xPrecision, yPrecision, xCursorPosition, yCursorPosition);
Garfield Tane84e6f92019-08-29 17:28:41 -0700265
266 for (uint32_t i = 0; i < pointerCount; i++) {
267 if (i) {
268 msg += ", ";
269 }
270 msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
271 pointerCoords[i].getY());
272 }
273 msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500274 return msg;
Garfield Tane84e6f92019-08-29 17:28:41 -0700275}
276
Chris Yef59a2f42020-10-16 12:55:26 -0700277// --- SensorEntry ---
278
279SensorEntry::SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
280 uint32_t policyFlags, nsecs_t hwTimestamp,
281 InputDeviceSensorType sensorType, InputDeviceSensorAccuracy accuracy,
282 bool accuracyChanged, std::vector<float> values)
283 : EventEntry(id, Type::SENSOR, eventTime, policyFlags),
284 deviceId(deviceId),
285 source(source),
286 sensorType(sensorType),
287 accuracy(accuracy),
288 accuracyChanged(accuracyChanged),
289 hwTimestamp(hwTimestamp),
290 values(std::move(values)) {}
291
292SensorEntry::~SensorEntry() {}
293
294std::string SensorEntry::getDescription() const {
295 std::string msg;
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800296 msg += StringPrintf("SensorEntry(deviceId=%d, source=%s, sensorType=%s, "
Chris Yef59a2f42020-10-16 12:55:26 -0700297 "accuracy=0x%08x, hwTimestamp=%" PRId64,
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -0800298 deviceId, inputEventSourceToString(source).c_str(),
299 ftl::enum_string(sensorType).c_str(), accuracy, hwTimestamp);
Chris Yef59a2f42020-10-16 12:55:26 -0700300
Siarhei Vishniakou16e4fa02023-02-16 17:48:56 -0800301 if (DEBUGGABLE) {
Chris Yef59a2f42020-10-16 12:55:26 -0700302 for (size_t i = 0; i < values.size(); i++) {
303 if (i > 0) {
304 msg += ", ";
305 }
306 msg += StringPrintf("(%.3f)", values[i]);
307 }
308 }
309 msg += StringPrintf(", policyFlags=0x%08x", policyFlags);
310 return msg;
311}
312
Garfield Tane84e6f92019-08-29 17:28:41 -0700313// --- DispatchEntry ---
314
315volatile int32_t DispatchEntry::sNextSeqAtomic;
316
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800317DispatchEntry::DispatchEntry(std::shared_ptr<EventEntry> eventEntry,
318 ftl::Flags<InputTarget::Flags> targetFlags,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700319 const ui::Transform& transform, const ui::Transform& rawTransform,
320 float globalScaleFactor)
Garfield Tane84e6f92019-08-29 17:28:41 -0700321 : seq(nextSeq()),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700322 eventEntry(std::move(eventEntry)),
Garfield Tane84e6f92019-08-29 17:28:41 -0700323 targetFlags(targetFlags),
chaviw1ff3d1e2020-07-01 15:53:47 -0700324 transform(transform),
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700325 rawTransform(rawTransform),
Garfield Tane84e6f92019-08-29 17:28:41 -0700326 globalScaleFactor(globalScaleFactor),
Garfield Tane84e6f92019-08-29 17:28:41 -0700327 deliveryTime(0),
328 resolvedAction(0),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700329 resolvedFlags(0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -0700330
331uint32_t DispatchEntry::nextSeq() {
332 // Sequence number 0 is reserved and will never be returned.
333 uint32_t seq;
334 do {
335 seq = android_atomic_inc(&sNextSeqAtomic);
336 } while (!seq);
337 return seq;
338}
339
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800340std::ostream& operator<<(std::ostream& out, const DispatchEntry& entry) {
341 out << "DispatchEntry{resolvedAction=";
342 switch (entry.eventEntry->type) {
343 case EventEntry::Type::KEY: {
344 out << KeyEvent::actionToString(entry.resolvedAction);
345 break;
346 }
347 case EventEntry::Type::MOTION: {
348 out << MotionEvent::actionToString(entry.resolvedAction);
349 break;
350 }
351 default: {
352 out << "<invalid, not a key or a motion>";
353 break;
354 }
355 }
356 std::string transform;
357 entry.transform.dump(transform, "transform");
358 out << ", resolvedFlags=" << entry.resolvedFlags
359 << ", targetFlags=" << entry.targetFlags.string() << ", transform=" << transform
360 << "} original =" << entry.eventEntry->getDescription();
361 return out;
362}
363
Garfield Tane84e6f92019-08-29 17:28:41 -0700364} // namespace android::inputdispatcher