blob: d44a21153500145dc0e7d82bf132663bd6140c6d [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
Prabir Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Garfield Tane84e6f92019-08-29 17:28:41 -070018
19#include "InjectionState.h"
20#include "InputTarget.h"
21
chaviw3277faf2021-05-19 16:45:23 -050022#include <gui/InputApplication.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070023#include <input/Input.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070024#include <stdint.h>
25#include <utils/Timers.h>
26#include <functional>
Siarhei Vishniakou72945a02023-09-18 18:30:25 -070027#include <ostream>
Garfield Tane84e6f92019-08-29 17:28:41 -070028#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,
Antonio Kantek7242d8b2021-08-05 16:07:20 -070042 TOUCH_MODE_CHANGED,
Dominik Laskowski75788452021-02-09 18:51:25 -080043
44 ftl_last = TOUCH_MODE_CHANGED
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010045 };
Siarhei Vishniakou49483272019-10-22 13:13:47 -070046
Garfield Tan6a5a14e2020-01-28 13:24:04 -080047 int32_t id;
Siarhei Vishniakou49483272019-10-22 13:13:47 -070048 Type type;
Garfield Tane84e6f92019-08-29 17:28:41 -070049 nsecs_t eventTime;
50 uint32_t policyFlags;
Prabir Pradhana8cdbe12023-11-01 21:30:02 +000051 std::shared_ptr<InjectionState> injectionState;
Garfield Tane84e6f92019-08-29 17:28:41 -070052
53 bool dispatchInProgress; // initially false, set to true while dispatching
54
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050055 /**
56 * Injected keys are events from an external (probably untrusted) application
57 * and are not related to real hardware state. They come in via
58 * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED.
59 */
Garfield Tane84e6f92019-08-29 17:28:41 -070060 inline bool isInjected() const { return injectionState != nullptr; }
61
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050062 /**
63 * Synthesized events are either injected events, or events that come
64 * from real hardware, but aren't directly attributable to a specific hardware event.
65 * Key repeat is a synthesized event, because it is related to an actual hardware state
66 * (a key is currently pressed), but the repeat itself is generated by the framework.
67 */
Garfield Tanff1f1bb2020-01-28 13:24:04 -080068 inline bool isSynthesized() const {
69 return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER;
70 }
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050071
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050072 virtual std::string getDescription() const = 0;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070073
Garfield Tan6a5a14e2020-01-28 13:24:04 -080074 EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags);
Prabir Pradhana8cdbe12023-11-01 21:30:02 +000075 virtual ~EventEntry() = default;
Garfield Tane84e6f92019-08-29 17:28:41 -070076};
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};
82
83struct DeviceResetEntry : EventEntry {
84 int32_t deviceId;
85
Garfield Tan6a5a14e2020-01-28 13:24:04 -080086 DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050087 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -070088};
89
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010090struct FocusEntry : EventEntry {
91 sp<IBinder> connectionToken;
92 bool hasFocus;
Vishnu Nairc519ff72021-01-21 08:23:08 -080093 std::string reason;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010094
Vishnu Nair7d3d00d2020-08-03 11:20:42 -070095 FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -080096 const std::string& reason);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050097 std::string getDescription() const override;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010098};
99
Prabir Pradhan99987712020-11-10 18:43:05 -0800100struct PointerCaptureChangedEntry : EventEntry {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000101 const PointerCaptureRequest pointerCaptureRequest;
Prabir Pradhan99987712020-11-10 18:43:05 -0800102
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000103 PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, const PointerCaptureRequest&);
Prabir Pradhan99987712020-11-10 18:43:05 -0800104 std::string getDescription() const override;
Prabir Pradhan99987712020-11-10 18:43:05 -0800105};
106
arthurhungb89ccb02020-12-30 16:19:01 +0800107struct DragEntry : EventEntry {
108 sp<IBinder> connectionToken;
109 bool isExiting;
110 float x, y;
111
112 DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting, float x,
113 float y);
114 std::string getDescription() const override;
arthurhungb89ccb02020-12-30 16:19:01 +0800115};
116
Garfield Tane84e6f92019-08-29 17:28:41 -0700117struct KeyEntry : EventEntry {
118 int32_t deviceId;
119 uint32_t source;
120 int32_t displayId;
121 int32_t action;
122 int32_t flags;
123 int32_t keyCode;
124 int32_t scanCode;
125 int32_t metaState;
126 int32_t repeatCount;
127 nsecs_t downTime;
128
129 bool syntheticRepeat; // set to true for synthetic key repeats
130
Michael Wright5caf55a2022-11-24 22:31:42 +0000131 enum class InterceptKeyResult {
132 UNKNOWN,
133 SKIP,
134 CONTINUE,
135 TRY_AGAIN_LATER,
Garfield Tane84e6f92019-08-29 17:28:41 -0700136 };
137 InterceptKeyResult interceptKeyResult; // set based on the interception result
138 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
139
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000140 KeyEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime,
141 int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
142 int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
143 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};
146
147struct MotionEntry : EventEntry {
Garfield Tane84e6f92019-08-29 17:28:41 -0700148 int32_t deviceId;
149 uint32_t source;
150 int32_t displayId;
151 int32_t action;
152 int32_t actionButton;
153 int32_t flags;
154 int32_t metaState;
155 int32_t buttonState;
156 MotionClassification classification;
157 int32_t edgeFlags;
158 float xPrecision;
159 float yPrecision;
160 float xCursorPosition;
161 float yCursorPosition;
162 nsecs_t downTime;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700163 std::vector<PointerProperties> pointerProperties;
164 std::vector<PointerCoords> pointerCoords;
165
166 size_t getPointerCount() const { return pointerProperties.size(); }
Garfield Tane84e6f92019-08-29 17:28:41 -0700167
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000168 MotionEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime,
169 int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
170 int32_t action, int32_t actionButton, int32_t flags, int32_t metaState,
171 int32_t buttonState, MotionClassification classification, int32_t edgeFlags,
172 float xPrecision, float yPrecision, float xCursorPosition, float yCursorPosition,
173 nsecs_t downTime, const std::vector<PointerProperties>& pointerProperties,
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700174 const std::vector<PointerCoords>& pointerCoords);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500175 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700176};
177
Siarhei Vishniakou72945a02023-09-18 18:30:25 -0700178std::ostream& operator<<(std::ostream& out, const MotionEntry& motionEntry);
179
Chris Yef59a2f42020-10-16 12:55:26 -0700180struct SensorEntry : EventEntry {
181 int32_t deviceId;
182 uint32_t source;
183 InputDeviceSensorType sensorType;
184 InputDeviceSensorAccuracy accuracy;
185 bool accuracyChanged;
186 nsecs_t hwTimestamp;
187
188 std::vector<float> values;
189
190 SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
191 uint32_t policyFlags, nsecs_t hwTimestamp, InputDeviceSensorType sensorType,
192 InputDeviceSensorAccuracy accuracy, bool accuracyChanged,
193 std::vector<float> values);
194 std::string getDescription() const override;
Chris Yef59a2f42020-10-16 12:55:26 -0700195};
196
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700197struct TouchModeEntry : EventEntry {
198 bool inTouchMode;
Antonio Kantek15beb512022-06-13 22:35:41 +0000199 int32_t displayId;
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700200
Antonio Kantek15beb512022-06-13 22:35:41 +0000201 TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode, int32_t displayId);
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700202 std::string getDescription() const override;
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700203};
204
Garfield Tane84e6f92019-08-29 17:28:41 -0700205// Tracks the progress of dispatching a particular event to a particular connection.
206struct DispatchEntry {
207 const uint32_t seq; // unique sequence number, never 0
208
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -0700209 std::shared_ptr<EventEntry> eventEntry; // the event to dispatch
Prabir Pradhan8c90d782023-09-15 21:16:44 +0000210 const ftl::Flags<InputTarget::Flags> targetFlags;
chaviw1ff3d1e2020-07-01 15:53:47 -0700211 ui::Transform transform;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700212 ui::Transform rawTransform;
Garfield Tane84e6f92019-08-29 17:28:41 -0700213 float globalScaleFactor;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700214 // Both deliveryTime and timeoutTime are only populated when the entry is sent to the app,
215 // and will be undefined before that.
Garfield Tane84e6f92019-08-29 17:28:41 -0700216 nsecs_t deliveryTime; // time when the event was actually delivered
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700217 // An ANR will be triggered if a response for this entry is not received by timeoutTime
218 nsecs_t timeoutTime;
Garfield Tane84e6f92019-08-29 17:28:41 -0700219
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800220 // Set to the resolved ID, action and flags when the event is enqueued.
221 int32_t resolvedEventId;
Garfield Tane84e6f92019-08-29 17:28:41 -0700222 int32_t resolvedAction;
223 int32_t resolvedFlags;
224
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800225 DispatchEntry(std::shared_ptr<EventEntry> eventEntry,
226 ftl::Flags<InputTarget::Flags> targetFlags, const ui::Transform& transform,
227 const ui::Transform& rawTransform, float globalScaleFactor);
Prabir Pradhan8c90d782023-09-15 21:16:44 +0000228 DispatchEntry(const DispatchEntry&) = delete;
229 DispatchEntry& operator=(const DispatchEntry&) = delete;
Garfield Tane84e6f92019-08-29 17:28:41 -0700230
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800231 inline bool hasForegroundTarget() const {
232 return targetFlags.test(InputTarget::Flags::FOREGROUND);
233 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700234
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800235 inline bool isSplit() const { return targetFlags.test(InputTarget::Flags::SPLIT); }
Garfield Tane84e6f92019-08-29 17:28:41 -0700236
237private:
238 static volatile int32_t sNextSeqAtomic;
239
240 static uint32_t nextSeq();
241};
242
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800243std::ostream& operator<<(std::ostream& out, const DispatchEntry& entry);
244
Gang Wange9087892020-01-07 12:17:14 -0500245VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700246VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry,
247 const ui::Transform& rawTransform);
Gang Wange9087892020-01-07 12:17:14 -0500248
Garfield Tane84e6f92019-08-29 17:28:41 -0700249} // namespace android::inputdispatcher