blob: becfb05f673c8704fbdaf1ae9f7a90a68d05b5f7 [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 {
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010035 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
Prabir Pradhan24047542023-11-02 17:14:59 +000053 mutable bool dispatchInProgress; // initially false, set to true while dispatching
Garfield Tane84e6f92019-08-29 17:28:41 -070054
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 Pradhan24047542023-11-02 17:14:59 +000075 EventEntry(const EventEntry&) = delete;
76 EventEntry& operator=(const EventEntry&) = delete;
Prabir Pradhana8cdbe12023-11-01 21:30:02 +000077 virtual ~EventEntry() = default;
Garfield Tane84e6f92019-08-29 17:28:41 -070078};
79
Garfield Tane84e6f92019-08-29 17:28:41 -070080struct DeviceResetEntry : EventEntry {
81 int32_t deviceId;
82
Garfield Tan6a5a14e2020-01-28 13:24:04 -080083 DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050084 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -070085};
86
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010087struct FocusEntry : EventEntry {
88 sp<IBinder> connectionToken;
89 bool hasFocus;
Vishnu Nairc519ff72021-01-21 08:23:08 -080090 std::string reason;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010091
Vishnu Nair7d3d00d2020-08-03 11:20:42 -070092 FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
Vishnu Nairc519ff72021-01-21 08:23:08 -080093 const std::string& reason);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -050094 std::string getDescription() const override;
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +010095};
96
Prabir Pradhan99987712020-11-10 18:43:05 -080097struct PointerCaptureChangedEntry : EventEntry {
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000098 const PointerCaptureRequest pointerCaptureRequest;
Prabir Pradhan99987712020-11-10 18:43:05 -080099
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000100 PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, const PointerCaptureRequest&);
Prabir Pradhan99987712020-11-10 18:43:05 -0800101 std::string getDescription() const override;
Prabir Pradhan99987712020-11-10 18:43:05 -0800102};
103
arthurhungb89ccb02020-12-30 16:19:01 +0800104struct DragEntry : EventEntry {
105 sp<IBinder> connectionToken;
106 bool isExiting;
107 float x, y;
108
109 DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting, float x,
110 float y);
111 std::string getDescription() const override;
arthurhungb89ccb02020-12-30 16:19:01 +0800112};
113
Garfield Tane84e6f92019-08-29 17:28:41 -0700114struct KeyEntry : EventEntry {
115 int32_t deviceId;
116 uint32_t source;
Linnan Li13bf76a2024-05-05 19:18:02 +0800117 ui::LogicalDisplayId displayId;
Garfield Tane84e6f92019-08-29 17:28:41 -0700118 int32_t action;
Garfield Tane84e6f92019-08-29 17:28:41 -0700119 int32_t keyCode;
120 int32_t scanCode;
121 int32_t metaState;
Garfield Tane84e6f92019-08-29 17:28:41 -0700122 nsecs_t downTime;
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000123 std::unique_ptr<trace::EventTrackerInterface> traceTracker;
Garfield Tane84e6f92019-08-29 17:28:41 -0700124
125 bool syntheticRepeat; // set to true for synthetic key repeats
126
Michael Wright5caf55a2022-11-24 22:31:42 +0000127 enum class InterceptKeyResult {
128 UNKNOWN,
129 SKIP,
130 CONTINUE,
131 TRY_AGAIN_LATER,
Garfield Tane84e6f92019-08-29 17:28:41 -0700132 };
Prabir Pradhan24047542023-11-02 17:14:59 +0000133 // These are special fields that may need to be modified while the event is being dispatched.
134 mutable InterceptKeyResult interceptKeyResult; // set based on the interception result
135 mutable nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
136 mutable int32_t flags;
Prabir Pradhan560d0d12024-03-07 18:08:27 +0000137 // TODO(b/328618922): Refactor key repeat generation to make repeatCount non-mutable.
Prabir Pradhan24047542023-11-02 17:14:59 +0000138 mutable int32_t repeatCount;
Garfield Tane84e6f92019-08-29 17:28:41 -0700139
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000140 KeyEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime,
Linnan Li13bf76a2024-05-05 19:18:02 +0800141 int32_t deviceId, uint32_t source, ui::LogicalDisplayId displayId,
142 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
143 int32_t metaState, 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
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000147std::ostream& operator<<(std::ostream& out, const KeyEntry& motionEntry);
148
Garfield Tane84e6f92019-08-29 17:28:41 -0700149struct MotionEntry : EventEntry {
Garfield Tane84e6f92019-08-29 17:28:41 -0700150 int32_t deviceId;
151 uint32_t source;
Linnan Li13bf76a2024-05-05 19:18:02 +0800152 ui::LogicalDisplayId displayId;
Garfield Tane84e6f92019-08-29 17:28:41 -0700153 int32_t action;
154 int32_t actionButton;
155 int32_t flags;
156 int32_t metaState;
157 int32_t buttonState;
158 MotionClassification classification;
159 int32_t edgeFlags;
160 float xPrecision;
161 float yPrecision;
162 float xCursorPosition;
163 float yCursorPosition;
164 nsecs_t downTime;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700165 std::vector<PointerProperties> pointerProperties;
166 std::vector<PointerCoords> pointerCoords;
Prabir Pradhanabcdf5c2023-12-15 07:30:22 +0000167 std::unique_ptr<trace::EventTrackerInterface> traceTracker;
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700168
169 size_t getPointerCount() const { return pointerProperties.size(); }
Garfield Tane84e6f92019-08-29 17:28:41 -0700170
Prabir Pradhana8cdbe12023-11-01 21:30:02 +0000171 MotionEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime,
Linnan Li13bf76a2024-05-05 19:18:02 +0800172 int32_t deviceId, uint32_t source, ui::LogicalDisplayId displayId,
173 uint32_t policyFlags, int32_t action, int32_t actionButton, int32_t flags,
174 int32_t metaState, int32_t buttonState, MotionClassification classification,
175 int32_t edgeFlags, float xPrecision, float yPrecision, float xCursorPosition,
176 float yCursorPosition, nsecs_t downTime,
177 const std::vector<PointerProperties>& pointerProperties,
Siarhei Vishniakouedd61202023-10-18 11:22:40 -0700178 const std::vector<PointerCoords>& pointerCoords);
Siarhei Vishniakou14411c92020-09-18 21:15:05 -0500179 std::string getDescription() const override;
Garfield Tane84e6f92019-08-29 17:28:41 -0700180};
181
Siarhei Vishniakou72945a02023-09-18 18:30:25 -0700182std::ostream& operator<<(std::ostream& out, const MotionEntry& motionEntry);
183
Chris Yef59a2f42020-10-16 12:55:26 -0700184struct SensorEntry : EventEntry {
185 int32_t deviceId;
186 uint32_t source;
187 InputDeviceSensorType sensorType;
188 InputDeviceSensorAccuracy accuracy;
189 bool accuracyChanged;
190 nsecs_t hwTimestamp;
191
192 std::vector<float> values;
193
194 SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
195 uint32_t policyFlags, nsecs_t hwTimestamp, InputDeviceSensorType sensorType,
196 InputDeviceSensorAccuracy accuracy, bool accuracyChanged,
197 std::vector<float> values);
198 std::string getDescription() const override;
Chris Yef59a2f42020-10-16 12:55:26 -0700199};
200
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700201struct TouchModeEntry : EventEntry {
202 bool inTouchMode;
Linnan Li13bf76a2024-05-05 19:18:02 +0800203 ui::LogicalDisplayId displayId;
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700204
Linnan Li13bf76a2024-05-05 19:18:02 +0800205 TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode, ui::LogicalDisplayId displayId);
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700206 std::string getDescription() const override;
Antonio Kantek7242d8b2021-08-05 16:07:20 -0700207};
208
Garfield Tane84e6f92019-08-29 17:28:41 -0700209// Tracks the progress of dispatching a particular event to a particular connection.
210struct DispatchEntry {
211 const uint32_t seq; // unique sequence number, never 0
212
Prabir Pradhan24047542023-11-02 17:14:59 +0000213 std::shared_ptr<const EventEntry> eventEntry; // the event to dispatch
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800214 const ftl::Flags<InputTargetFlags> targetFlags;
chaviw1ff3d1e2020-07-01 15:53:47 -0700215 ui::Transform transform;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700216 ui::Transform rawTransform;
Garfield Tane84e6f92019-08-29 17:28:41 -0700217 float globalScaleFactor;
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700218 // Both deliveryTime and timeoutTime are only populated when the entry is sent to the app,
219 // and will be undefined before that.
Garfield Tane84e6f92019-08-29 17:28:41 -0700220 nsecs_t deliveryTime; // time when the event was actually delivered
Siarhei Vishniakoud44dddf2020-03-25 16:16:40 -0700221 // An ANR will be triggered if a response for this entry is not received by timeoutTime
222 nsecs_t timeoutTime;
Garfield Tane84e6f92019-08-29 17:28:41 -0700223
Garfield Tane84e6f92019-08-29 17:28:41 -0700224 int32_t resolvedFlags;
225
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000226 // Information about the dispatch window used for tracing. We avoid holding a window handle
227 // here because information in a window handle may be dynamically updated within the lifespan
228 // of this dispatch entry.
229 gui::Uid targetUid;
230 int64_t vsyncId;
231 // The window that this event is targeting. The only case when this windowId is not populated
232 // is when dispatching an event to a global monitor.
233 std::optional<int32_t> windowId;
234
Prabir Pradhan24047542023-11-02 17:14:59 +0000235 DispatchEntry(std::shared_ptr<const EventEntry> eventEntry,
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800236 ftl::Flags<InputTargetFlags> targetFlags, const ui::Transform& transform,
Prabir Pradhanadc59b42023-12-15 05:34:11 +0000237 const ui::Transform& rawTransform, float globalScaleFactor, gui::Uid targetUid,
238 int64_t vsyncId, std::optional<int32_t> windowId);
Prabir Pradhan8c90d782023-09-15 21:16:44 +0000239 DispatchEntry(const DispatchEntry&) = delete;
240 DispatchEntry& operator=(const DispatchEntry&) = delete;
Garfield Tane84e6f92019-08-29 17:28:41 -0700241
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800242 inline bool hasForegroundTarget() const {
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800243 return targetFlags.test(InputTargetFlags::FOREGROUND);
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800244 }
Garfield Tane84e6f92019-08-29 17:28:41 -0700245
Siarhei Vishniakou18cbdb82024-01-31 17:58:13 -0800246 inline bool isSplit() const { return targetFlags.test(InputTargetFlags::SPLIT); }
Garfield Tane84e6f92019-08-29 17:28:41 -0700247
248private:
249 static volatile int32_t sNextSeqAtomic;
250
251 static uint32_t nextSeq();
252};
253
Siarhei Vishniakoud010b012023-01-18 15:00:53 -0800254std::ostream& operator<<(std::ostream& out, const DispatchEntry& entry);
255
Gang Wange9087892020-01-07 12:17:14 -0500256VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry);
Prabir Pradhanb5cb9572021-09-24 06:35:16 -0700257VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry,
258 const ui::Transform& rawTransform);
Gang Wange9087892020-01-07 12:17:14 -0500259
Garfield Tane84e6f92019-08-29 17:28:41 -0700260} // namespace android::inputdispatcher