blob: d8a6548856c7d24bde9d528d4603351f10e0da58 [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
Prabir Pradhan99987712020-11-10 18:43:05 -0800117// --- PointerCaptureChangedEntry ---
118
119// PointerCaptureChanged notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER
120// for all entries.
121PointerCaptureChangedEntry::PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime,
122 bool hasPointerCapture)
123 : EventEntry(id, Type::POINTER_CAPTURE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
124 pointerCaptureEnabled(hasPointerCapture) {}
125
126PointerCaptureChangedEntry::~PointerCaptureChangedEntry() {}
127
128std::string PointerCaptureChangedEntry::getDescription() const {
129 return StringPrintf("PointerCaptureChangedEvent(pointerCaptureEnabled=%s)",
130 pointerCaptureEnabled ? "true" : "false");
131}
132
Garfield Tane84e6f92019-08-29 17:28:41 -0700133// --- KeyEntry ---
134
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800135KeyEntry::KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700136 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
137 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
138 nsecs_t downTime)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800139 : EventEntry(id, Type::KEY, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700140 deviceId(deviceId),
141 source(source),
142 displayId(displayId),
143 action(action),
144 flags(flags),
145 keyCode(keyCode),
146 scanCode(scanCode),
147 metaState(metaState),
148 repeatCount(repeatCount),
149 downTime(downTime),
150 syntheticRepeat(false),
151 interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN),
152 interceptKeyWakeupTime(0) {}
153
154KeyEntry::~KeyEntry() {}
155
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500156std::string KeyEntry::getDescription() const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700157 if (!GetBoolProperty("ro.debuggable", false)) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500158 return "KeyEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700159 }
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -0500160 return StringPrintf("KeyEvent(deviceId=%d, eventTime=%" PRIu64
161 ", source=0x%08x, displayId=%" PRId32 ", action=%s, "
Garfield Tane84e6f92019-08-29 17:28:41 -0700162 "flags=0x%08x, keyCode=%d, scanCode=%d, metaState=0x%08x, "
163 "repeatCount=%d), policyFlags=0x%08x",
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -0500164 deviceId, eventTime, source, displayId, KeyEvent::actionToString(action),
165 flags, keyCode, scanCode, metaState, repeatCount, policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -0700166}
167
168void KeyEntry::recycle() {
169 releaseInjectionState();
170
171 dispatchInProgress = false;
172 syntheticRepeat = false;
173 interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
174 interceptKeyWakeupTime = 0;
175}
176
177// --- MotionEntry ---
178
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800179MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
Garfield Tane84e6f92019-08-29 17:28:41 -0700180 int32_t displayId, uint32_t policyFlags, int32_t action,
181 int32_t actionButton, int32_t flags, int32_t metaState,
182 int32_t buttonState, MotionClassification classification,
183 int32_t edgeFlags, float xPrecision, float yPrecision,
184 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
185 uint32_t pointerCount, const PointerProperties* pointerProperties,
186 const PointerCoords* pointerCoords, float xOffset, float yOffset)
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800187 : EventEntry(id, Type::MOTION, eventTime, policyFlags),
Garfield Tane84e6f92019-08-29 17:28:41 -0700188 deviceId(deviceId),
189 source(source),
190 displayId(displayId),
191 action(action),
192 actionButton(actionButton),
193 flags(flags),
194 metaState(metaState),
195 buttonState(buttonState),
196 classification(classification),
197 edgeFlags(edgeFlags),
198 xPrecision(xPrecision),
199 yPrecision(yPrecision),
200 xCursorPosition(xCursorPosition),
201 yCursorPosition(yCursorPosition),
202 downTime(downTime),
203 pointerCount(pointerCount) {
204 for (uint32_t i = 0; i < pointerCount; i++) {
205 this->pointerProperties[i].copyFrom(pointerProperties[i]);
206 this->pointerCoords[i].copyFrom(pointerCoords[i]);
207 if (xOffset || yOffset) {
208 this->pointerCoords[i].applyOffset(xOffset, yOffset);
209 }
210 }
211}
212
213MotionEntry::~MotionEntry() {}
214
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500215std::string MotionEntry::getDescription() const {
Ashwini Orugantid399a522019-10-10 11:16:45 -0700216 if (!GetBoolProperty("ro.debuggable", false)) {
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500217 return "MotionEvent";
Ashwini Orugantid399a522019-10-10 11:16:45 -0700218 }
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500219 std::string msg;
Siarhei Vishniakoua1188f52020-10-20 20:14:52 -0500220 msg += StringPrintf("MotionEvent(deviceId=%d, eventTime=%" PRIu64
221 ", source=0x%08x, displayId=%" PRId32
Garfield Tane84e6f92019-08-29 17:28:41 -0700222 ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, "
223 "buttonState=0x%08x, "
224 "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
225 "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[",
Siarhei Vishniakouc68fdec2020-10-22 14:58:14 -0500226 deviceId, eventTime, source, displayId,
227 MotionEvent::actionToString(action).c_str(), actionButton, flags, metaState,
228 buttonState, motionClassificationToString(classification), edgeFlags,
229 xPrecision, yPrecision, xCursorPosition, yCursorPosition);
Garfield Tane84e6f92019-08-29 17:28:41 -0700230
231 for (uint32_t i = 0; i < pointerCount; i++) {
232 if (i) {
233 msg += ", ";
234 }
235 msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
236 pointerCoords[i].getY());
237 }
238 msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500239 return msg;
Garfield Tane84e6f92019-08-29 17:28:41 -0700240}
241
242// --- DispatchEntry ---
243
244volatile int32_t DispatchEntry::sNextSeqAtomic;
245
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700246DispatchEntry::DispatchEntry(std::shared_ptr<EventEntry> eventEntry, int32_t targetFlags,
247 ui::Transform transform, float globalScaleFactor)
Garfield Tane84e6f92019-08-29 17:28:41 -0700248 : seq(nextSeq()),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700249 eventEntry(std::move(eventEntry)),
Garfield Tane84e6f92019-08-29 17:28:41 -0700250 targetFlags(targetFlags),
chaviw1ff3d1e2020-07-01 15:53:47 -0700251 transform(transform),
Garfield Tane84e6f92019-08-29 17:28:41 -0700252 globalScaleFactor(globalScaleFactor),
Garfield Tane84e6f92019-08-29 17:28:41 -0700253 deliveryTime(0),
254 resolvedAction(0),
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700255 resolvedFlags(0) {}
Garfield Tane84e6f92019-08-29 17:28:41 -0700256
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