Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | namespace android::inputdispatcher { |
| 26 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 27 | /* Tracks dispatched key and motion event state so that cancellation events can be |
| 28 | * synthesized when events are dropped. */ |
| 29 | class InputState { |
| 30 | public: |
| 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 Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame^] | 44 | bool trackKey(const KeyEntry& entry, int32_t action, int32_t flags); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 45 | |
| 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 Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame^] | 49 | bool trackMotion(const MotionEntry& entry, int32_t action, int32_t flags); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 50 | |
| 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 | |
| 74 | private: |
| 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 Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame^] | 103 | void setPointers(const MotionEntry& entry); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | std::vector<KeyMemento> mKeyMementos; |
| 107 | std::vector<MotionMemento> mMotionMementos; |
| 108 | KeyedVector<int32_t, int32_t> mFallbackKeys; |
| 109 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame^] | 110 | ssize_t findKeyMemento(const KeyEntry& entry) const; |
| 111 | ssize_t findMotionMemento(const MotionEntry& entry, bool hovering) const; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 112 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame^] | 113 | void addKeyMemento(const KeyEntry& entry, int32_t flags); |
| 114 | void addMotionMemento(const MotionEntry& entry, int32_t flags, bool hovering); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 115 | |
| 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 |