blob: 6cb86bd258cdc7051dde11b02fb7387307d6025b [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
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. Lewisa7b82e12020-02-12 15:40:45 -080020#include <input/DisplayViewport.h>
21#include <input/InputDevice.h>
22#include <stdint.h>
23#include <utils/PropertyMap.h>
24
25#include <optional>
26#include <unordered_map>
27#include <vector>
28
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070029#include "EventHub.h"
30#include "InputReaderBase.h"
31#include "InputReaderContext.h"
32
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070033namespace android {
34
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080035class InputDeviceContext;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070036class InputMapper;
37
38/* Represents the state of a single input device. */
39class InputDevice {
40public:
41 InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080042 const InputDeviceIdentifier& identifier);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070043 ~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; }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080053 inline bool hasEventHubDevices() const { return !mDevices.empty(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070054
55 inline bool isExternal() { return mIsExternal; }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070056 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
57 return mAssociatedDisplayPort;
58 }
59 inline std::optional<DisplayViewport> getAssociatedViewport() const {
60 return mAssociatedViewport;
61 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070062 inline bool hasMic() const { return mHasMic; }
63
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080064 inline bool isIgnored() { return !getMapperCount(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070065
66 bool isEnabled();
67 void setEnabled(bool enabled, nsecs_t when);
68
69 void dump(std::string& dump);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080070 void addEventHubDevice(int32_t eventHubId, bool populateMappers = true);
71 void removeEventHubDevice(int32_t eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070072 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);
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000084 void vibrate(const std::vector<VibrationElement>& pattern, ssize_t repeat, int32_t token);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070085 void cancelVibrate(int32_t token);
86 void cancelTouch(nsecs_t when);
87
88 int32_t getMetaState();
89 void updateMetaState(int32_t keyCode);
90
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070091 void bumpGeneration();
92
93 void notifyReset(nsecs_t when);
94
95 inline const PropertyMap& getConfiguration() { return mConfiguration; }
96 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
97
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098 std::optional<int32_t> getAssociatedDisplayId();
99
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800100 size_t getMapperCount();
101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800102 // construct and add a mapper to the input device
103 template <class T, typename... Args>
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800104 T& addMapper(int32_t eventHubId, Args... args) {
105 // ensure a device entry exists for this eventHubId
106 addEventHubDevice(eventHubId, false);
107
108 // create mapper
109 auto& devicePair = mDevices[eventHubId];
110 auto& deviceContext = devicePair.first;
111 auto& mappers = devicePair.second;
112 T* mapper = new T(*deviceContext, args...);
113 mappers.emplace_back(mapper);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800114 return *mapper;
115 }
116
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700117private:
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
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800126 // map from eventHubId to device context and mappers
127 using MapperVector = std::vector<std::unique_ptr<InputMapper>>;
128 using DevicePair = std::pair<std::unique_ptr<InputDeviceContext>, MapperVector>;
129 std::unordered_map<int32_t, DevicePair> mDevices;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700130
131 uint32_t mSources;
132 bool mIsExternal;
133 std::optional<uint8_t> mAssociatedDisplayPort;
134 std::optional<DisplayViewport> mAssociatedViewport;
135 bool mHasMic;
136 bool mDropUntilNextSync;
137
138 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
139 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
140
141 PropertyMap mConfiguration;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800142
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800143 // helpers to interate over the devices collection
144 // run a function against every mapper on every subdevice
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800145 inline void for_each_mapper(std::function<void(InputMapper&)> f) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800146 for (auto& deviceEntry : mDevices) {
147 auto& devicePair = deviceEntry.second;
148 auto& mappers = devicePair.second;
149 for (auto& mapperPtr : mappers) {
150 f(*mapperPtr);
151 }
152 }
153 }
154
155 // run a function against every mapper on a specific subdevice
156 inline void for_each_mapper_in_subdevice(int32_t eventHubDevice,
157 std::function<void(InputMapper&)> f) {
158 auto deviceIt = mDevices.find(eventHubDevice);
159 if (deviceIt != mDevices.end()) {
160 auto& devicePair = deviceIt->second;
161 auto& mappers = devicePair.second;
162 for (auto& mapperPtr : mappers) {
163 f(*mapperPtr);
164 }
165 }
166 }
167
168 // run a function against every subdevice
169 inline void for_each_subdevice(std::function<void(InputDeviceContext&)> f) {
170 for (auto& deviceEntry : mDevices) {
171 auto& devicePair = deviceEntry.second;
172 auto& contextPtr = devicePair.first;
173 f(*contextPtr);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800174 }
175 }
176
177 // return the first value returned by a function over every mapper.
178 // if all mappers return nullopt, return nullopt.
179 template <typename T>
180 inline std::optional<T> first_in_mappers(std::function<std::optional<T>(InputMapper&)> f) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800181 for (auto& deviceEntry : mDevices) {
182 auto& devicePair = deviceEntry.second;
183 auto& mappers = devicePair.second;
184 for (auto& mapperPtr : mappers) {
185 std::optional<T> ret = f(*mapperPtr);
186 if (ret) {
187 return ret;
188 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800189 }
190 }
191 return std::nullopt;
192 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700193};
194
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800195/* Provides access to EventHub methods, but limits access to the current InputDevice.
196 * Essentially an implementation of EventHubInterface, but for a specific device id.
197 * Helps hide implementation details of InputDevice and EventHub. Used by mappers to
198 * check the status of the associated hardware device
199 */
200class InputDeviceContext {
201public:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800202 InputDeviceContext(InputDevice& device, int32_t eventHubId);
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800203 ~InputDeviceContext();
204
205 inline InputReaderContext* getContext() { return mContext; }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800206 inline int32_t getId() { return mDeviceId; }
207 inline int32_t getEventHubId() { return mId; }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800208
209 inline uint32_t getDeviceClasses() const { return mEventHub->getDeviceClasses(mId); }
210 inline InputDeviceIdentifier getDeviceIdentifier() const {
211 return mEventHub->getDeviceIdentifier(mId);
212 }
213 inline int32_t getDeviceControllerNumber() const {
214 return mEventHub->getDeviceControllerNumber(mId);
215 }
216 inline void getConfiguration(PropertyMap* outConfiguration) const {
217 return mEventHub->getConfiguration(mId, outConfiguration);
218 }
219 inline status_t getAbsoluteAxisInfo(int32_t code, RawAbsoluteAxisInfo* axisInfo) const {
220 return mEventHub->getAbsoluteAxisInfo(mId, code, axisInfo);
221 }
222 inline bool hasRelativeAxis(int32_t code) const {
223 return mEventHub->hasRelativeAxis(mId, code);
224 }
225 inline bool hasInputProperty(int property) const {
226 return mEventHub->hasInputProperty(mId, property);
227 }
228 inline status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t metaState,
229 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const {
230 return mEventHub->mapKey(mId, scanCode, usageCode, metaState, outKeycode, outMetaState,
231 outFlags);
232 }
233 inline status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
234 return mEventHub->mapAxis(mId, scanCode, outAxisInfo);
235 }
236 inline std::vector<TouchVideoFrame> getVideoFrames() { return mEventHub->getVideoFrames(mId); }
237 inline int32_t getScanCodeState(int32_t scanCode) const {
238 return mEventHub->getScanCodeState(mId, scanCode);
239 }
240 inline int32_t getKeyCodeState(int32_t keyCode) const {
241 return mEventHub->getKeyCodeState(mId, keyCode);
242 }
243 inline int32_t getSwitchState(int32_t sw) const { return mEventHub->getSwitchState(mId, sw); }
244 inline status_t getAbsoluteAxisValue(int32_t code, int32_t* outValue) const {
245 return mEventHub->getAbsoluteAxisValue(mId, code, outValue);
246 }
247 inline bool markSupportedKeyCodes(size_t numCodes, const int32_t* keyCodes,
248 uint8_t* outFlags) const {
249 return mEventHub->markSupportedKeyCodes(mId, numCodes, keyCodes, outFlags);
250 }
251 inline bool hasScanCode(int32_t scanCode) const {
252 return mEventHub->hasScanCode(mId, scanCode);
253 }
254 inline bool hasLed(int32_t led) const { return mEventHub->hasLed(mId, led); }
255 inline void setLedState(int32_t led, bool on) { return mEventHub->setLedState(mId, led, on); }
256 inline void getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
257 return mEventHub->getVirtualKeyDefinitions(mId, outVirtualKeys);
258 }
259 inline sp<KeyCharacterMap> getKeyCharacterMap() const {
260 return mEventHub->getKeyCharacterMap(mId);
261 }
262 inline bool setKeyboardLayoutOverlay(const sp<KeyCharacterMap>& map) {
263 return mEventHub->setKeyboardLayoutOverlay(mId, map);
264 }
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000265 inline void vibrate(const VibrationElement& element) {
266 return mEventHub->vibrate(mId, element);
267 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800268 inline void cancelVibrate() { return mEventHub->cancelVibrate(mId); }
269
270 inline bool hasAbsoluteAxis(int32_t code) const {
271 RawAbsoluteAxisInfo info;
272 mEventHub->getAbsoluteAxisInfo(mId, code, &info);
273 return info.valid;
274 }
275 inline bool isKeyPressed(int32_t code) const {
276 return mEventHub->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
277 }
278 inline int32_t getAbsoluteAxisValue(int32_t code) const {
279 int32_t value;
280 mEventHub->getAbsoluteAxisValue(mId, code, &value);
281 return value;
282 }
283 inline bool isDeviceEnabled() { return mEventHub->isDeviceEnabled(mId); }
284 inline status_t enableDevice() { return mEventHub->enableDevice(mId); }
285 inline status_t disableDevice() { return mEventHub->disableDevice(mId); }
286
287 inline const std::string getName() { return mDevice.getName(); }
288 inline const std::string getDescriptor() { return mDevice.getDescriptor(); }
289 inline bool isExternal() { return mDevice.isExternal(); }
290 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
291 return mDevice.getAssociatedDisplayPort();
292 }
293 inline std::optional<DisplayViewport> getAssociatedViewport() const {
294 return mDevice.getAssociatedViewport();
295 }
296 inline void cancelTouch(nsecs_t when) { mDevice.cancelTouch(when); }
297 inline void bumpGeneration() { mDevice.bumpGeneration(); }
298 inline const PropertyMap& getConfiguration() { return mDevice.getConfiguration(); }
299
300private:
301 InputDevice& mDevice;
302 InputReaderContext* mContext;
303 EventHubInterface* mEventHub;
304 int32_t mId;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800305 int32_t mDeviceId;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800306};
307
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700308} // namespace android
309
310#endif //_UI_INPUTREADER_INPUT_DEVICE_H