blob: e27338e11d97d4cc1895d0883a4cd2dea9666131 [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#ifndef _UI_INPUT_INPUTDISPATCHER_ENTRY_H
18#define _UI_INPUT_INPUTDISPATCHER_ENTRY_H
19
20#include "InjectionState.h"
21#include "InputTarget.h"
22
chaviw3277faf2021-05-19 16:45:23 -050023#include <gui/InputApplication.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070024#include <input/Input.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070025#include <stdint.h>
26#include <utils/Timers.h>
27#include <functional>
28#include <string>
29
30namespace android::inputdispatcher {
31
32struct EventEntry {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010033 enum class Type {
34 CONFIGURATION_CHANGED,
35 DEVICE_RESET,
36 FOCUS,
37 KEY,
38 MOTION,
Chris Yef59a2f42020-10-16 12:55:26 -070039 SENSOR,
Prabir Pradhan99987712020-11-10 18:43:05 -080040 POINTER_CAPTURE_CHANGED,
arthurhungb89ccb02020-12-30 16:19:01 +080041 DRAG,
Antonio Kantek7242d8b2021-08-05 16:07:20 -070042 TOUCH_MODE_CHANGED,
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010043 };
Siarhei Vishniakou49483272019-10-22 13:13:47 -070044
Garfield Tan6a5a14e2020-01-28 13:24:04 -080045 int32_t id;
Siarhei Vishniakou49483272019-10-22 13:13:47 -070046 Type type;
Garfield Tane84e6f92019-08-29 17:28:41 -070047 nsecs_t eventTime;
48 uint32_t policyFlags;
49 InjectionState* injectionState;
50
51 bool dispatchInProgress; // initially false, set to true while dispatching
52
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050053 /**
54 * Injected keys are events from an external (probably untrusted) application
55 * and are not related to real hardware state. They come in via
56 * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED.
57 */
Garfield Tane84e6f92019-08-29 17:28:41 -070058 inline bool isInjected() const { return injectionState != nullptr; }
59
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050060 /**
61 * Synthesized events are either injected events, or events that come
62 * from real hardware, but aren't directly attributable to a specific hardware event.
63 * Key repeat is a synthesized event, because it is related to an actual hardware state
64 * (a key is currently pressed), but the repeat itself is generated by the framework.
65 */
Garfield Tanff1f1bb2020-01-28 13:24:04 -080066 inline bool isSynthesized() const {
67 return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER;
68 }
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050069
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050070 virtual std::string getDescription() const = 0;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070071
Garfield Tan6a5a14e2020-01-28 13:24:04 -080072 EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070073 virtual ~EventEntry();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -070074
75protected:
Garfield Tane84e6f92019-08-29 17:28:41 -070076 void releaseInjectionState();
77};
78
79struct ConfigurationChangedEntry : EventEntry {
Garfield Tan6a5a14e2020-01-28 13:24:04 -080080 explicit ConfigurationChangedEntry(int32_t id, nsecs_t eventTime);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050081 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -070082
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000083 ~ConfigurationChangedEntry() override;
Garfield Tane84e6f92019-08-29 17:28:41 -070084};
85
86struct DeviceResetEntry : EventEntry {
87 int32_t deviceId;
88
Garfield Tan6a5a14e2020-01-28 13:24:04 -080089 DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050090 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -070091
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000092 ~DeviceResetEntry() override;
Garfield Tane84e6f92019-08-29 17:28:41 -070093};
94
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010095struct FocusEntry : EventEntry {
96 sp<IBinder> connectionToken;
97 bool hasFocus;
Vishnu Nairc519ff72021-01-21 08:23:08 -080098 std::string reason;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010099
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700100 FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -0800101 const std::string& reason);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500102 std::string getDescription() const override;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100103
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000104 ~FocusEntry() override;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100105};
106
Prabir Pradhan99987712020-11-10 18:43:05 -0800107struct PointerCaptureChangedEntry : EventEntry {
108 bool pointerCaptureEnabled;
109
110 PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, bool hasPointerCapture);
111 std::string getDescription() const override;
112
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000113 ~PointerCaptureChangedEntry() override;
Prabir Pradhan99987712020-11-10 18:43:05 -0800114};
115
arthurhungb89ccb02020-12-30 16:19:01 +0800116struct DragEntry : EventEntry {
117 sp<IBinder> connectionToken;
118 bool isExiting;
119 float x, y;
120
121 DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting, float x,
122 float y);
123 std::string getDescription() const override;
124
125 ~DragEntry() override;
126};
127
Garfield Tane84e6f92019-08-29 17:28:41 -0700128struct KeyEntry : EventEntry {
129 int32_t deviceId;
130 uint32_t source;
131 int32_t displayId;
132 int32_t action;
133 int32_t flags;
134 int32_t keyCode;
135 int32_t scanCode;
136 int32_t metaState;
137 int32_t repeatCount;
138 nsecs_t downTime;
139
140 bool syntheticRepeat; // set to true for synthetic key repeats
141
142 enum InterceptKeyResult {
143 INTERCEPT_KEY_RESULT_UNKNOWN,
144 INTERCEPT_KEY_RESULT_SKIP,
145 INTERCEPT_KEY_RESULT_CONTINUE,
146 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
147 };
148 InterceptKeyResult interceptKeyResult; // set based on the interception result
149 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
150
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800151 KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
152 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
153 int32_t metaState, int32_t repeatCount, nsecs_t downTime);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500154 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700155 void recycle();
156
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000157 ~KeyEntry() override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700158};
159
160struct MotionEntry : EventEntry {
Garfield Tane84e6f92019-08-29 17:28:41 -0700161 int32_t deviceId;
162 uint32_t source;
163 int32_t displayId;
164 int32_t action;
165 int32_t actionButton;
166 int32_t flags;
167 int32_t metaState;
168 int32_t buttonState;
169 MotionClassification classification;
170 int32_t edgeFlags;
171 float xPrecision;
172 float yPrecision;
173 float xCursorPosition;
174 float yCursorPosition;
175 nsecs_t downTime;
176 uint32_t pointerCount;
177 PointerProperties pointerProperties[MAX_POINTERS];
178 PointerCoords pointerCoords[MAX_POINTERS];
179
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800180 MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
181 uint32_t policyFlags, int32_t action, int32_t actionButton, int32_t flags,
182 int32_t metaState, int32_t buttonState, MotionClassification classification,
183 int32_t edgeFlags, float xPrecision, float yPrecision, float xCursorPosition,
184 float yCursorPosition, nsecs_t downTime, uint32_t pointerCount,
185 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
186 float xOffset, float yOffset);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500187 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700188
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700189 ~MotionEntry() override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700190};
191
Chris Yef59a2f42020-10-16 12:55:26 -0700192struct SensorEntry : EventEntry {
193 int32_t deviceId;
194 uint32_t source;
195 InputDeviceSensorType sensorType;
196 InputDeviceSensorAccuracy accuracy;
197 bool accuracyChanged;
198 nsecs_t hwTimestamp;
199
200 std::vector<float> values;
201
202 SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
203 uint32_t policyFlags, nsecs_t hwTimestamp, InputDeviceSensorType sensorType,
204 InputDeviceSensorAccuracy accuracy, bool accuracyChanged,
205 std::vector<float> values);
206 std::string getDescription() const override;
207
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000208 ~SensorEntry() override;
Chris Yef59a2f42020-10-16 12:55:26 -0700209};
210
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700211struct TouchModeEntry : EventEntry {
212 bool inTouchMode;
213
214 TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode);
215 std::string getDescription() const override;
216
217 ~TouchModeEntry() override;
218};
219
Garfield Tane84e6f92019-08-29 17:28:41 -0700220// Tracks the progress of dispatching a particular event to a particular connection.
221struct DispatchEntry {
222 const uint32_t seq; // unique sequence number, never 0
223
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700224 std::shared_ptr<EventEntry> eventEntry; // the event to dispatch
Garfield Tane84e6f92019-08-29 17:28:41 -0700225 int32_t targetFlags;
chaviw1ff3d1e2020-07-01 15:53:47 -0700226 ui::Transform transform;
Garfield Tane84e6f92019-08-29 17:28:41 -0700227 float globalScaleFactor;
Evan Rosky09576692021-07-01 12:22:09 -0700228 uint32_t displayOrientation;
Evan Rosky44edce92021-05-14 18:09:55 -0700229 int2 displaySize;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700230 // Both deliveryTime and timeoutTime are only populated when the entry is sent to the app,
231 // and will be undefined before that.
Garfield Tane84e6f92019-08-29 17:28:41 -0700232 nsecs_t deliveryTime; // time when the event was actually delivered
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700233 // An ANR will be triggered if a response for this entry is not received by timeoutTime
234 nsecs_t timeoutTime;
Garfield Tane84e6f92019-08-29 17:28:41 -0700235
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800236 // Set to the resolved ID, action and flags when the event is enqueued.
237 int32_t resolvedEventId;
Garfield Tane84e6f92019-08-29 17:28:41 -0700238 int32_t resolvedAction;
239 int32_t resolvedFlags;
240
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700241 DispatchEntry(std::shared_ptr<EventEntry> eventEntry, int32_t targetFlags,
Evan Rosky09576692021-07-01 12:22:09 -0700242 ui::Transform transform, float globalScaleFactor, uint32_t displayOrientation,
243 int2 displaySize);
Garfield Tane84e6f92019-08-29 17:28:41 -0700244
245 inline bool hasForegroundTarget() const { return targetFlags & InputTarget::FLAG_FOREGROUND; }
246
247 inline bool isSplit() const { return targetFlags & InputTarget::FLAG_SPLIT; }
248
249private:
250 static volatile int32_t sNextSeqAtomic;
251
252 static uint32_t nextSeq();
253};
254
Gang Wange9087892020-01-07 12:17:14 -0500255VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry);
256VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry);
257
Garfield Tane84e6f92019-08-29 17:28:41 -0700258} // namespace android::inputdispatcher
259
260#endif // _UI_INPUT_INPUTDISPATCHER_ENTRY_H