blob: e5fb26c17749a96745141ae7d48c068d8b6fc56f [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
23#include <input/Input.h>
24#include <input/InputApplication.h>
25#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,
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010042 };
Siarhei Vishniakou49483272019-10-22 13:13:47 -070043
Garfield Tan6a5a14e2020-01-28 13:24:04 -080044 int32_t id;
Siarhei Vishniakou49483272019-10-22 13:13:47 -070045 Type type;
Garfield Tane84e6f92019-08-29 17:28:41 -070046 nsecs_t eventTime;
47 uint32_t policyFlags;
48 InjectionState* injectionState;
49
50 bool dispatchInProgress; // initially false, set to true while dispatching
51
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050052 /**
53 * Injected keys are events from an external (probably untrusted) application
54 * and are not related to real hardware state. They come in via
55 * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED.
56 */
Garfield Tane84e6f92019-08-29 17:28:41 -070057 inline bool isInjected() const { return injectionState != nullptr; }
58
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050059 /**
60 * Synthesized events are either injected events, or events that come
61 * from real hardware, but aren't directly attributable to a specific hardware event.
62 * Key repeat is a synthesized event, because it is related to an actual hardware state
63 * (a key is currently pressed), but the repeat itself is generated by the framework.
64 */
Garfield Tanff1f1bb2020-01-28 13:24:04 -080065 inline bool isSynthesized() const {
66 return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER;
67 }
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050068
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050069 virtual std::string getDescription() const = 0;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070070
Garfield Tan6a5a14e2020-01-28 13:24:04 -080071 EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070072 virtual ~EventEntry();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -070073
74protected:
Garfield Tane84e6f92019-08-29 17:28:41 -070075 void releaseInjectionState();
76};
77
78struct ConfigurationChangedEntry : EventEntry {
Garfield Tan6a5a14e2020-01-28 13:24:04 -080079 explicit ConfigurationChangedEntry(int32_t id, nsecs_t eventTime);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050080 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -070081
Garfield Tane84e6f92019-08-29 17:28:41 -070082 virtual ~ConfigurationChangedEntry();
83};
84
85struct DeviceResetEntry : EventEntry {
86 int32_t deviceId;
87
Garfield Tan6a5a14e2020-01-28 13:24:04 -080088 DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050089 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -070090
Garfield Tane84e6f92019-08-29 17:28:41 -070091 virtual ~DeviceResetEntry();
92};
93
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010094struct FocusEntry : EventEntry {
95 sp<IBinder> connectionToken;
96 bool hasFocus;
Vishnu Nairc519ff72021-01-21 08:23:08 -080097 std::string reason;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010098
Vishnu Nair7d3d00d2020-08-03 11:20:42 -070099 FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -0800100 const std::string& reason);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500101 std::string getDescription() const override;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100102
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100103 virtual ~FocusEntry();
104};
105
Prabir Pradhan99987712020-11-10 18:43:05 -0800106struct PointerCaptureChangedEntry : EventEntry {
107 bool pointerCaptureEnabled;
108
109 PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, bool hasPointerCapture);
110 std::string getDescription() const override;
111
112 virtual ~PointerCaptureChangedEntry();
113};
114
arthurhungb89ccb02020-12-30 16:19:01 +0800115struct DragEntry : EventEntry {
116 sp<IBinder> connectionToken;
117 bool isExiting;
118 float x, y;
119
120 DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting, float x,
121 float y);
122 std::string getDescription() const override;
123
124 ~DragEntry() override;
125};
126
Garfield Tane84e6f92019-08-29 17:28:41 -0700127struct KeyEntry : EventEntry {
128 int32_t deviceId;
129 uint32_t source;
130 int32_t displayId;
131 int32_t action;
132 int32_t flags;
133 int32_t keyCode;
134 int32_t scanCode;
135 int32_t metaState;
136 int32_t repeatCount;
137 nsecs_t downTime;
138
139 bool syntheticRepeat; // set to true for synthetic key repeats
140
141 enum InterceptKeyResult {
142 INTERCEPT_KEY_RESULT_UNKNOWN,
143 INTERCEPT_KEY_RESULT_SKIP,
144 INTERCEPT_KEY_RESULT_CONTINUE,
145 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
146 };
147 InterceptKeyResult interceptKeyResult; // set based on the interception result
148 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
149
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800150 KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
151 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
152 int32_t metaState, int32_t repeatCount, nsecs_t downTime);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500153 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700154 void recycle();
155
Garfield Tane84e6f92019-08-29 17:28:41 -0700156 virtual ~KeyEntry();
157};
158
159struct MotionEntry : EventEntry {
Garfield Tane84e6f92019-08-29 17:28:41 -0700160 int32_t deviceId;
161 uint32_t source;
162 int32_t displayId;
163 int32_t action;
164 int32_t actionButton;
165 int32_t flags;
166 int32_t metaState;
167 int32_t buttonState;
168 MotionClassification classification;
169 int32_t edgeFlags;
170 float xPrecision;
171 float yPrecision;
172 float xCursorPosition;
173 float yCursorPosition;
174 nsecs_t downTime;
175 uint32_t pointerCount;
176 PointerProperties pointerProperties[MAX_POINTERS];
177 PointerCoords pointerCoords[MAX_POINTERS];
178
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800179 MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
180 uint32_t policyFlags, int32_t action, int32_t actionButton, int32_t flags,
181 int32_t metaState, int32_t buttonState, MotionClassification classification,
182 int32_t edgeFlags, float xPrecision, float yPrecision, float xCursorPosition,
183 float yCursorPosition, nsecs_t downTime, uint32_t pointerCount,
184 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
185 float xOffset, float yOffset);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500186 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700187
Garfield Tane84e6f92019-08-29 17:28:41 -0700188 virtual ~MotionEntry();
189};
190
Chris Yef59a2f42020-10-16 12:55:26 -0700191struct SensorEntry : EventEntry {
192 int32_t deviceId;
193 uint32_t source;
194 InputDeviceSensorType sensorType;
195 InputDeviceSensorAccuracy accuracy;
196 bool accuracyChanged;
197 nsecs_t hwTimestamp;
198
199 std::vector<float> values;
200
201 SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
202 uint32_t policyFlags, nsecs_t hwTimestamp, InputDeviceSensorType sensorType,
203 InputDeviceSensorAccuracy accuracy, bool accuracyChanged,
204 std::vector<float> values);
205 std::string getDescription() const override;
206
207 virtual ~SensorEntry();
208};
209
Garfield Tane84e6f92019-08-29 17:28:41 -0700210// Tracks the progress of dispatching a particular event to a particular connection.
211struct DispatchEntry {
212 const uint32_t seq; // unique sequence number, never 0
213
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700214 std::shared_ptr<EventEntry> eventEntry; // the event to dispatch
Garfield Tane84e6f92019-08-29 17:28:41 -0700215 int32_t targetFlags;
chaviw1ff3d1e2020-07-01 15:53:47 -0700216 ui::Transform transform;
Garfield Tane84e6f92019-08-29 17:28:41 -0700217 float globalScaleFactor;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700218 // Both deliveryTime and timeoutTime are only populated when the entry is sent to the app,
219 // and will be undefined before that.
Garfield Tane84e6f92019-08-29 17:28:41 -0700220 nsecs_t deliveryTime; // time when the event was actually delivered
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700221 // An ANR will be triggered if a response for this entry is not received by timeoutTime
222 nsecs_t timeoutTime;
Garfield Tane84e6f92019-08-29 17:28:41 -0700223
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800224 // Set to the resolved ID, action and flags when the event is enqueued.
225 int32_t resolvedEventId;
Garfield Tane84e6f92019-08-29 17:28:41 -0700226 int32_t resolvedAction;
227 int32_t resolvedFlags;
228
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700229 DispatchEntry(std::shared_ptr<EventEntry> eventEntry, int32_t targetFlags,
230 ui::Transform transform, float globalScaleFactor);
Garfield Tane84e6f92019-08-29 17:28:41 -0700231
232 inline bool hasForegroundTarget() const { return targetFlags & InputTarget::FLAG_FOREGROUND; }
233
234 inline bool isSplit() const { return targetFlags & InputTarget::FLAG_SPLIT; }
235
236private:
237 static volatile int32_t sNextSeqAtomic;
238
239 static uint32_t nextSeq();
240};
241
Gang Wange9087892020-01-07 12:17:14 -0500242VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry);
243VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry);
244
Garfield Tane84e6f92019-08-29 17:28:41 -0700245class InputDispatcher;
246// A command entry captures state and behavior for an action to be performed in the
247// dispatch loop after the initial processing has taken place. It is essentially
248// a kind of continuation used to postpone sensitive policy interactions to a point
249// in the dispatch loop where it is safe to release the lock (generally after finishing
250// the critical parts of the dispatch cycle).
251//
252// The special thing about commands is that they can voluntarily release and reacquire
253// the dispatcher lock at will. Initially when the command starts running, the
254// dispatcher lock is held. However, if the command needs to call into the policy to
255// do some work, it can release the lock, do the work, then reacquire the lock again
256// before returning.
257//
258// This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
259// never calls into the policy while holding its lock.
260//
261// Commands are implicitly 'LockedInterruptible'.
262struct CommandEntry;
263typedef std::function<void(InputDispatcher&, CommandEntry*)> Command;
264
265class Connection;
266struct CommandEntry {
267 explicit CommandEntry(Command command);
268 ~CommandEntry();
269
270 Command command;
271
272 // parameters for the command (usage varies by command)
273 sp<Connection> connection;
274 nsecs_t eventTime;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700275 std::shared_ptr<KeyEntry> keyEntry;
Chris Yef59a2f42020-10-16 12:55:26 -0700276 std::shared_ptr<SensorEntry> sensorEntry;
Chris Yea209fde2020-07-22 13:54:51 -0700277 std::shared_ptr<InputApplicationHandle> inputApplicationHandle;
Garfield Tane84e6f92019-08-29 17:28:41 -0700278 std::string reason;
279 int32_t userActivityEventType;
280 uint32_t seq;
281 bool handled;
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600282 sp<IBinder> connectionToken;
Garfield Tane84e6f92019-08-29 17:28:41 -0700283 sp<IBinder> oldToken;
284 sp<IBinder> newToken;
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000285 std::string obscuringPackage;
Prabir Pradhan99987712020-11-10 18:43:05 -0800286 bool enabled;
Siarhei Vishniakou3c63fa42020-12-15 02:59:54 +0000287 int32_t pid;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000288 nsecs_t consumeTime; // time when the event was consumed by InputConsumer
Sean Stoutb4e0a592021-02-23 07:34:53 -0800289 int32_t displayId;
Garfield Tane84e6f92019-08-29 17:28:41 -0700290};
291
292} // namespace android::inputdispatcher
293
294#endif // _UI_INPUT_INPUTDISPATCHER_ENTRY_H