xref: /aosp_15_r20/frameworks/native/services/inputflinger/dispatcher/Entry.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include "InjectionState.h"
20*38e8c45fSAndroid Build Coastguard Worker #include "InputTargetFlags.h"
21*38e8c45fSAndroid Build Coastguard Worker #include "trace/EventTrackerInterface.h"
22*38e8c45fSAndroid Build Coastguard Worker 
23*38e8c45fSAndroid Build Coastguard Worker #include <gui/InputApplication.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <input/Input.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <stdint.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <utils/Timers.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <functional>
28*38e8c45fSAndroid Build Coastguard Worker #include <ostream>
29*38e8c45fSAndroid Build Coastguard Worker #include <string>
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker namespace android::inputdispatcher {
32*38e8c45fSAndroid Build Coastguard Worker 
33*38e8c45fSAndroid Build Coastguard Worker struct EventEntry {
34*38e8c45fSAndroid Build Coastguard Worker     enum class Type {
35*38e8c45fSAndroid Build Coastguard Worker         DEVICE_RESET,
36*38e8c45fSAndroid Build Coastguard Worker         FOCUS,
37*38e8c45fSAndroid Build Coastguard Worker         KEY,
38*38e8c45fSAndroid Build Coastguard Worker         MOTION,
39*38e8c45fSAndroid Build Coastguard Worker         SENSOR,
40*38e8c45fSAndroid Build Coastguard Worker         POINTER_CAPTURE_CHANGED,
41*38e8c45fSAndroid Build Coastguard Worker         DRAG,
42*38e8c45fSAndroid Build Coastguard Worker         TOUCH_MODE_CHANGED,
43*38e8c45fSAndroid Build Coastguard Worker 
44*38e8c45fSAndroid Build Coastguard Worker         ftl_last = TOUCH_MODE_CHANGED
45*38e8c45fSAndroid Build Coastguard Worker     };
46*38e8c45fSAndroid Build Coastguard Worker 
47*38e8c45fSAndroid Build Coastguard Worker     int32_t id;
48*38e8c45fSAndroid Build Coastguard Worker     Type type;
49*38e8c45fSAndroid Build Coastguard Worker     nsecs_t eventTime;
50*38e8c45fSAndroid Build Coastguard Worker     uint32_t policyFlags;
51*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<InjectionState> injectionState;
52*38e8c45fSAndroid Build Coastguard Worker 
53*38e8c45fSAndroid Build Coastguard Worker     mutable bool dispatchInProgress; // initially false, set to true while dispatching
54*38e8c45fSAndroid Build Coastguard Worker 
55*38e8c45fSAndroid Build Coastguard Worker     /**
56*38e8c45fSAndroid Build Coastguard Worker      * Injected keys are events from an external (probably untrusted) application
57*38e8c45fSAndroid Build Coastguard Worker      * and are not related to real hardware state. They come in via
58*38e8c45fSAndroid Build Coastguard Worker      * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED.
59*38e8c45fSAndroid Build Coastguard Worker      */
isInjectedEventEntry60*38e8c45fSAndroid Build Coastguard Worker     inline bool isInjected() const { return injectionState != nullptr; }
61*38e8c45fSAndroid Build Coastguard Worker 
62*38e8c45fSAndroid Build Coastguard Worker     /**
63*38e8c45fSAndroid Build Coastguard Worker      * Synthesized events are either injected events, or events that come
64*38e8c45fSAndroid Build Coastguard Worker      * from real hardware, but aren't directly attributable to a specific hardware event.
65*38e8c45fSAndroid Build Coastguard Worker      * Key repeat is a synthesized event, because it is related to an actual hardware state
66*38e8c45fSAndroid Build Coastguard Worker      * (a key is currently pressed), but the repeat itself is generated by the framework.
67*38e8c45fSAndroid Build Coastguard Worker      */
isSynthesizedEventEntry68*38e8c45fSAndroid Build Coastguard Worker     inline bool isSynthesized() const {
69*38e8c45fSAndroid Build Coastguard Worker         return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER;
70*38e8c45fSAndroid Build Coastguard Worker     }
71*38e8c45fSAndroid Build Coastguard Worker 
72*38e8c45fSAndroid Build Coastguard Worker     virtual std::string getDescription() const = 0;
73*38e8c45fSAndroid Build Coastguard Worker 
74*38e8c45fSAndroid Build Coastguard Worker     EventEntry(int32_t id, Type type, nsecs_t eventTime, uint32_t policyFlags);
75*38e8c45fSAndroid Build Coastguard Worker     EventEntry(const EventEntry&) = delete;
76*38e8c45fSAndroid Build Coastguard Worker     EventEntry& operator=(const EventEntry&) = delete;
77*38e8c45fSAndroid Build Coastguard Worker     virtual ~EventEntry() = default;
78*38e8c45fSAndroid Build Coastguard Worker };
79*38e8c45fSAndroid Build Coastguard Worker 
80*38e8c45fSAndroid Build Coastguard Worker struct DeviceResetEntry : EventEntry {
81*38e8c45fSAndroid Build Coastguard Worker     int32_t deviceId;
82*38e8c45fSAndroid Build Coastguard Worker 
83*38e8c45fSAndroid Build Coastguard Worker     DeviceResetEntry(int32_t id, nsecs_t eventTime, int32_t deviceId);
84*38e8c45fSAndroid Build Coastguard Worker     std::string getDescription() const override;
85*38e8c45fSAndroid Build Coastguard Worker };
86*38e8c45fSAndroid Build Coastguard Worker 
87*38e8c45fSAndroid Build Coastguard Worker struct FocusEntry : EventEntry {
88*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> connectionToken;
89*38e8c45fSAndroid Build Coastguard Worker     bool hasFocus;
90*38e8c45fSAndroid Build Coastguard Worker     std::string reason;
91*38e8c45fSAndroid Build Coastguard Worker 
92*38e8c45fSAndroid Build Coastguard Worker     FocusEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool hasFocus,
93*38e8c45fSAndroid Build Coastguard Worker                const std::string& reason);
94*38e8c45fSAndroid Build Coastguard Worker     std::string getDescription() const override;
95*38e8c45fSAndroid Build Coastguard Worker };
96*38e8c45fSAndroid Build Coastguard Worker 
97*38e8c45fSAndroid Build Coastguard Worker struct PointerCaptureChangedEntry : EventEntry {
98*38e8c45fSAndroid Build Coastguard Worker     const PointerCaptureRequest pointerCaptureRequest;
99*38e8c45fSAndroid Build Coastguard Worker 
100*38e8c45fSAndroid Build Coastguard Worker     PointerCaptureChangedEntry(int32_t id, nsecs_t eventTime, const PointerCaptureRequest&);
101*38e8c45fSAndroid Build Coastguard Worker     std::string getDescription() const override;
102*38e8c45fSAndroid Build Coastguard Worker };
103*38e8c45fSAndroid Build Coastguard Worker 
104*38e8c45fSAndroid Build Coastguard Worker struct DragEntry : EventEntry {
105*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> connectionToken;
106*38e8c45fSAndroid Build Coastguard Worker     bool isExiting;
107*38e8c45fSAndroid Build Coastguard Worker     float x, y;
108*38e8c45fSAndroid Build Coastguard Worker 
109*38e8c45fSAndroid Build Coastguard Worker     DragEntry(int32_t id, nsecs_t eventTime, sp<IBinder> connectionToken, bool isExiting, float x,
110*38e8c45fSAndroid Build Coastguard Worker               float y);
111*38e8c45fSAndroid Build Coastguard Worker     std::string getDescription() const override;
112*38e8c45fSAndroid Build Coastguard Worker };
113*38e8c45fSAndroid Build Coastguard Worker 
114*38e8c45fSAndroid Build Coastguard Worker struct KeyEntry : EventEntry {
115*38e8c45fSAndroid Build Coastguard Worker     int32_t deviceId;
116*38e8c45fSAndroid Build Coastguard Worker     uint32_t source;
117*38e8c45fSAndroid Build Coastguard Worker     ui::LogicalDisplayId displayId;
118*38e8c45fSAndroid Build Coastguard Worker     int32_t action;
119*38e8c45fSAndroid Build Coastguard Worker     int32_t keyCode;
120*38e8c45fSAndroid Build Coastguard Worker     int32_t scanCode;
121*38e8c45fSAndroid Build Coastguard Worker     int32_t metaState;
122*38e8c45fSAndroid Build Coastguard Worker     nsecs_t downTime;
123*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<trace::EventTrackerInterface> traceTracker;
124*38e8c45fSAndroid Build Coastguard Worker 
125*38e8c45fSAndroid Build Coastguard Worker     bool syntheticRepeat; // set to true for synthetic key repeats
126*38e8c45fSAndroid Build Coastguard Worker 
127*38e8c45fSAndroid Build Coastguard Worker     enum class InterceptKeyResult {
128*38e8c45fSAndroid Build Coastguard Worker         UNKNOWN,
129*38e8c45fSAndroid Build Coastguard Worker         SKIP,
130*38e8c45fSAndroid Build Coastguard Worker         CONTINUE,
131*38e8c45fSAndroid Build Coastguard Worker         TRY_AGAIN_LATER,
132*38e8c45fSAndroid Build Coastguard Worker     };
133*38e8c45fSAndroid Build Coastguard Worker     // These are special fields that may need to be modified while the event is being dispatched.
134*38e8c45fSAndroid Build Coastguard Worker     mutable InterceptKeyResult interceptKeyResult; // set based on the interception result
135*38e8c45fSAndroid Build Coastguard Worker     mutable nsecs_t interceptKeyWakeupTime;        // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
136*38e8c45fSAndroid Build Coastguard Worker     mutable int32_t flags;
137*38e8c45fSAndroid Build Coastguard Worker     // TODO(b/328618922): Refactor key repeat generation to make repeatCount non-mutable.
138*38e8c45fSAndroid Build Coastguard Worker     mutable int32_t repeatCount;
139*38e8c45fSAndroid Build Coastguard Worker 
140*38e8c45fSAndroid Build Coastguard Worker     KeyEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime,
141*38e8c45fSAndroid Build Coastguard Worker              int32_t deviceId, uint32_t source, ui::LogicalDisplayId displayId,
142*38e8c45fSAndroid Build Coastguard Worker              uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
143*38e8c45fSAndroid Build Coastguard Worker              int32_t metaState, int32_t repeatCount, nsecs_t downTime);
144*38e8c45fSAndroid Build Coastguard Worker     std::string getDescription() const override;
145*38e8c45fSAndroid Build Coastguard Worker };
146*38e8c45fSAndroid Build Coastguard Worker 
147*38e8c45fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& out, const KeyEntry& motionEntry);
148*38e8c45fSAndroid Build Coastguard Worker 
149*38e8c45fSAndroid Build Coastguard Worker struct MotionEntry : EventEntry {
150*38e8c45fSAndroid Build Coastguard Worker     int32_t deviceId;
151*38e8c45fSAndroid Build Coastguard Worker     uint32_t source;
152*38e8c45fSAndroid Build Coastguard Worker     ui::LogicalDisplayId displayId;
153*38e8c45fSAndroid Build Coastguard Worker     int32_t action;
154*38e8c45fSAndroid Build Coastguard Worker     int32_t actionButton;
155*38e8c45fSAndroid Build Coastguard Worker     int32_t flags;
156*38e8c45fSAndroid Build Coastguard Worker     int32_t metaState;
157*38e8c45fSAndroid Build Coastguard Worker     int32_t buttonState;
158*38e8c45fSAndroid Build Coastguard Worker     MotionClassification classification;
159*38e8c45fSAndroid Build Coastguard Worker     int32_t edgeFlags;
160*38e8c45fSAndroid Build Coastguard Worker     float xPrecision;
161*38e8c45fSAndroid Build Coastguard Worker     float yPrecision;
162*38e8c45fSAndroid Build Coastguard Worker     float xCursorPosition;
163*38e8c45fSAndroid Build Coastguard Worker     float yCursorPosition;
164*38e8c45fSAndroid Build Coastguard Worker     nsecs_t downTime;
165*38e8c45fSAndroid Build Coastguard Worker     std::vector<PointerProperties> pointerProperties;
166*38e8c45fSAndroid Build Coastguard Worker     std::vector<PointerCoords> pointerCoords;
167*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<trace::EventTrackerInterface> traceTracker;
168*38e8c45fSAndroid Build Coastguard Worker 
getPointerCountMotionEntry169*38e8c45fSAndroid Build Coastguard Worker     size_t getPointerCount() const { return pointerProperties.size(); }
170*38e8c45fSAndroid Build Coastguard Worker 
171*38e8c45fSAndroid Build Coastguard Worker     MotionEntry(int32_t id, std::shared_ptr<InjectionState> injectionState, nsecs_t eventTime,
172*38e8c45fSAndroid Build Coastguard Worker                 int32_t deviceId, uint32_t source, ui::LogicalDisplayId displayId,
173*38e8c45fSAndroid Build Coastguard Worker                 uint32_t policyFlags, int32_t action, int32_t actionButton, int32_t flags,
174*38e8c45fSAndroid Build Coastguard Worker                 int32_t metaState, int32_t buttonState, MotionClassification classification,
175*38e8c45fSAndroid Build Coastguard Worker                 int32_t edgeFlags, float xPrecision, float yPrecision, float xCursorPosition,
176*38e8c45fSAndroid Build Coastguard Worker                 float yCursorPosition, nsecs_t downTime,
177*38e8c45fSAndroid Build Coastguard Worker                 const std::vector<PointerProperties>& pointerProperties,
178*38e8c45fSAndroid Build Coastguard Worker                 const std::vector<PointerCoords>& pointerCoords);
179*38e8c45fSAndroid Build Coastguard Worker     std::string getDescription() const override;
180*38e8c45fSAndroid Build Coastguard Worker };
181*38e8c45fSAndroid Build Coastguard Worker 
182*38e8c45fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& out, const MotionEntry& motionEntry);
183*38e8c45fSAndroid Build Coastguard Worker 
184*38e8c45fSAndroid Build Coastguard Worker struct SensorEntry : EventEntry {
185*38e8c45fSAndroid Build Coastguard Worker     int32_t deviceId;
186*38e8c45fSAndroid Build Coastguard Worker     uint32_t source;
187*38e8c45fSAndroid Build Coastguard Worker     InputDeviceSensorType sensorType;
188*38e8c45fSAndroid Build Coastguard Worker     InputDeviceSensorAccuracy accuracy;
189*38e8c45fSAndroid Build Coastguard Worker     bool accuracyChanged;
190*38e8c45fSAndroid Build Coastguard Worker     nsecs_t hwTimestamp;
191*38e8c45fSAndroid Build Coastguard Worker 
192*38e8c45fSAndroid Build Coastguard Worker     std::vector<float> values;
193*38e8c45fSAndroid Build Coastguard Worker 
194*38e8c45fSAndroid Build Coastguard Worker     SensorEntry(int32_t id, nsecs_t eventTime, int32_t deviceId, uint32_t source,
195*38e8c45fSAndroid Build Coastguard Worker                 uint32_t policyFlags, nsecs_t hwTimestamp, InputDeviceSensorType sensorType,
196*38e8c45fSAndroid Build Coastguard Worker                 InputDeviceSensorAccuracy accuracy, bool accuracyChanged,
197*38e8c45fSAndroid Build Coastguard Worker                 std::vector<float> values);
198*38e8c45fSAndroid Build Coastguard Worker     std::string getDescription() const override;
199*38e8c45fSAndroid Build Coastguard Worker };
200*38e8c45fSAndroid Build Coastguard Worker 
201*38e8c45fSAndroid Build Coastguard Worker struct TouchModeEntry : EventEntry {
202*38e8c45fSAndroid Build Coastguard Worker     bool inTouchMode;
203*38e8c45fSAndroid Build Coastguard Worker     ui::LogicalDisplayId displayId;
204*38e8c45fSAndroid Build Coastguard Worker 
205*38e8c45fSAndroid Build Coastguard Worker     TouchModeEntry(int32_t id, nsecs_t eventTime, bool inTouchMode, ui::LogicalDisplayId displayId);
206*38e8c45fSAndroid Build Coastguard Worker     std::string getDescription() const override;
207*38e8c45fSAndroid Build Coastguard Worker };
208*38e8c45fSAndroid Build Coastguard Worker 
209*38e8c45fSAndroid Build Coastguard Worker // Tracks the progress of dispatching a particular event to a particular connection.
210*38e8c45fSAndroid Build Coastguard Worker struct DispatchEntry {
211*38e8c45fSAndroid Build Coastguard Worker     const uint32_t seq; // unique sequence number, never 0
212*38e8c45fSAndroid Build Coastguard Worker 
213*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<const EventEntry> eventEntry; // the event to dispatch
214*38e8c45fSAndroid Build Coastguard Worker     const ftl::Flags<InputTargetFlags> targetFlags;
215*38e8c45fSAndroid Build Coastguard Worker     ui::Transform transform;
216*38e8c45fSAndroid Build Coastguard Worker     ui::Transform rawTransform;
217*38e8c45fSAndroid Build Coastguard Worker     float globalScaleFactor;
218*38e8c45fSAndroid Build Coastguard Worker     // Both deliveryTime and timeoutTime are only populated when the entry is sent to the app,
219*38e8c45fSAndroid Build Coastguard Worker     // and will be undefined before that.
220*38e8c45fSAndroid Build Coastguard Worker     nsecs_t deliveryTime; // time when the event was actually delivered
221*38e8c45fSAndroid Build Coastguard Worker     // An ANR will be triggered if a response for this entry is not received by timeoutTime
222*38e8c45fSAndroid Build Coastguard Worker     nsecs_t timeoutTime;
223*38e8c45fSAndroid Build Coastguard Worker 
224*38e8c45fSAndroid Build Coastguard Worker     int32_t resolvedFlags;
225*38e8c45fSAndroid Build Coastguard Worker 
226*38e8c45fSAndroid Build Coastguard Worker     // Information about the dispatch window used for tracing. We avoid holding a window handle
227*38e8c45fSAndroid Build Coastguard Worker     // here because information in a window handle may be dynamically updated within the lifespan
228*38e8c45fSAndroid Build Coastguard Worker     // of this dispatch entry.
229*38e8c45fSAndroid Build Coastguard Worker     gui::Uid targetUid;
230*38e8c45fSAndroid Build Coastguard Worker     int64_t vsyncId;
231*38e8c45fSAndroid Build Coastguard Worker     // The window that this event is targeting. The only case when this windowId is not populated
232*38e8c45fSAndroid Build Coastguard Worker     // is when dispatching an event to a global monitor.
233*38e8c45fSAndroid Build Coastguard Worker     std::optional<int32_t> windowId;
234*38e8c45fSAndroid Build Coastguard Worker 
235*38e8c45fSAndroid Build Coastguard Worker     DispatchEntry(std::shared_ptr<const EventEntry> eventEntry,
236*38e8c45fSAndroid Build Coastguard Worker                   ftl::Flags<InputTargetFlags> targetFlags, const ui::Transform& transform,
237*38e8c45fSAndroid Build Coastguard Worker                   const ui::Transform& rawTransform, float globalScaleFactor, gui::Uid targetUid,
238*38e8c45fSAndroid Build Coastguard Worker                   int64_t vsyncId, std::optional<int32_t> windowId);
239*38e8c45fSAndroid Build Coastguard Worker     DispatchEntry(const DispatchEntry&) = delete;
240*38e8c45fSAndroid Build Coastguard Worker     DispatchEntry& operator=(const DispatchEntry&) = delete;
241*38e8c45fSAndroid Build Coastguard Worker 
hasForegroundTargetDispatchEntry242*38e8c45fSAndroid Build Coastguard Worker     inline bool hasForegroundTarget() const {
243*38e8c45fSAndroid Build Coastguard Worker         return targetFlags.test(InputTargetFlags::FOREGROUND);
244*38e8c45fSAndroid Build Coastguard Worker     }
245*38e8c45fSAndroid Build Coastguard Worker 
isSplitDispatchEntry246*38e8c45fSAndroid Build Coastguard Worker     inline bool isSplit() const { return targetFlags.test(InputTargetFlags::SPLIT); }
247*38e8c45fSAndroid Build Coastguard Worker 
248*38e8c45fSAndroid Build Coastguard Worker private:
249*38e8c45fSAndroid Build Coastguard Worker     static volatile int32_t sNextSeqAtomic;
250*38e8c45fSAndroid Build Coastguard Worker 
251*38e8c45fSAndroid Build Coastguard Worker     static uint32_t nextSeq();
252*38e8c45fSAndroid Build Coastguard Worker };
253*38e8c45fSAndroid Build Coastguard Worker 
254*38e8c45fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& out, const DispatchEntry& entry);
255*38e8c45fSAndroid Build Coastguard Worker 
256*38e8c45fSAndroid Build Coastguard Worker VerifiedKeyEvent verifiedKeyEventFromKeyEntry(const KeyEntry& entry);
257*38e8c45fSAndroid Build Coastguard Worker VerifiedMotionEvent verifiedMotionEventFromMotionEntry(const MotionEntry& entry,
258*38e8c45fSAndroid Build Coastguard Worker                                                        const ui::Transform& rawTransform);
259*38e8c45fSAndroid Build Coastguard Worker 
260*38e8c45fSAndroid Build Coastguard Worker } // namespace android::inputdispatcher
261