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