blob: cc72152d04e407b9a88cf1c430ce07fb3db934c6 [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
25namespace android::inputdispatcher {
26
Garfield Tane84e6f92019-08-29 17:28:41 -070027/* Tracks dispatched key and motion event state so that cancellation events can be
28 * synthesized when events are dropped. */
29class InputState {
30public:
31 InputState();
32 ~InputState();
33
34 // Returns true if there is no state to be canceled.
35 bool isNeutral() const;
36
37 // Returns true if the specified source is known to have received a hover enter
38 // motion event.
39 bool isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const;
40
41 // Records tracking information for a key event that has just been published.
42 // Returns true if the event should be delivered, false if it is inconsistent
43 // and should be skipped.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070044 bool trackKey(const KeyEntry& entry, int32_t action, int32_t flags);
Garfield Tane84e6f92019-08-29 17:28:41 -070045
46 // Records tracking information for a motion event that has just been published.
47 // Returns true if the event should be delivered, false if it is inconsistent
48 // and should be skipped.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -070049 bool trackMotion(const MotionEntry& entry, int32_t action, int32_t flags);
Garfield Tane84e6f92019-08-29 17:28:41 -070050
51 // Synthesizes cancelation events for the current state and resets the tracked state.
52 void synthesizeCancelationEvents(nsecs_t currentTime, std::vector<EventEntry*>& outEvents,
53 const CancelationOptions& options);
54
55 // Clears the current state.
56 void clear();
57
58 // Copies pointer-related parts of the input state to another instance.
59 void copyPointerStateTo(InputState& other) const;
60
61 // Gets the fallback key associated with a keycode.
62 // Returns -1 if none.
63 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
64 int32_t getFallbackKey(int32_t originalKeyCode);
65
66 // Sets the fallback key for a particular keycode.
67 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
68
69 // Removes the fallback key for a particular keycode.
70 void removeFallbackKey(int32_t originalKeyCode);
71
72 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const { return mFallbackKeys; }
73
74private:
75 struct KeyMemento {
76 int32_t deviceId;
77 uint32_t source;
78 int32_t displayId;
79 int32_t keyCode;
80 int32_t scanCode;
81 int32_t metaState;
82 int32_t flags;
83 nsecs_t downTime;
84 uint32_t policyFlags;
85 };
86
87 struct MotionMemento {
88 int32_t deviceId;
89 uint32_t source;
90 int32_t displayId;
91 int32_t flags;
92 float xPrecision;
93 float yPrecision;
94 float xCursorPosition;
95 float yCursorPosition;
96 nsecs_t downTime;
97 uint32_t pointerCount;
98 PointerProperties pointerProperties[MAX_POINTERS];
99 PointerCoords pointerCoords[MAX_POINTERS];
100 bool hovering;
101 uint32_t policyFlags;
102
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700103 void setPointers(const MotionEntry& entry);
Garfield Tane84e6f92019-08-29 17:28:41 -0700104 };
105
106 std::vector<KeyMemento> mKeyMementos;
107 std::vector<MotionMemento> mMotionMementos;
108 KeyedVector<int32_t, int32_t> mFallbackKeys;
109
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700110 ssize_t findKeyMemento(const KeyEntry& entry) const;
111 ssize_t findMotionMemento(const MotionEntry& entry, bool hovering) const;
Garfield Tane84e6f92019-08-29 17:28:41 -0700112
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700113 void addKeyMemento(const KeyEntry& entry, int32_t flags);
114 void addMotionMemento(const MotionEntry& entry, int32_t flags, bool hovering);
Garfield Tane84e6f92019-08-29 17:28:41 -0700115
116 static bool shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options);
117 static bool shouldCancelMotion(const MotionMemento& memento, const CancelationOptions& options);
118};
119
120} // namespace android::inputdispatcher
121
122#endif // _UI_INPUT_INPUTDISPATCHER_INPUTSTATE_H