blob: 60f319a05614094ee45f20c7b78163b816fd0633 [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
Prabir Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Garfield Tane84e6f92019-08-29 17:28:41 -070018
19#include "InjectionState.h"
20#include "InputTarget.h"
21
chaviw3277faf2021-05-19 16:45:23 -050022#include <gui/InputApplication.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070023#include <input/Input.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070024#include <stdint.h>
25#include <utils/Timers.h>
26#include <functional>
27#include <string>
28
29namespace android::inputdispatcher {
30
31struct EventEntry {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010032 enum class Type {
33 CONFIGURATION_CHANGED,
34 DEVICE_RESET,
35 FOCUS,
36 KEY,
37 MOTION,
Chris Yef59a2f42020-10-16 12:55:26 -070038 SENSOR,
Prabir Pradhan99987712020-11-10 18:43:05 -080039 POINTER_CAPTURE_CHANGED,
arthurhungb89ccb02020-12-30 16:19:01 +080040 DRAG,
Antonio Kantek7242d8b2021-08-05 16:07:20 -070041 TOUCH_MODE_CHANGED,
Dominik Laskowski75788452021-02-09 18:51:25 -080042
43 ftl_last = TOUCH_MODE_CHANGED
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010044 };
Siarhei Vishniakou49483272019-10-22 13:13:47 -070045
Garfield Tan6a5a14e2020-01-28 13:24:04 -080046 int32_t id;
Siarhei Vishniakou49483272019-10-22 13:13:47 -070047 Type type;
Garfield Tane84e6f92019-08-29 17:28:41 -070048 nsecs_t eventTime;
49 uint32_t policyFlags;
50 InjectionState* injectionState;
51
52 bool dispatchInProgress; // initially false, set to true while dispatching
53
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050054 /**
55 * Injected keys are events from an external (probably untrusted) application
56 * and are not related to real hardware state. They come in via
57 * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED.
58 */
Garfield Tane84e6f92019-08-29 17:28:41 -070059 inline bool isInjected() const { return injectionState != nullptr; }
60
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050061 /**
62 * Synthesized events are either injected events, or events that come
63 * from real hardware, but aren't directly attributable to a specific hardware event.
64 * Key repeat is a synthesized event, because it is related to an actual hardware state
65 * (a key is currently pressed), but the repeat itself is generated by the framework.
66 */
Garfield Tanff1f1bb2020-01-28 13:24:04 -080067 inline bool isSynthesized() const {
68 return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER;
69 }
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050070
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050071 virtual std::string getDescription() const = 0;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070072
Garfield Tan6a5a14e2020-01-28 13:24:04 -080073 EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070074 virtual ~EventEntry();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -070075
76protected:
Garfield Tane84e6f92019-08-29 17:28:41 -070077 void releaseInjectionState();
78};
79
80struct ConfigurationChangedEntry : EventEntry {
Garfield Tan6a5a14e2020-01-28 13:24:04 -080081 explicit ConfigurationChangedEntry(int32_t id, nsecs_t eventTime);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050082 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -070083
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000084 ~ConfigurationChangedEntry() override;
Garfield Tane84e6f92019-08-29 17:28:41 -070085};
86
87struct DeviceResetEntry : EventEntry {
88 int32_t deviceId;
89
Garfield Tan6a5a14e2020-01-28 13:24:04 -080090 DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050091 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -070092
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000093 ~DeviceResetEntry() override;
Garfield Tane84e6f92019-08-29 17:28:41 -070094};
95
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010096struct FocusEntry : EventEntry {
97 sp<IBinder> connectionToken;
98 bool hasFocus;
Vishnu Nairc519ff72021-01-21 08:23:08 -080099 std::string reason;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100100
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700101 FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -0800102 const std::string& reason);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500103 std::string getDescription() const override;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100104
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000105 ~FocusEntry() override;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100106};
107
Prabir Pradhan99987712020-11-10 18:43:05 -0800108struct PointerCaptureChangedEntry : EventEntry {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000109 const PointerCaptureRequest pointerCaptureRequest;
Prabir Pradhan99987712020-11-10 18:43:05 -0800110
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000111 PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, const PointerCaptureRequest&);
Prabir Pradhan99987712020-11-10 18:43:05 -0800112 std::string getDescription() const override;
113
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000114 ~PointerCaptureChangedEntry() override;
Prabir Pradhan99987712020-11-10 18:43:05 -0800115};
116
arthurhungb89ccb02020-12-30 16:19:01 +0800117struct DragEntry : EventEntry {
118 sp<IBinder> connectionToken;
119 bool isExiting;
120 float x, y;
121
122 DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting, float x,
123 float y);
124 std::string getDescription() const override;
125
126 ~DragEntry() override;
127};
128
Garfield Tane84e6f92019-08-29 17:28:41 -0700129struct KeyEntry : EventEntry {
130 int32_t deviceId;
131 uint32_t source;
132 int32_t displayId;
133 int32_t action;
134 int32_t flags;
135 int32_t keyCode;
136 int32_t scanCode;
137 int32_t metaState;
138 int32_t repeatCount;
139 nsecs_t downTime;
140
141 bool syntheticRepeat; // set to true for synthetic key repeats
142
143 enum InterceptKeyResult {
144 INTERCEPT_KEY_RESULT_UNKNOWN,
145 INTERCEPT_KEY_RESULT_SKIP,
146 INTERCEPT_KEY_RESULT_CONTINUE,
147 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
148 };
149 InterceptKeyResult interceptKeyResult; // set based on the interception result
150 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
151
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800152 KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
153 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
154 int32_t metaState, int32_t repeatCount, nsecs_t downTime);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500155 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700156 void recycle();
157
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000158 ~KeyEntry() override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700159};
160
161struct MotionEntry : EventEntry {
Garfield Tane84e6f92019-08-29 17:28:41 -0700162 int32_t deviceId;
163 uint32_t source;
164 int32_t displayId;
165 int32_t action;
166 int32_t actionButton;
167 int32_t flags;
168 int32_t metaState;
169 int32_t buttonState;
170 MotionClassification classification;
171 int32_t edgeFlags;
172 float xPrecision;
173 float yPrecision;
174 float xCursorPosition;
175 float yCursorPosition;
176 nsecs_t downTime;
177 uint32_t pointerCount;
178 PointerProperties pointerProperties[MAX_POINTERS];
179 PointerCoords pointerCoords[MAX_POINTERS];
180
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800181 MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
182 uint32_t policyFlags, int32_t action, int32_t actionButton, int32_t flags,
183 int32_t metaState, int32_t buttonState, MotionClassification classification,
184 int32_t edgeFlags, float xPrecision, float yPrecision, float xCursorPosition,
185 float yCursorPosition, nsecs_t downTime, uint32_t pointerCount,
Prabir Pradhan5beda762021-12-10 09:30:08 +0000186 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords);
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;
Antonio Kantek15beb512022-06-13 22:35:41 +0000213 int32_t displayId;
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700214
Antonio Kantek15beb512022-06-13 22:35:41 +0000215 TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode, int32_t displayId);
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700216 std::string getDescription() const override;
217
218 ~TouchModeEntry() override;
219};
220
Garfield Tane84e6f92019-08-29 17:28:41 -0700221// Tracks the progress of dispatching a particular event to a particular connection.
222struct DispatchEntry {
223 const uint32_t seq; // unique sequence number, never 0
224
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700225 std::shared_ptr<EventEntry> eventEntry; // the event to dispatch
Garfield Tane84e6f92019-08-29 17:28:41 -0700226 int32_t targetFlags;
chaviw1ff3d1e2020-07-01 15:53:47 -0700227 ui::Transform transform;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700228 ui::Transform rawTransform;
Garfield Tane84e6f92019-08-29 17:28:41 -0700229 float globalScaleFactor;
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,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700242 const ui::Transform& transform, const ui::Transform& rawTransform,
243 float globalScaleFactor);
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);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700256VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry,
257 const ui::Transform& rawTransform);
Gang Wange9087892020-01-07 12:17:14 -0500258
Garfield Tane84e6f92019-08-29 17:28:41 -0700259} // namespace android::inputdispatcher