blob: 640a69a2ec7d9b3643256349a6e597a03b4c3d9a [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
21#include <android-base/stringprintf.h>
22#include <cutils/atomic.h>
23#include <inttypes.h>
24
25using android::base::StringPrintf;
26
27namespace android::inputdispatcher {
28
29static std::string motionActionToString(int32_t action) {
30 // Convert MotionEvent action to string
31 switch (action & AMOTION_EVENT_ACTION_MASK) {
32 case AMOTION_EVENT_ACTION_DOWN:
33 return "DOWN";
34 case AMOTION_EVENT_ACTION_MOVE:
35 return "MOVE";
36 case AMOTION_EVENT_ACTION_UP:
37 return "UP";
38 case AMOTION_EVENT_ACTION_POINTER_DOWN:
39 return "POINTER_DOWN";
40 case AMOTION_EVENT_ACTION_POINTER_UP:
41 return "POINTER_UP";
42 }
43 return StringPrintf("%" PRId32, action);
44}
45
46static std::string keyActionToString(int32_t action) {
47 // Convert KeyEvent action to string
48 switch (action) {
49 case AKEY_EVENT_ACTION_DOWN:
50 return "DOWN";
51 case AKEY_EVENT_ACTION_UP:
52 return "UP";
53 case AKEY_EVENT_ACTION_MULTIPLE:
54 return "MULTIPLE";
55 }
56 return StringPrintf("%" PRId32, action);
57}
58
59// --- EventEntry ---
60
61EventEntry::EventEntry(uint32_t sequenceNum, int32_t type, nsecs_t eventTime, uint32_t policyFlags)
62 : sequenceNum(sequenceNum),
63 refCount(1),
64 type(type),
65 eventTime(eventTime),
66 policyFlags(policyFlags),
67 injectionState(nullptr),
68 dispatchInProgress(false) {}
69
70EventEntry::~EventEntry() {
71 releaseInjectionState();
72}
73
74void EventEntry::release() {
75 refCount -= 1;
76 if (refCount == 0) {
77 delete this;
78 } else {
79 ALOG_ASSERT(refCount > 0);
80 }
81}
82
83void EventEntry::releaseInjectionState() {
84 if (injectionState) {
85 injectionState->release();
86 injectionState = nullptr;
87 }
88}
89
90// --- ConfigurationChangedEntry ---
91
92ConfigurationChangedEntry::ConfigurationChangedEntry(uint32_t sequenceNum, nsecs_t eventTime)
93 : EventEntry(sequenceNum, TYPE_CONFIGURATION_CHANGED, eventTime, 0) {}
94
95ConfigurationChangedEntry::~ConfigurationChangedEntry() {}
96
97void ConfigurationChangedEntry::appendDescription(std::string& msg) const {
98 msg += StringPrintf("ConfigurationChangedEvent(), policyFlags=0x%08x", policyFlags);
99}
100
101// --- DeviceResetEntry ---
102
103DeviceResetEntry::DeviceResetEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId)
104 : EventEntry(sequenceNum, TYPE_DEVICE_RESET, eventTime, 0), deviceId(deviceId) {}
105
106DeviceResetEntry::~DeviceResetEntry() {}
107
108void DeviceResetEntry::appendDescription(std::string& msg) const {
109 msg += StringPrintf("DeviceResetEvent(deviceId=%d), policyFlags=0x%08x", deviceId, policyFlags);
110}
111
112// --- KeyEntry ---
113
114KeyEntry::KeyEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
115 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
116 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
117 nsecs_t downTime)
118 : EventEntry(sequenceNum, TYPE_KEY, eventTime, policyFlags),
119 deviceId(deviceId),
120 source(source),
121 displayId(displayId),
122 action(action),
123 flags(flags),
124 keyCode(keyCode),
125 scanCode(scanCode),
126 metaState(metaState),
127 repeatCount(repeatCount),
128 downTime(downTime),
129 syntheticRepeat(false),
130 interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN),
131 interceptKeyWakeupTime(0) {}
132
133KeyEntry::~KeyEntry() {}
134
135void KeyEntry::appendDescription(std::string& msg) const {
136 msg += StringPrintf("KeyEvent(deviceId=%d, source=0x%08x, displayId=%" PRId32 ", action=%s, "
137 "flags=0x%08x, keyCode=%d, scanCode=%d, metaState=0x%08x, "
138 "repeatCount=%d), policyFlags=0x%08x",
139 deviceId, source, displayId, keyActionToString(action).c_str(), flags,
140 keyCode, scanCode, metaState, repeatCount, policyFlags);
141}
142
143void KeyEntry::recycle() {
144 releaseInjectionState();
145
146 dispatchInProgress = false;
147 syntheticRepeat = false;
148 interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
149 interceptKeyWakeupTime = 0;
150}
151
152// --- MotionEntry ---
153
154MotionEntry::MotionEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
155 int32_t displayId, uint32_t policyFlags, int32_t action,
156 int32_t actionButton, int32_t flags, int32_t metaState,
157 int32_t buttonState, MotionClassification classification,
158 int32_t edgeFlags, float xPrecision, float yPrecision,
159 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
160 uint32_t pointerCount, const PointerProperties* pointerProperties,
161 const PointerCoords* pointerCoords, float xOffset, float yOffset)
162 : EventEntry(sequenceNum, TYPE_MOTION, eventTime, policyFlags),
163 eventTime(eventTime),
164 deviceId(deviceId),
165 source(source),
166 displayId(displayId),
167 action(action),
168 actionButton(actionButton),
169 flags(flags),
170 metaState(metaState),
171 buttonState(buttonState),
172 classification(classification),
173 edgeFlags(edgeFlags),
174 xPrecision(xPrecision),
175 yPrecision(yPrecision),
176 xCursorPosition(xCursorPosition),
177 yCursorPosition(yCursorPosition),
178 downTime(downTime),
179 pointerCount(pointerCount) {
180 for (uint32_t i = 0; i < pointerCount; i++) {
181 this->pointerProperties[i].copyFrom(pointerProperties[i]);
182 this->pointerCoords[i].copyFrom(pointerCoords[i]);
183 if (xOffset || yOffset) {
184 this->pointerCoords[i].applyOffset(xOffset, yOffset);
185 }
186 }
187}
188
189MotionEntry::~MotionEntry() {}
190
191void MotionEntry::appendDescription(std::string& msg) const {
192 msg += StringPrintf("MotionEvent(deviceId=%d, source=0x%08x, displayId=%" PRId32
193 ", action=%s, actionButton=0x%08x, flags=0x%08x, metaState=0x%08x, "
194 "buttonState=0x%08x, "
195 "classification=%s, edgeFlags=0x%08x, xPrecision=%.1f, yPrecision=%.1f, "
196 "xCursorPosition=%0.1f, yCursorPosition=%0.1f, pointers=[",
197 deviceId, source, displayId, motionActionToString(action).c_str(),
198 actionButton, flags, metaState, buttonState,
199 motionClassificationToString(classification), edgeFlags, xPrecision,
200 yPrecision, xCursorPosition, yCursorPosition);
201
202 for (uint32_t i = 0; i < pointerCount; i++) {
203 if (i) {
204 msg += ", ";
205 }
206 msg += StringPrintf("%d: (%.1f, %.1f)", pointerProperties[i].id, pointerCoords[i].getX(),
207 pointerCoords[i].getY());
208 }
209 msg += StringPrintf("]), policyFlags=0x%08x", policyFlags);
210}
211
212// --- DispatchEntry ---
213
214volatile int32_t DispatchEntry::sNextSeqAtomic;
215
216DispatchEntry::DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, float xOffset,
217 float yOffset, float globalScaleFactor, float windowXScale,
218 float windowYScale)
219 : seq(nextSeq()),
220 eventEntry(eventEntry),
221 targetFlags(targetFlags),
222 xOffset(xOffset),
223 yOffset(yOffset),
224 globalScaleFactor(globalScaleFactor),
225 windowXScale(windowXScale),
226 windowYScale(windowYScale),
227 deliveryTime(0),
228 resolvedAction(0),
229 resolvedFlags(0) {
230 eventEntry->refCount += 1;
231}
232
233DispatchEntry::~DispatchEntry() {
234 eventEntry->release();
235}
236
237uint32_t DispatchEntry::nextSeq() {
238 // Sequence number 0 is reserved and will never be returned.
239 uint32_t seq;
240 do {
241 seq = android_atomic_inc(&sNextSeqAtomic);
242 } while (!seq);
243 return seq;
244}
245
246// --- CommandEntry ---
247
248CommandEntry::CommandEntry(Command command)
249 : command(command),
250 eventTime(0),
251 keyEntry(nullptr),
252 userActivityEventType(0),
253 seq(0),
254 handled(false) {}
255
256CommandEntry::~CommandEntry() {}
257
258} // namespace android::inputdispatcher