blob: e8c37f0e6e61b523da83211ed60454d5ecc8e0b7 [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
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050032// Sequence number for synthesized or injected events.
33constexpr uint32_t SYNTHESIZED_EVENT_SEQUENCE_NUM = 0;
34
Garfield Tane84e6f92019-08-29 17:28:41 -070035struct EventEntry {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010036 enum class Type {
37 CONFIGURATION_CHANGED,
38 DEVICE_RESET,
39 FOCUS,
40 KEY,
41 MOTION,
42 };
Siarhei Vishniakou49483272019-10-22 13:13:47 -070043
44 static const char* typeToString(Type type) {
45 switch (type) {
46 case Type::CONFIGURATION_CHANGED:
47 return "CONFIGURATION_CHANGED";
48 case Type::DEVICE_RESET:
49 return "DEVICE_RESET";
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010050 case Type::FOCUS:
51 return "FOCUS";
Siarhei Vishniakou49483272019-10-22 13:13:47 -070052 case Type::KEY:
53 return "KEY";
54 case Type::MOTION:
55 return "MOTION";
56 }
57 }
Garfield Tane84e6f92019-08-29 17:28:41 -070058
59 uint32_t sequenceNum;
60 mutable int32_t refCount;
Siarhei Vishniakou49483272019-10-22 13:13:47 -070061 Type type;
Garfield Tane84e6f92019-08-29 17:28:41 -070062 nsecs_t eventTime;
63 uint32_t policyFlags;
64 InjectionState* injectionState;
65
66 bool dispatchInProgress; // initially false, set to true while dispatching
67
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050068 /**
69 * Injected keys are events from an external (probably untrusted) application
70 * and are not related to real hardware state. They come in via
71 * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED.
72 */
Garfield Tane84e6f92019-08-29 17:28:41 -070073 inline bool isInjected() const { return injectionState != nullptr; }
74
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050075 /**
76 * Synthesized events are either injected events, or events that come
77 * from real hardware, but aren't directly attributable to a specific hardware event.
78 * Key repeat is a synthesized event, because it is related to an actual hardware state
79 * (a key is currently pressed), but the repeat itself is generated by the framework.
80 */
81 inline bool isSynthesized() const {
82 return isInjected() || sequenceNum == SYNTHESIZED_EVENT_SEQUENCE_NUM;
83 }
84
Garfield Tane84e6f92019-08-29 17:28:41 -070085 void release();
86
87 virtual void appendDescription(std::string& msg) const = 0;
88
89protected:
Siarhei Vishniakou49483272019-10-22 13:13:47 -070090 EventEntry(uint32_t sequenceNum, Type type, nsecs_t eventTime, uint32_t policyFlags);
Garfield Tane84e6f92019-08-29 17:28:41 -070091 virtual ~EventEntry();
92 void releaseInjectionState();
93};
94
95struct ConfigurationChangedEntry : EventEntry {
96 explicit ConfigurationChangedEntry(uint32_t sequenceNum, nsecs_t eventTime);
97 virtual void appendDescription(std::string& msg) const;
98
99protected:
100 virtual ~ConfigurationChangedEntry();
101};
102
103struct DeviceResetEntry : EventEntry {
104 int32_t deviceId;
105
106 DeviceResetEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId);
107 virtual void appendDescription(std::string& msg) const;
108
109protected:
110 virtual ~DeviceResetEntry();
111};
112
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100113struct FocusEntry : EventEntry {
114 sp<IBinder> connectionToken;
115 bool hasFocus;
116
117 FocusEntry(uint32_t sequenceNum, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus);
118 virtual void appendDescription(std::string& msg) const;
119
120protected:
121 virtual ~FocusEntry();
122};
123
Garfield Tane84e6f92019-08-29 17:28:41 -0700124struct KeyEntry : EventEntry {
125 int32_t deviceId;
126 uint32_t source;
127 int32_t displayId;
128 int32_t action;
129 int32_t flags;
130 int32_t keyCode;
131 int32_t scanCode;
132 int32_t metaState;
133 int32_t repeatCount;
134 nsecs_t downTime;
135
136 bool syntheticRepeat; // set to true for synthetic key repeats
137
138 enum InterceptKeyResult {
139 INTERCEPT_KEY_RESULT_UNKNOWN,
140 INTERCEPT_KEY_RESULT_SKIP,
141 INTERCEPT_KEY_RESULT_CONTINUE,
142 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
143 };
144 InterceptKeyResult interceptKeyResult; // set based on the interception result
145 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
146
147 KeyEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
148 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
149 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
150 nsecs_t downTime);
151 virtual void appendDescription(std::string& msg) const;
152 void recycle();
153
154protected:
155 virtual ~KeyEntry();
156};
157
158struct MotionEntry : EventEntry {
159 nsecs_t eventTime;
160 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
179 MotionEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
180 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t actionButton,
181 int32_t flags, int32_t metaState, int32_t buttonState,
182 MotionClassification classification, int32_t edgeFlags, float xPrecision,
183 float yPrecision, float xCursorPosition, float yCursorPosition, nsecs_t downTime,
184 uint32_t pointerCount, const PointerProperties* pointerProperties,
185 const PointerCoords* pointerCoords, float xOffset, float yOffset);
186 virtual void appendDescription(std::string& msg) const;
187
188protected:
189 virtual ~MotionEntry();
190};
191
192// Tracks the progress of dispatching a particular event to a particular connection.
193struct DispatchEntry {
194 const uint32_t seq; // unique sequence number, never 0
195
196 EventEntry* eventEntry; // the event to dispatch
197 int32_t targetFlags;
198 float xOffset;
199 float yOffset;
200 float globalScaleFactor;
201 float windowXScale = 1.0f;
202 float windowYScale = 1.0f;
203 nsecs_t deliveryTime; // time when the event was actually delivered
204
205 // Set to the resolved action and flags when the event is enqueued.
206 int32_t resolvedAction;
207 int32_t resolvedFlags;
208
209 DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, float xOffset, float yOffset,
210 float globalScaleFactor, float windowXScale, float windowYScale);
211 ~DispatchEntry();
212
213 inline bool hasForegroundTarget() const { return targetFlags & InputTarget::FLAG_FOREGROUND; }
214
215 inline bool isSplit() const { return targetFlags & InputTarget::FLAG_SPLIT; }
216
217private:
218 static volatile int32_t sNextSeqAtomic;
219
220 static uint32_t nextSeq();
221};
222
223class InputDispatcher;
224// A command entry captures state and behavior for an action to be performed in the
225// dispatch loop after the initial processing has taken place. It is essentially
226// a kind of continuation used to postpone sensitive policy interactions to a point
227// in the dispatch loop where it is safe to release the lock (generally after finishing
228// the critical parts of the dispatch cycle).
229//
230// The special thing about commands is that they can voluntarily release and reacquire
231// the dispatcher lock at will. Initially when the command starts running, the
232// dispatcher lock is held. However, if the command needs to call into the policy to
233// do some work, it can release the lock, do the work, then reacquire the lock again
234// before returning.
235//
236// This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
237// never calls into the policy while holding its lock.
238//
239// Commands are implicitly 'LockedInterruptible'.
240struct CommandEntry;
241typedef std::function<void(InputDispatcher&, CommandEntry*)> Command;
242
243class Connection;
244struct CommandEntry {
245 explicit CommandEntry(Command command);
246 ~CommandEntry();
247
248 Command command;
249
250 // parameters for the command (usage varies by command)
251 sp<Connection> connection;
252 nsecs_t eventTime;
253 KeyEntry* keyEntry;
254 sp<InputApplicationHandle> inputApplicationHandle;
255 std::string reason;
256 int32_t userActivityEventType;
257 uint32_t seq;
258 bool handled;
259 sp<InputChannel> inputChannel;
260 sp<IBinder> oldToken;
261 sp<IBinder> newToken;
262};
263
264} // namespace android::inputdispatcher
265
266#endif // _UI_INPUT_INPUTDISPATCHER_ENTRY_H