blob: 51f93994a8cfcb7b1e875fc7cde0611350819e01 [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 Vishniakoua1188f52020-10-20 20:14:52 -0500154 return StringPrintf("KeyEvent(deviceId=%d, eventTime=%" PRIu64
155 ", source=0x%08x, displayId=%" PRId32 ", action=%s, "
Garfield Tane84e6f92019-08-29 17:28:41 -0700156 "flags=0x%08x, keyCode=%d, scanCode=%d, metaState=0x%08x, "
157 "repeatCount=%d), policyFlags=0x%08x",
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -0500158 deviceId, eventTime, source, displayId, KeyEvent::actionToString(action),
159 flags, keyCode, scanCode, metaState, repeatCount, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -0700160}
161
162void KeyEntry::recycle() {
163 releaseInjectionState();
164
165 dispatchInProgress = false;
166 syntheticRepeat = false;
167 interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
168 interceptKeyWakeupTime = 0;
169}
170
171// --- MotionEntry ---
172
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800173MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700174 int32_t displayId, uint32_t policyFlags, int32_t action,
175 int32_t actionButton, int32_t flags, int32_t metaState,
176 int32_t buttonState, MotionClassification classification,
177 int32_t edgeFlags, float xPrecision, float yPrecision,
178 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
179 uint32_t pointerCount, const PointerProperties* pointerProperties,
180 const PointerCoords* pointerCoords, float xOffset, float yOffset)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800181 : EventEntry(id, Type::MOTION, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700182 deviceId(deviceId),
183 source(source),
184 displayId(displayId),
185 action(action),
186 actionButton(actionButton),
187 flags(flags),
188 metaState(metaState),
189 buttonState(buttonState),
190 classification(classification),
191 edgeFlags(edgeFlags),
192 xPrecision(xPrecision),
193 yPrecision(yPrecision),
194 xCursorPosition(xCursorPosition),
195 yCursorPosition(yCursorPosition),
196 downTime(downTime),
197 pointerCount(pointerCount) {
198 for (uint32_t i = 0; i < pointerCount; i++) {
199 this->pointerProperties[i].copyFrom(pointerProperties[i]);
200 this->pointerCoords[i].copyFrom(pointerCoords[i]);
201 if (xOffset || yOffset) {
202 this->pointerCoords[i].applyOffset(xOffset, yOffset);
203 }
204 }
205}
206
207MotionEntry::~MotionEntry() {}
208
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500209std::string MotionEntry::getDescription() const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700210 if (!GetBoolProperty("ro.debuggable", false)) {
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
215 ", source=0x%08x, 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 Vishniakoua1188f52020-10-20 20:14:52 -0500220 deviceId, eventTime, source, displayId, MotionEvent::actionToString(action),
Garfield Tane84e6f92019-08-29 17:28:41 -0700221 actionButton, flags, metaState, buttonState,
222 motionClassificationToString(classification), edgeFlags, xPrecision,
223 yPrecision, xCursorPosition, yCursorPosition);
224
225 for (uint32_t i = 0; i < pointerCount; i++) {
226 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
236// --- DispatchEntry ---
237
238volatile int32_t DispatchEntry::sNextSeqAtomic;
239
chaviw1ff3d1e2020-07-01 15:53:47 -0700240DispatchEntry::DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, ui::Transform transform,
241 float globalScaleFactor)
Garfield Tane84e6f92019-08-29 17:28:41 -0700242 : seq(nextSeq()),
243 eventEntry(eventEntry),
244 targetFlags(targetFlags),
chaviw1ff3d1e2020-07-01 15:53:47 -0700245 transform(transform),
Garfield Tane84e6f92019-08-29 17:28:41 -0700246 globalScaleFactor(globalScaleFactor),
Garfield Tane84e6f92019-08-29 17:28:41 -0700247 deliveryTime(0),
248 resolvedAction(0),
249 resolvedFlags(0) {
250 eventEntry->refCount += 1;
251}
252
253DispatchEntry::~DispatchEntry() {
254 eventEntry->release();
255}
256
257uint32_t DispatchEntry::nextSeq() {
258 // Sequence number 0 is reserved and will never be returned.
259 uint32_t seq;
260 do {
261 seq = android_atomic_inc(&sNextSeqAtomic);
262 } while (!seq);
263 return seq;
264}
265
266// --- CommandEntry ---
267
268CommandEntry::CommandEntry(Command command)
269 : command(command),
270 eventTime(0),
271 keyEntry(nullptr),
272 userActivityEventType(0),
273 seq(0),
274 handled(false) {}
275
276CommandEntry::~CommandEntry() {}
277
278} // namespace android::inputdispatcher