blob: ce7c882f7d37fd0d32532c2697835b07b8eb9c68 [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
Gang Wange9087892020-01-07 12:17:14 -050031VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry) {
32 return {{VerifiedInputEvent::Type::KEY, entry.deviceId, entry.eventTime, entry.source,
33 entry.displayId},
34 entry.action,
Gang Wange9087892020-01-07 12:17:14 -050035 entry.flags & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -080036 entry.downTime,
Gang Wange9087892020-01-07 12:17:14 -050037 entry.keyCode,
38 entry.scanCode,
39 entry.metaState,
40 entry.repeatCount};
41}
42
Prabir Pradhanb5cb9572021-09-24 06:35:16 -070043VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry,
44 const ui::Transform& rawTransform) {
45 const vec2 rawXY = MotionEvent::calculateTransformedXY(entry.source, rawTransform,
46 entry.pointerCoords[0].getXYValue());
Gang Wange9087892020-01-07 12:17:14 -050047 const int actionMasked = entry.action & AMOTION_EVENT_ACTION_MASK;
48 return {{VerifiedInputEvent::Type::MOTION, entry.deviceId, entry.eventTime, entry.source,
49 entry.displayId},
Prabir Pradhanb5cb9572021-09-24 06:35:16 -070050 rawXY.x,
51 rawXY.y,
Gang Wange9087892020-01-07 12:17:14 -050052 actionMasked,
Gang Wange9087892020-01-07 12:17:14 -050053 entry.flags & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -080054 entry.downTime,
Gang Wange9087892020-01-07 12:17:14 -050055 entry.metaState,
56 entry.buttonState};
57}
Garfield Tane84e6f92019-08-29 17:28:41 -070058
59// --- EventEntry ---
60
Garfield Tanc51d1ba2020-01-28 13:24:04 -080061EventEntry::EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags)
62 : id(id),
Garfield Tane84e6f92019-08-29 17:28:41 -070063 type(type),
64 eventTime(eventTime),
65 policyFlags(policyFlags),
66 injectionState(nullptr),
67 dispatchInProgress(false) {}
68
69EventEntry::~EventEntry() {
70 releaseInjectionState();
71}
72
Garfield Tane84e6f92019-08-29 17:28:41 -070073void EventEntry::releaseInjectionState() {
74 if (injectionState) {
75 injectionState->release();
76 injectionState = nullptr;
77 }
78}
79
80// --- ConfigurationChangedEntry ---
81
Garfield Tanc51d1ba2020-01-28 13:24:04 -080082ConfigurationChangedEntry::ConfigurationChangedEntry(int32_t id, nsecs_t eventTime)
83 : EventEntry(id, Type::CONFIGURATION_CHANGED, eventTime, 0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070084
85ConfigurationChangedEntry::~ConfigurationChangedEntry() {}
86
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050087std::string ConfigurationChangedEntry::getDescription() const {
88 return StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070089}
90
91// --- DeviceResetEntry ---
92
Garfield Tanc51d1ba2020-01-28 13:24:04 -080093DeviceResetEntry::DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId)
94 : EventEntry(id, Type::DEVICE_RESET, eventTime, 0), deviceId(deviceId) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070095
96DeviceResetEntry::~DeviceResetEntry() {}
97
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050098std::string DeviceResetEntry::getDescription() const {
99 return StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -0700100}
101
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100102// --- FocusEntry ---
103
104// 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 -0700105FocusEntry::FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -0800106 const std::string& reason)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800107 : EventEntry(id, Type::FOCUS, eventTime, POLICY_FLAG_PASS_TO_USER),
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100108 connectionToken(connectionToken),
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700109 hasFocus(hasFocus),
110 reason(reason) {}
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100111
112FocusEntry::~FocusEntry() {}
113
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500114std::string FocusEntry::getDescription() const {
115 return StringPrintf("FocusEvent(hasFocus=%s)", hasFocus ? "true" : "false");
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100116}
117
Prabir Pradhan99987712020-11-10 18:43:05 -0800118// --- PointerCaptureChangedEntry ---
119
120// PointerCaptureChanged notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER
121// for all entries.
122PointerCaptureChangedEntry::PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime,
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000123 const PointerCaptureRequest& request)
Prabir Pradhan99987712020-11-10 18:43:05 -0800124 : EventEntry(id, Type::POINTER_CAPTURE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000125 pointerCaptureRequest(request) {}
Prabir Pradhan99987712020-11-10 18:43:05 -0800126
127PointerCaptureChangedEntry::~PointerCaptureChangedEntry() {}
128
129std::string PointerCaptureChangedEntry::getDescription() const {
130 return StringPrintf("PointerCaptureChangedEvent(pointerCaptureEnabled=%s)",
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000131 pointerCaptureRequest.enable ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800132}
133
arthurhungb89ccb02020-12-30 16:19:01 +0800134// --- DragEntry ---
135
136// Drag notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER for all entries
137DragEntry::DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting,
138 float x, float y)
139 : EventEntry(id, Type::DRAG, eventTime, POLICY_FLAG_PASS_TO_USER),
140 connectionToken(connectionToken),
141 isExiting(isExiting),
142 x(x),
143 y(y) {}
144
145DragEntry::~DragEntry() {}
146
147std::string DragEntry::getDescription() const {
148 return StringPrintf("DragEntry(isExiting=%s, x=%f, y=%f)", isExiting ? "true" : "false", x, y);
149}
150
Garfield Tane84e6f92019-08-29 17:28:41 -0700151// --- KeyEntry ---
152
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800153KeyEntry::KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700154 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
155 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
156 nsecs_t downTime)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800157 : EventEntry(id, Type::KEY, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700158 deviceId(deviceId),
159 source(source),
160 displayId(displayId),
161 action(action),
162 flags(flags),
163 keyCode(keyCode),
164 scanCode(scanCode),
165 metaState(metaState),
166 repeatCount(repeatCount),
167 downTime(downTime),
168 syntheticRepeat(false),
Michael Wright5caf55a2022-11-24 22:31:42 +0000169 interceptKeyResult(KeyEntry::InterceptKeyResult::UNKNOWN),
Garfield Tane84e6f92019-08-29 17:28:41 -0700170 interceptKeyWakeupTime(0) {}
171
172KeyEntry::~KeyEntry() {}
173
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500174std::string KeyEntry::getDescription() const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700175 if (!GetBoolProperty("ro.debuggable", false)) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500176 return "KeyEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700177 }
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800178 return StringPrintf("KeyEvent(deviceId=%d, eventTime=%" PRIu64 ", source=%s, displayId=%" PRId32
179 ", action=%s, "
Siarhei Vishniakou5b6c96f2021-02-25 00:21:51 -1000180 "flags=0x%08x, keyCode=%s(%d), scanCode=%d, metaState=0x%08x, "
Garfield Tane84e6f92019-08-29 17:28:41 -0700181 "repeatCount=%d), policyFlags=0x%08x",
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800182 deviceId, eventTime, inputEventSourceToString(source).c_str(), displayId,
183 KeyEvent::actionToString(action), flags, KeyEvent::getLabel(keyCode),
184 keyCode, scanCode, metaState, repeatCount, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -0700185}
186
187void KeyEntry::recycle() {
188 releaseInjectionState();
189
190 dispatchInProgress = false;
191 syntheticRepeat = false;
Michael Wright5caf55a2022-11-24 22:31:42 +0000192 interceptKeyResult = KeyEntry::InterceptKeyResult::UNKNOWN;
Garfield Tane84e6f92019-08-29 17:28:41 -0700193 interceptKeyWakeupTime = 0;
194}
195
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700196// --- TouchModeEntry ---
197
Antonio Kantek15beb512022-06-13 22:35:41 +0000198TouchModeEntry::TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode, int displayId)
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700199 : EventEntry(id, Type::TOUCH_MODE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
Antonio Kantek15beb512022-06-13 22:35:41 +0000200 inTouchMode(inTouchMode),
201 displayId(displayId) {}
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700202
203TouchModeEntry::~TouchModeEntry() {}
204
205std::string TouchModeEntry::getDescription() const {
206 return StringPrintf("TouchModeEvent(inTouchMode=%s)", inTouchMode ? "true" : "false");
207}
208
Garfield Tane84e6f92019-08-29 17:28:41 -0700209// --- MotionEntry ---
210
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800211MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700212 int32_t displayId, uint32_t policyFlags, int32_t action,
213 int32_t actionButton, int32_t flags, int32_t metaState,
214 int32_t buttonState, MotionClassification classification,
215 int32_t edgeFlags, float xPrecision, float yPrecision,
216 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
217 uint32_t pointerCount, const PointerProperties* pointerProperties,
Prabir Pradhan5beda762021-12-10 09:30:08 +0000218 const PointerCoords* pointerCoords)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800219 : EventEntry(id, Type::MOTION, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700220 deviceId(deviceId),
221 source(source),
222 displayId(displayId),
223 action(action),
224 actionButton(actionButton),
225 flags(flags),
226 metaState(metaState),
227 buttonState(buttonState),
228 classification(classification),
229 edgeFlags(edgeFlags),
230 xPrecision(xPrecision),
231 yPrecision(yPrecision),
232 xCursorPosition(xCursorPosition),
233 yCursorPosition(yCursorPosition),
234 downTime(downTime),
235 pointerCount(pointerCount) {
236 for (uint32_t i = 0; i < pointerCount; i++) {
237 this->pointerProperties[i].copyFrom(pointerProperties[i]);
238 this->pointerCoords[i].copyFrom(pointerCoords[i]);
Garfield Tane84e6f92019-08-29 17:28:41 -0700239 }
240}
241
242MotionEntry::~MotionEntry() {}
243
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500244std::string MotionEntry::getDescription() const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700245 if (!GetBoolProperty("ro.debuggable", false)) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500246 return "MotionEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700247 }
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500248 std::string msg;
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -0500249 msg += StringPrintf("MotionEvent(deviceId=%d, eventTime=%" PRIu64
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800250 ", source=%s, displayId=%" PRId32
Garfield Tane84e6f92019-08-29 17:28:41 -0700251 ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, "
252 "buttonState=0x%08x, "
253 "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
254 "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[",
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800255 deviceId, eventTime, inputEventSourceToString(source).c_str(), displayId,
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500256 MotionEvent::actionToString(action).c_str(), actionButton, flags, metaState,
257 buttonState, motionClassificationToString(classification), edgeFlags,
258 xPrecision, yPrecision, xCursorPosition, yCursorPosition);
Garfield Tane84e6f92019-08-29 17:28:41 -0700259
260 for (uint32_t i = 0; i < pointerCount; i++) {
261 if (i) {
262 msg += ", ";
263 }
264 msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
265 pointerCoords[i].getY());
266 }
267 msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500268 return msg;
Garfield Tane84e6f92019-08-29 17:28:41 -0700269}
270
Chris Yef59a2f42020-10-16 12:55:26 -0700271// --- SensorEntry ---
272
273SensorEntry::SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
274 uint32_t policyFlags, nsecs_t hwTimestamp,
275 InputDeviceSensorType sensorType, InputDeviceSensorAccuracy accuracy,
276 bool accuracyChanged, std::vector<float> values)
277 : EventEntry(id, Type::SENSOR, eventTime, policyFlags),
278 deviceId(deviceId),
279 source(source),
280 sensorType(sensorType),
281 accuracy(accuracy),
282 accuracyChanged(accuracyChanged),
283 hwTimestamp(hwTimestamp),
284 values(std::move(values)) {}
285
286SensorEntry::~SensorEntry() {}
287
288std::string SensorEntry::getDescription() const {
289 std::string msg;
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800290 msg += StringPrintf("SensorEntry(deviceId=%d, source=%s, sensorType=%s, "
Chris Yef59a2f42020-10-16 12:55:26 -0700291 "accuracy=0x%08x, hwTimestamp=%" PRId64,
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -0800292 deviceId, inputEventSourceToString(source).c_str(),
293 ftl::enum_string(sensorType).c_str(), accuracy, hwTimestamp);
Chris Yef59a2f42020-10-16 12:55:26 -0700294
295 if (!GetBoolProperty("ro.debuggable", false)) {
296 for (size_t i = 0; i < values.size(); i++) {
297 if (i > 0) {
298 msg += ", ";
299 }
300 msg += StringPrintf("(%.3f)", values[i]);
301 }
302 }
303 msg += StringPrintf(", policyFlags=0x%08x", policyFlags);
304 return msg;
305}
306
Garfield Tane84e6f92019-08-29 17:28:41 -0700307// --- DispatchEntry ---
308
309volatile int32_t DispatchEntry::sNextSeqAtomic;
310
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800311DispatchEntry::DispatchEntry(std::shared_ptr<EventEntry> eventEntry,
312 ftl::Flags<InputTarget::Flags> targetFlags,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700313 const ui::Transform& transform, const ui::Transform& rawTransform,
314 float globalScaleFactor)
Garfield Tane84e6f92019-08-29 17:28:41 -0700315 : seq(nextSeq()),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700316 eventEntry(std::move(eventEntry)),
Garfield Tane84e6f92019-08-29 17:28:41 -0700317 targetFlags(targetFlags),
chaviw1ff3d1e2020-07-01 15:53:47 -0700318 transform(transform),
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700319 rawTransform(rawTransform),
Garfield Tane84e6f92019-08-29 17:28:41 -0700320 globalScaleFactor(globalScaleFactor),
Garfield Tane84e6f92019-08-29 17:28:41 -0700321 deliveryTime(0),
322 resolvedAction(0),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700323 resolvedFlags(0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -0700324
325uint32_t DispatchEntry::nextSeq() {
326 // Sequence number 0 is reserved and will never be returned.
327 uint32_t seq;
328 do {
329 seq = android_atomic_inc(&sNextSeqAtomic);
330 } while (!seq);
331 return seq;
332}
333
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800334std::ostream& operator<<(std::ostream& out, const DispatchEntry& entry) {
335 out << "DispatchEntry{resolvedAction=";
336 switch (entry.eventEntry->type) {
337 case EventEntry::Type::KEY: {
338 out << KeyEvent::actionToString(entry.resolvedAction);
339 break;
340 }
341 case EventEntry::Type::MOTION: {
342 out << MotionEvent::actionToString(entry.resolvedAction);
343 break;
344 }
345 default: {
346 out << "<invalid, not a key or a motion>";
347 break;
348 }
349 }
350 std::string transform;
351 entry.transform.dump(transform, "transform");
352 out << ", resolvedFlags=" << entry.resolvedFlags
353 << ", targetFlags=" << entry.targetFlags.string() << ", transform=" << transform
354 << "} original =" << entry.eventEntry->getDescription();
355 return out;
356}
357
Garfield Tane84e6f92019-08-29 17:28:41 -0700358} // namespace android::inputdispatcher