blob: ad9cec1791bf43d3a14c9821606a2289e15efe9f [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
Prabir Pradhan65613802023-02-22 23:36:58 +000017#define LOG_TAG "InputDispatcher"
18
Garfield Tane84e6f92019-08-29 17:28:41 -070019#include "Entry.h"
20
21#include "Connection.h"
Prabir Pradhan65613802023-02-22 23:36:58 +000022#include "DebugConfig.h"
Garfield Tane84e6f92019-08-29 17:28:41 -070023
24#include <android-base/stringprintf.h>
25#include <cutils/atomic.h>
Harry Cuttsc57cd3c2024-04-24 13:52:55 +000026#include <ftl/enum.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070027#include <inttypes.h>
28
29using android::base::StringPrintf;
30
31namespace android::inputdispatcher {
32
Gang Wange9087892020-01-07 12:17:14 -050033VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry) {
34 return {{VerifiedInputEvent::Type::KEY, entry.deviceId, entry.eventTime, entry.source,
35 entry.displayId},
36 entry.action,
Gang Wange9087892020-01-07 12:17:14 -050037 entry.flags & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -080038 entry.downTime,
Gang Wange9087892020-01-07 12:17:14 -050039 entry.keyCode,
40 entry.scanCode,
41 entry.metaState,
42 entry.repeatCount};
43}
44
Prabir Pradhanb5cb9572021-09-24 06:35:16 -070045VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry,
46 const ui::Transform& rawTransform) {
47 const vec2 rawXY = MotionEvent::calculateTransformedXY(entry.source, rawTransform,
48 entry.pointerCoords[0].getXYValue());
Gang Wange9087892020-01-07 12:17:14 -050049 const int actionMasked = entry.action & AMOTION_EVENT_ACTION_MASK;
50 return {{VerifiedInputEvent::Type::MOTION, entry.deviceId, entry.eventTime, entry.source,
51 entry.displayId},
Prabir Pradhanb5cb9572021-09-24 06:35:16 -070052 rawXY.x,
53 rawXY.y,
Gang Wange9087892020-01-07 12:17:14 -050054 actionMasked,
Gang Wange9087892020-01-07 12:17:14 -050055 entry.flags & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -080056 entry.downTime,
Gang Wange9087892020-01-07 12:17:14 -050057 entry.metaState,
58 entry.buttonState};
59}
Garfield Tane84e6f92019-08-29 17:28:41 -070060
61// --- EventEntry ---
62
Garfield Tanc51d1ba2020-01-28 13:24:04 -080063EventEntry::EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags)
64 : id(id),
Garfield Tane84e6f92019-08-29 17:28:41 -070065 type(type),
66 eventTime(eventTime),
67 policyFlags(policyFlags),
68 injectionState(nullptr),
69 dispatchInProgress(false) {}
70
Garfield Tane84e6f92019-08-29 17:28:41 -070071// --- ConfigurationChangedEntry ---
72
Garfield Tanc51d1ba2020-01-28 13:24:04 -080073ConfigurationChangedEntry::ConfigurationChangedEntry(int32_t id, nsecs_t eventTime)
74 : EventEntry(id, Type::CONFIGURATION_CHANGED, eventTime, 0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070075
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050076std::string ConfigurationChangedEntry::getDescription() const {
77 return StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070078}
79
80// --- DeviceResetEntry ---
81
Garfield Tanc51d1ba2020-01-28 13:24:04 -080082DeviceResetEntry::DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId)
83 : EventEntry(id, Type::DEVICE_RESET, eventTime, 0), deviceId(deviceId) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070084
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050085std::string DeviceResetEntry::getDescription() const {
86 return StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070087}
88
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010089// --- FocusEntry ---
90
91// 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 -070092FocusEntry::FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -080093 const std::string& reason)
Garfield Tanc51d1ba2020-01-28 13:24:04 -080094 : EventEntry(id, Type::FOCUS, eventTime, POLICY_FLAG_PASS_TO_USER),
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010095 connectionToken(connectionToken),
Vishnu Nair7d3d00d2020-08-03 11:20:42 -070096 hasFocus(hasFocus),
97 reason(reason) {}
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010098
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050099std::string FocusEntry::getDescription() const {
100 return StringPrintf("FocusEvent(hasFocus=%s)", hasFocus ? "true" : "false");
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100101}
102
Prabir Pradhan99987712020-11-10 18:43:05 -0800103// --- PointerCaptureChangedEntry ---
104
105// PointerCaptureChanged notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER
106// for all entries.
107PointerCaptureChangedEntry::PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime,
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000108 const PointerCaptureRequest& request)
Prabir Pradhan99987712020-11-10 18:43:05 -0800109 : EventEntry(id, Type::POINTER_CAPTURE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000110 pointerCaptureRequest(request) {}
Prabir Pradhan99987712020-11-10 18:43:05 -0800111
Prabir Pradhan99987712020-11-10 18:43:05 -0800112std::string PointerCaptureChangedEntry::getDescription() const {
113 return StringPrintf("PointerCaptureChangedEvent(pointerCaptureEnabled=%s)",
Hiroki Sato25040232024-02-22 17:21:22 +0900114 pointerCaptureRequest.isEnable() ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800115}
116
arthurhungb89ccb02020-12-30 16:19:01 +0800117// --- DragEntry ---
118
119// Drag notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER for all entries
120DragEntry::DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting,
121 float x, float y)
122 : EventEntry(id, Type::DRAG, eventTime, POLICY_FLAG_PASS_TO_USER),
123 connectionToken(connectionToken),
124 isExiting(isExiting),
125 x(x),
126 y(y) {}
127
arthurhungb89ccb02020-12-30 16:19:01 +0800128std::string DragEntry::getDescription() const {
129 return StringPrintf("DragEntry(isExiting=%s, x=%f, y=%f)", isExiting ? "true" : "false", x, y);
130}
131
Garfield Tane84e6f92019-08-29 17:28:41 -0700132// --- KeyEntry ---
133
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000134KeyEntry::KeyEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime,
Linnan Li13bf76a2024-05-05 19:18:02 +0800135 int32_t deviceId, uint32_t source, ui::LogicalDisplayId displayId,
136 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
137 int32_t scanCode, int32_t metaState, int32_t repeatCount, nsecs_t downTime)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800138 : EventEntry(id, Type::KEY, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700139 deviceId(deviceId),
140 source(source),
141 displayId(displayId),
142 action(action),
Garfield Tane84e6f92019-08-29 17:28:41 -0700143 keyCode(keyCode),
144 scanCode(scanCode),
145 metaState(metaState),
Garfield Tane84e6f92019-08-29 17:28:41 -0700146 downTime(downTime),
147 syntheticRepeat(false),
Michael Wright5caf55a2022-11-24 22:31:42 +0000148 interceptKeyResult(KeyEntry::InterceptKeyResult::UNKNOWN),
Prabir Pradhan24047542023-11-02 17:14:59 +0000149 interceptKeyWakeupTime(0),
150 flags(flags),
151 repeatCount(repeatCount) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000152 EventEntry::injectionState = std::move(injectionState);
153}
Garfield Tane84e6f92019-08-29 17:28:41 -0700154
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500155std::string KeyEntry::getDescription() const {
Prabir Pradhan65613802023-02-22 23:36:58 +0000156 if (!IS_DEBUGGABLE_BUILD) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500157 return "KeyEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700158 }
Linnan Li13bf76a2024-05-05 19:18:02 +0800159 return StringPrintf("KeyEvent(deviceId=%d, eventTime=%" PRIu64 ", source=%s, displayId=%s, "
160 "action=%s, "
Siarhei Vishniakou5b6c96f2021-02-25 00:21:51 -1000161 "flags=0x%08x, keyCode=%s(%d), scanCode=%d, metaState=0x%08x, "
Garfield Tane84e6f92019-08-29 17:28:41 -0700162 "repeatCount=%d), policyFlags=0x%08x",
Linnan Li13bf76a2024-05-05 19:18:02 +0800163 deviceId, eventTime, inputEventSourceToString(source).c_str(),
164 displayId.toString().c_str(), KeyEvent::actionToString(action), flags,
165 KeyEvent::getLabel(keyCode), keyCode, scanCode, metaState, repeatCount,
166 policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -0700167}
168
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000169std::ostream& operator<<(std::ostream& out, const KeyEntry& keyEntry) {
170 out << keyEntry.getDescription();
171 return out;
172}
173
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700174// --- TouchModeEntry ---
175
Linnan Li13bf76a2024-05-05 19:18:02 +0800176TouchModeEntry::TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode,
177 ui::LogicalDisplayId displayId)
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700178 : EventEntry(id, Type::TOUCH_MODE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
Antonio Kantek15beb512022-06-13 22:35:41 +0000179 inTouchMode(inTouchMode),
180 displayId(displayId) {}
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700181
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700182std::string TouchModeEntry::getDescription() const {
183 return StringPrintf("TouchModeEvent(inTouchMode=%s)", inTouchMode ? "true" : "false");
184}
185
Garfield Tane84e6f92019-08-29 17:28:41 -0700186// --- MotionEntry ---
187
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000188MotionEntry::MotionEntry(int32_t id, std::shared_ptr<InjectionState> injectionState,
Linnan Li13bf76a2024-05-05 19:18:02 +0800189 nsecs_t eventTime, int32_t deviceId, uint32_t source,
190 ui::LogicalDisplayId displayId, uint32_t policyFlags, int32_t action,
191 int32_t actionButton, int32_t flags, int32_t metaState,
192 int32_t buttonState, MotionClassification classification,
193 int32_t edgeFlags, float xPrecision, float yPrecision,
194 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
195 const std::vector<PointerProperties>& pointerProperties,
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700196 const std::vector<PointerCoords>& pointerCoords)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800197 : EventEntry(id, Type::MOTION, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700198 deviceId(deviceId),
199 source(source),
200 displayId(displayId),
201 action(action),
202 actionButton(actionButton),
203 flags(flags),
204 metaState(metaState),
205 buttonState(buttonState),
206 classification(classification),
207 edgeFlags(edgeFlags),
208 xPrecision(xPrecision),
209 yPrecision(yPrecision),
210 xCursorPosition(xCursorPosition),
211 yCursorPosition(yCursorPosition),
212 downTime(downTime),
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700213 pointerProperties(pointerProperties),
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000214 pointerCoords(pointerCoords) {
215 EventEntry::injectionState = std::move(injectionState);
216}
Garfield Tane84e6f92019-08-29 17:28:41 -0700217
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500218std::string MotionEntry::getDescription() const {
Prabir Pradhan65613802023-02-22 23:36:58 +0000219 if (!IS_DEBUGGABLE_BUILD) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500220 return "MotionEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700221 }
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500222 std::string msg;
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -0500223 msg += StringPrintf("MotionEvent(deviceId=%d, eventTime=%" PRIu64
Linnan Li13bf76a2024-05-05 19:18:02 +0800224 ", source=%s, displayId=%s, action=%s, actionButton=0x%08x, flags=0x%08x,"
225 " metaState=0x%08x, "
Garfield Tane84e6f92019-08-29 17:28:41 -0700226 "buttonState=0x%08x, "
227 "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
228 "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[",
Linnan Li13bf76a2024-05-05 19:18:02 +0800229 deviceId, eventTime, inputEventSourceToString(source).c_str(),
230 displayId.toString().c_str(), MotionEvent::actionToString(action).c_str(),
231 actionButton, flags, metaState, buttonState,
232 motionClassificationToString(classification), edgeFlags, xPrecision,
233 yPrecision, xCursorPosition, yCursorPosition);
Garfield Tane84e6f92019-08-29 17:28:41 -0700234
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700235 for (uint32_t i = 0; i < getPointerCount(); i++) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700236 if (i) {
237 msg += ", ";
238 }
239 msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
240 pointerCoords[i].getY());
241 }
242 msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500243 return msg;
Garfield Tane84e6f92019-08-29 17:28:41 -0700244}
245
Siarhei Vishniakou72945a02023-09-18 18:30:25 -0700246std::ostream& operator<<(std::ostream& out, const MotionEntry& motionEntry) {
247 out << motionEntry.getDescription();
248 return out;
249}
250
Chris Yef59a2f42020-10-16 12:55:26 -0700251// --- SensorEntry ---
252
253SensorEntry::SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
254 uint32_t policyFlags, nsecs_t hwTimestamp,
255 InputDeviceSensorType sensorType, InputDeviceSensorAccuracy accuracy,
256 bool accuracyChanged, std::vector<float> values)
257 : EventEntry(id, Type::SENSOR, eventTime, policyFlags),
258 deviceId(deviceId),
259 source(source),
260 sensorType(sensorType),
261 accuracy(accuracy),
262 accuracyChanged(accuracyChanged),
263 hwTimestamp(hwTimestamp),
264 values(std::move(values)) {}
265
Chris Yef59a2f42020-10-16 12:55:26 -0700266std::string SensorEntry::getDescription() const {
267 std::string msg;
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800268 msg += StringPrintf("SensorEntry(deviceId=%d, source=%s, sensorType=%s, "
Harry Cuttsc57cd3c2024-04-24 13:52:55 +0000269 "accuracy=%s, hwTimestamp=%" PRId64,
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -0800270 deviceId, inputEventSourceToString(source).c_str(),
Harry Cuttsc57cd3c2024-04-24 13:52:55 +0000271 ftl::enum_string(sensorType).c_str(), ftl::enum_string(accuracy).c_str(),
272 hwTimestamp);
Chris Yef59a2f42020-10-16 12:55:26 -0700273
Prabir Pradhan65613802023-02-22 23:36:58 +0000274 if (IS_DEBUGGABLE_BUILD) {
Chris Yef59a2f42020-10-16 12:55:26 -0700275 for (size_t i = 0; i < values.size(); i++) {
276 if (i > 0) {
277 msg += ", ";
278 }
279 msg += StringPrintf("(%.3f)", values[i]);
280 }
281 }
282 msg += StringPrintf(", policyFlags=0x%08x", policyFlags);
283 return msg;
284}
285
Garfield Tane84e6f92019-08-29 17:28:41 -0700286// --- DispatchEntry ---
287
288volatile int32_t DispatchEntry::sNextSeqAtomic;
289
Prabir Pradhan24047542023-11-02 17:14:59 +0000290DispatchEntry::DispatchEntry(std::shared_ptr<const EventEntry> eventEntry,
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800291 ftl::Flags<InputTargetFlags> targetFlags,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700292 const ui::Transform& transform, const ui::Transform& rawTransform,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000293 float globalScaleFactor, gui::Uid targetUid, int64_t vsyncId,
294 std::optional<int32_t> windowId)
Garfield Tane84e6f92019-08-29 17:28:41 -0700295 : seq(nextSeq()),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700296 eventEntry(std::move(eventEntry)),
Garfield Tane84e6f92019-08-29 17:28:41 -0700297 targetFlags(targetFlags),
chaviw1ff3d1e2020-07-01 15:53:47 -0700298 transform(transform),
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700299 rawTransform(rawTransform),
Garfield Tane84e6f92019-08-29 17:28:41 -0700300 globalScaleFactor(globalScaleFactor),
Garfield Tane84e6f92019-08-29 17:28:41 -0700301 deliveryTime(0),
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000302 resolvedFlags(0),
303 targetUid(targetUid),
304 vsyncId(vsyncId),
305 windowId(windowId) {
Siarhei Vishniakou2af5b032023-07-10 18:52:17 -0700306 switch (this->eventEntry->type) {
307 case EventEntry::Type::KEY: {
Prabir Pradhan24047542023-11-02 17:14:59 +0000308 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*this->eventEntry);
Siarhei Vishniakou2af5b032023-07-10 18:52:17 -0700309 resolvedFlags = keyEntry.flags;
Siarhei Vishniakou2af5b032023-07-10 18:52:17 -0700310 break;
311 }
312 case EventEntry::Type::MOTION: {
Prabir Pradhan24047542023-11-02 17:14:59 +0000313 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*this->eventEntry);
Siarhei Vishniakou2af5b032023-07-10 18:52:17 -0700314 resolvedFlags = motionEntry.flags;
315 break;
316 }
317 default: {
318 break;
319 }
320 }
321}
Garfield Tane84e6f92019-08-29 17:28:41 -0700322
323uint32_t DispatchEntry::nextSeq() {
324 // Sequence number 0 is reserved and will never be returned.
325 uint32_t seq;
326 do {
327 seq = android_atomic_inc(&sNextSeqAtomic);
328 } while (!seq);
329 return seq;
330}
331
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800332std::ostream& operator<<(std::ostream& out, const DispatchEntry& entry) {
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800333 std::string transform;
334 entry.transform.dump(transform, "transform");
Prabir Pradhan2a2da1d2023-11-03 02:16:20 +0000335 out << "DispatchEntry{resolvedFlags=" << entry.resolvedFlags
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800336 << ", targetFlags=" << entry.targetFlags.string() << ", transform=" << transform
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -0700337 << "} original: " << entry.eventEntry->getDescription();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800338 return out;
339}
340
Garfield Tane84e6f92019-08-29 17:28:41 -0700341} // namespace android::inputdispatcher