blob: c03581d8da9523ae151970335f83cfe525ef0c05 [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 Vishniakoua1188f52020-10-20 20:14:52 -0500178 return StringPrintf("KeyEvent(deviceId=%d, eventTime=%" PRIu64
179 ", source=0x%08x, displayId=%" PRId32 ", 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 Vishniakoua1188f52020-10-20 20:14:52 -0500182 deviceId, eventTime, source, displayId, KeyEvent::actionToString(action),
Siarhei Vishniakou5b6c96f2021-02-25 00:21:51 -1000183 flags, KeyEvent::getLabel(keyCode), keyCode, scanCode, metaState,
184 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,
217 const PointerCoords* pointerCoords, float xOffset, float yOffset)
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]);
238 if (xOffset || yOffset) {
239 this->pointerCoords[i].applyOffset(xOffset, yOffset);
240 }
241 }
242}
243
244MotionEntry::~MotionEntry() {}
245
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500246std::string MotionEntry::getDescription() const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700247 if (!GetBoolProperty("ro.debuggable", false)) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500248 return "MotionEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700249 }
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500250 std::string msg;
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -0500251 msg += StringPrintf("MotionEvent(deviceId=%d, eventTime=%" PRIu64
252 ", source=0x%08x, displayId=%" PRId32
Garfield Tane84e6f92019-08-29 17:28:41 -0700253 ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, "
254 "buttonState=0x%08x, "
255 "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
256 "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[",
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500257 deviceId, eventTime, source, displayId,
258 MotionEvent::actionToString(action).c_str(), actionButton, flags, metaState,
259 buttonState, motionClassificationToString(classification), edgeFlags,
260 xPrecision, yPrecision, xCursorPosition, yCursorPosition);
Garfield Tane84e6f92019-08-29 17:28:41 -0700261
262 for (uint32_t i = 0; i < pointerCount; i++) {
263 if (i) {
264 msg += ", ";
265 }
266 msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
267 pointerCoords[i].getY());
268 }
269 msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500270 return msg;
Garfield Tane84e6f92019-08-29 17:28:41 -0700271}
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;
292 msg += StringPrintf("SensorEntry(deviceId=%d, source=0x%08x, sensorType=0x%08x, "
293 "accuracy=0x%08x, hwTimestamp=%" PRId64,
294 deviceId, source, sensorType, accuracy, hwTimestamp);
295
296 if (!GetBoolProperty("ro.debuggable", false)) {
297 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 Vishniakoua9a7ee82019-10-14 16:28:19 -0700312DispatchEntry::DispatchEntry(std::shared_ptr<EventEntry> eventEntry, int32_t 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
Garfield Tane84e6f92019-08-29 17:28:41 -0700334} // namespace android::inputdispatcher