Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -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 |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 18 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 19 | #include <ftl/flags.h> |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 20 | #include <input/DisplayViewport.h> |
| 21 | #include <input/InputDevice.h> |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 22 | #include <input/PropertyMap.h> |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 23 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 24 | #include <cstdint> |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 25 | #include <optional> |
| 26 | #include <unordered_map> |
| 27 | #include <vector> |
| 28 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 29 | #include "EventHub.h" |
| 30 | #include "InputReaderBase.h" |
| 31 | #include "InputReaderContext.h" |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 32 | #include "NotifyArgs.h" |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 33 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | |
Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 36 | class PeripheralController; |
| 37 | class PeripheralControllerInterface; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 38 | class InputDeviceContext; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 39 | class InputMapper; |
| 40 | |
| 41 | /* Represents the state of a single input device. */ |
| 42 | class InputDevice { |
| 43 | public: |
| 44 | InputDevice(InputReaderContext* context, int32_t id, int32_t generation, |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 45 | const InputDeviceIdentifier& identifier); |
Prabir Pradhan | 76a4212 | 2024-08-05 14:42:56 +0000 | [diff] [blame] | 46 | virtual ~InputDevice(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 47 | |
| 48 | inline InputReaderContext* getContext() { return mContext; } |
| 49 | inline int32_t getId() const { return mId; } |
| 50 | inline int32_t getControllerNumber() const { return mControllerNumber; } |
| 51 | inline int32_t getGeneration() const { return mGeneration; } |
| 52 | inline const std::string getName() const { return mIdentifier.name; } |
| 53 | inline const std::string getDescriptor() { return mIdentifier.descriptor; } |
Prabir Pradhan | b54ffb2 | 2022-10-27 18:03:34 +0000 | [diff] [blame] | 54 | inline std::optional<std::string> getBluetoothAddress() const { |
| 55 | return mIdentifier.bluetoothAddress; |
| 56 | } |
Zixuan Qu | fecb606 | 2022-11-12 04:44:31 +0000 | [diff] [blame] | 57 | inline const std::string getLocation() const { return mIdentifier.location; } |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 58 | inline ftl::Flags<InputDeviceClass> getClasses() const { return mClasses; } |
Prabir Pradhan | 76a4212 | 2024-08-05 14:42:56 +0000 | [diff] [blame] | 59 | inline virtual uint32_t getSources() const { return mSources; } |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 60 | inline bool hasEventHubDevices() const { return !mDevices.empty(); } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 61 | |
| 62 | inline bool isExternal() { return mIsExternal; } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 63 | inline std::optional<uint8_t> getAssociatedDisplayPort() const { |
| 64 | return mAssociatedDisplayPort; |
| 65 | } |
Antonio Kantek | 0ac5e09 | 2024-04-22 17:10:27 +0000 | [diff] [blame] | 66 | inline std::optional<std::string> getAssociatedDisplayUniqueIdByPort() const { |
| 67 | return mAssociatedDisplayUniqueIdByPort; |
| 68 | } |
| 69 | inline std::optional<std::string> getAssociatedDisplayUniqueIdByDescriptor() const { |
| 70 | return mAssociatedDisplayUniqueIdByDescriptor; |
Christine Franks | 2a2293c | 2022-01-18 11:51:16 -0800 | [diff] [blame] | 71 | } |
Ambrus Weisz | 7bc23bf | 2022-10-04 13:13:07 +0000 | [diff] [blame] | 72 | inline std::optional<std::string> getDeviceTypeAssociation() const { |
| 73 | return mAssociatedDeviceType; |
| 74 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 75 | inline std::optional<DisplayViewport> getAssociatedViewport() const { |
| 76 | return mAssociatedViewport; |
| 77 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 78 | inline bool hasMic() const { return mHasMic; } |
| 79 | |
Vaibhav Devmurari | 16c2419 | 2023-05-04 15:20:12 +0000 | [diff] [blame] | 80 | inline bool isIgnored() { return !getMapperCount() && !mController; } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 81 | |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 82 | inline KeyboardType getKeyboardType() const { return mKeyboardType; } |
| 83 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 84 | bool isEnabled(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 85 | |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame] | 86 | void dump(std::string& dump, const std::string& eventHubDevStr); |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 87 | void addEmptyEventHubDevice(int32_t eventHubId); |
Arpit Singh | 82f29a1 | 2023-06-13 15:05:53 +0000 | [diff] [blame] | 88 | [[nodiscard]] std::list<NotifyArgs> addEventHubDevice( |
| 89 | nsecs_t when, int32_t eventHubId, const InputReaderConfiguration& readerConfig); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 90 | void removeEventHubDevice(int32_t eventHubId); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 91 | [[nodiscard]] std::list<NotifyArgs> configure(nsecs_t when, |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 92 | const InputReaderConfiguration& readerConfig, |
Arpit Singh | 7f1765e | 2023-07-07 13:12:37 +0000 | [diff] [blame] | 93 | ConfigurationChanges changes); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 94 | [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when); |
| 95 | [[nodiscard]] std::list<NotifyArgs> process(const RawEvent* rawEvents, size_t count); |
| 96 | [[nodiscard]] std::list<NotifyArgs> timeoutExpired(nsecs_t when); |
| 97 | [[nodiscard]] std::list<NotifyArgs> updateExternalStylusState(const StylusState& state); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 98 | |
Siarhei Vishniakou | 1983a71 | 2021-06-04 19:27:09 +0000 | [diff] [blame] | 99 | InputDeviceInfo getDeviceInfo(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 100 | int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 101 | int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 102 | int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 103 | int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const; |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 104 | bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 105 | uint8_t* outFlags); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 106 | [[nodiscard]] std::list<NotifyArgs> vibrate(const VibrationSequence& sequence, ssize_t repeat, |
| 107 | int32_t token); |
| 108 | [[nodiscard]] std::list<NotifyArgs> cancelVibrate(int32_t token); |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 109 | bool isVibrating(); |
| 110 | std::vector<int32_t> getVibratorIds(); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 111 | [[nodiscard]] std::list<NotifyArgs> cancelTouch(nsecs_t when, nsecs_t readTime); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 112 | bool enableSensor(InputDeviceSensorType sensorType, std::chrono::microseconds samplingPeriod, |
| 113 | std::chrono::microseconds maxBatchReportLatency); |
| 114 | void disableSensor(InputDeviceSensorType sensorType); |
| 115 | void flushSensor(InputDeviceSensorType sensorType); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 116 | |
Andy Chen | f9f1a02 | 2022-08-29 20:07:10 -0400 | [diff] [blame] | 117 | std::optional<int32_t> getBatteryEventHubId() const; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 118 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 119 | bool setLightColor(int32_t lightId, int32_t color); |
| 120 | bool setLightPlayerId(int32_t lightId, int32_t playerId); |
| 121 | std::optional<int32_t> getLightColor(int32_t lightId); |
| 122 | std::optional<int32_t> getLightPlayerId(int32_t lightId); |
| 123 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 124 | int32_t getMetaState(); |
| 125 | void updateMetaState(int32_t keyCode); |
| 126 | |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 127 | void addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode); |
| 128 | |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 129 | void setKeyboardType(KeyboardType keyboardType); |
| 130 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 131 | void bumpGeneration(); |
| 132 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 133 | [[nodiscard]] NotifyDeviceResetArgs notifyReset(nsecs_t when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 134 | |
Prabir Pradhan | 76a4212 | 2024-08-05 14:42:56 +0000 | [diff] [blame] | 135 | inline virtual const PropertyMap& getConfiguration() const { return mConfiguration; } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 136 | inline EventHubInterface* getEventHub() { return mContext->getEventHub(); } |
| 137 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 138 | std::optional<ui::LogicalDisplayId> getAssociatedDisplayId(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 139 | |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 140 | void updateLedState(bool reset); |
| 141 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 142 | size_t getMapperCount(); |
| 143 | |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 144 | // construct and add a mapper to the input device |
| 145 | template <class T, typename... Args> |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 146 | T& addMapper(int32_t eventHubId, Args... args) { |
| 147 | // ensure a device entry exists for this eventHubId |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 148 | addEmptyEventHubDevice(eventHubId); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 149 | |
| 150 | // create mapper |
| 151 | auto& devicePair = mDevices[eventHubId]; |
| 152 | auto& deviceContext = devicePair.first; |
| 153 | auto& mappers = devicePair.second; |
| 154 | T* mapper = new T(*deviceContext, args...); |
| 155 | mappers.emplace_back(mapper); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 156 | return *mapper; |
| 157 | } |
| 158 | |
Arpit Singh | 56adebc | 2023-04-25 13:56:05 +0000 | [diff] [blame] | 159 | template <class T, typename... Args> |
| 160 | T& constructAndAddMapper(int32_t eventHubId, Args... args) { |
| 161 | // create mapper |
| 162 | auto& devicePair = mDevices[eventHubId]; |
| 163 | auto& deviceContext = devicePair.first; |
| 164 | auto& mappers = devicePair.second; |
| 165 | mappers.push_back(createInputMapper<T>(*deviceContext, args...)); |
| 166 | return static_cast<T&>(*mappers.back()); |
| 167 | } |
| 168 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 169 | // construct and add a controller to the input device |
| 170 | template <class T> |
| 171 | T& addController(int32_t eventHubId) { |
| 172 | // ensure a device entry exists for this eventHubId |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 173 | addEmptyEventHubDevice(eventHubId); |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 174 | |
| 175 | // create controller |
| 176 | auto& devicePair = mDevices[eventHubId]; |
| 177 | auto& deviceContext = devicePair.first; |
| 178 | |
| 179 | mController = std::make_unique<T>(*deviceContext); |
| 180 | return *(reinterpret_cast<T*>(mController.get())); |
| 181 | } |
| 182 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 183 | private: |
| 184 | InputReaderContext* mContext; |
| 185 | int32_t mId; |
| 186 | int32_t mGeneration; |
| 187 | int32_t mControllerNumber; |
| 188 | InputDeviceIdentifier mIdentifier; |
| 189 | std::string mAlias; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 190 | ftl::Flags<InputDeviceClass> mClasses; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 191 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 192 | // map from eventHubId to device context and mappers |
| 193 | using MapperVector = std::vector<std::unique_ptr<InputMapper>>; |
| 194 | using DevicePair = std::pair<std::unique_ptr<InputDeviceContext>, MapperVector>; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 195 | // Map from EventHub ID to pair of device context and vector of mapper. |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 196 | std::unordered_map<int32_t, DevicePair> mDevices; |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 197 | // Misc devices controller for lights, battery, etc. |
Chris Ye | 1dd2e5c | 2021-04-04 23:12:41 -0700 | [diff] [blame] | 198 | std::unique_ptr<PeripheralControllerInterface> mController; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 199 | |
| 200 | uint32_t mSources; |
Yeabkal Wubshit | e03e8b1 | 2023-06-27 16:23:12 -0700 | [diff] [blame] | 201 | bool mIsWaking; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 202 | bool mIsExternal; |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 203 | KeyboardType mKeyboardType = KeyboardType::NONE; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 204 | std::optional<uint8_t> mAssociatedDisplayPort; |
Antonio Kantek | 0ac5e09 | 2024-04-22 17:10:27 +0000 | [diff] [blame] | 205 | std::optional<std::string> mAssociatedDisplayUniqueIdByPort; |
| 206 | std::optional<std::string> mAssociatedDisplayUniqueIdByDescriptor; |
Ambrus Weisz | 7bc23bf | 2022-10-04 13:13:07 +0000 | [diff] [blame] | 207 | std::optional<std::string> mAssociatedDeviceType; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 208 | std::optional<DisplayViewport> mAssociatedViewport; |
| 209 | bool mHasMic; |
| 210 | bool mDropUntilNextSync; |
Yeabkal Wubshit | b1b96db | 2024-01-24 12:47:00 -0800 | [diff] [blame] | 211 | std::optional<bool> mShouldSmoothScroll; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 212 | |
| 213 | typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code); |
| 214 | int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc); |
| 215 | |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 216 | std::vector<std::unique_ptr<InputMapper>> createMappers( |
| 217 | InputDeviceContext& contextPtr, const InputReaderConfiguration& readerConfig); |
| 218 | |
Arpit Singh | 82f29a1 | 2023-06-13 15:05:53 +0000 | [diff] [blame] | 219 | [[nodiscard]] std::list<NotifyArgs> configureInternal( |
| 220 | nsecs_t when, const InputReaderConfiguration& readerConfig, |
| 221 | ConfigurationChanges changes, bool forceEnable = false); |
| 222 | |
Arpit Singh | 4818977 | 2023-05-30 14:12:49 +0000 | [diff] [blame] | 223 | [[nodiscard]] std::list<NotifyArgs> updateEnableState( |
Arpit Singh | 82f29a1 | 2023-06-13 15:05:53 +0000 | [diff] [blame] | 224 | nsecs_t when, const InputReaderConfiguration& readerConfig, bool forceEnable = false); |
Arpit Singh | 4818977 | 2023-05-30 14:12:49 +0000 | [diff] [blame] | 225 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 226 | PropertyMap mConfiguration; |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 227 | |
Yeabkal Wubshit | e03e8b1 | 2023-06-27 16:23:12 -0700 | [diff] [blame] | 228 | // Runs logic post a `process` call. This can be used to update the generated `NotifyArgs` as |
| 229 | // per the properties of the InputDevice. |
| 230 | void postProcess(std::list<NotifyArgs>& args) const; |
| 231 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 232 | // helpers to interate over the devices collection |
| 233 | // run a function against every mapper on every subdevice |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 234 | inline void for_each_mapper(std::function<void(InputMapper&)> f) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 235 | for (auto& deviceEntry : mDevices) { |
| 236 | auto& devicePair = deviceEntry.second; |
| 237 | auto& mappers = devicePair.second; |
| 238 | for (auto& mapperPtr : mappers) { |
| 239 | f(*mapperPtr); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // run a function against every mapper on a specific subdevice |
| 245 | inline void for_each_mapper_in_subdevice(int32_t eventHubDevice, |
| 246 | std::function<void(InputMapper&)> f) { |
| 247 | auto deviceIt = mDevices.find(eventHubDevice); |
| 248 | if (deviceIt != mDevices.end()) { |
| 249 | auto& devicePair = deviceIt->second; |
| 250 | auto& mappers = devicePair.second; |
| 251 | for (auto& mapperPtr : mappers) { |
| 252 | f(*mapperPtr); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // run a function against every subdevice |
| 258 | inline void for_each_subdevice(std::function<void(InputDeviceContext&)> f) { |
| 259 | for (auto& deviceEntry : mDevices) { |
| 260 | auto& devicePair = deviceEntry.second; |
| 261 | auto& contextPtr = devicePair.first; |
| 262 | f(*contextPtr); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | // return the first value returned by a function over every mapper. |
| 267 | // if all mappers return nullopt, return nullopt. |
| 268 | template <typename T> |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 269 | inline std::optional<T> first_in_mappers( |
| 270 | std::function<std::optional<T>(InputMapper&)> f) const { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 271 | for (auto& deviceEntry : mDevices) { |
| 272 | auto& devicePair = deviceEntry.second; |
| 273 | auto& mappers = devicePair.second; |
| 274 | for (auto& mapperPtr : mappers) { |
| 275 | std::optional<T> ret = f(*mapperPtr); |
| 276 | if (ret) { |
| 277 | return ret; |
| 278 | } |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | return std::nullopt; |
| 282 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 283 | }; |
| 284 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 285 | /* Provides access to EventHub methods, but limits access to the current InputDevice. |
| 286 | * Essentially an implementation of EventHubInterface, but for a specific device id. |
| 287 | * Helps hide implementation details of InputDevice and EventHub. Used by mappers to |
| 288 | * check the status of the associated hardware device |
| 289 | */ |
| 290 | class InputDeviceContext { |
| 291 | public: |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 292 | InputDeviceContext(InputDevice& device, int32_t eventHubId); |
Harry Cutts | 92559ef | 2024-01-05 12:45:36 +0000 | [diff] [blame] | 293 | virtual ~InputDeviceContext(); |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 294 | |
| 295 | inline InputReaderContext* getContext() { return mContext; } |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 296 | inline int32_t getId() { return mDeviceId; } |
| 297 | inline int32_t getEventHubId() { return mId; } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 298 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 299 | inline ftl::Flags<InputDeviceClass> getDeviceClasses() const { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 300 | return mEventHub->getDeviceClasses(mId); |
| 301 | } |
Prabir Pradhan | 34f3570 | 2024-07-23 21:59:36 +0000 | [diff] [blame] | 302 | inline uint32_t getDeviceSources() const { return mDevice.getSources(); } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 303 | inline InputDeviceIdentifier getDeviceIdentifier() const { |
| 304 | return mEventHub->getDeviceIdentifier(mId); |
| 305 | } |
| 306 | inline int32_t getDeviceControllerNumber() const { |
| 307 | return mEventHub->getDeviceControllerNumber(mId); |
| 308 | } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 309 | inline status_t getAbsoluteAxisInfo(int32_t code, RawAbsoluteAxisInfo* axisInfo) const { |
Prabir Pradhan | 1030142 | 2023-07-26 21:48:30 +0000 | [diff] [blame] | 310 | if (const auto status = mEventHub->getAbsoluteAxisInfo(mId, code, axisInfo); status != OK) { |
| 311 | return status; |
| 312 | } |
| 313 | |
| 314 | // Validate axis info for InputDevice. |
| 315 | if (axisInfo->valid && axisInfo->minValue == axisInfo->maxValue) { |
| 316 | // Historically, we deem axes with the same min and max values as invalid to avoid |
| 317 | // dividing by zero when scaling by max - min. |
| 318 | // TODO(b/291772515): Perform axis info validation on a per-axis basis when it is used. |
| 319 | axisInfo->valid = false; |
| 320 | } |
| 321 | return OK; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 322 | } |
| 323 | inline bool hasRelativeAxis(int32_t code) const { |
| 324 | return mEventHub->hasRelativeAxis(mId, code); |
| 325 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 326 | inline bool hasInputProperty(int32_t property) const { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 327 | return mEventHub->hasInputProperty(mId, property); |
| 328 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 329 | |
| 330 | inline bool hasMscEvent(int mscEvent) const { return mEventHub->hasMscEvent(mId, mscEvent); } |
| 331 | |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 332 | inline void addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode) const { |
| 333 | mEventHub->addKeyRemapping(mId, fromKeyCode, toKeyCode); |
| 334 | } |
| 335 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 336 | inline status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t metaState, |
| 337 | int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const { |
| 338 | return mEventHub->mapKey(mId, scanCode, usageCode, metaState, outKeycode, outMetaState, |
| 339 | outFlags); |
| 340 | } |
| 341 | inline status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const { |
| 342 | return mEventHub->mapAxis(mId, scanCode, outAxisInfo); |
| 343 | } |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 344 | inline base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t absCode) { |
| 345 | return mEventHub->mapSensor(mId, absCode); |
| 346 | } |
| 347 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 348 | inline const std::vector<int32_t> getRawLightIds() { return mEventHub->getRawLightIds(mId); } |
| 349 | |
| 350 | inline std::optional<RawLightInfo> getRawLightInfo(int32_t lightId) { |
| 351 | return mEventHub->getRawLightInfo(mId, lightId); |
| 352 | } |
| 353 | |
| 354 | inline std::optional<int32_t> getLightBrightness(int32_t lightId) { |
| 355 | return mEventHub->getLightBrightness(mId, lightId); |
| 356 | } |
| 357 | |
| 358 | inline void setLightBrightness(int32_t lightId, int32_t brightness) { |
| 359 | return mEventHub->setLightBrightness(mId, lightId, brightness); |
| 360 | } |
| 361 | |
| 362 | inline std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities( |
| 363 | int32_t lightId) { |
| 364 | return mEventHub->getLightIntensities(mId, lightId); |
| 365 | } |
| 366 | |
| 367 | inline void setLightIntensities(int32_t lightId, |
| 368 | std::unordered_map<LightColor, int32_t> intensities) { |
| 369 | return mEventHub->setLightIntensities(mId, lightId, intensities); |
| 370 | } |
| 371 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 372 | inline std::vector<TouchVideoFrame> getVideoFrames() { return mEventHub->getVideoFrames(mId); } |
| 373 | inline int32_t getScanCodeState(int32_t scanCode) const { |
| 374 | return mEventHub->getScanCodeState(mId, scanCode); |
| 375 | } |
| 376 | inline int32_t getKeyCodeState(int32_t keyCode) const { |
| 377 | return mEventHub->getKeyCodeState(mId, keyCode); |
| 378 | } |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 379 | inline int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const { |
| 380 | return mEventHub->getKeyCodeForKeyLocation(mId, locationKeyCode); |
| 381 | } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 382 | inline int32_t getSwitchState(int32_t sw) const { return mEventHub->getSwitchState(mId, sw); } |
| 383 | inline status_t getAbsoluteAxisValue(int32_t code, int32_t* outValue) const { |
| 384 | return mEventHub->getAbsoluteAxisValue(mId, code, outValue); |
| 385 | } |
Arpit Singh | 4b4a457 | 2023-11-24 18:19:56 +0000 | [diff] [blame] | 386 | inline base::Result<std::vector<int32_t>> getMtSlotValues(int32_t axis, |
| 387 | size_t slotCount) const { |
| 388 | return mEventHub->getMtSlotValues(mId, axis, slotCount); |
| 389 | } |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 390 | inline bool markSupportedKeyCodes(const std::vector<int32_t>& keyCodes, |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 391 | uint8_t* outFlags) const { |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 392 | return mEventHub->markSupportedKeyCodes(mId, keyCodes, outFlags); |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 393 | } |
| 394 | inline bool hasScanCode(int32_t scanCode) const { |
| 395 | return mEventHub->hasScanCode(mId, scanCode); |
| 396 | } |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 397 | inline bool hasKeyCode(int32_t keyCode) const { return mEventHub->hasKeyCode(mId, keyCode); } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 398 | inline bool hasLed(int32_t led) const { return mEventHub->hasLed(mId, led); } |
| 399 | inline void setLedState(int32_t led, bool on) { return mEventHub->setLedState(mId, led, on); } |
| 400 | inline void getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition>& outVirtualKeys) const { |
| 401 | return mEventHub->getVirtualKeyDefinitions(mId, outVirtualKeys); |
| 402 | } |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 403 | inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 404 | return mEventHub->getKeyCharacterMap(mId); |
| 405 | } |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 406 | inline bool setKeyboardLayoutOverlay(std::shared_ptr<KeyCharacterMap> map) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 407 | return mEventHub->setKeyboardLayoutOverlay(mId, map); |
| 408 | } |
Vaibhav Devmurari | 7fb4113 | 2023-01-02 13:30:26 +0000 | [diff] [blame] | 409 | inline const std::optional<RawLayoutInfo> getRawLayoutInfo() { |
| 410 | return mEventHub->getRawLayoutInfo(mId); |
| 411 | } |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 412 | inline void vibrate(const VibrationElement& element) { |
| 413 | return mEventHub->vibrate(mId, element); |
| 414 | } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 415 | inline void cancelVibrate() { return mEventHub->cancelVibrate(mId); } |
| 416 | |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 417 | inline std::vector<int32_t> getVibratorIds() { return mEventHub->getVibratorIds(mId); } |
| 418 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 419 | inline const std::vector<int32_t> getRawBatteryIds() { |
| 420 | return mEventHub->getRawBatteryIds(mId); |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Chris Ye | e2b1e5c | 2021-03-10 22:45:12 -0800 | [diff] [blame] | 423 | inline std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t batteryId) { |
| 424 | return mEventHub->getRawBatteryInfo(mId, batteryId); |
| 425 | } |
| 426 | |
| 427 | inline std::optional<int32_t> getBatteryCapacity(int32_t batteryId) { |
| 428 | return mEventHub->getBatteryCapacity(mId, batteryId); |
| 429 | } |
| 430 | |
| 431 | inline std::optional<int32_t> getBatteryStatus(int32_t batteryId) { |
| 432 | return mEventHub->getBatteryStatus(mId, batteryId); |
| 433 | } |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 434 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 435 | inline bool hasAbsoluteAxis(int32_t code) const { |
| 436 | RawAbsoluteAxisInfo info; |
| 437 | mEventHub->getAbsoluteAxisInfo(mId, code, &info); |
| 438 | return info.valid; |
| 439 | } |
Prabir Pradhan | 4f05b5f | 2022-10-11 21:24:07 +0000 | [diff] [blame] | 440 | inline bool isKeyPressed(int32_t scanCode) const { |
| 441 | return mEventHub->getScanCodeState(mId, scanCode) == AKEY_STATE_DOWN; |
| 442 | } |
| 443 | inline bool isKeyCodePressed(int32_t keyCode) const { |
| 444 | return mEventHub->getKeyCodeState(mId, keyCode) == AKEY_STATE_DOWN; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 445 | } |
| 446 | inline int32_t getAbsoluteAxisValue(int32_t code) const { |
| 447 | int32_t value; |
| 448 | mEventHub->getAbsoluteAxisValue(mId, code, &value); |
| 449 | return value; |
| 450 | } |
| 451 | inline bool isDeviceEnabled() { return mEventHub->isDeviceEnabled(mId); } |
| 452 | inline status_t enableDevice() { return mEventHub->enableDevice(mId); } |
| 453 | inline status_t disableDevice() { return mEventHub->disableDevice(mId); } |
| 454 | |
Harry Cutts | ba9364a | 2022-12-15 12:39:04 +0000 | [diff] [blame] | 455 | inline const std::string getName() const { return mDevice.getName(); } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 456 | inline const std::string getDescriptor() { return mDevice.getDescriptor(); } |
Zixuan Qu | fecb606 | 2022-11-12 04:44:31 +0000 | [diff] [blame] | 457 | inline const std::string getLocation() { return mDevice.getLocation(); } |
Arpit Singh | 403e53c | 2023-04-18 11:46:56 +0000 | [diff] [blame] | 458 | inline bool isExternal() const { return mDevice.isExternal(); } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 459 | inline std::optional<uint8_t> getAssociatedDisplayPort() const { |
| 460 | return mDevice.getAssociatedDisplayPort(); |
| 461 | } |
Antonio Kantek | 0ac5e09 | 2024-04-22 17:10:27 +0000 | [diff] [blame] | 462 | inline std::optional<std::string> getAssociatedDisplayUniqueIdByPort() const { |
| 463 | return mDevice.getAssociatedDisplayUniqueIdByPort(); |
| 464 | } |
| 465 | inline std::optional<std::string> getAssociatedDisplayUniqueIdByDescriptor() const { |
| 466 | return mDevice.getAssociatedDisplayUniqueIdByDescriptor(); |
Christine Franks | 2a2293c | 2022-01-18 11:51:16 -0800 | [diff] [blame] | 467 | } |
Ambrus Weisz | 7bc23bf | 2022-10-04 13:13:07 +0000 | [diff] [blame] | 468 | inline std::optional<std::string> getDeviceTypeAssociation() const { |
| 469 | return mDevice.getDeviceTypeAssociation(); |
| 470 | } |
Harry Cutts | 92559ef | 2024-01-05 12:45:36 +0000 | [diff] [blame] | 471 | virtual std::optional<DisplayViewport> getAssociatedViewport() const { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 472 | return mDevice.getAssociatedViewport(); |
| 473 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 474 | [[nodiscard]] inline std::list<NotifyArgs> cancelTouch(nsecs_t when, nsecs_t readTime) { |
| 475 | return mDevice.cancelTouch(when, readTime); |
| 476 | } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 477 | inline void bumpGeneration() { mDevice.bumpGeneration(); } |
Arpit Singh | 403e53c | 2023-04-18 11:46:56 +0000 | [diff] [blame] | 478 | inline const PropertyMap& getConfiguration() const { return mDevice.getConfiguration(); } |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 479 | inline KeyboardType getKeyboardType() const { return mDevice.getKeyboardType(); } |
| 480 | inline void setKeyboardType(KeyboardType keyboardType) { |
| 481 | return mDevice.setKeyboardType(keyboardType); |
| 482 | } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 483 | |
| 484 | private: |
| 485 | InputDevice& mDevice; |
| 486 | InputReaderContext* mContext; |
| 487 | EventHubInterface* mEventHub; |
| 488 | int32_t mId; |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 489 | int32_t mDeviceId; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 490 | }; |
| 491 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 492 | } // namespace android |