blob: 28c2799477b387214664485c9f77e66ca67ca7be [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 {
36 enum { TYPE_CONFIGURATION_CHANGED, TYPE_DEVICE_RESET, TYPE_KEY, TYPE_MOTION };
37
38 uint32_t sequenceNum;
39 mutable int32_t refCount;
40 int32_t type;
41 nsecs_t eventTime;
42 uint32_t policyFlags;
43 InjectionState* injectionState;
44
45 bool dispatchInProgress; // initially false, set to true while dispatching
46
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050047 /**
48 * Injected keys are events from an external (probably untrusted) application
49 * and are not related to real hardware state. They come in via
50 * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED.
51 */
Garfield Tane84e6f92019-08-29 17:28:41 -070052 inline bool isInjected() const { return injectionState != nullptr; }
53
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050054 /**
55 * Synthesized events are either injected events, or events that come
56 * from real hardware, but aren't directly attributable to a specific hardware event.
57 * Key repeat is a synthesized event, because it is related to an actual hardware state
58 * (a key is currently pressed), but the repeat itself is generated by the framework.
59 */
60 inline bool isSynthesized() const {
61 return isInjected() || sequenceNum == SYNTHESIZED_EVENT_SEQUENCE_NUM;
62 }
63
Garfield Tane84e6f92019-08-29 17:28:41 -070064 void release();
65
66 virtual void appendDescription(std::string& msg) const = 0;
67
68protected:
69 EventEntry(uint32_t sequenceNum, int32_t type, nsecs_t eventTime, uint32_t policyFlags);
70 virtual ~EventEntry();
71 void releaseInjectionState();
72};
73
74struct ConfigurationChangedEntry : EventEntry {
75 explicit ConfigurationChangedEntry(uint32_t sequenceNum, nsecs_t eventTime);
76 virtual void appendDescription(std::string& msg) const;
77
78protected:
79 virtual ~ConfigurationChangedEntry();
80};
81
82struct DeviceResetEntry : EventEntry {
83 int32_t deviceId;
84
85 DeviceResetEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId);
86 virtual void appendDescription(std::string& msg) const;
87
88protected:
89 virtual ~DeviceResetEntry();
90};
91
92struct KeyEntry : EventEntry {
93 int32_t deviceId;
94 uint32_t source;
95 int32_t displayId;
96 int32_t action;
97 int32_t flags;
98 int32_t keyCode;
99 int32_t scanCode;
100 int32_t metaState;
101 int32_t repeatCount;
102 nsecs_t downTime;
103
104 bool syntheticRepeat; // set to true for synthetic key repeats
105
106 enum InterceptKeyResult {
107 INTERCEPT_KEY_RESULT_UNKNOWN,
108 INTERCEPT_KEY_RESULT_SKIP,
109 INTERCEPT_KEY_RESULT_CONTINUE,
110 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
111 };
112 InterceptKeyResult interceptKeyResult; // set based on the interception result
113 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
114
115 KeyEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
116 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags,
117 int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount,
118 nsecs_t downTime);
119 virtual void appendDescription(std::string& msg) const;
120 void recycle();
121
122protected:
123 virtual ~KeyEntry();
124};
125
126struct MotionEntry : EventEntry {
127 nsecs_t eventTime;
128 int32_t deviceId;
129 uint32_t source;
130 int32_t displayId;
131 int32_t action;
132 int32_t actionButton;
133 int32_t flags;
134 int32_t metaState;
135 int32_t buttonState;
136 MotionClassification classification;
137 int32_t edgeFlags;
138 float xPrecision;
139 float yPrecision;
140 float xCursorPosition;
141 float yCursorPosition;
142 nsecs_t downTime;
143 uint32_t pointerCount;
144 PointerProperties pointerProperties[MAX_POINTERS];
145 PointerCoords pointerCoords[MAX_POINTERS];
146
147 MotionEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source,
148 int32_t displayId, uint32_t policyFlags, int32_t action, int32_t actionButton,
149 int32_t flags, int32_t metaState, int32_t buttonState,
150 MotionClassification classification, int32_t edgeFlags, float xPrecision,
151 float yPrecision, float xCursorPosition, float yCursorPosition, nsecs_t downTime,
152 uint32_t pointerCount, const PointerProperties* pointerProperties,
153 const PointerCoords* pointerCoords, float xOffset, float yOffset);
154 virtual void appendDescription(std::string& msg) const;
155
156protected:
157 virtual ~MotionEntry();
158};
159
160// Tracks the progress of dispatching a particular event to a particular connection.
161struct DispatchEntry {
162 const uint32_t seq; // unique sequence number, never 0
163
164 EventEntry* eventEntry; // the event to dispatch
165 int32_t targetFlags;
166 float xOffset;
167 float yOffset;
168 float globalScaleFactor;
169 float windowXScale = 1.0f;
170 float windowYScale = 1.0f;
171 nsecs_t deliveryTime; // time when the event was actually delivered
172
173 // Set to the resolved action and flags when the event is enqueued.
174 int32_t resolvedAction;
175 int32_t resolvedFlags;
176
177 DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, float xOffset, float yOffset,
178 float globalScaleFactor, float windowXScale, float windowYScale);
179 ~DispatchEntry();
180
181 inline bool hasForegroundTarget() const { return targetFlags & InputTarget::FLAG_FOREGROUND; }
182
183 inline bool isSplit() const { return targetFlags & InputTarget::FLAG_SPLIT; }
184
185private:
186 static volatile int32_t sNextSeqAtomic;
187
188 static uint32_t nextSeq();
189};
190
191class InputDispatcher;
192// A command entry captures state and behavior for an action to be performed in the
193// dispatch loop after the initial processing has taken place. It is essentially
194// a kind of continuation used to postpone sensitive policy interactions to a point
195// in the dispatch loop where it is safe to release the lock (generally after finishing
196// the critical parts of the dispatch cycle).
197//
198// The special thing about commands is that they can voluntarily release and reacquire
199// the dispatcher lock at will. Initially when the command starts running, the
200// dispatcher lock is held. However, if the command needs to call into the policy to
201// do some work, it can release the lock, do the work, then reacquire the lock again
202// before returning.
203//
204// This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
205// never calls into the policy while holding its lock.
206//
207// Commands are implicitly 'LockedInterruptible'.
208struct CommandEntry;
209typedef std::function<void(InputDispatcher&, CommandEntry*)> Command;
210
211class Connection;
212struct CommandEntry {
213 explicit CommandEntry(Command command);
214 ~CommandEntry();
215
216 Command command;
217
218 // parameters for the command (usage varies by command)
219 sp<Connection> connection;
220 nsecs_t eventTime;
221 KeyEntry* keyEntry;
222 sp<InputApplicationHandle> inputApplicationHandle;
223 std::string reason;
224 int32_t userActivityEventType;
225 uint32_t seq;
226 bool handled;
227 sp<InputChannel> inputChannel;
228 sp<IBinder> oldToken;
229 sp<IBinder> newToken;
230};
231
232} // namespace android::inputdispatcher
233
234#endif // _UI_INPUT_INPUTDISPATCHER_ENTRY_H