blob: 49630ad436aaf87c5de2e0ce44d29b8218e3d5c6 [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
31static std::string motionActionToString(int32_t action) {
32 // Convert MotionEvent action to string
33 switch (action & AMOTION_EVENT_ACTION_MASK) {
34 case AMOTION_EVENT_ACTION_DOWN:
35 return "DOWN";
36 case AMOTION_EVENT_ACTION_MOVE:
37 return "MOVE";
38 case AMOTION_EVENT_ACTION_UP:
39 return "UP";
40 case AMOTION_EVENT_ACTION_POINTER_DOWN:
41 return "POINTER_DOWN";
42 case AMOTION_EVENT_ACTION_POINTER_UP:
43 return "POINTER_UP";
44 }
45 return StringPrintf("%" PRId32, action);
46}
47
48static std::string keyActionToString(int32_t action) {
49 // Convert KeyEvent action to string
50 switch (action) {
51 case AKEY_EVENT_ACTION_DOWN:
52 return "DOWN";
53 case AKEY_EVENT_ACTION_UP:
54 return "UP";
55 case AKEY_EVENT_ACTION_MULTIPLE:
56 return "MULTIPLE";
57 }
58 return StringPrintf("%" PRId32, action);
59}
Gang Wange9087892020-01-07 12:17:14 -050060VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry) {
61 return {{VerifiedInputEvent::Type::KEY, entry.deviceId, entry.eventTime, entry.source,
62 entry.displayId},
63 entry.action,
64 entry.downTime,
65 entry.flags & VERIFIED_KEY_EVENT_FLAGS,
66 entry.keyCode,
67 entry.scanCode,
68 entry.metaState,
69 entry.repeatCount};
70}
71
72VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry) {
73 const float rawX = entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
74 const float rawY = entry.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
75 const int actionMasked = entry.action & AMOTION_EVENT_ACTION_MASK;
76 return {{VerifiedInputEvent::Type::MOTION, entry.deviceId, entry.eventTime, entry.source,
77 entry.displayId},
78 rawX,
79 rawY,
80 actionMasked,
81 entry.downTime,
82 entry.flags & VERIFIED_MOTION_EVENT_FLAGS,
83 entry.metaState,
84 entry.buttonState};
85}
Garfield Tane84e6f92019-08-29 17:28:41 -070086
87// --- EventEntry ---
88
Garfield Tanc51d1ba2020-01-28 13:24:04 -080089EventEntry::EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags)
90 : id(id),
Garfield Tane84e6f92019-08-29 17:28:41 -070091 refCount(1),
92 type(type),
93 eventTime(eventTime),
94 policyFlags(policyFlags),
95 injectionState(nullptr),
96 dispatchInProgress(false) {}
97
98EventEntry::~EventEntry() {
99 releaseInjectionState();
100}
101
102void EventEntry::release() {
103 refCount -= 1;
104 if (refCount == 0) {
105 delete this;
106 } else {
107 ALOG_ASSERT(refCount > 0);
108 }
109}
110
111void EventEntry::releaseInjectionState() {
112 if (injectionState) {
113 injectionState->release();
114 injectionState = nullptr;
115 }
116}
117
118// --- ConfigurationChangedEntry ---
119
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800120ConfigurationChangedEntry::ConfigurationChangedEntry(int32_t id, nsecs_t eventTime)
121 : EventEntry(id, Type::CONFIGURATION_CHANGED, eventTime, 0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -0700122
123ConfigurationChangedEntry::~ConfigurationChangedEntry() {}
124
125void ConfigurationChangedEntry::appendDescription(std::string& msg) const {
126 msg += StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags);
127}
128
129// --- DeviceResetEntry ---
130
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800131DeviceResetEntry::DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId)
132 : EventEntry(id, Type::DEVICE_RESET, eventTime, 0), deviceId(deviceId) {}
Garfield Tane84e6f92019-08-29 17:28:41 -0700133
134DeviceResetEntry::~DeviceResetEntry() {}
135
136void DeviceResetEntry::appendDescription(std::string& msg) const {
137 msg += StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags);
138}
139
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100140// --- FocusEntry ---
141
142// Focus notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER for all entries
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800143FocusEntry::FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus)
144 : EventEntry(id, Type::FOCUS, eventTime, POLICY_FLAG_PASS_TO_USER),
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100145 connectionToken(connectionToken),
146 hasFocus(hasFocus) {}
147
148FocusEntry::~FocusEntry() {}
149
150void FocusEntry::appendDescription(std::string& msg) const {
151 msg += StringPrintf("FocusEvent(hasFocus=%s)", hasFocus ? "true" : "false");
152}
153
Garfield Tane84e6f92019-08-29 17:28:41 -0700154// --- KeyEntry ---
155
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800156KeyEntry::KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700157 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
158 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
159 nsecs_t downTime)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800160 : EventEntry(id, Type::KEY, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700161 deviceId(deviceId),
162 source(source),
163 displayId(displayId),
164 action(action),
165 flags(flags),
166 keyCode(keyCode),
167 scanCode(scanCode),
168 metaState(metaState),
169 repeatCount(repeatCount),
170 downTime(downTime),
171 syntheticRepeat(false),
172 interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN),
173 interceptKeyWakeupTime(0) {}
174
175KeyEntry::~KeyEntry() {}
176
177void KeyEntry::appendDescription(std::string& msg) const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700178 msg += StringPrintf("KeyEvent");
179 if (!GetBoolProperty("ro.debuggable", false)) {
180 return;
181 }
182 msg += StringPrintf("(deviceId=%d, source=0x%08x, displayId=%" PRId32 ", action=%s, "
Garfield Tane84e6f92019-08-29 17:28:41 -0700183 "flags=0x%08x, keyCode=%d, scanCode=%d, metaState=0x%08x, "
184 "repeatCount=%d), policyFlags=0x%08x",
185 deviceId, source, displayId, keyActionToString(action).c_str(), flags,
186 keyCode, scanCode, metaState, repeatCount, policyFlags);
187}
188
189void KeyEntry::recycle() {
190 releaseInjectionState();
191
192 dispatchInProgress = false;
193 syntheticRepeat = false;
194 interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
195 interceptKeyWakeupTime = 0;
196}
197
198// --- MotionEntry ---
199
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800200MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700201 int32_t displayId, uint32_t policyFlags, int32_t action,
202 int32_t actionButton, int32_t flags, int32_t metaState,
203 int32_t buttonState, MotionClassification classification,
204 int32_t edgeFlags, float xPrecision, float yPrecision,
205 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
206 uint32_t pointerCount, const PointerProperties* pointerProperties,
207 const PointerCoords* pointerCoords, float xOffset, float yOffset)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800208 : EventEntry(id, Type::MOTION, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700209 eventTime(eventTime),
210 deviceId(deviceId),
211 source(source),
212 displayId(displayId),
213 action(action),
214 actionButton(actionButton),
215 flags(flags),
216 metaState(metaState),
217 buttonState(buttonState),
218 classification(classification),
219 edgeFlags(edgeFlags),
220 xPrecision(xPrecision),
221 yPrecision(yPrecision),
222 xCursorPosition(xCursorPosition),
223 yCursorPosition(yCursorPosition),
224 downTime(downTime),
225 pointerCount(pointerCount) {
226 for (uint32_t i = 0; i < pointerCount; i++) {
227 this->pointerProperties[i].copyFrom(pointerProperties[i]);
228 this->pointerCoords[i].copyFrom(pointerCoords[i]);
229 if (xOffset || yOffset) {
230 this->pointerCoords[i].applyOffset(xOffset, yOffset);
231 }
232 }
233}
234
235MotionEntry::~MotionEntry() {}
236
237void MotionEntry::appendDescription(std::string& msg) const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700238 msg += StringPrintf("MotionEvent");
239 if (!GetBoolProperty("ro.debuggable", false)) {
240 return;
241 }
242 msg += StringPrintf("(deviceId=%d, source=0x%08x, displayId=%" PRId32
Garfield Tane84e6f92019-08-29 17:28:41 -0700243 ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, "
244 "buttonState=0x%08x, "
245 "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
246 "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[",
247 deviceId, source, displayId, motionActionToString(action).c_str(),
248 actionButton, flags, metaState, buttonState,
249 motionClassificationToString(classification), edgeFlags, xPrecision,
250 yPrecision, xCursorPosition, yCursorPosition);
251
252 for (uint32_t i = 0; i < pointerCount; i++) {
253 if (i) {
254 msg += ", ";
255 }
256 msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
257 pointerCoords[i].getY());
258 }
259 msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
260}
261
262// --- DispatchEntry ---
263
264volatile int32_t DispatchEntry::sNextSeqAtomic;
265
266DispatchEntry::DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, float xOffset,
267 float yOffset, float globalScaleFactor, float windowXScale,
268 float windowYScale)
269 : seq(nextSeq()),
270 eventEntry(eventEntry),
271 targetFlags(targetFlags),
272 xOffset(xOffset),
273 yOffset(yOffset),
274 globalScaleFactor(globalScaleFactor),
275 windowXScale(windowXScale),
276 windowYScale(windowYScale),
277 deliveryTime(0),
278 resolvedAction(0),
279 resolvedFlags(0) {
280 eventEntry->refCount += 1;
281}
282
283DispatchEntry::~DispatchEntry() {
284 eventEntry->release();
285}
286
287uint32_t DispatchEntry::nextSeq() {
288 // Sequence number 0 is reserved and will never be returned.
289 uint32_t seq;
290 do {
291 seq = android_atomic_inc(&sNextSeqAtomic);
292 } while (!seq);
293 return seq;
294}
295
296// --- CommandEntry ---
297
298CommandEntry::CommandEntry(Command command)
299 : command(command),
300 eventTime(0),
301 keyEntry(nullptr),
302 userActivityEventType(0),
303 seq(0),
304 handled(false) {}
305
306CommandEntry::~CommandEntry() {}
307
308} // namespace android::inputdispatcher