blob: 7a121ce798c1db5873b625296128edcebbcbaf2e [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,
Dominik Laskowski75788452021-02-09 18:51:25 -080043
44 ftl_last = TOUCH_MODE_CHANGED
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010045 };
Siarhei Vishniakou49483272019-10-22 13:13:47 -070046
Garfield Tan6a5a14e2020-01-28 13:24:04 -080047 int32_t id;
Siarhei Vishniakou49483272019-10-22 13:13:47 -070048 Type type;
Garfield Tane84e6f92019-08-29 17:28:41 -070049 nsecs_t eventTime;
50 uint32_t policyFlags;
51 InjectionState* injectionState;
52
53 bool dispatchInProgress; // initially false, set to true while dispatching
54
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050055 /**
56 * Injected keys are events from an external (probably untrusted) application
57 * and are not related to real hardware state. They come in via
58 * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED.
59 */
Garfield Tane84e6f92019-08-29 17:28:41 -070060 inline bool isInjected() const { return injectionState != nullptr; }
61
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050062 /**
63 * Synthesized events are either injected events, or events that come
64 * from real hardware, but aren't directly attributable to a specific hardware event.
65 * Key repeat is a synthesized event, because it is related to an actual hardware state
66 * (a key is currently pressed), but the repeat itself is generated by the framework.
67 */
Garfield Tanff1f1bb2020-01-28 13:24:04 -080068 inline bool isSynthesized() const {
69 return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER;
70 }
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050071
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050072 virtual std::string getDescription() const = 0;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070073
Garfield Tan6a5a14e2020-01-28 13:24:04 -080074 EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070075 virtual ~EventEntry();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -070076
77protected:
Garfield Tane84e6f92019-08-29 17:28:41 -070078 void releaseInjectionState();
79};
80
81struct ConfigurationChangedEntry : EventEntry {
Garfield Tan6a5a14e2020-01-28 13:24:04 -080082 explicit ConfigurationChangedEntry(int32_t id, nsecs_t eventTime);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050083 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -070084
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000085 ~ConfigurationChangedEntry() override;
Garfield Tane84e6f92019-08-29 17:28:41 -070086};
87
88struct DeviceResetEntry : EventEntry {
89 int32_t deviceId;
90
Garfield Tan6a5a14e2020-01-28 13:24:04 -080091 DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050092 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -070093
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000094 ~DeviceResetEntry() override;
Garfield Tane84e6f92019-08-29 17:28:41 -070095};
96
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010097struct FocusEntry : EventEntry {
98 sp<IBinder> connectionToken;
99 bool hasFocus;
Vishnu Nairc519ff72021-01-21 08:23:08 -0800100 std::string reason;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100101
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700102 FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -0800103 const std::string& reason);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500104 std::string getDescription() const override;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100105
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000106 ~FocusEntry() override;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100107};
108
Prabir Pradhan99987712020-11-10 18:43:05 -0800109struct PointerCaptureChangedEntry : EventEntry {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000110 const PointerCaptureRequest pointerCaptureRequest;
Prabir Pradhan99987712020-11-10 18:43:05 -0800111
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000112 PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, const PointerCaptureRequest&);
Prabir Pradhan99987712020-11-10 18:43:05 -0800113 std::string getDescription() const override;
114
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000115 ~PointerCaptureChangedEntry() override;
Prabir Pradhan99987712020-11-10 18:43:05 -0800116};
117
arthurhungb89ccb02020-12-30 16:19:01 +0800118struct DragEntry : EventEntry {
119 sp<IBinder> connectionToken;
120 bool isExiting;
121 float x, y;
122
123 DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting, float x,
124 float y);
125 std::string getDescription() const override;
126
127 ~DragEntry() override;
128};
129
Garfield Tane84e6f92019-08-29 17:28:41 -0700130struct KeyEntry : EventEntry {
131 int32_t deviceId;
132 uint32_t source;
133 int32_t displayId;
134 int32_t action;
135 int32_t flags;
136 int32_t keyCode;
137 int32_t scanCode;
138 int32_t metaState;
139 int32_t repeatCount;
140 nsecs_t downTime;
141
142 bool syntheticRepeat; // set to true for synthetic key repeats
143
144 enum InterceptKeyResult {
145 INTERCEPT_KEY_RESULT_UNKNOWN,
146 INTERCEPT_KEY_RESULT_SKIP,
147 INTERCEPT_KEY_RESULT_CONTINUE,
148 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
149 };
150 InterceptKeyResult interceptKeyResult; // set based on the interception result
151 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
152
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800153 KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
154 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
155 int32_t metaState, int32_t repeatCount, nsecs_t downTime);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500156 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700157 void recycle();
158
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000159 ~KeyEntry() override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700160};
161
162struct MotionEntry : EventEntry {
Garfield Tane84e6f92019-08-29 17:28:41 -0700163 int32_t deviceId;
164 uint32_t source;
165 int32_t displayId;
166 int32_t action;
167 int32_t actionButton;
168 int32_t flags;
169 int32_t metaState;
170 int32_t buttonState;
171 MotionClassification classification;
172 int32_t edgeFlags;
173 float xPrecision;
174 float yPrecision;
175 float xCursorPosition;
176 float yCursorPosition;
177 nsecs_t downTime;
178 uint32_t pointerCount;
179 PointerProperties pointerProperties[MAX_POINTERS];
180 PointerCoords pointerCoords[MAX_POINTERS];
181
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800182 MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
183 uint32_t policyFlags, int32_t action, int32_t actionButton, int32_t flags,
184 int32_t metaState, int32_t buttonState, MotionClassification classification,
185 int32_t edgeFlags, float xPrecision, float yPrecision, float xCursorPosition,
186 float yCursorPosition, nsecs_t downTime, uint32_t pointerCount,
187 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
188 float xOffset, float yOffset);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500189 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700190
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700191 ~MotionEntry() override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700192};
193
Chris Yef59a2f42020-10-16 12:55:26 -0700194struct SensorEntry : EventEntry {
195 int32_t deviceId;
196 uint32_t source;
197 InputDeviceSensorType sensorType;
198 InputDeviceSensorAccuracy accuracy;
199 bool accuracyChanged;
200 nsecs_t hwTimestamp;
201
202 std::vector<float> values;
203
204 SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
205 uint32_t policyFlags, nsecs_t hwTimestamp, InputDeviceSensorType sensorType,
206 InputDeviceSensorAccuracy accuracy, bool accuracyChanged,
207 std::vector<float> values);
208 std::string getDescription() const override;
209
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000210 ~SensorEntry() override;
Chris Yef59a2f42020-10-16 12:55:26 -0700211};
212
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700213struct TouchModeEntry : EventEntry {
214 bool inTouchMode;
215
216 TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode);
217 std::string getDescription() const override;
218
219 ~TouchModeEntry() override;
220};
221
Garfield Tane84e6f92019-08-29 17:28:41 -0700222// Tracks the progress of dispatching a particular event to a particular connection.
223struct DispatchEntry {
224 const uint32_t seq; // unique sequence number, never 0
225
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700226 std::shared_ptr<EventEntry> eventEntry; // the event to dispatch
Garfield Tane84e6f92019-08-29 17:28:41 -0700227 int32_t targetFlags;
chaviw1ff3d1e2020-07-01 15:53:47 -0700228 ui::Transform transform;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700229 ui::Transform rawTransform;
Garfield Tane84e6f92019-08-29 17:28:41 -0700230 float globalScaleFactor;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700231 // Both deliveryTime and timeoutTime are only populated when the entry is sent to the app,
232 // and will be undefined before that.
Garfield Tane84e6f92019-08-29 17:28:41 -0700233 nsecs_t deliveryTime; // time when the event was actually delivered
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700234 // An ANR will be triggered if a response for this entry is not received by timeoutTime
235 nsecs_t timeoutTime;
Garfield Tane84e6f92019-08-29 17:28:41 -0700236
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800237 // Set to the resolved ID, action and flags when the event is enqueued.
238 int32_t resolvedEventId;
Garfield Tane84e6f92019-08-29 17:28:41 -0700239 int32_t resolvedAction;
240 int32_t resolvedFlags;
241
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700242 DispatchEntry(std::shared_ptr<EventEntry> eventEntry, int32_t targetFlags,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700243 const ui::Transform& transform, const ui::Transform& rawTransform,
244 float globalScaleFactor);
Garfield Tane84e6f92019-08-29 17:28:41 -0700245
246 inline bool hasForegroundTarget() const { return targetFlags & InputTarget::FLAG_FOREGROUND; }
247
248 inline bool isSplit() const { return targetFlags & InputTarget::FLAG_SPLIT; }
249
250private:
251 static volatile int32_t sNextSeqAtomic;
252
253 static uint32_t nextSeq();
254};
255
Gang Wange9087892020-01-07 12:17:14 -0500256VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry);
257VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry);
258
Garfield Tane84e6f92019-08-29 17:28:41 -0700259} // namespace android::inputdispatcher
260
261#endif // _UI_INPUT_INPUTDISPATCHER_ENTRY_H