blob: 3d0818b7386487ec27efd199c1c2d05389186fb1 [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,
35 entry.downTime,
36 entry.flags & VERIFIED_KEY_EVENT_FLAGS,
37 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,
53 entry.downTime,
54 entry.flags & VERIFIED_MOTION_EVENT_FLAGS,
55 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),
169 interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN),
170 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;
192 interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
193 interceptKeyWakeupTime = 0;
194}
195
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700196// --- TouchModeEntry ---
197
198TouchModeEntry::TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode)
199 : EventEntry(id, Type::TOUCH_MODE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
200 inTouchMode(inTouchMode) {}
201
202TouchModeEntry::~TouchModeEntry() {}
203
204std::string TouchModeEntry::getDescription() const {
205 return StringPrintf("TouchModeEvent(inTouchMode=%s)", inTouchMode ? "true" : "false");
206}
207
Garfield Tane84e6f92019-08-29 17:28:41 -0700208// --- MotionEntry ---
209
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800210MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700211 int32_t displayId, uint32_t policyFlags, int32_t action,
212 int32_t actionButton, int32_t flags, int32_t metaState,
213 int32_t buttonState, MotionClassification classification,
214 int32_t edgeFlags, float xPrecision, float yPrecision,
215 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
216 uint32_t pointerCount, const PointerProperties* pointerProperties,
Prabir Pradhand6a7f222021-11-24 15:36:43 +0000217 const PointerCoords* pointerCoords)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800218 : EventEntry(id, Type::MOTION, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700219 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]);
Garfield Tane84e6f92019-08-29 17:28:41 -0700238 }
239}
240
241MotionEntry::~MotionEntry() {}
242
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500243std::string MotionEntry::getDescription() const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700244 if (!GetBoolProperty("ro.debuggable", false)) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500245 return "MotionEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700246 }
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500247 std::string msg;
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -0500248 msg += StringPrintf("MotionEvent(deviceId=%d, eventTime=%" PRIu64
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800249 ", source=%s, displayId=%" PRId32
Garfield Tane84e6f92019-08-29 17:28:41 -0700250 ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, "
251 "buttonState=0x%08x, "
252 "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
253 "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[",
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800254 deviceId, eventTime, inputEventSourceToString(source).c_str(), displayId,
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500255 MotionEvent::actionToString(action).c_str(), actionButton, flags, metaState,
256 buttonState, motionClassificationToString(classification), edgeFlags,
257 xPrecision, yPrecision, xCursorPosition, yCursorPosition);
Garfield Tane84e6f92019-08-29 17:28:41 -0700258
259 for (uint32_t i = 0; i < pointerCount; i++) {
260 if (i) {
261 msg += ", ";
262 }
263 msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
264 pointerCoords[i].getY());
265 }
266 msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500267 return msg;
Garfield Tane84e6f92019-08-29 17:28:41 -0700268}
269
Chris Yef59a2f42020-10-16 12:55:26 -0700270// --- SensorEntry ---
271
272SensorEntry::SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
273 uint32_t policyFlags, nsecs_t hwTimestamp,
274 InputDeviceSensorType sensorType, InputDeviceSensorAccuracy accuracy,
275 bool accuracyChanged, std::vector<float> values)
276 : EventEntry(id, Type::SENSOR, eventTime, policyFlags),
277 deviceId(deviceId),
278 source(source),
279 sensorType(sensorType),
280 accuracy(accuracy),
281 accuracyChanged(accuracyChanged),
282 hwTimestamp(hwTimestamp),
283 values(std::move(values)) {}
284
285SensorEntry::~SensorEntry() {}
286
287std::string SensorEntry::getDescription() const {
288 std::string msg;
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800289 std::string sensorTypeStr(ftl::enum_name(sensorType).value_or("?"));
290 msg += StringPrintf("SensorEntry(deviceId=%d, source=%s, sensorType=%s, "
Chris Yef59a2f42020-10-16 12:55:26 -0700291 "accuracy=0x%08x, hwTimestamp=%" PRId64,
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800292 deviceId, inputEventSourceToString(source).c_str(), sensorTypeStr.c_str(),
293 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 Vishniakoua9a7ee82019-10-14 16:28:19 -0700311DispatchEntry::DispatchEntry(std::shared_ptr<EventEntry> eventEntry, int32_t targetFlags,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700312 const ui::Transform& transform, const ui::Transform& rawTransform,
313 float globalScaleFactor)
Garfield Tane84e6f92019-08-29 17:28:41 -0700314 : seq(nextSeq()),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700315 eventEntry(std::move(eventEntry)),
Garfield Tane84e6f92019-08-29 17:28:41 -0700316 targetFlags(targetFlags),
chaviw1ff3d1e2020-07-01 15:53:47 -0700317 transform(transform),
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700318 rawTransform(rawTransform),
Garfield Tane84e6f92019-08-29 17:28:41 -0700319 globalScaleFactor(globalScaleFactor),
Garfield Tane84e6f92019-08-29 17:28:41 -0700320 deliveryTime(0),
321 resolvedAction(0),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700322 resolvedFlags(0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -0700323
324uint32_t DispatchEntry::nextSeq() {
325 // Sequence number 0 is reserved and will never be returned.
326 uint32_t seq;
327 do {
328 seq = android_atomic_inc(&sNextSeqAtomic);
329 } while (!seq);
330 return seq;
331}
332
Garfield Tane84e6f92019-08-29 17:28:41 -0700333} // namespace android::inputdispatcher