blob: a4d9e4edc22c263f20a003ace62f1b12bacb5c08 [file] [log] [blame]
Garfield Tan73007b62019-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 Oruganti12592522019-10-10 11:16:45 -070021#include <android-base/properties.h>
Garfield Tan73007b62019-08-29 17:28:41 -070022#include <android-base/stringprintf.h>
23#include <cutils/atomic.h>
24#include <inttypes.h>
25
Ashwini Oruganti12592522019-10-10 11:16:45 -070026using android::base::GetBoolProperty;
Garfield Tan73007b62019-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}
60
61// --- EventEntry ---
62
63EventEntry::EventEntry(uint32_t sequenceNum, int32_t type, nsecs_t eventTime, uint32_t policyFlags)
64 : sequenceNum(sequenceNum),
65 refCount(1),
66 type(type),
67 eventTime(eventTime),
68 policyFlags(policyFlags),
69 injectionState(nullptr),
70 dispatchInProgress(false) {}
71
72EventEntry::~EventEntry() {
73 releaseInjectionState();
74}
75
76void EventEntry::release() {
77 refCount -= 1;
78 if (refCount == 0) {
79 delete this;
80 } else {
81 ALOG_ASSERT(refCount > 0);
82 }
83}
84
85void EventEntry::releaseInjectionState() {
86 if (injectionState) {
87 injectionState->release();
88 injectionState = nullptr;
89 }
90}
91
92// --- ConfigurationChangedEntry ---
93
94ConfigurationChangedEntry::ConfigurationChangedEntry(uint32_t sequenceNum, nsecs_t eventTime)
95 : EventEntry(sequenceNum, TYPE_CONFIGURATION_CHANGED, eventTime, 0) {}
96
97ConfigurationChangedEntry::~ConfigurationChangedEntry() {}
98
99void ConfigurationChangedEntry::appendDescription(std::string& msg) const {
100 msg += StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags);
101}
102
103// --- DeviceResetEntry ---
104
105DeviceResetEntry::DeviceResetEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId)
106 : EventEntry(sequenceNum, TYPE_DEVICE_RESET, eventTime, 0), deviceId(deviceId) {}
107
108DeviceResetEntry::~DeviceResetEntry() {}
109
110void DeviceResetEntry::appendDescription(std::string& msg) const {
111 msg += StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags);
112}
113
114// --- KeyEntry ---
115
116KeyEntry::KeyEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
117 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
118 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
119 nsecs_t downTime)
120 : EventEntry(sequenceNum, TYPE_KEY, eventTime, policyFlags),
121 deviceId(deviceId),
122 source(source),
123 displayId(displayId),
124 action(action),
125 flags(flags),
126 keyCode(keyCode),
127 scanCode(scanCode),
128 metaState(metaState),
129 repeatCount(repeatCount),
130 downTime(downTime),
131 syntheticRepeat(false),
132 interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN),
133 interceptKeyWakeupTime(0) {}
134
135KeyEntry::~KeyEntry() {}
136
137void KeyEntry::appendDescription(std::string& msg) const {
Ashwini Oruganti12592522019-10-10 11:16:45 -0700138 msg += StringPrintf("KeyEvent");
139 if (!GetBoolProperty("ro.debuggable", false)) {
140 return;
141 }
142 msg += StringPrintf("(deviceId=%d, source=0x%08x, displayId=%" PRId32 ", action=%s, "
Garfield Tan73007b62019-08-29 17:28:41 -0700143 "flags=0x%08x, keyCode=%d, scanCode=%d, metaState=0x%08x, "
144 "repeatCount=%d), policyFlags=0x%08x",
145 deviceId, source, displayId, keyActionToString(action).c_str(), flags,
146 keyCode, scanCode, metaState, repeatCount, policyFlags);
147}
148
149void KeyEntry::recycle() {
150 releaseInjectionState();
151
152 dispatchInProgress = false;
153 syntheticRepeat = false;
154 interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
155 interceptKeyWakeupTime = 0;
156}
157
158// --- MotionEntry ---
159
160MotionEntry::MotionEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
161 int32_t displayId, uint32_t policyFlags, int32_t action,
162 int32_t actionButton, int32_t flags, int32_t metaState,
163 int32_t buttonState, MotionClassification classification,
164 int32_t edgeFlags, float xPrecision, float yPrecision, nsecs_t downTime,
165 uint32_t pointerCount, const PointerProperties* pointerProperties,
166 const PointerCoords* pointerCoords, float xOffset, float yOffset)
167 : EventEntry(sequenceNum, TYPE_MOTION, eventTime, policyFlags),
168 eventTime(eventTime),
169 deviceId(deviceId),
170 source(source),
171 displayId(displayId),
172 action(action),
173 actionButton(actionButton),
174 flags(flags),
175 metaState(metaState),
176 buttonState(buttonState),
177 classification(classification),
178 edgeFlags(edgeFlags),
179 xPrecision(xPrecision),
180 yPrecision(yPrecision),
181 downTime(downTime),
182 pointerCount(pointerCount) {
183 for (uint32_t i = 0; i < pointerCount; i++) {
184 this->pointerProperties[i].copyFrom(pointerProperties[i]);
185 this->pointerCoords[i].copyFrom(pointerCoords[i]);
186 if (xOffset || yOffset) {
187 this->pointerCoords[i].applyOffset(xOffset, yOffset);
188 }
189 }
190}
191
192MotionEntry::~MotionEntry() {}
193
194void MotionEntry::appendDescription(std::string& msg) const {
Ashwini Oruganti12592522019-10-10 11:16:45 -0700195 msg += StringPrintf("MotionEvent");
196 if (!GetBoolProperty("ro.debuggable", false)) {
197 return;
198 }
199 msg += StringPrintf("(deviceId=%d, source=0x%08x, displayId=%" PRId32
Garfield Tan73007b62019-08-29 17:28:41 -0700200 ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, "
201 "buttonState=0x%08x, "
202 "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
203 "pointers=[",
204 deviceId, source, displayId, motionActionToString(action).c_str(),
205 actionButton, flags, metaState, buttonState,
206 motionClassificationToString(classification), edgeFlags, xPrecision,
207 yPrecision);
208
209 for (uint32_t i = 0; i < pointerCount; i++) {
210 if (i) {
211 msg += ", ";
212 }
213 msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
214 pointerCoords[i].getY());
215 }
216 msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
217}
218
219// --- DispatchEntry ---
220
221volatile int32_t DispatchEntry::sNextSeqAtomic;
222
223DispatchEntry::DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, float xOffset,
224 float yOffset, float globalScaleFactor, float windowXScale,
225 float windowYScale)
226 : seq(nextSeq()),
227 eventEntry(eventEntry),
228 targetFlags(targetFlags),
229 xOffset(xOffset),
230 yOffset(yOffset),
231 globalScaleFactor(globalScaleFactor),
232 windowXScale(windowXScale),
233 windowYScale(windowYScale),
234 deliveryTime(0),
235 resolvedAction(0),
236 resolvedFlags(0) {
237 eventEntry->refCount += 1;
238}
239
240DispatchEntry::~DispatchEntry() {
241 eventEntry->release();
242}
243
244uint32_t DispatchEntry::nextSeq() {
245 // Sequence number 0 is reserved and will never be returned.
246 uint32_t seq;
247 do {
248 seq = android_atomic_inc(&sNextSeqAtomic);
249 } while (!seq);
250 return seq;
251}
252
253// --- CommandEntry ---
254
255CommandEntry::CommandEntry(Command command)
256 : command(command),
257 eventTime(0),
258 keyEntry(nullptr),
259 userActivityEventType(0),
260 seq(0),
261 handled(false) {}
262
263CommandEntry::~CommandEntry() {}
264
265} // namespace android::inputdispatcher