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 #pragma once 18 19 #include "CancelationOptions.h" 20 #include "Entry.h" 21 22 #include <utils/Timers.h> 23 #include <bitset> 24 25 namespace android { 26 namespace inputdispatcher { 27 28 static constexpr int32_t INVALID_POINTER_INDEX = -1; 29 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: 34 explicit InputState(const IdGenerator& idGenerator); 35 ~InputState(); 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. 44 bool trackKey(const KeyEntry& entry, int32_t action, int32_t flags); 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. 49 bool trackMotion(const MotionEntry& entry, int32_t action, int32_t flags); 50 51 // Synthesizes cancelation events for the current state and resets the tracked state. 52 std::vector<std::unique_ptr<EventEntry>> synthesizeCancelationEvents( 53 nsecs_t currentTime, const CancelationOptions& options); 54 55 // Synthesizes down events for the current state. 56 std::vector<std::unique_ptr<EventEntry>> synthesizePointerDownEvents(nsecs_t currentTime); 57 58 // Clears the current state. 59 void clear(); 60 61 // Merges pointer-related parts of the input state into another instance. 62 void mergePointerStateTo(InputState& other); 63 64 // Gets the fallback key associated with a keycode. 65 // Returns std::nullopt if none. 66 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy. 67 std::optional<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 getFallbackKeys()75 inline const std::map<int32_t, int32_t>& getFallbackKeys() const { return mFallbackKeys; } 76 77 private: 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 // Track for which pointers the target doesn't know about. 104 int32_t firstNewPointerIdx = INVALID_POINTER_INDEX; 105 bool hovering; 106 uint32_t policyFlags; 107 108 void setPointers(const MotionEntry& entry); 109 void mergePointerStateTo(MotionMemento& other) const; 110 }; 111 112 const IdGenerator& mIdGenerator; // InputDispatcher owns it so we won't have dangling reference. 113 114 std::vector<KeyMemento> mKeyMementos; 115 std::vector<MotionMemento> mMotionMementos; 116 std::map</*originalKeyCode*/int32_t, /*fallbackKeyCode*/int32_t> mFallbackKeys; 117 118 ssize_t findKeyMemento(const KeyEntry& entry) const; 119 ssize_t findMotionMemento(const MotionEntry& entry, bool hovering) const; 120 121 void addKeyMemento(const KeyEntry& entry, int32_t flags); 122 void addMotionMemento(const MotionEntry& entry, int32_t flags, bool hovering); 123 124 static bool shouldCancelKey(const KeyMemento& memento, const CancelationOptions& options); 125 static bool shouldCancelMotion(const MotionMemento& memento, const CancelationOptions& options); 126 127 // Synthesizes pointer cancel events for a particular set of pointers. 128 std::vector<std::unique_ptr<MotionEntry>> synthesizeCancelationEventsForPointers( 129 const MotionMemento& memento, std::bitset<MAX_POINTER_ID + 1> pointerIds, 130 nsecs_t currentTime); 131 }; 132 133 } // namespace inputdispatcher 134 } // namespace android 135