blob: ebc04cf75ab782caf468b4e47e08dfab0896655d [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>
26#include <inttypes.h>
27
28using android::base::StringPrintf;
29
30namespace android::inputdispatcher {
31
Gang Wange9087892020-01-07 12:17:14 -050032VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry) {
33 return {{VerifiedInputEvent::Type::KEY, entry.deviceId, entry.eventTime, entry.source,
34 entry.displayId},
35 entry.action,
Gang Wange9087892020-01-07 12:17:14 -050036 entry.flags & VERIFIED_KEY_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -080037 entry.downTime,
Gang Wange9087892020-01-07 12:17:14 -050038 entry.keyCode,
39 entry.scanCode,
40 entry.metaState,
41 entry.repeatCount};
42}
43
Prabir Pradhanb5cb9572021-09-24 06:35:16 -070044VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry,
45 const ui::Transform& rawTransform) {
46 const vec2 rawXY = MotionEvent::calculateTransformedXY(entry.source, rawTransform,
47 entry.pointerCoords[0].getXYValue());
Gang Wange9087892020-01-07 12:17:14 -050048 const int actionMasked = entry.action & AMOTION_EVENT_ACTION_MASK;
49 return {{VerifiedInputEvent::Type::MOTION, entry.deviceId, entry.eventTime, entry.source,
50 entry.displayId},
Prabir Pradhanb5cb9572021-09-24 06:35:16 -070051 rawXY.x,
52 rawXY.y,
Gang Wange9087892020-01-07 12:17:14 -050053 actionMasked,
Gang Wange9087892020-01-07 12:17:14 -050054 entry.flags & VERIFIED_MOTION_EVENT_FLAGS,
Siarhei Vishniakouf355bf92021-12-09 10:43:21 -080055 entry.downTime,
Gang Wange9087892020-01-07 12:17:14 -050056 entry.metaState,
57 entry.buttonState};
58}
Garfield Tane84e6f92019-08-29 17:28:41 -070059
60// --- EventEntry ---
61
Garfield Tanc51d1ba2020-01-28 13:24:04 -080062EventEntry::EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags)
63 : id(id),
Garfield Tane84e6f92019-08-29 17:28:41 -070064 type(type),
65 eventTime(eventTime),
66 policyFlags(policyFlags),
67 injectionState(nullptr),
68 dispatchInProgress(false) {}
69
Garfield Tane84e6f92019-08-29 17:28:41 -070070// --- ConfigurationChangedEntry ---
71
Garfield Tanc51d1ba2020-01-28 13:24:04 -080072ConfigurationChangedEntry::ConfigurationChangedEntry(int32_t id, nsecs_t eventTime)
73 : EventEntry(id, Type::CONFIGURATION_CHANGED, eventTime, 0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070074
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050075std::string ConfigurationChangedEntry::getDescription() const {
76 return StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070077}
78
79// --- DeviceResetEntry ---
80
Garfield Tanc51d1ba2020-01-28 13:24:04 -080081DeviceResetEntry::DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId)
82 : EventEntry(id, Type::DEVICE_RESET, eventTime, 0), deviceId(deviceId) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070083
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050084std::string DeviceResetEntry::getDescription() const {
85 return StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070086}
87
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010088// --- FocusEntry ---
89
90// 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 -070091FocusEntry::FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -080092 const std::string& reason)
Garfield Tanc51d1ba2020-01-28 13:24:04 -080093 : EventEntry(id, Type::FOCUS, eventTime, POLICY_FLAG_PASS_TO_USER),
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010094 connectionToken(connectionToken),
Vishnu Nair7d3d00d2020-08-03 11:20:42 -070095 hasFocus(hasFocus),
96 reason(reason) {}
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010097
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050098std::string FocusEntry::getDescription() const {
99 return StringPrintf("FocusEvent(hasFocus=%s)", hasFocus ? "true" : "false");
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100100}
101
Prabir Pradhan99987712020-11-10 18:43:05 -0800102// --- PointerCaptureChangedEntry ---
103
104// PointerCaptureChanged notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER
105// for all entries.
106PointerCaptureChangedEntry::PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime,
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000107 const PointerCaptureRequest& request)
Prabir Pradhan99987712020-11-10 18:43:05 -0800108 : EventEntry(id, Type::POINTER_CAPTURE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000109 pointerCaptureRequest(request) {}
Prabir Pradhan99987712020-11-10 18:43:05 -0800110
Prabir Pradhan99987712020-11-10 18:43:05 -0800111std::string PointerCaptureChangedEntry::getDescription() const {
112 return StringPrintf("PointerCaptureChangedEvent(pointerCaptureEnabled=%s)",
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000113 pointerCaptureRequest.enable ? "true" : "false");
Prabir Pradhan99987712020-11-10 18:43:05 -0800114}
115
arthurhungb89ccb02020-12-30 16:19:01 +0800116// --- DragEntry ---
117
118// Drag notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER for all entries
119DragEntry::DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting,
120 float x, float y)
121 : EventEntry(id, Type::DRAG, eventTime, POLICY_FLAG_PASS_TO_USER),
122 connectionToken(connectionToken),
123 isExiting(isExiting),
124 x(x),
125 y(y) {}
126
arthurhungb89ccb02020-12-30 16:19:01 +0800127std::string DragEntry::getDescription() const {
128 return StringPrintf("DragEntry(isExiting=%s, x=%f, y=%f)", isExiting ? "true" : "false", x, y);
129}
130
Garfield Tane84e6f92019-08-29 17:28:41 -0700131// --- KeyEntry ---
132
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000133KeyEntry::KeyEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime,
134 int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
135 int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
136 int32_t metaState, int32_t repeatCount, nsecs_t downTime)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800137 : EventEntry(id, Type::KEY, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700138 deviceId(deviceId),
139 source(source),
140 displayId(displayId),
141 action(action),
Garfield Tane84e6f92019-08-29 17:28:41 -0700142 keyCode(keyCode),
143 scanCode(scanCode),
144 metaState(metaState),
Garfield Tane84e6f92019-08-29 17:28:41 -0700145 downTime(downTime),
146 syntheticRepeat(false),
Michael Wright5caf55a2022-11-24 22:31:42 +0000147 interceptKeyResult(KeyEntry::InterceptKeyResult::UNKNOWN),
Prabir Pradhan24047542023-11-02 17:14:59 +0000148 interceptKeyWakeupTime(0),
149 flags(flags),
150 repeatCount(repeatCount) {
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000151 EventEntry::injectionState = std::move(injectionState);
152}
Garfield Tane84e6f92019-08-29 17:28:41 -0700153
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500154std::string KeyEntry::getDescription() const {
Prabir Pradhan65613802023-02-22 23:36:58 +0000155 if (!IS_DEBUGGABLE_BUILD) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500156 return "KeyEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700157 }
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800158 return StringPrintf("KeyEvent(deviceId=%d, eventTime=%" PRIu64 ", source=%s, displayId=%" PRId32
159 ", action=%s, "
Siarhei Vishniakou5b6c96f2021-02-25 00:21:51 -1000160 "flags=0x%08x, keyCode=%s(%d), scanCode=%d, metaState=0x%08x, "
Garfield Tane84e6f92019-08-29 17:28:41 -0700161 "repeatCount=%d), policyFlags=0x%08x",
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800162 deviceId, eventTime, inputEventSourceToString(source).c_str(), displayId,
163 KeyEvent::actionToString(action), flags, KeyEvent::getLabel(keyCode),
164 keyCode, scanCode, metaState, repeatCount, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -0700165}
166
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700167// --- TouchModeEntry ---
168
Antonio Kantek15beb512022-06-13 22:35:41 +0000169TouchModeEntry::TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode, int displayId)
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700170 : EventEntry(id, Type::TOUCH_MODE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
Antonio Kantek15beb512022-06-13 22:35:41 +0000171 inTouchMode(inTouchMode),
172 displayId(displayId) {}
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700173
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700174std::string TouchModeEntry::getDescription() const {
175 return StringPrintf("TouchModeEvent(inTouchMode=%s)", inTouchMode ? "true" : "false");
176}
177
Garfield Tane84e6f92019-08-29 17:28:41 -0700178// --- MotionEntry ---
179
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000180MotionEntry::MotionEntry(int32_t id, std::shared_ptr<InjectionState> injectionState,
181 nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
182 uint32_t policyFlags, int32_t action, int32_t actionButton, int32_t flags,
183 int32_t metaState, int32_t buttonState,
184 MotionClassification classification, int32_t edgeFlags, float xPrecision,
185 float yPrecision, float xCursorPosition, float yCursorPosition,
186 nsecs_t downTime, const std::vector<PointerProperties>& pointerProperties,
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700187 const std::vector<PointerCoords>& pointerCoords)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800188 : EventEntry(id, Type::MOTION, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700189 deviceId(deviceId),
190 source(source),
191 displayId(displayId),
192 action(action),
193 actionButton(actionButton),
194 flags(flags),
195 metaState(metaState),
196 buttonState(buttonState),
197 classification(classification),
198 edgeFlags(edgeFlags),
199 xPrecision(xPrecision),
200 yPrecision(yPrecision),
201 xCursorPosition(xCursorPosition),
202 yCursorPosition(yCursorPosition),
203 downTime(downTime),
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700204 pointerProperties(pointerProperties),
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000205 pointerCoords(pointerCoords) {
206 EventEntry::injectionState = std::move(injectionState);
207}
Garfield Tane84e6f92019-08-29 17:28:41 -0700208
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500209std::string MotionEntry::getDescription() const {
Prabir Pradhan65613802023-02-22 23:36:58 +0000210 if (!IS_DEBUGGABLE_BUILD) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500211 return "MotionEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700212 }
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500213 std::string msg;
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -0500214 msg += StringPrintf("MotionEvent(deviceId=%d, eventTime=%" PRIu64
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800215 ", source=%s, displayId=%" PRId32
Garfield Tane84e6f92019-08-29 17:28:41 -0700216 ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, "
217 "buttonState=0x%08x, "
218 "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
219 "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[",
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800220 deviceId, eventTime, inputEventSourceToString(source).c_str(), displayId,
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500221 MotionEvent::actionToString(action).c_str(), actionButton, flags, metaState,
222 buttonState, motionClassificationToString(classification), edgeFlags,
223 xPrecision, yPrecision, xCursorPosition, yCursorPosition);
Garfield Tane84e6f92019-08-29 17:28:41 -0700224
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700225 for (uint32_t i = 0; i < getPointerCount(); i++) {
Garfield Tane84e6f92019-08-29 17:28:41 -0700226 if (i) {
227 msg += ", ";
228 }
229 msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
230 pointerCoords[i].getY());
231 }
232 msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500233 return msg;
Garfield Tane84e6f92019-08-29 17:28:41 -0700234}
235
Siarhei Vishniakou72945a02023-09-18 18:30:25 -0700236std::ostream& operator<<(std::ostream& out, const MotionEntry& motionEntry) {
237 out << motionEntry.getDescription();
238 return out;
239}
240
Chris Yef59a2f42020-10-16 12:55:26 -0700241// --- SensorEntry ---
242
243SensorEntry::SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
244 uint32_t policyFlags, nsecs_t hwTimestamp,
245 InputDeviceSensorType sensorType, InputDeviceSensorAccuracy accuracy,
246 bool accuracyChanged, std::vector<float> values)
247 : EventEntry(id, Type::SENSOR, eventTime, policyFlags),
248 deviceId(deviceId),
249 source(source),
250 sensorType(sensorType),
251 accuracy(accuracy),
252 accuracyChanged(accuracyChanged),
253 hwTimestamp(hwTimestamp),
254 values(std::move(values)) {}
255
Chris Yef59a2f42020-10-16 12:55:26 -0700256std::string SensorEntry::getDescription() const {
257 std::string msg;
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800258 msg += StringPrintf("SensorEntry(deviceId=%d, source=%s, sensorType=%s, "
Chris Yef59a2f42020-10-16 12:55:26 -0700259 "accuracy=0x%08x, hwTimestamp=%" PRId64,
Siarhei Vishniakouf12f2f72021-11-17 17:49:45 -0800260 deviceId, inputEventSourceToString(source).c_str(),
261 ftl::enum_string(sensorType).c_str(), accuracy, hwTimestamp);
Chris Yef59a2f42020-10-16 12:55:26 -0700262
Prabir Pradhan65613802023-02-22 23:36:58 +0000263 if (IS_DEBUGGABLE_BUILD) {
Chris Yef59a2f42020-10-16 12:55:26 -0700264 for (size_t i = 0; i < values.size(); i++) {
265 if (i > 0) {
266 msg += ", ";
267 }
268 msg += StringPrintf("(%.3f)", values[i]);
269 }
270 }
271 msg += StringPrintf(", policyFlags=0x%08x", policyFlags);
272 return msg;
273}
274
Garfield Tane84e6f92019-08-29 17:28:41 -0700275// --- DispatchEntry ---
276
277volatile int32_t DispatchEntry::sNextSeqAtomic;
278
Prabir Pradhan24047542023-11-02 17:14:59 +0000279DispatchEntry::DispatchEntry(std::shared_ptr<const EventEntry> eventEntry,
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800280 ftl::Flags<InputTarget::Flags> targetFlags,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700281 const ui::Transform& transform, const ui::Transform& rawTransform,
282 float globalScaleFactor)
Garfield Tane84e6f92019-08-29 17:28:41 -0700283 : seq(nextSeq()),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700284 eventEntry(std::move(eventEntry)),
Garfield Tane84e6f92019-08-29 17:28:41 -0700285 targetFlags(targetFlags),
chaviw1ff3d1e2020-07-01 15:53:47 -0700286 transform(transform),
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700287 rawTransform(rawTransform),
Garfield Tane84e6f92019-08-29 17:28:41 -0700288 globalScaleFactor(globalScaleFactor),
Garfield Tane84e6f92019-08-29 17:28:41 -0700289 deliveryTime(0),
290 resolvedAction(0),
Siarhei Vishniakou2af5b032023-07-10 18:52:17 -0700291 resolvedFlags(0) {
292 switch (this->eventEntry->type) {
293 case EventEntry::Type::KEY: {
Prabir Pradhan24047542023-11-02 17:14:59 +0000294 const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*this->eventEntry);
Siarhei Vishniakou2af5b032023-07-10 18:52:17 -0700295 resolvedEventId = keyEntry.id;
296 resolvedAction = keyEntry.action;
297 resolvedFlags = keyEntry.flags;
298
299 break;
300 }
301 case EventEntry::Type::MOTION: {
Prabir Pradhan24047542023-11-02 17:14:59 +0000302 const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*this->eventEntry);
Siarhei Vishniakou2af5b032023-07-10 18:52:17 -0700303 resolvedEventId = motionEntry.id;
304 resolvedAction = motionEntry.action;
305 resolvedFlags = motionEntry.flags;
306 break;
307 }
308 default: {
309 break;
310 }
311 }
312}
Garfield Tane84e6f92019-08-29 17:28:41 -0700313
314uint32_t DispatchEntry::nextSeq() {
315 // Sequence number 0 is reserved and will never be returned.
316 uint32_t seq;
317 do {
318 seq = android_atomic_inc(&sNextSeqAtomic);
319 } while (!seq);
320 return seq;
321}
322
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800323std::ostream& operator<<(std::ostream& out, const DispatchEntry& entry) {
324 out << "DispatchEntry{resolvedAction=";
325 switch (entry.eventEntry->type) {
326 case EventEntry::Type::KEY: {
327 out << KeyEvent::actionToString(entry.resolvedAction);
328 break;
329 }
330 case EventEntry::Type::MOTION: {
331 out << MotionEvent::actionToString(entry.resolvedAction);
332 break;
333 }
334 default: {
335 out << "<invalid, not a key or a motion>";
336 break;
337 }
338 }
339 std::string transform;
340 entry.transform.dump(transform, "transform");
341 out << ", resolvedFlags=" << entry.resolvedFlags
342 << ", targetFlags=" << entry.targetFlags.string() << ", transform=" << transform
Siarhei Vishniakouadb9fc92023-05-26 10:46:09 -0700343 << "} original: " << entry.eventEntry->getDescription();
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800344 return out;
345}
346
Garfield Tane84e6f92019-08-29 17:28:41 -0700347} // namespace android::inputdispatcher