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 | |
Prabir Pradhan | 4810866 | 2022-09-09 21:22:04 +0000 | [diff] [blame] | 17 | #pragma once |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 18 | |
| 19 | #include "CancelationOptions.h" |
| 20 | #include "Entry.h" |
| 21 | |
| 22 | #include <utils/Timers.h> |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 23 | #include <bitset> |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 24 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace inputdispatcher { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 27 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 28 | static constexpr int32_t INVALID_POINTER_INDEX = -1; |
| 29 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 30 | /* Tracks dispatched key and motion event state so that cancellation events can be |
| 31 | * synthesized when events are dropped. */ |
| 32 | class InputState { |
| 33 | public: |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 34 | explicit InputState(const IdGenerator& idGenerator); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 35 | ~InputState(); |
| 36 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 37 | // Returns true if the specified source is known to have received a hover enter |
| 38 | // motion event. |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 39 | bool isHovering(DeviceId deviceId, uint32_t source, ui::LogicalDisplayId displayId) const; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 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. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 44 | bool trackKey(const KeyEntry& entry, 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. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 49 | bool trackMotion(const MotionEntry& entry, int32_t flags); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 50 | |
Siarhei Vishniakou | e9ef6bc | 2023-12-21 19:47:20 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Return the PointerProperties and the PointerCoords for the last event, if found. Return |
| 53 | * std::nullopt if not found. We should not return std::vector<PointerCoords> in isolation, |
| 54 | * because the pointers can technically be stored in the vector in any order, so the |
| 55 | * PointerProperties are needed to specify the order in which the pointer coords are stored. |
| 56 | */ |
| 57 | std::optional<std::pair<std::vector<PointerProperties>, std::vector<PointerCoords>>> |
| 58 | getPointersOfLastEvent(const MotionEntry& entry, bool hovering) const; |
| 59 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 60 | // Create cancel events for the previous stream if the current motionEntry requires it. |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 61 | std::unique_ptr<EventEntry> cancelConflictingInputStream(const MotionEntry& motionEntry); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 62 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 63 | // Synthesizes cancelation events for the current state and resets the tracked state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 64 | std::vector<std::unique_ptr<EventEntry>> synthesizeCancelationEvents( |
| 65 | nsecs_t currentTime, const CancelationOptions& options); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 66 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 67 | // Synthesizes down events for the current state. |
Siarhei Vishniakou | a9a7ee8 | 2019-10-14 16:28:19 -0700 | [diff] [blame] | 68 | std::vector<std::unique_ptr<EventEntry>> synthesizePointerDownEvents(nsecs_t currentTime); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 69 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 70 | // Clears the current state. |
| 71 | void clear(); |
| 72 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 73 | // Merges pointer-related parts of the input state into another instance. |
| 74 | void mergePointerStateTo(InputState& other); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 75 | |
| 76 | // Gets the fallback key associated with a keycode. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 77 | // Returns std::nullopt if none. |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 78 | // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy. |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 79 | std::optional<int32_t> getFallbackKey(int32_t originalKeyCode); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 80 | |
| 81 | // Sets the fallback key for a particular keycode. |
| 82 | void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode); |
| 83 | |
| 84 | // Removes the fallback key for a particular keycode. |
| 85 | void removeFallbackKey(int32_t originalKeyCode); |
| 86 | |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 87 | inline const std::map<int32_t, int32_t>& getFallbackKeys() const { return mFallbackKeys; } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | struct KeyMemento { |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 91 | DeviceId deviceId; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 92 | uint32_t source; |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 93 | ui::LogicalDisplayId displayId{ui::ADISPLAY_ID_NONE}; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 94 | int32_t keyCode; |
| 95 | int32_t scanCode; |
| 96 | int32_t metaState; |
| 97 | int32_t flags; |
| 98 | nsecs_t downTime; |
| 99 | uint32_t policyFlags; |
| 100 | }; |
| 101 | |
| 102 | struct MotionMemento { |
Siarhei Vishniakou | 7e2f8f1 | 2023-07-11 10:51:20 -0700 | [diff] [blame] | 103 | DeviceId deviceId; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 104 | uint32_t source; |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 105 | ui::LogicalDisplayId displayId{ui::ADISPLAY_ID_NONE}; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 106 | int32_t flags; |
| 107 | float xPrecision; |
| 108 | float yPrecision; |
| 109 | float xCursorPosition; |
| 110 | float yCursorPosition; |
| 111 | nsecs_t downTime; |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 112 | std::vector<PointerProperties> pointerProperties; |
| 113 | std::vector<PointerCoords> pointerCoords; |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 114 | // Track for which pointers the target doesn't know about. |
| 115 | int32_t firstNewPointerIdx = INVALID_POINTER_INDEX; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 116 | bool hovering; |
| 117 | uint32_t policyFlags; |
| 118 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 119 | void setPointers(const MotionEntry& entry); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 120 | void mergePointerStateTo(MotionMemento& other) const; |
Siarhei Vishniakou | 47a02a1 | 2023-10-18 09:56:00 -0700 | [diff] [blame] | 121 | size_t getPointerCount() const; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Garfield Tan | ff1f1bb | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 124 | const IdGenerator& mIdGenerator; // InputDispatcher owns it so we won't have dangling reference. |
| 125 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 126 | std::vector<KeyMemento> mKeyMementos; |
| 127 | std::vector<MotionMemento> mMotionMementos; |
Siarhei Vishniakou | 0fe0126 | 2023-04-17 08:11:37 -0700 | [diff] [blame] | 128 | std::map</*originalKeyCode*/int32_t, /*fallbackKeyCode*/int32_t> mFallbackKeys; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 129 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 130 | ssize_t findKeyMemento(const KeyEntry& entry) const; |
| 131 | ssize_t findMotionMemento(const MotionEntry& entry, bool hovering) const; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 132 | |
Siarhei Vishniakou | d277004 | 2019-10-29 11:08:14 -0700 | [diff] [blame] | 133 | void addKeyMemento(const KeyEntry& entry, int32_t flags); |
| 134 | void addMotionMemento(const MotionEntry& entry, int32_t flags, bool hovering); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 135 | |
| 136 | static bool shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options); |
| 137 | static bool shouldCancelMotion(const MotionMemento& memento, const CancelationOptions& options); |
Prabir Pradhan | 2a2da1d | 2023-11-03 02:16:20 +0000 | [diff] [blame] | 138 | bool shouldCancelPreviousStream(const MotionEntry& motionEntry) const; |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 139 | std::unique_ptr<MotionEntry> createCancelEntryForMemento(const MotionMemento& memento, |
| 140 | nsecs_t eventTime) const; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 141 | |
| 142 | // Synthesizes pointer cancel events for a particular set of pointers. |
| 143 | std::vector<std::unique_ptr<MotionEntry>> synthesizeCancelationEventsForPointers( |
Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 144 | const MotionMemento& memento, std::bitset<MAX_POINTER_ID + 1> pointerIds, |
| 145 | nsecs_t currentTime); |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 146 | friend std::ostream& operator<<(std::ostream& out, const InputState& state); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
Siarhei Vishniakou | d38a1e0 | 2023-07-18 11:55:17 -0700 | [diff] [blame] | 149 | std::ostream& operator<<(std::ostream& out, const InputState& state); |
| 150 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 151 | } // namespace inputdispatcher |
| 152 | } // namespace android |