blob: b6b7e634074af00877c8b65372eb508b79b88967 [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 type(type),
63 eventTime(eventTime),
64 policyFlags(policyFlags),
65 injectionState(nullptr),
66 dispatchInProgress(false) {}
67
68EventEntry::~EventEntry() {
69 releaseInjectionState();
70}
71
Garfield Tane84e6f92019-08-29 17:28:41 -070072void EventEntry::releaseInjectionState() {
73 if (injectionState) {
74 injectionState->release();
75 injectionState = nullptr;
76 }
77}
78
79// --- ConfigurationChangedEntry ---
80
Garfield Tanc51d1ba2020-01-28 13:24:04 -080081ConfigurationChangedEntry::ConfigurationChangedEntry(int32_t id, nsecs_t eventTime)
82 : EventEntry(id, Type::CONFIGURATION_CHANGED, eventTime, 0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070083
84ConfigurationChangedEntry::~ConfigurationChangedEntry() {}
85
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050086std::string ConfigurationChangedEntry::getDescription() const {
87 return StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070088}
89
90// --- DeviceResetEntry ---
91
Garfield Tanc51d1ba2020-01-28 13:24:04 -080092DeviceResetEntry::DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId)
93 : EventEntry(id, Type::DEVICE_RESET, eventTime, 0), deviceId(deviceId) {}
Garfield Tane84e6f92019-08-29 17:28:41 -070094
95DeviceResetEntry::~DeviceResetEntry() {}
96
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050097std::string DeviceResetEntry::getDescription() const {
98 return StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070099}
100
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100101// --- FocusEntry ---
102
103// 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 -0700104FocusEntry::FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
105 std::string_view reason)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800106 : EventEntry(id, Type::FOCUS, eventTime, POLICY_FLAG_PASS_TO_USER),
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100107 connectionToken(connectionToken),
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700108 hasFocus(hasFocus),
109 reason(reason) {}
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100110
111FocusEntry::~FocusEntry() {}
112
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500113std::string FocusEntry::getDescription() const {
114 return StringPrintf("FocusEvent(hasFocus=%s)", hasFocus ? "true" : "false");
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100115}
116
Garfield Tane84e6f92019-08-29 17:28:41 -0700117// --- KeyEntry ---
118
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800119KeyEntry::KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700120 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
121 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
122 nsecs_t downTime)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800123 : EventEntry(id, Type::KEY, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700124 deviceId(deviceId),
125 source(source),
126 displayId(displayId),
127 action(action),
128 flags(flags),
129 keyCode(keyCode),
130 scanCode(scanCode),
131 metaState(metaState),
132 repeatCount(repeatCount),
133 downTime(downTime),
134 syntheticRepeat(false),
135 interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN),
136 interceptKeyWakeupTime(0) {}
137
138KeyEntry::~KeyEntry() {}
139
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500140std::string KeyEntry::getDescription() const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700141 if (!GetBoolProperty("ro.debuggable", false)) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500142 return "KeyEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700143 }
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500144 return StringPrintf("KeyEvent(deviceId=%d, source=0x%08x, displayId=%" PRId32 ", action=%s, "
Garfield Tane84e6f92019-08-29 17:28:41 -0700145 "flags=0x%08x, keyCode=%d, scanCode=%d, metaState=0x%08x, "
146 "repeatCount=%d), policyFlags=0x%08x",
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700147 deviceId, source, displayId, KeyEvent::actionToString(action), flags,
Garfield Tane84e6f92019-08-29 17:28:41 -0700148 keyCode, scanCode, metaState, repeatCount, policyFlags);
149}
150
151void KeyEntry::recycle() {
152 releaseInjectionState();
153
154 dispatchInProgress = false;
155 syntheticRepeat = false;
156 interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
157 interceptKeyWakeupTime = 0;
158}
159
160// --- MotionEntry ---
161
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800162MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700163 int32_t displayId, uint32_t policyFlags, int32_t action,
164 int32_t actionButton, int32_t flags, int32_t metaState,
165 int32_t buttonState, MotionClassification classification,
166 int32_t edgeFlags, float xPrecision, float yPrecision,
167 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
168 uint32_t pointerCount, const PointerProperties* pointerProperties,
169 const PointerCoords* pointerCoords, float xOffset, float yOffset)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800170 : EventEntry(id, Type::MOTION, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700171 deviceId(deviceId),
172 source(source),
173 displayId(displayId),
174 action(action),
175 actionButton(actionButton),
176 flags(flags),
177 metaState(metaState),
178 buttonState(buttonState),
179 classification(classification),
180 edgeFlags(edgeFlags),
181 xPrecision(xPrecision),
182 yPrecision(yPrecision),
183 xCursorPosition(xCursorPosition),
184 yCursorPosition(yCursorPosition),
185 downTime(downTime),
186 pointerCount(pointerCount) {
187 for (uint32_t i = 0; i < pointerCount; i++) {
188 this->pointerProperties[i].copyFrom(pointerProperties[i]);
189 this->pointerCoords[i].copyFrom(pointerCoords[i]);
190 if (xOffset || yOffset) {
191 this->pointerCoords[i].applyOffset(xOffset, yOffset);
192 }
193 }
194}
195
196MotionEntry::~MotionEntry() {}
197
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500198std::string MotionEntry::getDescription() const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700199 if (!GetBoolProperty("ro.debuggable", false)) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500200 return "MotionEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700201 }
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500202 std::string msg;
203 msg += StringPrintf("MotionEvent(deviceId=%d, source=0x%08x, displayId=%" PRId32
Garfield Tane84e6f92019-08-29 17:28:41 -0700204 ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, "
205 "buttonState=0x%08x, "
206 "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
207 "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[",
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700208 deviceId, source, displayId, MotionEvent::actionToString(action),
Garfield Tane84e6f92019-08-29 17:28:41 -0700209 actionButton, flags, metaState, buttonState,
210 motionClassificationToString(classification), edgeFlags, xPrecision,
211 yPrecision, xCursorPosition, yCursorPosition);
212
213 for (uint32_t i = 0; i < pointerCount; i++) {
214 if (i) {
215 msg += ", ";
216 }
217 msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
218 pointerCoords[i].getY());
219 }
220 msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500221 return msg;
Garfield Tane84e6f92019-08-29 17:28:41 -0700222}
223
224// --- DispatchEntry ---
225
226volatile int32_t DispatchEntry::sNextSeqAtomic;
227
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700228DispatchEntry::DispatchEntry(std::shared_ptr<EventEntry> eventEntry, int32_t targetFlags,
229 ui::Transform transform, float globalScaleFactor)
Garfield Tane84e6f92019-08-29 17:28:41 -0700230 : seq(nextSeq()),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700231 eventEntry(std::move(eventEntry)),
Garfield Tane84e6f92019-08-29 17:28:41 -0700232 targetFlags(targetFlags),
chaviw1ff3d1e2020-07-01 15:53:47 -0700233 transform(transform),
Garfield Tane84e6f92019-08-29 17:28:41 -0700234 globalScaleFactor(globalScaleFactor),
Garfield Tane84e6f92019-08-29 17:28:41 -0700235 deliveryTime(0),
236 resolvedAction(0),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700237 resolvedFlags(0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -0700238
239uint32_t DispatchEntry::nextSeq() {
240 // Sequence number 0 is reserved and will never be returned.
241 uint32_t seq;
242 do {
243 seq = android_atomic_inc(&sNextSeqAtomic);
244 } while (!seq);
245 return seq;
246}
247
248// --- CommandEntry ---
249
250CommandEntry::CommandEntry(Command command)
251 : command(command),
252 eventTime(0),
253 keyEntry(nullptr),
254 userActivityEventType(0),
255 seq(0),
256 handled(false) {}
257
258CommandEntry::~CommandEntry() {}
259
260} // namespace android::inputdispatcher