blob: 06d5c7dd4b60950907353e4fcd03792592be1d3b [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"
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -080020#include "InputTargetFlags.h"
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +000021#include "trace/EventTrackerInterface.h"
Garfield Tane84e6f92019-08-29 17:28:41 -070022
chaviw3277faf2021-05-19 16:45:23 -050023#include <gui/InputApplication.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070024#include <input/Input.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070025#include <stdint.h>
26#include <utils/Timers.h>
27#include <functional>
Siarhei Vishniakou72945a02023-09-18 18:30:25 -070028#include <ostream>
Garfield Tane84e6f92019-08-29 17:28:41 -070029#include <string>
30
31namespace android::inputdispatcher {
32
33struct EventEntry {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010034 enum class Type {
35 CONFIGURATION_CHANGED,
36 DEVICE_RESET,
37 FOCUS,
38 KEY,
39 MOTION,
Chris Yef59a2f42020-10-16 12:55:26 -070040 SENSOR,
Prabir Pradhan99987712020-11-10 18:43:05 -080041 POINTER_CAPTURE_CHANGED,
arthurhungb89ccb02020-12-30 16:19:01 +080042 DRAG,
Antonio Kantek7242d8b2021-08-05 16:07:20 -070043 TOUCH_MODE_CHANGED,
Dominik Laskowski75788452021-02-09 18:51:25 -080044
45 ftl_last = TOUCH_MODE_CHANGED
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010046 };
Siarhei Vishniakou49483272019-10-22 13:13:47 -070047
Garfield Tan6a5a14e2020-01-28 13:24:04 -080048 int32_t id;
Siarhei Vishniakou49483272019-10-22 13:13:47 -070049 Type type;
Garfield Tane84e6f92019-08-29 17:28:41 -070050 nsecs_t eventTime;
51 uint32_t policyFlags;
Prabir Pradhana8cdbe12023-11-01 21:30:02 +000052 std::shared_ptr<InjectionState> injectionState;
Garfield Tane84e6f92019-08-29 17:28:41 -070053
Prabir Pradhan24047542023-11-02 17:14:59 +000054 mutable bool dispatchInProgress; // initially false, set to true while dispatching
Garfield Tane84e6f92019-08-29 17:28:41 -070055
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050056 /**
57 * Injected keys are events from an external (probably untrusted) application
58 * and are not related to real hardware state. They come in via
59 * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED.
60 */
Garfield Tane84e6f92019-08-29 17:28:41 -070061 inline bool isInjected() const { return injectionState != nullptr; }
62
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050063 /**
64 * Synthesized events are either injected events, or events that come
65 * from real hardware, but aren't directly attributable to a specific hardware event.
66 * Key repeat is a synthesized event, because it is related to an actual hardware state
67 * (a key is currently pressed), but the repeat itself is generated by the framework.
68 */
Garfield Tanff1f1bb2020-01-28 13:24:04 -080069 inline bool isSynthesized() const {
70 return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER;
71 }
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050072
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050073 virtual std::string getDescription() const = 0;
Siarhei Vishniakou4cb50ca2020-05-26 21:43:02 -070074
Garfield Tan6a5a14e2020-01-28 13:24:04 -080075 EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags);
Prabir Pradhan24047542023-11-02 17:14:59 +000076 EventEntry(const EventEntry&) = delete;
77 EventEntry& operator=(const EventEntry&) = delete;
Prabir Pradhana8cdbe12023-11-01 21:30:02 +000078 virtual ~EventEntry() = default;
Garfield Tane84e6f92019-08-29 17:28:41 -070079};
80
81struct ConfigurationChangedEntry : EventEntry {
Garfield Tan6a5a14e2020-01-28 13:24:04 -080082 explicit ConfigurationChangedEntry(int32_t id, nsecs_t eventTime);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050083 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -070084};
85
86struct DeviceResetEntry : EventEntry {
87 int32_t deviceId;
88
Garfield Tan6a5a14e2020-01-28 13:24:04 -080089 DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050090 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -070091};
92
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010093struct FocusEntry : EventEntry {
94 sp<IBinder> connectionToken;
95 bool hasFocus;
Vishnu Nairc519ff72021-01-21 08:23:08 -080096 std::string reason;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010097
Vishnu Nair7d3d00d2020-08-03 11:20:42 -070098 FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -080099 const std::string& reason);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500100 std::string getDescription() const override;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100101};
102
Prabir Pradhan99987712020-11-10 18:43:05 -0800103struct PointerCaptureChangedEntry : EventEntry {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000104 const PointerCaptureRequest pointerCaptureRequest;
Prabir Pradhan99987712020-11-10 18:43:05 -0800105
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000106 PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, const PointerCaptureRequest&);
Prabir Pradhan99987712020-11-10 18:43:05 -0800107 std::string getDescription() const override;
Prabir Pradhan99987712020-11-10 18:43:05 -0800108};
109
arthurhungb89ccb02020-12-30 16:19:01 +0800110struct DragEntry : EventEntry {
111 sp<IBinder> connectionToken;
112 bool isExiting;
113 float x, y;
114
115 DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting, float x,
116 float y);
117 std::string getDescription() const override;
arthurhungb89ccb02020-12-30 16:19:01 +0800118};
119
Garfield Tane84e6f92019-08-29 17:28:41 -0700120struct KeyEntry : EventEntry {
121 int32_t deviceId;
122 uint32_t source;
123 int32_t displayId;
124 int32_t action;
Garfield Tane84e6f92019-08-29 17:28:41 -0700125 int32_t keyCode;
126 int32_t scanCode;
127 int32_t metaState;
Garfield Tane84e6f92019-08-29 17:28:41 -0700128 nsecs_t downTime;
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000129 std::unique_ptr<trace::EventTrackerInterface> traceTracker;
Garfield Tane84e6f92019-08-29 17:28:41 -0700130
131 bool syntheticRepeat; // set to true for synthetic key repeats
132
Michael Wright5caf55a2022-11-24 22:31:42 +0000133 enum class InterceptKeyResult {
134 UNKNOWN,
135 SKIP,
136 CONTINUE,
137 TRY_AGAIN_LATER,
Garfield Tane84e6f92019-08-29 17:28:41 -0700138 };
Prabir Pradhan24047542023-11-02 17:14:59 +0000139 // These are special fields that may need to be modified while the event is being dispatched.
140 mutable InterceptKeyResult interceptKeyResult; // set based on the interception result
141 mutable nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
142 mutable int32_t flags;
Prabir Pradhan560d0d12024-03-07 18:08:27 +0000143 // TODO(b/328618922): Refactor key repeat generation to make repeatCount non-mutable.
Prabir Pradhan24047542023-11-02 17:14:59 +0000144 mutable int32_t repeatCount;
Garfield Tane84e6f92019-08-29 17:28:41 -0700145
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000146 KeyEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime,
147 int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
148 int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
149 int32_t repeatCount, nsecs_t downTime);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500150 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700151};
152
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000153std::ostream& operator<<(std::ostream& out, const KeyEntry& motionEntry);
154
Garfield Tane84e6f92019-08-29 17:28:41 -0700155struct MotionEntry : EventEntry {
Garfield Tane84e6f92019-08-29 17:28:41 -0700156 int32_t deviceId;
157 uint32_t source;
158 int32_t displayId;
159 int32_t action;
160 int32_t actionButton;
161 int32_t flags;
162 int32_t metaState;
163 int32_t buttonState;
164 MotionClassification classification;
165 int32_t edgeFlags;
166 float xPrecision;
167 float yPrecision;
168 float xCursorPosition;
169 float yCursorPosition;
170 nsecs_t downTime;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700171 std::vector<PointerProperties> pointerProperties;
172 std::vector<PointerCoords> pointerCoords;
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000173 std::unique_ptr<trace::EventTrackerInterface> traceTracker;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700174
175 size_t getPointerCount() const { return pointerProperties.size(); }
Garfield Tane84e6f92019-08-29 17:28:41 -0700176
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000177 MotionEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime,
178 int32_t deviceId, uint32_t source, int32_t displayId, uint32_t policyFlags,
179 int32_t action, int32_t actionButton, int32_t flags, int32_t metaState,
180 int32_t buttonState, MotionClassification classification, int32_t edgeFlags,
181 float xPrecision, float yPrecision, float xCursorPosition, float yCursorPosition,
182 nsecs_t downTime, const std::vector<PointerProperties>& pointerProperties,
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700183 const std::vector<PointerCoords>& pointerCoords);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500184 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700185};
186
Siarhei Vishniakou72945a02023-09-18 18:30:25 -0700187std::ostream& operator<<(std::ostream& out, const MotionEntry& motionEntry);
188
Chris Yef59a2f42020-10-16 12:55:26 -0700189struct SensorEntry : EventEntry {
190 int32_t deviceId;
191 uint32_t source;
192 InputDeviceSensorType sensorType;
193 InputDeviceSensorAccuracy accuracy;
194 bool accuracyChanged;
195 nsecs_t hwTimestamp;
196
197 std::vector<float> values;
198
199 SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
200 uint32_t policyFlags, nsecs_t hwTimestamp, InputDeviceSensorType sensorType,
201 InputDeviceSensorAccuracy accuracy, bool accuracyChanged,
202 std::vector<float> values);
203 std::string getDescription() const override;
Chris Yef59a2f42020-10-16 12:55:26 -0700204};
205
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700206struct TouchModeEntry : EventEntry {
207 bool inTouchMode;
Antonio Kantek15beb512022-06-13 22:35:41 +0000208 int32_t displayId;
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700209
Antonio Kantek15beb512022-06-13 22:35:41 +0000210 TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode, int32_t displayId);
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700211 std::string getDescription() const override;
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700212};
213
Garfield Tane84e6f92019-08-29 17:28:41 -0700214// Tracks the progress of dispatching a particular event to a particular connection.
215struct DispatchEntry {
216 const uint32_t seq; // unique sequence number, never 0
217
Prabir Pradhan24047542023-11-02 17:14:59 +0000218 std::shared_ptr<const EventEntry> eventEntry; // the event to dispatch
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800219 const ftl::Flags<InputTargetFlags> targetFlags;
chaviw1ff3d1e2020-07-01 15:53:47 -0700220 ui::Transform transform;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700221 ui::Transform rawTransform;
Garfield Tane84e6f92019-08-29 17:28:41 -0700222 float globalScaleFactor;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700223 // Both deliveryTime and timeoutTime are only populated when the entry is sent to the app,
224 // and will be undefined before that.
Garfield Tane84e6f92019-08-29 17:28:41 -0700225 nsecs_t deliveryTime; // time when the event was actually delivered
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700226 // An ANR will be triggered if a response for this entry is not received by timeoutTime
227 nsecs_t timeoutTime;
Garfield Tane84e6f92019-08-29 17:28:41 -0700228
Garfield Tane84e6f92019-08-29 17:28:41 -0700229 int32_t resolvedFlags;
230
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000231 // Information about the dispatch window used for tracing. We avoid holding a window handle
232 // here because information in a window handle may be dynamically updated within the lifespan
233 // of this dispatch entry.
234 gui::Uid targetUid;
235 int64_t vsyncId;
236 // The window that this event is targeting. The only case when this windowId is not populated
237 // is when dispatching an event to a global monitor.
238 std::optional<int32_t> windowId;
239
Prabir Pradhan24047542023-11-02 17:14:59 +0000240 DispatchEntry(std::shared_ptr<const EventEntry> eventEntry,
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800241 ftl::Flags<InputTargetFlags> targetFlags, const ui::Transform& transform,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000242 const ui::Transform& rawTransform, float globalScaleFactor, gui::Uid targetUid,
243 int64_t vsyncId, std::optional<int32_t> windowId);
Prabir Pradhan8c90d782023-09-15 21:16:44 +0000244 DispatchEntry(const DispatchEntry&) = delete;
245 DispatchEntry& operator=(const DispatchEntry&) = delete;
Garfield Tane84e6f92019-08-29 17:28:41 -0700246
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800247 inline bool hasForegroundTarget() const {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800248 return targetFlags.test(InputTargetFlags::FOREGROUND);
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800249 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700250
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800251 inline bool isSplit() const { return targetFlags.test(InputTargetFlags::SPLIT); }
Garfield Tane84e6f92019-08-29 17:28:41 -0700252
253private:
254 static volatile int32_t sNextSeqAtomic;
255
256 static uint32_t nextSeq();
257};
258
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800259std::ostream& operator<<(std::ostream& out, const DispatchEntry& entry);
260
Gang Wange9087892020-01-07 12:17:14 -0500261VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700262VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry,
263 const ui::Transform& rawTransform);
Gang Wange9087892020-01-07 12:17:14 -0500264
Garfield Tane84e6f92019-08-29 17:28:41 -0700265} // namespace android::inputdispatcher