blob: 77a6db19bc04ed3e48542c30a85b5bdec52fa8b6 [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_INPUTSTATE_H
18#define _UI_INPUT_INPUTDISPATCHER_INPUTSTATE_H
19
20#include "CancelationOptions.h"
21#include "Entry.h"
22
23#include <utils/Timers.h>
24
Vaibhav Devmurariff798f32022-05-09 23:45:04 +000025namespace android {
26namespace inputdispatcher {
Garfield Tane84e6f92019-08-29 17:28:41 -070027
Svet Ganov5d3bc372020-01-26 23:11:07 -080028static constexpr int32_t INVALID_POINTER_INDEX = -1;
29
Garfield Tane84e6f92019-08-29 17:28:41 -070030/* Tracks dispatched key and motion event state so that cancellation events can be
31 * synthesized when events are dropped. */
32class InputState {
33public:
Garfield Tanff1f1bb2020-01-28 13:24:04 -080034 explicit InputState(const IdGenerator& idGenerator);
Garfield Tane84e6f92019-08-29 17:28:41 -070035 ~InputState();
36
37 // Returns true if there is no state to be canceled.
38 bool isNeutral() const;
39
40 // Returns true if the specified source is known to have received a hover enter
41 // motion event.
42 bool isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const;
43
44 // Records tracking information for a key event that has just been published.
45 // Returns true if the event should be delivered, false if it is inconsistent
46 // and should be skipped.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070047 bool trackKey(const KeyEntry& entry, int32_t action, int32_t flags);
Garfield Tane84e6f92019-08-29 17:28:41 -070048
49 // Records tracking information for a motion event that has just been published.
50 // Returns true if the event should be delivered, false if it is inconsistent
51 // and should be skipped.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070052 bool trackMotion(const MotionEntry& entry, int32_t action, int32_t flags);
Garfield Tane84e6f92019-08-29 17:28:41 -070053
54 // Synthesizes cancelation events for the current state and resets the tracked state.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -070055 std::vector<std::unique_ptr<EventEntry>> synthesizeCancelationEvents(
56 nsecs_t currentTime, const CancelationOptions& options);
Garfield Tane84e6f92019-08-29 17:28:41 -070057
Svet Ganov5d3bc372020-01-26 23:11:07 -080058 // Synthesizes down events for the current state.
Siarhei Vishniakoua9a7ee82019-10-14 16:28:19 -070059 std::vector<std::unique_ptr<EventEntry>> synthesizePointerDownEvents(nsecs_t currentTime);
Svet Ganov5d3bc372020-01-26 23:11:07 -080060
Garfield Tane84e6f92019-08-29 17:28:41 -070061 // Clears the current state.
62 void clear();
63
Svet Ganov5d3bc372020-01-26 23:11:07 -080064 // Merges pointer-related parts of the input state into another instance.
65 void mergePointerStateTo(InputState& other);
Garfield Tane84e6f92019-08-29 17:28:41 -070066
67 // Gets the fallback key associated with a keycode.
68 // Returns -1 if none.
69 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
70 int32_t getFallbackKey(int32_t originalKeyCode);
71
72 // Sets the fallback key for a particular keycode.
73 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
74
75 // Removes the fallback key for a particular keycode.
76 void removeFallbackKey(int32_t originalKeyCode);
77
78 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const { return mFallbackKeys; }
79
80private:
81 struct KeyMemento {
82 int32_t deviceId;
83 uint32_t source;
84 int32_t displayId;
85 int32_t keyCode;
86 int32_t scanCode;
87 int32_t metaState;
88 int32_t flags;
89 nsecs_t downTime;
90 uint32_t policyFlags;
91 };
92
93 struct MotionMemento {
94 int32_t deviceId;
95 uint32_t source;
96 int32_t displayId;
97 int32_t flags;
98 float xPrecision;
99 float yPrecision;
100 float xCursorPosition;
101 float yCursorPosition;
102 nsecs_t downTime;
103 uint32_t pointerCount;
104 PointerProperties pointerProperties[MAX_POINTERS];
105 PointerCoords pointerCoords[MAX_POINTERS];
Svet Ganov5d3bc372020-01-26 23:11:07 -0800106 // Track for which pointers the target doesn't know about.
107 int32_t firstNewPointerIdx = INVALID_POINTER_INDEX;
Garfield Tane84e6f92019-08-29 17:28:41 -0700108 bool hovering;
109 uint32_t policyFlags;
110
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700111 void setPointers(const MotionEntry& entry);
Svet Ganov5d3bc372020-01-26 23:11:07 -0800112 void mergePointerStateTo(MotionMemento& other) const;
Garfield Tane84e6f92019-08-29 17:28:41 -0700113 };
114
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800115 const IdGenerator& mIdGenerator; // InputDispatcher owns it so we won't have dangling reference.
116
Garfield Tane84e6f92019-08-29 17:28:41 -0700117 std::vector<KeyMemento> mKeyMementos;
118 std::vector<MotionMemento> mMotionMementos;
119 KeyedVector<int32_t, int32_t> mFallbackKeys;
120
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700121 ssize_t findKeyMemento(const KeyEntry& entry) const;
122 ssize_t findMotionMemento(const MotionEntry& entry, bool hovering) const;
Garfield Tane84e6f92019-08-29 17:28:41 -0700123
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700124 void addKeyMemento(const KeyEntry& entry, int32_t flags);
125 void addMotionMemento(const MotionEntry& entry, int32_t flags, bool hovering);
Garfield Tane84e6f92019-08-29 17:28:41 -0700126
127 static bool shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options);
128 static bool shouldCancelMotion(const MotionMemento& memento, const CancelationOptions& options);
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000129
130 // Synthesizes pointer cancel events for a particular set of pointers.
131 std::vector<std::unique_ptr<MotionEntry>> synthesizeCancelationEventsForPointers(
132 const MotionMemento& memento, const BitSet32 pointerIds, nsecs_t currentTime);
Garfield Tane84e6f92019-08-29 17:28:41 -0700133};
134
Vaibhav Devmurariff798f32022-05-09 23:45:04 +0000135} // namespace inputdispatcher
136} // namespace android
Garfield Tane84e6f92019-08-29 17:28:41 -0700137
138#endif // _UI_INPUT_INPUTDISPATCHER_INPUTSTATE_H