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 | |
| 20 | #include "EventHub.h" |
| 21 | #include "InputReaderBase.h" |
| 22 | #include "InputReaderContext.h" |
| 23 | |
| 24 | #include <input/DisplayViewport.h> |
| 25 | #include <input/InputDevice.h> |
| 26 | #include <utils/PropertyMap.h> |
| 27 | |
| 28 | #include <stdint.h> |
| 29 | #include <optional> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace android { |
| 33 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame^] | 34 | class InputDeviceContext; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 35 | class InputMapper; |
| 36 | |
| 37 | /* Represents the state of a single input device. */ |
| 38 | class InputDevice { |
| 39 | public: |
| 40 | InputDevice(InputReaderContext* context, int32_t id, int32_t generation, |
| 41 | int32_t controllerNumber, const InputDeviceIdentifier& identifier, |
| 42 | uint32_t classes); |
| 43 | ~InputDevice(); |
| 44 | |
| 45 | inline InputReaderContext* getContext() { return mContext; } |
| 46 | inline int32_t getId() const { return mId; } |
| 47 | inline int32_t getControllerNumber() const { return mControllerNumber; } |
| 48 | inline int32_t getGeneration() const { return mGeneration; } |
| 49 | inline const std::string getName() const { return mIdentifier.name; } |
| 50 | inline const std::string getDescriptor() { return mIdentifier.descriptor; } |
| 51 | inline uint32_t getClasses() const { return mClasses; } |
| 52 | inline uint32_t getSources() const { return mSources; } |
| 53 | |
| 54 | inline bool isExternal() { return mIsExternal; } |
| 55 | inline void setExternal(bool external) { mIsExternal = external; } |
| 56 | inline std::optional<uint8_t> getAssociatedDisplayPort() const { |
| 57 | return mAssociatedDisplayPort; |
| 58 | } |
| 59 | inline std::optional<DisplayViewport> getAssociatedViewport() const { |
| 60 | return mAssociatedViewport; |
| 61 | } |
| 62 | inline void setMic(bool hasMic) { mHasMic = hasMic; } |
| 63 | inline bool hasMic() const { return mHasMic; } |
| 64 | |
| 65 | inline bool isIgnored() { return mMappers.empty(); } |
| 66 | |
| 67 | bool isEnabled(); |
| 68 | void setEnabled(bool enabled, nsecs_t when); |
| 69 | |
| 70 | void dump(std::string& dump); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 71 | void populateMappers(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 72 | void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes); |
| 73 | void reset(nsecs_t when); |
| 74 | void process(const RawEvent* rawEvents, size_t count); |
| 75 | void timeoutExpired(nsecs_t when); |
| 76 | void updateExternalStylusState(const StylusState& state); |
| 77 | |
| 78 | void getDeviceInfo(InputDeviceInfo* outDeviceInfo); |
| 79 | int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 80 | int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 81 | int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
| 82 | bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, const int32_t* keyCodes, |
| 83 | uint8_t* outFlags); |
| 84 | void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token); |
| 85 | void cancelVibrate(int32_t token); |
| 86 | void cancelTouch(nsecs_t when); |
| 87 | |
| 88 | int32_t getMetaState(); |
| 89 | void updateMetaState(int32_t keyCode); |
| 90 | |
| 91 | void fadePointer(); |
| 92 | |
| 93 | void bumpGeneration(); |
| 94 | |
| 95 | void notifyReset(nsecs_t when); |
| 96 | |
| 97 | inline const PropertyMap& getConfiguration() { return mConfiguration; } |
| 98 | inline EventHubInterface* getEventHub() { return mContext->getEventHub(); } |
| 99 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 100 | std::optional<int32_t> getAssociatedDisplayId(); |
| 101 | |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 102 | // construct and add a mapper to the input device |
| 103 | template <class T, typename... Args> |
| 104 | T& addMapper(Args... args) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame^] | 105 | T* mapper = new T(*mDeviceContext, args...); |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 106 | mMappers.emplace_back(mapper); |
| 107 | return *mapper; |
| 108 | } |
| 109 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 110 | private: |
| 111 | InputReaderContext* mContext; |
| 112 | int32_t mId; |
| 113 | int32_t mGeneration; |
| 114 | int32_t mControllerNumber; |
| 115 | InputDeviceIdentifier mIdentifier; |
| 116 | std::string mAlias; |
| 117 | uint32_t mClasses; |
| 118 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame^] | 119 | std::unique_ptr<InputDeviceContext> mDeviceContext; |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 120 | std::vector<std::unique_ptr<InputMapper>> mMappers; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 121 | |
| 122 | uint32_t mSources; |
| 123 | bool mIsExternal; |
| 124 | std::optional<uint8_t> mAssociatedDisplayPort; |
| 125 | std::optional<DisplayViewport> mAssociatedViewport; |
| 126 | bool mHasMic; |
| 127 | bool mDropUntilNextSync; |
| 128 | |
| 129 | typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code); |
| 130 | int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc); |
| 131 | |
| 132 | PropertyMap mConfiguration; |
Nathaniel R. Lewis | f4916ef | 2020-01-14 11:57:18 -0800 | [diff] [blame] | 133 | |
| 134 | // run a function against every mapper |
| 135 | inline void for_each_mapper(std::function<void(InputMapper&)> f) { |
| 136 | for (auto& mapperPtr : mMappers) { |
| 137 | f(*mapperPtr); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // return the first value returned by a function over every mapper. |
| 142 | // if all mappers return nullopt, return nullopt. |
| 143 | template <typename T> |
| 144 | inline std::optional<T> first_in_mappers(std::function<std::optional<T>(InputMapper&)> f) { |
| 145 | for (auto& mapperPtr : mMappers) { |
| 146 | std::optional<T> ret = f(*mapperPtr); |
| 147 | if (ret) { |
| 148 | return ret; |
| 149 | } |
| 150 | } |
| 151 | return std::nullopt; |
| 152 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame^] | 155 | /* Provides access to EventHub methods, but limits access to the current InputDevice. |
| 156 | * Essentially an implementation of EventHubInterface, but for a specific device id. |
| 157 | * Helps hide implementation details of InputDevice and EventHub. Used by mappers to |
| 158 | * check the status of the associated hardware device |
| 159 | */ |
| 160 | class InputDeviceContext { |
| 161 | public: |
| 162 | InputDeviceContext(InputDevice& device); |
| 163 | ~InputDeviceContext(); |
| 164 | |
| 165 | inline InputReaderContext* getContext() { return mContext; } |
| 166 | inline int32_t getId() { return mId; } |
| 167 | |
| 168 | inline uint32_t getDeviceClasses() const { return mEventHub->getDeviceClasses(mId); } |
| 169 | inline InputDeviceIdentifier getDeviceIdentifier() const { |
| 170 | return mEventHub->getDeviceIdentifier(mId); |
| 171 | } |
| 172 | inline int32_t getDeviceControllerNumber() const { |
| 173 | return mEventHub->getDeviceControllerNumber(mId); |
| 174 | } |
| 175 | inline void getConfiguration(PropertyMap* outConfiguration) const { |
| 176 | return mEventHub->getConfiguration(mId, outConfiguration); |
| 177 | } |
| 178 | inline status_t getAbsoluteAxisInfo(int32_t code, RawAbsoluteAxisInfo* axisInfo) const { |
| 179 | return mEventHub->getAbsoluteAxisInfo(mId, code, axisInfo); |
| 180 | } |
| 181 | inline bool hasRelativeAxis(int32_t code) const { |
| 182 | return mEventHub->hasRelativeAxis(mId, code); |
| 183 | } |
| 184 | inline bool hasInputProperty(int property) const { |
| 185 | return mEventHub->hasInputProperty(mId, property); |
| 186 | } |
| 187 | inline status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t metaState, |
| 188 | int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const { |
| 189 | return mEventHub->mapKey(mId, scanCode, usageCode, metaState, outKeycode, outMetaState, |
| 190 | outFlags); |
| 191 | } |
| 192 | inline status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const { |
| 193 | return mEventHub->mapAxis(mId, scanCode, outAxisInfo); |
| 194 | } |
| 195 | inline std::vector<TouchVideoFrame> getVideoFrames() { return mEventHub->getVideoFrames(mId); } |
| 196 | inline int32_t getScanCodeState(int32_t scanCode) const { |
| 197 | return mEventHub->getScanCodeState(mId, scanCode); |
| 198 | } |
| 199 | inline int32_t getKeyCodeState(int32_t keyCode) const { |
| 200 | return mEventHub->getKeyCodeState(mId, keyCode); |
| 201 | } |
| 202 | inline int32_t getSwitchState(int32_t sw) const { return mEventHub->getSwitchState(mId, sw); } |
| 203 | inline status_t getAbsoluteAxisValue(int32_t code, int32_t* outValue) const { |
| 204 | return mEventHub->getAbsoluteAxisValue(mId, code, outValue); |
| 205 | } |
| 206 | inline bool markSupportedKeyCodes(size_t numCodes, const int32_t* keyCodes, |
| 207 | uint8_t* outFlags) const { |
| 208 | return mEventHub->markSupportedKeyCodes(mId, numCodes, keyCodes, outFlags); |
| 209 | } |
| 210 | inline bool hasScanCode(int32_t scanCode) const { |
| 211 | return mEventHub->hasScanCode(mId, scanCode); |
| 212 | } |
| 213 | inline bool hasLed(int32_t led) const { return mEventHub->hasLed(mId, led); } |
| 214 | inline void setLedState(int32_t led, bool on) { return mEventHub->setLedState(mId, led, on); } |
| 215 | inline void getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition>& outVirtualKeys) const { |
| 216 | return mEventHub->getVirtualKeyDefinitions(mId, outVirtualKeys); |
| 217 | } |
| 218 | inline sp<KeyCharacterMap> getKeyCharacterMap() const { |
| 219 | return mEventHub->getKeyCharacterMap(mId); |
| 220 | } |
| 221 | inline bool setKeyboardLayoutOverlay(const sp<KeyCharacterMap>& map) { |
| 222 | return mEventHub->setKeyboardLayoutOverlay(mId, map); |
| 223 | } |
| 224 | inline void vibrate(nsecs_t duration) { return mEventHub->vibrate(mId, duration); } |
| 225 | inline void cancelVibrate() { return mEventHub->cancelVibrate(mId); } |
| 226 | |
| 227 | inline bool hasAbsoluteAxis(int32_t code) const { |
| 228 | RawAbsoluteAxisInfo info; |
| 229 | mEventHub->getAbsoluteAxisInfo(mId, code, &info); |
| 230 | return info.valid; |
| 231 | } |
| 232 | inline bool isKeyPressed(int32_t code) const { |
| 233 | return mEventHub->getScanCodeState(mId, code) == AKEY_STATE_DOWN; |
| 234 | } |
| 235 | inline int32_t getAbsoluteAxisValue(int32_t code) const { |
| 236 | int32_t value; |
| 237 | mEventHub->getAbsoluteAxisValue(mId, code, &value); |
| 238 | return value; |
| 239 | } |
| 240 | inline bool isDeviceEnabled() { return mEventHub->isDeviceEnabled(mId); } |
| 241 | inline status_t enableDevice() { return mEventHub->enableDevice(mId); } |
| 242 | inline status_t disableDevice() { return mEventHub->disableDevice(mId); } |
| 243 | |
| 244 | inline const std::string getName() { return mDevice.getName(); } |
| 245 | inline const std::string getDescriptor() { return mDevice.getDescriptor(); } |
| 246 | inline bool isExternal() { return mDevice.isExternal(); } |
| 247 | inline std::optional<uint8_t> getAssociatedDisplayPort() const { |
| 248 | return mDevice.getAssociatedDisplayPort(); |
| 249 | } |
| 250 | inline std::optional<DisplayViewport> getAssociatedViewport() const { |
| 251 | return mDevice.getAssociatedViewport(); |
| 252 | } |
| 253 | inline void cancelTouch(nsecs_t when) { mDevice.cancelTouch(when); } |
| 254 | inline void bumpGeneration() { mDevice.bumpGeneration(); } |
| 255 | inline const PropertyMap& getConfiguration() { return mDevice.getConfiguration(); } |
| 256 | |
| 257 | private: |
| 258 | InputDevice& mDevice; |
| 259 | InputReaderContext* mContext; |
| 260 | EventHubInterface* mEventHub; |
| 261 | int32_t mId; |
| 262 | }; |
| 263 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 264 | } // namespace android |
| 265 | |
| 266 | #endif //_UI_INPUTREADER_INPUT_DEVICE_H |