| 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 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 27 | static constexpr int32_t INVALID_POINTER_INDEX = -1; | 
 | 28 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 29 | /* Tracks dispatched key and motion event state so that cancellation events can be | 
 | 30 |  * synthesized when events are dropped. */ | 
 | 31 | class InputState { | 
 | 32 | public: | 
 | 33 |     InputState(); | 
 | 34 |     ~InputState(); | 
 | 35 |  | 
 | 36 |     // Returns true if there is no state to be canceled. | 
 | 37 |     bool isNeutral() const; | 
 | 38 |  | 
 | 39 |     // Returns true if the specified source is known to have received a hover enter | 
 | 40 |     // motion event. | 
 | 41 |     bool isHovering(int32_t deviceId, uint32_t source, int32_t displayId) const; | 
 | 42 |  | 
 | 43 |     // Records tracking information for a key event that has just been published. | 
 | 44 |     // Returns true if the event should be delivered, false if it is inconsistent | 
 | 45 |     // and should be skipped. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 46 |     bool trackKey(const KeyEntry& entry, int32_t action, int32_t flags); | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 47 |  | 
 | 48 |     // Records tracking information for a motion event that has just been published. | 
 | 49 |     // Returns true if the event should be delivered, false if it is inconsistent | 
 | 50 |     // and should be skipped. | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 51 |     bool trackMotion(const MotionEntry& entry, int32_t action, int32_t flags); | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 52 |  | 
 | 53 |     // Synthesizes cancelation events for the current state and resets the tracked state. | 
| Siarhei Vishniakou | 00fca7c | 2019-10-29 13:05:57 -0700 | [diff] [blame] | 54 |     std::vector<EventEntry*> synthesizeCancelationEvents(nsecs_t currentTime, | 
 | 55 |                                                          const CancelationOptions& options); | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 56 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 57 |     // Synthesizes down events for the current state. | 
 | 58 |     std::vector<EventEntry*> synthesizePointerDownEvents(nsecs_t currentTime); | 
 | 59 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 60 |     // Clears the current state. | 
 | 61 |     void clear(); | 
 | 62 |  | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 63 |     // Merges pointer-related parts of the input state into another instance. | 
 | 64 |     void mergePointerStateTo(InputState& other); | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 65 |  | 
 | 66 |     // Gets the fallback key associated with a keycode. | 
 | 67 |     // Returns -1 if none. | 
 | 68 |     // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy. | 
 | 69 |     int32_t getFallbackKey(int32_t originalKeyCode); | 
 | 70 |  | 
 | 71 |     // Sets the fallback key for a particular keycode. | 
 | 72 |     void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode); | 
 | 73 |  | 
 | 74 |     // Removes the fallback key for a particular keycode. | 
 | 75 |     void removeFallbackKey(int32_t originalKeyCode); | 
 | 76 |  | 
 | 77 |     inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const { return mFallbackKeys; } | 
 | 78 |  | 
 | 79 | private: | 
 | 80 |     struct KeyMemento { | 
 | 81 |         int32_t deviceId; | 
 | 82 |         uint32_t source; | 
 | 83 |         int32_t displayId; | 
 | 84 |         int32_t keyCode; | 
 | 85 |         int32_t scanCode; | 
 | 86 |         int32_t metaState; | 
 | 87 |         int32_t flags; | 
 | 88 |         nsecs_t downTime; | 
 | 89 |         uint32_t policyFlags; | 
 | 90 |     }; | 
 | 91 |  | 
 | 92 |     struct MotionMemento { | 
 | 93 |         int32_t deviceId; | 
 | 94 |         uint32_t source; | 
 | 95 |         int32_t displayId; | 
 | 96 |         int32_t flags; | 
 | 97 |         float xPrecision; | 
 | 98 |         float yPrecision; | 
 | 99 |         float xCursorPosition; | 
 | 100 |         float yCursorPosition; | 
 | 101 |         nsecs_t downTime; | 
 | 102 |         uint32_t pointerCount; | 
 | 103 |         PointerProperties pointerProperties[MAX_POINTERS]; | 
 | 104 |         PointerCoords pointerCoords[MAX_POINTERS]; | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 105 |         // Track for which pointers the target doesn't know about. | 
 | 106 |         int32_t firstNewPointerIdx = INVALID_POINTER_INDEX; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 107 |         bool hovering; | 
 | 108 |         uint32_t policyFlags; | 
 | 109 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 110 |         void setPointers(const MotionEntry& entry); | 
| Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 111 |         void mergePointerStateTo(MotionMemento& other) const; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 112 |     }; | 
 | 113 |  | 
 | 114 |     std::vector<KeyMemento> mKeyMementos; | 
 | 115 |     std::vector<MotionMemento> mMotionMementos; | 
 | 116 |     KeyedVector<int32_t, int32_t> mFallbackKeys; | 
 | 117 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 118 |     ssize_t findKeyMemento(const KeyEntry& entry) const; | 
 | 119 |     ssize_t findMotionMemento(const MotionEntry& entry, bool hovering) const; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 120 |  | 
| Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 121 |     void addKeyMemento(const KeyEntry& entry, int32_t flags); | 
 | 122 |     void addMotionMemento(const MotionEntry& entry, int32_t flags, bool hovering); | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 123 |  | 
 | 124 |     static bool shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options); | 
 | 125 |     static bool shouldCancelMotion(const MotionMemento& memento, const CancelationOptions& options); | 
 | 126 | }; | 
 | 127 |  | 
 | 128 | } // namespace android::inputdispatcher | 
 | 129 |  | 
 | 130 | #endif // _UI_INPUT_INPUTDISPATCHER_INPUTSTATE_H |