blob: bccef0fca35602775151b14341f5e111872f9434 [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
27// Sequence number for synthesized or injected events.
28constexpr uint32_t SYNTHESIZED_EVENT_SEQUENCE_NUM = 0;
29
30/* Tracks dispatched key and motion event state so that cancellation events can be
31 * synthesized when events are dropped. */
32class InputState {
33public:
34 InputState();
35 ~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.
47 bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags);
48
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.
52 bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags);
53
54 // Synthesizes cancelation events for the current state and resets the tracked state.
55 void synthesizeCancelationEvents(nsecs_t currentTime, std::vector<EventEntry*>& outEvents,
56 const CancelationOptions& options);
57
58 // Clears the current state.
59 void clear();
60
61 // Copies pointer-related parts of the input state to another instance.
62 void copyPointerStateTo(InputState& other) const;
63
64 // Gets the fallback key associated with a keycode.
65 // Returns -1 if none.
66 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
67 int32_t getFallbackKey(int32_t originalKeyCode);
68
69 // Sets the fallback key for a particular keycode.
70 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
71
72 // Removes the fallback key for a particular keycode.
73 void removeFallbackKey(int32_t originalKeyCode);
74
75 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const { return mFallbackKeys; }
76
77private:
78 struct KeyMemento {
79 int32_t deviceId;
80 uint32_t source;
81 int32_t displayId;
82 int32_t keyCode;
83 int32_t scanCode;
84 int32_t metaState;
85 int32_t flags;
86 nsecs_t downTime;
87 uint32_t policyFlags;
88 };
89
90 struct MotionMemento {
91 int32_t deviceId;
92 uint32_t source;
93 int32_t displayId;
94 int32_t flags;
95 float xPrecision;
96 float yPrecision;
97 float xCursorPosition;
98 float yCursorPosition;
99 nsecs_t downTime;
100 uint32_t pointerCount;
101 PointerProperties pointerProperties[MAX_POINTERS];
102 PointerCoords pointerCoords[MAX_POINTERS];
103 bool hovering;
104 uint32_t policyFlags;
105
106 void setPointers(const MotionEntry* entry);
107 };
108
109 std::vector<KeyMemento> mKeyMementos;
110 std::vector<MotionMemento> mMotionMementos;
111 KeyedVector<int32_t, int32_t> mFallbackKeys;
112
113 ssize_t findKeyMemento(const KeyEntry* entry) const;
114 ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const;
115
116 void addKeyMemento(const KeyEntry* entry, int32_t flags);
117 void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering);
118
119 static bool shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options);
120 static bool shouldCancelMotion(const MotionMemento& memento, const CancelationOptions& options);
121};
122
123} // namespace android::inputdispatcher
124
125#endif // _UI_INPUT_INPUTDISPATCHER_INPUTSTATE_H