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 | |
| 17 | #ifndef _UI_INPUTREADER_INPUT_DEVICE_H |
| 18 | #define _UI_INPUTREADER_INPUT_DEVICE_H |
| 19 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 20 | #include <input/DisplayViewport.h> |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 21 | #include <input/Flags.h> |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 22 | #include <input/InputDevice.h> |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 23 | #include <input/PropertyMap.h> |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 24 | #include <stdint.h> |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 25 | |
| 26 | #include <optional> |
| 27 | #include <unordered_map> |
| 28 | #include <vector> |
| 29 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 30 | #include "EventHub.h" |
| 31 | #include "InputReaderBase.h" |
| 32 | #include "InputReaderContext.h" |
| 33 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 36 | class InputDeviceContext; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 37 | class InputMapper; |
| 38 | |
| 39 | /* Represents the state of a single input device. */ |
| 40 | class InputDevice { |
| 41 | public: |
| 42 | InputDevice(InputReaderContext* context, int32_t id, int32_t generation, |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 43 | const InputDeviceIdentifier& identifier); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 44 | ~InputDevice(); |
| 45 | |
| 46 | inline InputReaderContext* getContext() { return mContext; } |
| 47 | inline int32_t getId() const { return mId; } |
| 48 | inline int32_t getControllerNumber() const { return mControllerNumber; } |
| 49 | inline int32_t getGeneration() const { return mGeneration; } |
| 50 | inline const std::string getName() const { return mIdentifier.name; } |
| 51 | inline const std::string getDescriptor() { return mIdentifier.descriptor; } |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 52 | inline Flags<InputDeviceClass> getClasses() const { return mClasses; } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 53 | inline uint32_t getSources() const { return mSources; } |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 54 | inline bool hasEventHubDevices() const { return !mDevices.empty(); } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 55 | |
| 56 | inline bool isExternal() { return mIsExternal; } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 57 | inline std::optional<uint8_t> getAssociatedDisplayPort() const { |
| 58 | return mAssociatedDisplayPort; |
| 59 | } |
| 60 | inline std::optional<DisplayViewport> getAssociatedViewport() const { |
| 61 | return mAssociatedViewport; |
| 62 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 63 | inline bool hasMic() const { return mHasMic; } |
| 64 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 65 | inline bool isIgnored() { return !getMapperCount(); } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 66 | |
| 67 | bool isEnabled(); |
| 68 | void setEnabled(bool enabled, nsecs_t when); |
| 69 | |
Chris Ye | e731003 | 2020-09-22 15:36:28 -0700 | [diff] [blame^] | 70 | void dump(std::string& dump, const std::string& eventHubDevStr); |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 71 | void addEventHubDevice(int32_t eventHubId, bool populateMappers = true); |
| 72 | void removeEventHubDevice(int32_t eventHubId); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 73 | void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes); |
| 74 | void reset(nsecs_t when); |
| 75 | void process(const RawEvent* rawEvents, size_t count); |
| 76 | void timeoutExpired(nsecs_t when); |
| 77 | void updateExternalStylusState(const StylusState& state); |
| 78 | |
| 79 | void getDeviceInfo(InputDeviceInfo* outDeviceInfo); |
| 80 | int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 81 | int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 82 | int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
| 83 | bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, const int32_t* keyCodes, |
| 84 | uint8_t* outFlags); |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 85 | void vibrate(const std::vector<VibrationElement>& pattern, ssize_t repeat, int32_t token); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 86 | void cancelVibrate(int32_t token); |
| 87 | void cancelTouch(nsecs_t when); |
| 88 | |
| 89 | int32_t getMetaState(); |
| 90 | void updateMetaState(int32_t keyCode); |
| 91 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 92 | void bumpGeneration(); |
| 93 | |
| 94 | void notifyReset(nsecs_t when); |
| 95 | |
| 96 | inline const PropertyMap& getConfiguration() { return mConfiguration; } |
| 97 | inline EventHubInterface* getEventHub() { return mContext->getEventHub(); } |
| 98 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 99 | std::optional<int32_t> getAssociatedDisplayId(); |
| 100 | |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 101 | void updateLedState(bool reset); |
| 102 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 103 | size_t getMapperCount(); |
| 104 | |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 105 | // construct and add a mapper to the input device |
| 106 | template <class T, typename... Args> |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 107 | T& addMapper(int32_t eventHubId, Args... args) { |
| 108 | // ensure a device entry exists for this eventHubId |
| 109 | addEventHubDevice(eventHubId, false); |
| 110 | |
| 111 | // create mapper |
| 112 | auto& devicePair = mDevices[eventHubId]; |
| 113 | auto& deviceContext = devicePair.first; |
| 114 | auto& mappers = devicePair.second; |
| 115 | T* mapper = new T(*deviceContext, args...); |
| 116 | mappers.emplace_back(mapper); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 117 | return *mapper; |
| 118 | } |
| 119 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 120 | private: |
| 121 | InputReaderContext* mContext; |
| 122 | int32_t mId; |
| 123 | int32_t mGeneration; |
| 124 | int32_t mControllerNumber; |
| 125 | InputDeviceIdentifier mIdentifier; |
| 126 | std::string mAlias; |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 127 | Flags<InputDeviceClass> mClasses; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 128 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 129 | // map from eventHubId to device context and mappers |
| 130 | using MapperVector = std::vector<std::unique_ptr<InputMapper>>; |
| 131 | using DevicePair = std::pair<std::unique_ptr<InputDeviceContext>, MapperVector>; |
| 132 | std::unordered_map<int32_t, DevicePair> mDevices; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 133 | |
| 134 | uint32_t mSources; |
| 135 | bool mIsExternal; |
| 136 | std::optional<uint8_t> mAssociatedDisplayPort; |
| 137 | std::optional<DisplayViewport> mAssociatedViewport; |
| 138 | bool mHasMic; |
| 139 | bool mDropUntilNextSync; |
| 140 | |
| 141 | typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code); |
| 142 | int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc); |
| 143 | |
| 144 | PropertyMap mConfiguration; |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 145 | |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 146 | // helpers to interate over the devices collection |
| 147 | // run a function against every mapper on every subdevice |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 148 | inline void for_each_mapper(std::function<void(InputMapper&)> f) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 149 | for (auto& deviceEntry : mDevices) { |
| 150 | auto& devicePair = deviceEntry.second; |
| 151 | auto& mappers = devicePair.second; |
| 152 | for (auto& mapperPtr : mappers) { |
| 153 | f(*mapperPtr); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // run a function against every mapper on a specific subdevice |
| 159 | inline void for_each_mapper_in_subdevice(int32_t eventHubDevice, |
| 160 | std::function<void(InputMapper&)> f) { |
| 161 | auto deviceIt = mDevices.find(eventHubDevice); |
| 162 | if (deviceIt != mDevices.end()) { |
| 163 | auto& devicePair = deviceIt->second; |
| 164 | auto& mappers = devicePair.second; |
| 165 | for (auto& mapperPtr : mappers) { |
| 166 | f(*mapperPtr); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // run a function against every subdevice |
| 172 | inline void for_each_subdevice(std::function<void(InputDeviceContext&)> f) { |
| 173 | for (auto& deviceEntry : mDevices) { |
| 174 | auto& devicePair = deviceEntry.second; |
| 175 | auto& contextPtr = devicePair.first; |
| 176 | f(*contextPtr); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | // return the first value returned by a function over every mapper. |
| 181 | // if all mappers return nullopt, return nullopt. |
| 182 | template <typename T> |
| 183 | inline std::optional<T> first_in_mappers(std::function<std::optional<T>(InputMapper&)> f) { |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 184 | for (auto& deviceEntry : mDevices) { |
| 185 | auto& devicePair = deviceEntry.second; |
| 186 | auto& mappers = devicePair.second; |
| 187 | for (auto& mapperPtr : mappers) { |
| 188 | std::optional<T> ret = f(*mapperPtr); |
| 189 | if (ret) { |
| 190 | return ret; |
| 191 | } |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | return std::nullopt; |
| 195 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 196 | }; |
| 197 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 198 | /* Provides access to EventHub methods, but limits access to the current InputDevice. |
| 199 | * Essentially an implementation of EventHubInterface, but for a specific device id. |
| 200 | * Helps hide implementation details of InputDevice and EventHub. Used by mappers to |
| 201 | * check the status of the associated hardware device |
| 202 | */ |
| 203 | class InputDeviceContext { |
| 204 | public: |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 205 | InputDeviceContext(InputDevice& device, int32_t eventHubId); |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 206 | ~InputDeviceContext(); |
| 207 | |
| 208 | inline InputReaderContext* getContext() { return mContext; } |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 209 | inline int32_t getId() { return mDeviceId; } |
| 210 | inline int32_t getEventHubId() { return mId; } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 211 | |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 212 | inline Flags<InputDeviceClass> getDeviceClasses() const { |
| 213 | return mEventHub->getDeviceClasses(mId); |
| 214 | } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 215 | inline InputDeviceIdentifier getDeviceIdentifier() const { |
| 216 | return mEventHub->getDeviceIdentifier(mId); |
| 217 | } |
| 218 | inline int32_t getDeviceControllerNumber() const { |
| 219 | return mEventHub->getDeviceControllerNumber(mId); |
| 220 | } |
| 221 | inline void getConfiguration(PropertyMap* outConfiguration) const { |
| 222 | return mEventHub->getConfiguration(mId, outConfiguration); |
| 223 | } |
| 224 | inline status_t getAbsoluteAxisInfo(int32_t code, RawAbsoluteAxisInfo* axisInfo) const { |
| 225 | return mEventHub->getAbsoluteAxisInfo(mId, code, axisInfo); |
| 226 | } |
| 227 | inline bool hasRelativeAxis(int32_t code) const { |
| 228 | return mEventHub->hasRelativeAxis(mId, code); |
| 229 | } |
| 230 | inline bool hasInputProperty(int property) const { |
| 231 | return mEventHub->hasInputProperty(mId, property); |
| 232 | } |
| 233 | inline status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t metaState, |
| 234 | int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const { |
| 235 | return mEventHub->mapKey(mId, scanCode, usageCode, metaState, outKeycode, outMetaState, |
| 236 | outFlags); |
| 237 | } |
| 238 | inline status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const { |
| 239 | return mEventHub->mapAxis(mId, scanCode, outAxisInfo); |
| 240 | } |
| 241 | inline std::vector<TouchVideoFrame> getVideoFrames() { return mEventHub->getVideoFrames(mId); } |
| 242 | inline int32_t getScanCodeState(int32_t scanCode) const { |
| 243 | return mEventHub->getScanCodeState(mId, scanCode); |
| 244 | } |
| 245 | inline int32_t getKeyCodeState(int32_t keyCode) const { |
| 246 | return mEventHub->getKeyCodeState(mId, keyCode); |
| 247 | } |
| 248 | inline int32_t getSwitchState(int32_t sw) const { return mEventHub->getSwitchState(mId, sw); } |
| 249 | inline status_t getAbsoluteAxisValue(int32_t code, int32_t* outValue) const { |
| 250 | return mEventHub->getAbsoluteAxisValue(mId, code, outValue); |
| 251 | } |
| 252 | inline bool markSupportedKeyCodes(size_t numCodes, const int32_t* keyCodes, |
| 253 | uint8_t* outFlags) const { |
| 254 | return mEventHub->markSupportedKeyCodes(mId, numCodes, keyCodes, outFlags); |
| 255 | } |
| 256 | inline bool hasScanCode(int32_t scanCode) const { |
| 257 | return mEventHub->hasScanCode(mId, scanCode); |
| 258 | } |
| 259 | inline bool hasLed(int32_t led) const { return mEventHub->hasLed(mId, led); } |
| 260 | inline void setLedState(int32_t led, bool on) { return mEventHub->setLedState(mId, led, on); } |
| 261 | inline void getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition>& outVirtualKeys) const { |
| 262 | return mEventHub->getVirtualKeyDefinitions(mId, outVirtualKeys); |
| 263 | } |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 264 | inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 265 | return mEventHub->getKeyCharacterMap(mId); |
| 266 | } |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 267 | inline bool setKeyboardLayoutOverlay(std::shared_ptr<KeyCharacterMap> map) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 268 | return mEventHub->setKeyboardLayoutOverlay(mId, map); |
| 269 | } |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 270 | inline void vibrate(const VibrationElement& element) { |
| 271 | return mEventHub->vibrate(mId, element); |
| 272 | } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 273 | inline void cancelVibrate() { return mEventHub->cancelVibrate(mId); } |
| 274 | |
| 275 | inline bool hasAbsoluteAxis(int32_t code) const { |
| 276 | RawAbsoluteAxisInfo info; |
| 277 | mEventHub->getAbsoluteAxisInfo(mId, code, &info); |
| 278 | return info.valid; |
| 279 | } |
| 280 | inline bool isKeyPressed(int32_t code) const { |
| 281 | return mEventHub->getScanCodeState(mId, code) == AKEY_STATE_DOWN; |
| 282 | } |
| 283 | inline int32_t getAbsoluteAxisValue(int32_t code) const { |
| 284 | int32_t value; |
| 285 | mEventHub->getAbsoluteAxisValue(mId, code, &value); |
| 286 | return value; |
| 287 | } |
| 288 | inline bool isDeviceEnabled() { return mEventHub->isDeviceEnabled(mId); } |
| 289 | inline status_t enableDevice() { return mEventHub->enableDevice(mId); } |
| 290 | inline status_t disableDevice() { return mEventHub->disableDevice(mId); } |
| 291 | |
| 292 | inline const std::string getName() { return mDevice.getName(); } |
| 293 | inline const std::string getDescriptor() { return mDevice.getDescriptor(); } |
| 294 | inline bool isExternal() { return mDevice.isExternal(); } |
| 295 | inline std::optional<uint8_t> getAssociatedDisplayPort() const { |
| 296 | return mDevice.getAssociatedDisplayPort(); |
| 297 | } |
| 298 | inline std::optional<DisplayViewport> getAssociatedViewport() const { |
| 299 | return mDevice.getAssociatedViewport(); |
| 300 | } |
| 301 | inline void cancelTouch(nsecs_t when) { mDevice.cancelTouch(when); } |
| 302 | inline void bumpGeneration() { mDevice.bumpGeneration(); } |
| 303 | inline const PropertyMap& getConfiguration() { return mDevice.getConfiguration(); } |
| 304 | |
| 305 | private: |
| 306 | InputDevice& mDevice; |
| 307 | InputReaderContext* mContext; |
| 308 | EventHubInterface* mEventHub; |
| 309 | int32_t mId; |
Nathaniel R. Lewis | a7b82e1 | 2020-02-12 15:40:45 -0800 | [diff] [blame] | 310 | int32_t mDeviceId; |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 311 | }; |
| 312 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 313 | } // namespace android |
| 314 | |
| 315 | #endif //_UI_INPUTREADER_INPUT_DEVICE_H |