blob: ace57f1b2bb44175a077612a229b8f5ffb2346e9 [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;
Garfield Tane84e6f92019-08-29 17:28:41 -070057 mutable int32_t refCount;
Siarhei Vishniakou49483272019-10-22 13:13:47 -070058 Type type;
Garfield Tane84e6f92019-08-29 17:28:41 -070059 nsecs_t eventTime;
60 uint32_t policyFlags;
61 InjectionState* injectionState;
62
63 bool dispatchInProgress; // initially false, set to true while dispatching
64
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050065 /**
66 * Injected keys are events from an external (probably untrusted) application
67 * and are not related to real hardware state. They come in via
68 * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED.
69 */
Garfield Tane84e6f92019-08-29 17:28:41 -070070 inline bool isInjected() const { return injectionState != nullptr; }
71
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050072 /**
73 * Synthesized events are either injected events, or events that come
74 * from real hardware, but aren't directly attributable to a specific hardware event.
75 * Key repeat is a synthesized event, because it is related to an actual hardware state
76 * (a key is currently pressed), but the repeat itself is generated by the framework.
77 */
Garfield Tanff1f1bb2020-01-28 13:24:04 -080078 inline bool isSynthesized() const {
79 return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER;
80 }
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050081
Garfield Tane84e6f92019-08-29 17:28:41 -070082 void release();
83
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050084 virtual std::string getDescription() const = 0;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070085
Garfield Tane84e6f92019-08-29 17:28:41 -070086protected:
Garfield Tan6a5a14e2020-01-28 13:24:04 -080087 EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070088 virtual ~EventEntry();
89 void releaseInjectionState();
90};
91
92struct ConfigurationChangedEntry : EventEntry {
Garfield Tan6a5a14e2020-01-28 13:24:04 -080093 explicit ConfigurationChangedEntry(int32_t id, nsecs_t eventTime);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050094 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -070095
96protected:
97 virtual ~ConfigurationChangedEntry();
98};
99
100struct DeviceResetEntry : EventEntry {
101 int32_t deviceId;
102
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800103 DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500104 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700105
106protected:
107 virtual ~DeviceResetEntry();
108};
109
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100110struct FocusEntry : EventEntry {
111 sp<IBinder> connectionToken;
112 bool hasFocus;
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700113 std::string_view reason;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100114
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700115 FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
116 std::string_view reason);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500117 std::string getDescription() const override;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100118
119protected:
120 virtual ~FocusEntry();
121};
122
Garfield Tane84e6f92019-08-29 17:28:41 -0700123struct KeyEntry : EventEntry {
124 int32_t deviceId;
125 uint32_t source;
126 int32_t displayId;
127 int32_t action;
128 int32_t flags;
129 int32_t keyCode;
130 int32_t scanCode;
131 int32_t metaState;
132 int32_t repeatCount;
133 nsecs_t downTime;
134
135 bool syntheticRepeat; // set to true for synthetic key repeats
136
137 enum InterceptKeyResult {
138 INTERCEPT_KEY_RESULT_UNKNOWN,
139 INTERCEPT_KEY_RESULT_SKIP,
140 INTERCEPT_KEY_RESULT_CONTINUE,
141 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
142 };
143 InterceptKeyResult interceptKeyResult; // set based on the interception result
144 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
145
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800146 KeyEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
147 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
148 int32_t metaState, int32_t repeatCount, nsecs_t downTime);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500149 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700150 void recycle();
151
152protected:
153 virtual ~KeyEntry();
154};
155
156struct MotionEntry : EventEntry {
Garfield Tane84e6f92019-08-29 17:28:41 -0700157 int32_t deviceId;
158 uint32_t source;
159 int32_t displayId;
160 int32_t action;
161 int32_t actionButton;
162 int32_t flags;
163 int32_t metaState;
164 int32_t buttonState;
165 MotionClassification classification;
166 int32_t edgeFlags;
167 float xPrecision;
168 float yPrecision;
169 float xCursorPosition;
170 float yCursorPosition;
171 nsecs_t downTime;
172 uint32_t pointerCount;
173 PointerProperties pointerProperties[MAX_POINTERS];
174 PointerCoords pointerCoords[MAX_POINTERS];
175
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800176 MotionEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source, int32_t displayId,
177 uint32_t policyFlags, int32_t action, int32_t actionButton, int32_t flags,
178 int32_t metaState, int32_t buttonState, MotionClassification classification,
179 int32_t edgeFlags, float xPrecision, float yPrecision, float xCursorPosition,
180 float yCursorPosition, nsecs_t downTime, uint32_t pointerCount,
181 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords,
182 float xOffset, float yOffset);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500183 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700184
185protected:
186 virtual ~MotionEntry();
187};
188
189// Tracks the progress of dispatching a particular event to a particular connection.
190struct DispatchEntry {
191 const uint32_t seq; // unique sequence number, never 0
192
193 EventEntry* eventEntry; // the event to dispatch
194 int32_t targetFlags;
chaviw1ff3d1e2020-07-01 15:53:47 -0700195 ui::Transform transform;
Garfield Tane84e6f92019-08-29 17:28:41 -0700196 float globalScaleFactor;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700197 // Both deliveryTime and timeoutTime are only populated when the entry is sent to the app,
198 // and will be undefined before that.
Garfield Tane84e6f92019-08-29 17:28:41 -0700199 nsecs_t deliveryTime; // time when the event was actually delivered
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700200 // An ANR will be triggered if a response for this entry is not received by timeoutTime
201 nsecs_t timeoutTime;
Garfield Tane84e6f92019-08-29 17:28:41 -0700202
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800203 // Set to the resolved ID, action and flags when the event is enqueued.
204 int32_t resolvedEventId;
Garfield Tane84e6f92019-08-29 17:28:41 -0700205 int32_t resolvedAction;
206 int32_t resolvedFlags;
207
chaviw1ff3d1e2020-07-01 15:53:47 -0700208 DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, ui::Transform transform,
209 float globalScaleFactor);
Garfield Tane84e6f92019-08-29 17:28:41 -0700210 ~DispatchEntry();
211
212 inline bool hasForegroundTarget() const { return targetFlags & InputTarget::FLAG_FOREGROUND; }
213
214 inline bool isSplit() const { return targetFlags & InputTarget::FLAG_SPLIT; }
215
216private:
217 static volatile int32_t sNextSeqAtomic;
218
219 static uint32_t nextSeq();
220};
221
Gang Wange9087892020-01-07 12:17:14 -0500222VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry);
223VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry);
224
Garfield Tane84e6f92019-08-29 17:28:41 -0700225class InputDispatcher;
226// A command entry captures state and behavior for an action to be performed in the
227// dispatch loop after the initial processing has taken place. It is essentially
228// a kind of continuation used to postpone sensitive policy interactions to a point
229// in the dispatch loop where it is safe to release the lock (generally after finishing
230// the critical parts of the dispatch cycle).
231//
232// The special thing about commands is that they can voluntarily release and reacquire
233// the dispatcher lock at will. Initially when the command starts running, the
234// dispatcher lock is held. However, if the command needs to call into the policy to
235// do some work, it can release the lock, do the work, then reacquire the lock again
236// before returning.
237//
238// This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
239// never calls into the policy while holding its lock.
240//
241// Commands are implicitly 'LockedInterruptible'.
242struct CommandEntry;
243typedef std::function<void(InputDispatcher&, CommandEntry*)> Command;
244
245class Connection;
246struct CommandEntry {
247 explicit CommandEntry(Command command);
248 ~CommandEntry();
249
250 Command command;
251
252 // parameters for the command (usage varies by command)
253 sp<Connection> connection;
254 nsecs_t eventTime;
255 KeyEntry* keyEntry;
Chris Yea209fde2020-07-22 13:54:51 -0700256 std::shared_ptr<InputApplicationHandle> inputApplicationHandle;
Garfield Tane84e6f92019-08-29 17:28:41 -0700257 std::string reason;
258 int32_t userActivityEventType;
259 uint32_t seq;
260 bool handled;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500261 std::shared_ptr<InputChannel> inputChannel;
Garfield Tane84e6f92019-08-29 17:28:41 -0700262 sp<IBinder> oldToken;
263 sp<IBinder> newToken;
264};
265
266} // namespace android::inputdispatcher
267
268#endif // _UI_INPUT_INPUTDISPATCHER_ENTRY_H