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 | |
| 34 | class InputMapper; |
| 35 | |
| 36 | /* Represents the state of a single input device. */ |
| 37 | class InputDevice { |
| 38 | public: |
| 39 | InputDevice(InputReaderContext* context, int32_t id, int32_t generation, |
| 40 | int32_t controllerNumber, const InputDeviceIdentifier& identifier, |
| 41 | uint32_t classes); |
| 42 | ~InputDevice(); |
| 43 | |
| 44 | inline InputReaderContext* getContext() { return mContext; } |
| 45 | inline int32_t getId() const { return mId; } |
| 46 | inline int32_t getControllerNumber() const { return mControllerNumber; } |
| 47 | inline int32_t getGeneration() const { return mGeneration; } |
| 48 | inline const std::string getName() const { return mIdentifier.name; } |
| 49 | inline const std::string getDescriptor() { return mIdentifier.descriptor; } |
| 50 | inline uint32_t getClasses() const { return mClasses; } |
| 51 | inline uint32_t getSources() const { return mSources; } |
| 52 | |
| 53 | inline bool isExternal() { return mIsExternal; } |
| 54 | inline void setExternal(bool external) { mIsExternal = external; } |
| 55 | inline std::optional<uint8_t> getAssociatedDisplayPort() const { |
| 56 | return mAssociatedDisplayPort; |
| 57 | } |
| 58 | inline std::optional<DisplayViewport> getAssociatedViewport() const { |
| 59 | return mAssociatedViewport; |
| 60 | } |
| 61 | inline void setMic(bool hasMic) { mHasMic = hasMic; } |
| 62 | inline bool hasMic() const { return mHasMic; } |
| 63 | |
| 64 | inline bool isIgnored() { return mMappers.empty(); } |
| 65 | |
| 66 | bool isEnabled(); |
| 67 | void setEnabled(bool enabled, nsecs_t when); |
| 68 | |
| 69 | void dump(std::string& dump); |
| 70 | void addMapper(InputMapper* mapper); |
| 71 | void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes); |
| 72 | void reset(nsecs_t when); |
| 73 | void process(const RawEvent* rawEvents, size_t count); |
| 74 | void timeoutExpired(nsecs_t when); |
| 75 | void updateExternalStylusState(const StylusState& state); |
| 76 | |
| 77 | void getDeviceInfo(InputDeviceInfo* outDeviceInfo); |
| 78 | int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); |
| 79 | int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); |
| 80 | int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); |
| 81 | bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, const int32_t* keyCodes, |
| 82 | uint8_t* outFlags); |
| 83 | void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token); |
| 84 | void cancelVibrate(int32_t token); |
| 85 | void cancelTouch(nsecs_t when); |
| 86 | |
| 87 | int32_t getMetaState(); |
| 88 | void updateMetaState(int32_t keyCode); |
| 89 | |
| 90 | void fadePointer(); |
| 91 | |
| 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 | |
| 99 | bool hasKey(int32_t code) { return getEventHub()->hasScanCode(mId, code); } |
| 100 | |
| 101 | bool hasAbsoluteAxis(int32_t code) { |
| 102 | RawAbsoluteAxisInfo info; |
| 103 | getEventHub()->getAbsoluteAxisInfo(mId, code, &info); |
| 104 | return info.valid; |
| 105 | } |
| 106 | |
| 107 | bool isKeyPressed(int32_t code) { |
| 108 | return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN; |
| 109 | } |
| 110 | |
| 111 | int32_t getAbsoluteAxisValue(int32_t code) { |
| 112 | int32_t value; |
| 113 | getEventHub()->getAbsoluteAxisValue(mId, code, &value); |
| 114 | return value; |
| 115 | } |
| 116 | |
| 117 | std::optional<int32_t> getAssociatedDisplayId(); |
| 118 | |
| 119 | private: |
| 120 | InputReaderContext* mContext; |
| 121 | int32_t mId; |
| 122 | int32_t mGeneration; |
| 123 | int32_t mControllerNumber; |
| 124 | InputDeviceIdentifier mIdentifier; |
| 125 | std::string mAlias; |
| 126 | uint32_t mClasses; |
| 127 | |
| 128 | std::vector<InputMapper*> mMappers; |
| 129 | |
| 130 | uint32_t mSources; |
| 131 | bool mIsExternal; |
| 132 | std::optional<uint8_t> mAssociatedDisplayPort; |
| 133 | std::optional<DisplayViewport> mAssociatedViewport; |
| 134 | bool mHasMic; |
| 135 | bool mDropUntilNextSync; |
| 136 | |
| 137 | typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code); |
| 138 | int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc); |
| 139 | |
| 140 | PropertyMap mConfiguration; |
| 141 | }; |
| 142 | |
| 143 | } // namespace android |
| 144 | |
| 145 | #endif //_UI_INPUTREADER_INPUT_DEVICE_H |