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