blob: 0661709e2ef1db0ad40b02bd8d75846b65b4c254 [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,
39 };
Siarhei Vishniakou49483272019-10-22 13:13:47 -070040
41 static const char* typeToString(Type type) {
42 switch (type) {
43 case Type::CONFIGURATION_CHANGED:
44 return "CONFIGURATION_CHANGED";
45 case Type::DEVICE_RESET:
46 return "DEVICE_RESET";
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010047 case Type::FOCUS:
48 return "FOCUS";
Siarhei Vishniakou49483272019-10-22 13:13:47 -070049 case Type::KEY:
50 return "KEY";
51 case Type::MOTION:
52 return "MOTION";
53 }
54 }
Garfield Tane84e6f92019-08-29 17:28:41 -070055
Garfield Tan6a5a14e2020-01-28 13:24:04 -080056 int32_t id;
Siarhei Vishniakou49483272019-10-22 13:13:47 -070057 Type type;
Garfield Tane84e6f92019-08-29 17:28:41 -070058 nsecs_t eventTime;
59 uint32_t policyFlags;
60 InjectionState* injectionState;
61
62 bool dispatchInProgress; // initially false, set to true while dispatching
63
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050064 /**
65 * Injected keys are events from an external (probably untrusted) application
66 * and are not related to real hardware state. They come in via
67 * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED.
68 */
Garfield Tane84e6f92019-08-29 17:28:41 -070069 inline bool isInjected() const { return injectionState != nullptr; }
70
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050071 /**
72 * Synthesized events are either injected events, or events that come
73 * from real hardware, but aren't directly attributable to a specific hardware event.
74 * Key repeat is a synthesized event, because it is related to an actual hardware state
75 * (a key is currently pressed), but the repeat itself is generated by the framework.
76 */
Garfield Tanff1f1bb2020-01-28 13:24:04 -080077 inline bool isSynthesized() const {
78 return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER;
79 }
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050080
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050081 virtual std::string getDescription() const = 0;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070082
Garfield Tan6a5a14e2020-01-28 13:24:04 -080083 EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070084 virtual ~EventEntry();
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -070085
86protected:
Garfield Tane84e6f92019-08-29 17:28:41 -070087 void releaseInjectionState();
88};
89
90struct ConfigurationChangedEntry : EventEntry {
Garfield Tan6a5a14e2020-01-28 13:24:04 -080091 explicit ConfigurationChangedEntry(int32_t id, nsecs_t eventTime);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050092 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -070093
Garfield Tane84e6f92019-08-29 17:28:41 -070094 virtual ~ConfigurationChangedEntry();
95};
96
97struct DeviceResetEntry : EventEntry {
98 int32_t deviceId;
99
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800100 DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500101 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700102
Garfield Tane84e6f92019-08-29 17:28:41 -0700103 virtual ~DeviceResetEntry();
104};
105
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100106struct FocusEntry : EventEntry {
107 sp<IBinder> connectionToken;
108 bool hasFocus;
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700109 std::string_view reason;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100110
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700111 FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
112 std::string_view reason);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500113 std::string getDescription() const override;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100114
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100115 virtual ~FocusEntry();
116};
117
Garfield Tane84e6f92019-08-29 17:28:41 -0700118struct KeyEntry : EventEntry {
119 int32_t deviceId;
120 uint32_t source;
121 int32_t displayId;
122 int32_t action;
123 int32_t flags;
124 int32_t keyCode;
125 int32_t scanCode;
126 int32_t metaState;
127 int32_t repeatCount;
128 nsecs_t downTime;
129
130 bool syntheticRepeat; // set to true for synthetic key repeats
131
132 enum InterceptKeyResult {
133 INTERCEPT_KEY_RESULT_UNKNOWN,
134 INTERCEPT_KEY_RESULT_SKIP,
135 INTERCEPT_KEY_RESULT_CONTINUE,
136 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
137 };
138 InterceptKeyResult interceptKeyResult; // set based on the interception result
139 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
140
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800141 KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
142 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
143 int32_t metaState, int32_t repeatCount, nsecs_t downTime);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500144 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700145 void recycle();
146
Garfield Tane84e6f92019-08-29 17:28:41 -0700147 virtual ~KeyEntry();
148};
149
150struct MotionEntry : EventEntry {
Garfield Tane84e6f92019-08-29 17:28:41 -0700151 int32_t deviceId;
152 uint32_t source;
153 int32_t displayId;
154 int32_t action;
155 int32_t actionButton;
156 int32_t flags;
157 int32_t metaState;
158 int32_t buttonState;
159 MotionClassification classification;
160 int32_t edgeFlags;
161 float xPrecision;
162 float yPrecision;
163 float xCursorPosition;
164 float yCursorPosition;
165 nsecs_t downTime;
166 uint32_t pointerCount;
167 PointerProperties pointerProperties[MAX_POINTERS];
168 PointerCoords pointerCoords[MAX_POINTERS];
169
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800170 MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
171 uint32_t policyFlags, int32_t action, int32_t actionButton, int32_t flags,
172 int32_t metaState, int32_t buttonState, MotionClassification classification,
173 int32_t edgeFlags, float xPrecision, float yPrecision, float xCursorPosition,
174 float yCursorPosition, nsecs_t downTime, uint32_t pointerCount,
175 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
176 float xOffset, float yOffset);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500177 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700178
Garfield Tane84e6f92019-08-29 17:28:41 -0700179 virtual ~MotionEntry();
180};
181
182// Tracks the progress of dispatching a particular event to a particular connection.
183struct DispatchEntry {
184 const uint32_t seq; // unique sequence number, never 0
185
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700186 std::shared_ptr<EventEntry> eventEntry; // the event to dispatch
Garfield Tane84e6f92019-08-29 17:28:41 -0700187 int32_t targetFlags;
chaviw1ff3d1e2020-07-01 15:53:47 -0700188 ui::Transform transform;
Garfield Tane84e6f92019-08-29 17:28:41 -0700189 float globalScaleFactor;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700190 // Both deliveryTime and timeoutTime are only populated when the entry is sent to the app,
191 // and will be undefined before that.
Garfield Tane84e6f92019-08-29 17:28:41 -0700192 nsecs_t deliveryTime; // time when the event was actually delivered
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700193 // An ANR will be triggered if a response for this entry is not received by timeoutTime
194 nsecs_t timeoutTime;
Garfield Tane84e6f92019-08-29 17:28:41 -0700195
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800196 // Set to the resolved ID, action and flags when the event is enqueued.
197 int32_t resolvedEventId;
Garfield Tane84e6f92019-08-29 17:28:41 -0700198 int32_t resolvedAction;
199 int32_t resolvedFlags;
200
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700201 DispatchEntry(std::shared_ptr<EventEntry> eventEntry, int32_t targetFlags,
202 ui::Transform transform, float globalScaleFactor);
Garfield Tane84e6f92019-08-29 17:28:41 -0700203
204 inline bool hasForegroundTarget() const { return targetFlags & InputTarget::FLAG_FOREGROUND; }
205
206 inline bool isSplit() const { return targetFlags & InputTarget::FLAG_SPLIT; }
207
208private:
209 static volatile int32_t sNextSeqAtomic;
210
211 static uint32_t nextSeq();
212};
213
Gang Wange9087892020-01-07 12:17:14 -0500214VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry);
215VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry);
216
Garfield Tane84e6f92019-08-29 17:28:41 -0700217class InputDispatcher;
218// A command entry captures state and behavior for an action to be performed in the
219// dispatch loop after the initial processing has taken place. It is essentially
220// a kind of continuation used to postpone sensitive policy interactions to a point
221// in the dispatch loop where it is safe to release the lock (generally after finishing
222// the critical parts of the dispatch cycle).
223//
224// The special thing about commands is that they can voluntarily release and reacquire
225// the dispatcher lock at will. Initially when the command starts running, the
226// dispatcher lock is held. However, if the command needs to call into the policy to
227// do some work, it can release the lock, do the work, then reacquire the lock again
228// before returning.
229//
230// This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
231// never calls into the policy while holding its lock.
232//
233// Commands are implicitly 'LockedInterruptible'.
234struct CommandEntry;
235typedef std::function<void(InputDispatcher&, CommandEntry*)> Command;
236
237class Connection;
238struct CommandEntry {
239 explicit CommandEntry(Command command);
240 ~CommandEntry();
241
242 Command command;
243
244 // parameters for the command (usage varies by command)
245 sp<Connection> connection;
246 nsecs_t eventTime;
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700247 std::shared_ptr<KeyEntry> keyEntry;
Chris Yea209fde2020-07-22 13:54:51 -0700248 std::shared_ptr<InputApplicationHandle> inputApplicationHandle;
Garfield Tane84e6f92019-08-29 17:28:41 -0700249 std::string reason;
250 int32_t userActivityEventType;
251 uint32_t seq;
252 bool handled;
Siarhei Vishniakou2b4782c2020-11-07 01:51:18 -0600253 sp<IBinder> connectionToken;
Garfield Tane84e6f92019-08-29 17:28:41 -0700254 sp<IBinder> oldToken;
255 sp<IBinder> newToken;
Bernardo Rufino2e1f6512020-10-08 13:42:07 +0000256 std::string obscuringPackage;
Garfield Tane84e6f92019-08-29 17:28:41 -0700257};
258
259} // namespace android::inputdispatcher
260
261#endif // _UI_INPUT_INPUTDISPATCHER_ENTRY_H