• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
21 #include <android-base/properties.h>
22 #include <android-base/stringprintf.h>
23 #include <cutils/atomic.h>
24 #include <inttypes.h>
25 
26 using android::base::GetBoolProperty;
27 using android::base::StringPrintf;
28 
29 namespace android::inputdispatcher {
30 
verifiedKeyEventFromKeyEntry(const KeyEntry & entry)31 VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry) {
32     return {{VerifiedInputEvent::Type::KEY, entry.deviceId, entry.eventTime, entry.source,
33              entry.displayId},
34             entry.action,
35             entry.flags & VERIFIED_KEY_EVENT_FLAGS,
36             entry.downTime,
37             entry.keyCode,
38             entry.scanCode,
39             entry.metaState,
40             entry.repeatCount};
41 }
42 
verifiedMotionEventFromMotionEntry(const MotionEntry & entry,const ui::Transform & rawTransform)43 VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry,
44                                                        const ui::Transform& rawTransform) {
45     const vec2 rawXY = MotionEvent::calculateTransformedXY(entry.source, rawTransform,
46                                                            entry.pointerCoords[0].getXYValue());
47     const int actionMasked = entry.action & AMOTION_EVENT_ACTION_MASK;
48     return {{VerifiedInputEvent::Type::MOTION, entry.deviceId, entry.eventTime, entry.source,
49              entry.displayId},
50             rawXY.x,
51             rawXY.y,
52             actionMasked,
53             entry.flags & VERIFIED_MOTION_EVENT_FLAGS,
54             entry.downTime,
55             entry.metaState,
56             entry.buttonState};
57 }
58 
59 // --- EventEntry ---
60 
EventEntry(int32_t id,Type type,nsecs_t eventTime,uint32_t policyFlags)61 EventEntry::EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags)
62       : id(id),
63         type(type),
64         eventTime(eventTime),
65         policyFlags(policyFlags),
66         injectionState(nullptr),
67         dispatchInProgress(false) {}
68 
~EventEntry()69 EventEntry::~EventEntry() {
70     releaseInjectionState();
71 }
72 
releaseInjectionState()73 void EventEntry::releaseInjectionState() {
74     if (injectionState) {
75         injectionState->release();
76         injectionState = nullptr;
77     }
78 }
79 
80 // --- ConfigurationChangedEntry ---
81 
ConfigurationChangedEntry(int32_t id,nsecs_t eventTime)82 ConfigurationChangedEntry::ConfigurationChangedEntry(int32_t id, nsecs_t eventTime)
83       : EventEntry(id, Type::CONFIGURATION_CHANGED, eventTime, 0) {}
84 
~ConfigurationChangedEntry()85 ConfigurationChangedEntry::~ConfigurationChangedEntry() {}
86 
getDescription() const87 std::string ConfigurationChangedEntry::getDescription() const {
88     return StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags);
89 }
90 
91 // --- DeviceResetEntry ---
92 
DeviceResetEntry(int32_t id,nsecs_t eventTime,int32_t deviceId)93 DeviceResetEntry::DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId)
94       : EventEntry(id, Type::DEVICE_RESET, eventTime, 0), deviceId(deviceId) {}
95 
~DeviceResetEntry()96 DeviceResetEntry::~DeviceResetEntry() {}
97 
getDescription() const98 std::string DeviceResetEntry::getDescription() const {
99     return StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags);
100 }
101 
102 // --- FocusEntry ---
103 
104 // Focus notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER for all entries
FocusEntry(int32_t id,nsecs_t eventTime,sp<IBinder> connectionToken,bool hasFocus,const std::string & reason)105 FocusEntry::FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
106                        const std::string& reason)
107       : EventEntry(id, Type::FOCUS, eventTime, POLICY_FLAG_PASS_TO_USER),
108         connectionToken(connectionToken),
109         hasFocus(hasFocus),
110         reason(reason) {}
111 
~FocusEntry()112 FocusEntry::~FocusEntry() {}
113 
getDescription() const114 std::string FocusEntry::getDescription() const {
115     return StringPrintf("FocusEvent(hasFocus=%s)", hasFocus ? "true" : "false");
116 }
117 
118 // --- PointerCaptureChangedEntry ---
119 
120 // PointerCaptureChanged notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER
121 // for all entries.
PointerCaptureChangedEntry(int32_t id,nsecs_t eventTime,const PointerCaptureRequest & request)122 PointerCaptureChangedEntry::PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime,
123                                                        const PointerCaptureRequest& request)
124       : EventEntry(id, Type::POINTER_CAPTURE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
125         pointerCaptureRequest(request) {}
126 
~PointerCaptureChangedEntry()127 PointerCaptureChangedEntry::~PointerCaptureChangedEntry() {}
128 
getDescription() const129 std::string PointerCaptureChangedEntry::getDescription() const {
130     return StringPrintf("PointerCaptureChangedEvent(pointerCaptureEnabled=%s)",
131                         pointerCaptureRequest.enable ? "true" : "false");
132 }
133 
134 // --- DragEntry ---
135 
136 // Drag notifications always go to apps, so set the flag POLICY_FLAG_PASS_TO_USER for all entries
DragEntry(int32_t id,nsecs_t eventTime,sp<IBinder> connectionToken,bool isExiting,float x,float y)137 DragEntry::DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting,
138                      float x, float y)
139       : EventEntry(id, Type::DRAG, eventTime, POLICY_FLAG_PASS_TO_USER),
140         connectionToken(connectionToken),
141         isExiting(isExiting),
142         x(x),
143         y(y) {}
144 
~DragEntry()145 DragEntry::~DragEntry() {}
146 
getDescription() const147 std::string DragEntry::getDescription() const {
148     return StringPrintf("DragEntry(isExiting=%s, x=%f, y=%f)", isExiting ? "true" : "false", x, y);
149 }
150 
151 // --- KeyEntry ---
152 
KeyEntry(int32_t id,nsecs_t eventTime,int32_t deviceId,uint32_t source,int32_t displayId,uint32_t policyFlags,int32_t action,int32_t flags,int32_t keyCode,int32_t scanCode,int32_t metaState,int32_t repeatCount,nsecs_t downTime)153 KeyEntry::KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
154                    int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
155                    int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
156                    nsecs_t downTime)
157       : EventEntry(id, Type::KEY, eventTime, policyFlags),
158         deviceId(deviceId),
159         source(source),
160         displayId(displayId),
161         action(action),
162         flags(flags),
163         keyCode(keyCode),
164         scanCode(scanCode),
165         metaState(metaState),
166         repeatCount(repeatCount),
167         downTime(downTime),
168         syntheticRepeat(false),
169         interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN),
170         interceptKeyWakeupTime(0) {}
171 
~KeyEntry()172 KeyEntry::~KeyEntry() {}
173 
getDescription() const174 std::string KeyEntry::getDescription() const {
175     if (!GetBoolProperty("ro.debuggable", false)) {
176         return "KeyEvent";
177     }
178     return StringPrintf("KeyEvent(deviceId=%d, eventTime=%" PRIu64 ", source=%s, displayId=%" PRId32
179                         ", action=%s, "
180                         "flags=0x%08x, keyCode=%s(%d), scanCode=%d, metaState=0x%08x, "
181                         "repeatCount=%d), policyFlags=0x%08x",
182                         deviceId, eventTime, inputEventSourceToString(source).c_str(), displayId,
183                         KeyEvent::actionToString(action), flags, KeyEvent::getLabel(keyCode),
184                         keyCode, scanCode, metaState, repeatCount, policyFlags);
185 }
186 
recycle()187 void KeyEntry::recycle() {
188     releaseInjectionState();
189 
190     dispatchInProgress = false;
191     syntheticRepeat = false;
192     interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
193     interceptKeyWakeupTime = 0;
194 }
195 
196 // --- TouchModeEntry ---
197 
TouchModeEntry(int32_t id,nsecs_t eventTime,bool inTouchMode)198 TouchModeEntry::TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode)
199       : EventEntry(id, Type::TOUCH_MODE_CHANGED, eventTime, POLICY_FLAG_PASS_TO_USER),
200         inTouchMode(inTouchMode) {}
201 
~TouchModeEntry()202 TouchModeEntry::~TouchModeEntry() {}
203 
getDescription() const204 std::string TouchModeEntry::getDescription() const {
205     return StringPrintf("TouchModeEvent(inTouchMode=%s)", inTouchMode ? "true" : "false");
206 }
207 
208 // --- MotionEntry ---
209 
MotionEntry(int32_t id,nsecs_t eventTime,int32_t deviceId,uint32_t source,int32_t displayId,uint32_t policyFlags,int32_t action,int32_t actionButton,int32_t flags,int32_t metaState,int32_t buttonState,MotionClassification classification,int32_t edgeFlags,float xPrecision,float yPrecision,float xCursorPosition,float yCursorPosition,nsecs_t downTime,uint32_t pointerCount,const PointerProperties * pointerProperties,const PointerCoords * pointerCoords)210 MotionEntry::MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
211                          int32_t displayId, uint32_t policyFlags, int32_t action,
212                          int32_t actionButton, int32_t flags, int32_t metaState,
213                          int32_t buttonState, MotionClassification classification,
214                          int32_t edgeFlags, float xPrecision, float yPrecision,
215                          float xCursorPosition, float yCursorPosition, nsecs_t downTime,
216                          uint32_t pointerCount, const PointerProperties* pointerProperties,
217                          const PointerCoords* pointerCoords)
218       : EventEntry(id, Type::MOTION, eventTime, policyFlags),
219         deviceId(deviceId),
220         source(source),
221         displayId(displayId),
222         action(action),
223         actionButton(actionButton),
224         flags(flags),
225         metaState(metaState),
226         buttonState(buttonState),
227         classification(classification),
228         edgeFlags(edgeFlags),
229         xPrecision(xPrecision),
230         yPrecision(yPrecision),
231         xCursorPosition(xCursorPosition),
232         yCursorPosition(yCursorPosition),
233         downTime(downTime),
234         pointerCount(pointerCount) {
235     for (uint32_t i = 0; i < pointerCount; i++) {
236         this->pointerProperties[i].copyFrom(pointerProperties[i]);
237         this->pointerCoords[i].copyFrom(pointerCoords[i]);
238     }
239 }
240 
~MotionEntry()241 MotionEntry::~MotionEntry() {}
242 
getDescription() const243 std::string MotionEntry::getDescription() const {
244     if (!GetBoolProperty("ro.debuggable", false)) {
245         return "MotionEvent";
246     }
247     std::string msg;
248     msg += StringPrintf("MotionEvent(deviceId=%d, eventTime=%" PRIu64
249                         ", source=%s, displayId=%" PRId32
250                         ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, "
251                         "buttonState=0x%08x, "
252                         "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
253                         "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[",
254                         deviceId, eventTime, inputEventSourceToString(source).c_str(), displayId,
255                         MotionEvent::actionToString(action).c_str(), actionButton, flags, metaState,
256                         buttonState, motionClassificationToString(classification), edgeFlags,
257                         xPrecision, yPrecision, xCursorPosition, yCursorPosition);
258 
259     for (uint32_t i = 0; i < pointerCount; i++) {
260         if (i) {
261             msg += ", ";
262         }
263         msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
264                             pointerCoords[i].getY());
265     }
266     msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
267     return msg;
268 }
269 
270 // --- SensorEntry ---
271 
SensorEntry(int32_t id,nsecs_t eventTime,int32_t deviceId,uint32_t source,uint32_t policyFlags,nsecs_t hwTimestamp,InputDeviceSensorType sensorType,InputDeviceSensorAccuracy accuracy,bool accuracyChanged,std::vector<float> values)272 SensorEntry::SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
273                          uint32_t policyFlags, nsecs_t hwTimestamp,
274                          InputDeviceSensorType sensorType, InputDeviceSensorAccuracy accuracy,
275                          bool accuracyChanged, std::vector<float> values)
276       : EventEntry(id, Type::SENSOR, eventTime, policyFlags),
277         deviceId(deviceId),
278         source(source),
279         sensorType(sensorType),
280         accuracy(accuracy),
281         accuracyChanged(accuracyChanged),
282         hwTimestamp(hwTimestamp),
283         values(std::move(values)) {}
284 
~SensorEntry()285 SensorEntry::~SensorEntry() {}
286 
getDescription() const287 std::string SensorEntry::getDescription() const {
288     std::string msg;
289     msg += StringPrintf("SensorEntry(deviceId=%d, source=%s, sensorType=%s, "
290                         "accuracy=0x%08x, hwTimestamp=%" PRId64,
291                         deviceId, inputEventSourceToString(source).c_str(),
292                         ftl::enum_string(sensorType).c_str(), accuracy, hwTimestamp);
293 
294     if (!GetBoolProperty("ro.debuggable", false)) {
295         for (size_t i = 0; i < values.size(); i++) {
296             if (i > 0) {
297                 msg += ", ";
298             }
299             msg += StringPrintf("(%.3f)", values[i]);
300         }
301     }
302     msg += StringPrintf(", policyFlags=0x%08x", policyFlags);
303     return msg;
304 }
305 
306 // --- DispatchEntry ---
307 
308 volatile int32_t DispatchEntry::sNextSeqAtomic;
309 
DispatchEntry(std::shared_ptr<EventEntry> eventEntry,int32_t targetFlags,const ui::Transform & transform,const ui::Transform & rawTransform,float globalScaleFactor)310 DispatchEntry::DispatchEntry(std::shared_ptr<EventEntry> eventEntry, int32_t targetFlags,
311                              const ui::Transform& transform, const ui::Transform& rawTransform,
312                              float globalScaleFactor)
313       : seq(nextSeq()),
314         eventEntry(std::move(eventEntry)),
315         targetFlags(targetFlags),
316         transform(transform),
317         rawTransform(rawTransform),
318         globalScaleFactor(globalScaleFactor),
319         deliveryTime(0),
320         resolvedAction(0),
321         resolvedFlags(0) {}
322 
nextSeq()323 uint32_t DispatchEntry::nextSeq() {
324     // Sequence number 0 is reserved and will never be returned.
325     uint32_t seq;
326     do {
327         seq = android_atomic_inc(&sNextSeqAtomic);
328     } while (!seq);
329     return seq;
330 }
331 
332 } // namespace android::inputdispatcher
333