blob: 34fa2393bb71324f310f6f3000637cc1311276c0 [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 refCount(1),
63 type(type),
64 eventTime(eventTime),
65 policyFlags(policyFlags),
66 injectionState(nullptr),
67 dispatchInProgress(false) {}
68
69EventEntry::~EventEntry() {
70 releaseInjectionState();
71}
72
73void EventEntry::release() {
74 refCount -= 1;
75 if (refCount == 0) {
76 delete this;
77 } else {
78 ALOG_ASSERT(refCount > 0);
79 }
80}
81
82void EventEntry::releaseInjectionState() {
83 if (injectionState) {
84 injectionState->release();
85 injectionState = nullptr;
86 }
87}
88
89// --- ConfigurationChangedEntry ---
90
Garfield Tanc51d1ba2020-01-28 13:24:04 -080091ConfigurationChangedEntry::ConfigurationChangedEntry(int32_t id, nsecs_t eventTime)
92 : EventEntry(id, Type::CONFIGURATION_CHANGED, eventTime, 0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070093
94ConfigurationChangedEntry::~ConfigurationChangedEntry() {}
95
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050096std::string ConfigurationChangedEntry::getDescription() const {
97 return StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070098}
99
100// --- DeviceResetEntry ---
101
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800102DeviceResetEntry::DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId)
103 : EventEntry(id, Type::DEVICE_RESET, eventTime, 0), deviceId(deviceId) {}
Garfield Tane84e6f92019-08-29 17:28:41 -0700104
105DeviceResetEntry::~DeviceResetEntry() {}
106
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500107std::string DeviceResetEntry::getDescription() const {
108 return StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -0700109}
110
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100111// --- FocusEntry ---
112
113// 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 -0700114FocusEntry::FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
115 std::string_view reason)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800116 : EventEntry(id, Type::FOCUS, eventTime, POLICY_FLAG_PASS_TO_USER),
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100117 connectionToken(connectionToken),
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700118 hasFocus(hasFocus),
119 reason(reason) {}
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100120
121FocusEntry::~FocusEntry() {}
122
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500123std::string FocusEntry::getDescription() const {
124 return StringPrintf("FocusEvent(hasFocus=%s)", hasFocus ? "true" : "false");
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100125}
126
Garfield Tane84e6f92019-08-29 17:28:41 -0700127// --- KeyEntry ---
128
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800129KeyEntry::KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700130 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
131 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
132 nsecs_t downTime)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800133 : EventEntry(id, Type::KEY, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700134 deviceId(deviceId),
135 source(source),
136 displayId(displayId),
137 action(action),
138 flags(flags),
139 keyCode(keyCode),
140 scanCode(scanCode),
141 metaState(metaState),
142 repeatCount(repeatCount),
143 downTime(downTime),
144 syntheticRepeat(false),
145 interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN),
146 interceptKeyWakeupTime(0) {}
147
148KeyEntry::~KeyEntry() {}
149
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500150std::string KeyEntry::getDescription() const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700151 if (!GetBoolProperty("ro.debuggable", false)) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500152 return "KeyEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700153 }
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500154 return StringPrintf("KeyEvent(deviceId=%d, source=0x%08x, displayId=%" PRId32 ", action=%s, "
Garfield Tane84e6f92019-08-29 17:28:41 -0700155 "flags=0x%08x, keyCode=%d, scanCode=%d, metaState=0x%08x, "
156 "repeatCount=%d), policyFlags=0x%08x",
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700157 deviceId, source, displayId, KeyEvent::actionToString(action), flags,
Garfield Tane84e6f92019-08-29 17:28:41 -0700158 keyCode, scanCode, metaState, repeatCount, policyFlags);
159}
160
161void KeyEntry::recycle() {
162 releaseInjectionState();
163
164 dispatchInProgress = false;
165 syntheticRepeat = false;
166 interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
167 interceptKeyWakeupTime = 0;
168}
169
170// --- MotionEntry ---
171
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800172MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700173 int32_t displayId, uint32_t policyFlags, int32_t action,
174 int32_t actionButton, int32_t flags, int32_t metaState,
175 int32_t buttonState, MotionClassification classification,
176 int32_t edgeFlags, float xPrecision, float yPrecision,
177 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
178 uint32_t pointerCount, const PointerProperties* pointerProperties,
179 const PointerCoords* pointerCoords, float xOffset, float yOffset)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800180 : EventEntry(id, Type::MOTION, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700181 deviceId(deviceId),
182 source(source),
183 displayId(displayId),
184 action(action),
185 actionButton(actionButton),
186 flags(flags),
187 metaState(metaState),
188 buttonState(buttonState),
189 classification(classification),
190 edgeFlags(edgeFlags),
191 xPrecision(xPrecision),
192 yPrecision(yPrecision),
193 xCursorPosition(xCursorPosition),
194 yCursorPosition(yCursorPosition),
195 downTime(downTime),
196 pointerCount(pointerCount) {
197 for (uint32_t i = 0; i < pointerCount; i++) {
198 this->pointerProperties[i].copyFrom(pointerProperties[i]);
199 this->pointerCoords[i].copyFrom(pointerCoords[i]);
200 if (xOffset || yOffset) {
201 this->pointerCoords[i].applyOffset(xOffset, yOffset);
202 }
203 }
204}
205
206MotionEntry::~MotionEntry() {}
207
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500208std::string MotionEntry::getDescription() const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700209 if (!GetBoolProperty("ro.debuggable", false)) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500210 return "MotionEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700211 }
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500212 std::string msg;
213 msg += StringPrintf("MotionEvent(deviceId=%d, source=0x%08x, displayId=%" PRId32
Garfield Tane84e6f92019-08-29 17:28:41 -0700214 ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, "
215 "buttonState=0x%08x, "
216 "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
217 "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[",
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700218 deviceId, source, displayId, MotionEvent::actionToString(action),
Garfield Tane84e6f92019-08-29 17:28:41 -0700219 actionButton, flags, metaState, buttonState,
220 motionClassificationToString(classification), edgeFlags, xPrecision,
221 yPrecision, xCursorPosition, yCursorPosition);
222
223 for (uint32_t i = 0; i < pointerCount; i++) {
224 if (i) {
225 msg += ", ";
226 }
227 msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
228 pointerCoords[i].getY());
229 }
230 msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500231 return msg;
Garfield Tane84e6f92019-08-29 17:28:41 -0700232}
233
234// --- DispatchEntry ---
235
236volatile int32_t DispatchEntry::sNextSeqAtomic;
237
chaviw1ff3d1e2020-07-01 15:53:47 -0700238DispatchEntry::DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, ui::Transform transform,
239 float globalScaleFactor)
Garfield Tane84e6f92019-08-29 17:28:41 -0700240 : seq(nextSeq()),
241 eventEntry(eventEntry),
242 targetFlags(targetFlags),
chaviw1ff3d1e2020-07-01 15:53:47 -0700243 transform(transform),
Garfield Tane84e6f92019-08-29 17:28:41 -0700244 globalScaleFactor(globalScaleFactor),
Garfield Tane84e6f92019-08-29 17:28:41 -0700245 deliveryTime(0),
246 resolvedAction(0),
247 resolvedFlags(0) {
248 eventEntry->refCount += 1;
249}
250
251DispatchEntry::~DispatchEntry() {
252 eventEntry->release();
253}
254
255uint32_t DispatchEntry::nextSeq() {
256 // Sequence number 0 is reserved and will never be returned.
257 uint32_t seq;
258 do {
259 seq = android_atomic_inc(&sNextSeqAtomic);
260 } while (!seq);
261 return seq;
262}
263
264// --- CommandEntry ---
265
266CommandEntry::CommandEntry(Command command)
267 : command(command),
268 eventTime(0),
269 keyEntry(nullptr),
270 userActivityEventType(0),
271 seq(0),
272 handled(false) {}
273
274CommandEntry::~CommandEntry() {}
275
276} // namespace android::inputdispatcher