blob: bcb00719cde0ab1290fa933de60e0d60812794bf [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
43VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry) {
44 const float rawX = entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
45 const float rawY = entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
46 const int actionMasked = entry.action & AMOTION_EVENT_ACTION_MASK;
47 return {{VerifiedInputEvent::Type::MOTION, entry.deviceId, entry.eventTime, entry.source,
48 entry.displayId},
49 rawX,
50 rawY,
51 actionMasked,
52 entry.downTime,
53 entry.flags & VERIFIED_MOTION_EVENT_FLAGS,
54 entry.metaState,
55 entry.buttonState};
56}
Garfield Tane84e6f92019-08-29 17:28:41 -070057
58// --- EventEntry ---
59
Garfield Tanc51d1ba2020-01-28 13:24:04 -080060EventEntry::EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags)
61 : id(id),
Garfield Tane84e6f92019-08-29 17:28:41 -070062 type(type),
63 eventTime(eventTime),
64 policyFlags(policyFlags),
65 injectionState(nullptr),
66 dispatchInProgress(false) {}
67
68EventEntry::~EventEntry() {
69 releaseInjectionState();
70}
71
Garfield Tane84e6f92019-08-29 17:28:41 -070072void EventEntry::releaseInjectionState() {
73 if (injectionState) {
74 injectionState->release();
75 injectionState = nullptr;
76 }
77}
78
79// --- ConfigurationChangedEntry ---
80
Garfield Tanc51d1ba2020-01-28 13:24:04 -080081ConfigurationChangedEntry::ConfigurationChangedEntry(int32_t id, nsecs_t eventTime)
82 : EventEntry(id, Type::CONFIGURATION_CHANGED, eventTime, 0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070083
84ConfigurationChangedEntry::~ConfigurationChangedEntry() {}
85
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050086std::string ConfigurationChangedEntry::getDescription() const {
87 return StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070088}
89
90// --- DeviceResetEntry ---
91
Garfield Tanc51d1ba2020-01-28 13:24:04 -080092DeviceResetEntry::DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId)
93 : EventEntry(id, Type::DEVICE_RESET, eventTime, 0), deviceId(deviceId) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070094
95DeviceResetEntry::~DeviceResetEntry() {}
96
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050097std::string DeviceResetEntry::getDescription() const {
98 return StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070099}
100
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100101// --- FocusEntry ---
102
103// 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 -0700104FocusEntry::FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -0800105 const std::string& reason)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800106 : EventEntry(id, Type::FOCUS, eventTime, POLICY_FLAG_PASS_TO_USER),
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100107 connectionToken(connectionToken),
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700108 hasFocus(hasFocus),
109 reason(reason) {}
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100110
111FocusEntry::~FocusEntry() {}
112
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500113std::string FocusEntry::getDescription() const {
114 return StringPrintf("FocusEvent(hasFocus=%s)", hasFocus ? "true" : "false");
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100115}
116
Prabir Pradhan99987712020-11-10 18:43:05 -0800117// --- PointerCaptureChangedEntry ---
118
119// PointerCaptureChanged notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER
120// for all entries.
121PointerCaptureChangedEntry::PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime,
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000122 const PointerCaptureRequest& request)
Prabir Pradhan99987712020-11-10 18:43:05 -0800123 : EventEntry(id, Type::POINTER_CAPTURE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000124 pointerCaptureRequest(request) {}
Prabir Pradhan99987712020-11-10 18:43:05 -0800125
126PointerCaptureChangedEntry::~PointerCaptureChangedEntry() {}
127
128std::string PointerCaptureChangedEntry::getDescription() const {
129 return StringPrintf("PointerCaptureChangedEvent(pointerCaptureEnabled=%s)",
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000130 pointerCaptureRequest.enable ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800131}
132
arthurhungb89ccb02020-12-30 16:19:01 +0800133// --- DragEntry ---
134
135// Drag notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER for all entries
136DragEntry::DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting,
137 float x, float y)
138 : EventEntry(id, Type::DRAG, eventTime, POLICY_FLAG_PASS_TO_USER),
139 connectionToken(connectionToken),
140 isExiting(isExiting),
141 x(x),
142 y(y) {}
143
144DragEntry::~DragEntry() {}
145
146std::string DragEntry::getDescription() const {
147 return StringPrintf("DragEntry(isExiting=%s, x=%f, y=%f)", isExiting ? "true" : "false", x, y);
148}
149
Garfield Tane84e6f92019-08-29 17:28:41 -0700150// --- KeyEntry ---
151
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800152KeyEntry::KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700153 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
154 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
155 nsecs_t downTime)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800156 : EventEntry(id, Type::KEY, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700157 deviceId(deviceId),
158 source(source),
159 displayId(displayId),
160 action(action),
161 flags(flags),
162 keyCode(keyCode),
163 scanCode(scanCode),
164 metaState(metaState),
165 repeatCount(repeatCount),
166 downTime(downTime),
167 syntheticRepeat(false),
168 interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN),
169 interceptKeyWakeupTime(0) {}
170
171KeyEntry::~KeyEntry() {}
172
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500173std::string KeyEntry::getDescription() const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700174 if (!GetBoolProperty("ro.debuggable", false)) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500175 return "KeyEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700176 }
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -0500177 return StringPrintf("KeyEvent(deviceId=%d, eventTime=%" PRIu64
178 ", source=0x%08x, displayId=%" PRId32 ", action=%s, "
Siarhei Vishniakou5b6c96f2021-02-25 00:21:51 -1000179 "flags=0x%08x, keyCode=%s(%d), scanCode=%d, metaState=0x%08x, "
Garfield Tane84e6f92019-08-29 17:28:41 -0700180 "repeatCount=%d), policyFlags=0x%08x",
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -0500181 deviceId, eventTime, source, displayId, KeyEvent::actionToString(action),
Siarhei Vishniakou5b6c96f2021-02-25 00:21:51 -1000182 flags, KeyEvent::getLabel(keyCode), keyCode, scanCode, metaState,
183 repeatCount, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -0700184}
185
186void KeyEntry::recycle() {
187 releaseInjectionState();
188
189 dispatchInProgress = false;
190 syntheticRepeat = false;
191 interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
192 interceptKeyWakeupTime = 0;
193}
194
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700195// --- TouchModeEntry ---
196
197TouchModeEntry::TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode)
198 : EventEntry(id, Type::TOUCH_MODE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
199 inTouchMode(inTouchMode) {}
200
201TouchModeEntry::~TouchModeEntry() {}
202
203std::string TouchModeEntry::getDescription() const {
204 return StringPrintf("TouchModeEvent(inTouchMode=%s)", inTouchMode ? "true" : "false");
205}
206
Garfield Tane84e6f92019-08-29 17:28:41 -0700207// --- MotionEntry ---
208
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800209MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700210 int32_t displayId, uint32_t policyFlags, int32_t action,
211 int32_t actionButton, int32_t flags, int32_t metaState,
212 int32_t buttonState, MotionClassification classification,
213 int32_t edgeFlags, float xPrecision, float yPrecision,
214 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
215 uint32_t pointerCount, const PointerProperties* pointerProperties,
216 const PointerCoords* pointerCoords, float xOffset, float yOffset)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800217 : EventEntry(id, Type::MOTION, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700218 deviceId(deviceId),
219 source(source),
220 displayId(displayId),
221 action(action),
222 actionButton(actionButton),
223 flags(flags),
224 metaState(metaState),
225 buttonState(buttonState),
226 classification(classification),
227 edgeFlags(edgeFlags),
228 xPrecision(xPrecision),
229 yPrecision(yPrecision),
230 xCursorPosition(xCursorPosition),
231 yCursorPosition(yCursorPosition),
232 downTime(downTime),
233 pointerCount(pointerCount) {
234 for (uint32_t i = 0; i < pointerCount; i++) {
235 this->pointerProperties[i].copyFrom(pointerProperties[i]);
236 this->pointerCoords[i].copyFrom(pointerCoords[i]);
237 if (xOffset || yOffset) {
238 this->pointerCoords[i].applyOffset(xOffset, yOffset);
239 }
240 }
241}
242
243MotionEntry::~MotionEntry() {}
244
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500245std::string MotionEntry::getDescription() const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700246 if (!GetBoolProperty("ro.debuggable", false)) {
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
251 ", source=0x%08x, 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 Vishniakouc68fdec2020-10-22 14:58:14 -0500256 deviceId, eventTime, source, displayId,
257 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;
291 msg += StringPrintf("SensorEntry(deviceId=%d, source=0x%08x, sensorType=0x%08x, "
292 "accuracy=0x%08x, hwTimestamp=%" PRId64,
293 deviceId, source, sensorType, accuracy, hwTimestamp);
294
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