blob: aaa0d268b342a730cab94c103b7e3b246dcdd873 [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);
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 Pradhanbaa5c822019-08-30 15:27:05 -0700100 std::optional<int32_t> getAssociatedDisplayId();
101
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800102 size_t getMapperCount();
103
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800104 // construct and add a mapper to the input device
105 template <class T, typename... Args>
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800106 T& addMapper(int32_t eventHubId, Args... args) {
107 // ensure a device entry exists for this eventHubId
108 addEventHubDevice(eventHubId, false);
109
110 // create mapper
111 auto& devicePair = mDevices[eventHubId];
112 auto& deviceContext = devicePair.first;
113 auto& mappers = devicePair.second;
114 T* mapper = new T(*deviceContext, args...);
115 mappers.emplace_back(mapper);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800116 return *mapper;
117 }
118
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700119private:
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
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800128 // map from eventHubId to device context and mappers
129 using MapperVector = std::vector<std::unique_ptr<InputMapper>>;
130 using DevicePair = std::pair<std::unique_ptr<InputDeviceContext>, MapperVector>;
131 std::unordered_map<int32_t, DevicePair> mDevices;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700132
133 uint32_t mSources;
134 bool mIsExternal;
135 std::optional<uint8_t> mAssociatedDisplayPort;
136 std::optional<DisplayViewport> mAssociatedViewport;
137 bool mHasMic;
138 bool mDropUntilNextSync;
139
140 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
141 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
142
143 PropertyMap mConfiguration;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800144
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800145 // helpers to interate over the devices collection
146 // run a function against every mapper on every subdevice
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800147 inline void for_each_mapper(std::function<void(InputMapper&)> f) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800148 for (auto& deviceEntry : mDevices) {
149 auto& devicePair = deviceEntry.second;
150 auto& mappers = devicePair.second;
151 for (auto& mapperPtr : mappers) {
152 f(*mapperPtr);
153 }
154 }
155 }
156
157 // run a function against every mapper on a specific subdevice
158 inline void for_each_mapper_in_subdevice(int32_t eventHubDevice,
159 std::function<void(InputMapper&)> f) {
160 auto deviceIt = mDevices.find(eventHubDevice);
161 if (deviceIt != mDevices.end()) {
162 auto& devicePair = deviceIt->second;
163 auto& mappers = devicePair.second;
164 for (auto& mapperPtr : mappers) {
165 f(*mapperPtr);
166 }
167 }
168 }
169
170 // run a function against every subdevice
171 inline void for_each_subdevice(std::function<void(InputDeviceContext&)> f) {
172 for (auto& deviceEntry : mDevices) {
173 auto& devicePair = deviceEntry.second;
174 auto& contextPtr = devicePair.first;
175 f(*contextPtr);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800176 }
177 }
178
179 // return the first value returned by a function over every mapper.
180 // if all mappers return nullopt, return nullopt.
181 template <typename T>
182 inline std::optional<T> first_in_mappers(std::function<std::optional<T>(InputMapper&)> f) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800183 for (auto& deviceEntry : mDevices) {
184 auto& devicePair = deviceEntry.second;
185 auto& mappers = devicePair.second;
186 for (auto& mapperPtr : mappers) {
187 std::optional<T> ret = f(*mapperPtr);
188 if (ret) {
189 return ret;
190 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800191 }
192 }
193 return std::nullopt;
194 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700195};
196
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800197/* Provides access to EventHub methods, but limits access to the current InputDevice.
198 * Essentially an implementation of EventHubInterface, but for a specific device id.
199 * Helps hide implementation details of InputDevice and EventHub. Used by mappers to
200 * check the status of the associated hardware device
201 */
202class InputDeviceContext {
203public:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800204 InputDeviceContext(InputDevice& device, int32_t eventHubId);
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800205 ~InputDeviceContext();
206
207 inline InputReaderContext* getContext() { return mContext; }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800208 inline int32_t getId() { return mDeviceId; }
209 inline int32_t getEventHubId() { return mId; }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800210
211 inline uint32_t getDeviceClasses() const { return mEventHub->getDeviceClasses(mId); }
212 inline InputDeviceIdentifier getDeviceIdentifier() const {
213 return mEventHub->getDeviceIdentifier(mId);
214 }
215 inline int32_t getDeviceControllerNumber() const {
216 return mEventHub->getDeviceControllerNumber(mId);
217 }
218 inline void getConfiguration(PropertyMap* outConfiguration) const {
219 return mEventHub->getConfiguration(mId, outConfiguration);
220 }
221 inline status_t getAbsoluteAxisInfo(int32_t code, RawAbsoluteAxisInfo* axisInfo) const {
222 return mEventHub->getAbsoluteAxisInfo(mId, code, axisInfo);
223 }
224 inline bool hasRelativeAxis(int32_t code) const {
225 return mEventHub->hasRelativeAxis(mId, code);
226 }
227 inline bool hasInputProperty(int property) const {
228 return mEventHub->hasInputProperty(mId, property);
229 }
230 inline status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t metaState,
231 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const {
232 return mEventHub->mapKey(mId, scanCode, usageCode, metaState, outKeycode, outMetaState,
233 outFlags);
234 }
235 inline status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
236 return mEventHub->mapAxis(mId, scanCode, outAxisInfo);
237 }
238 inline std::vector<TouchVideoFrame> getVideoFrames() { return mEventHub->getVideoFrames(mId); }
239 inline int32_t getScanCodeState(int32_t scanCode) const {
240 return mEventHub->getScanCodeState(mId, scanCode);
241 }
242 inline int32_t getKeyCodeState(int32_t keyCode) const {
243 return mEventHub->getKeyCodeState(mId, keyCode);
244 }
245 inline int32_t getSwitchState(int32_t sw) const { return mEventHub->getSwitchState(mId, sw); }
246 inline status_t getAbsoluteAxisValue(int32_t code, int32_t* outValue) const {
247 return mEventHub->getAbsoluteAxisValue(mId, code, outValue);
248 }
249 inline bool markSupportedKeyCodes(size_t numCodes, const int32_t* keyCodes,
250 uint8_t* outFlags) const {
251 return mEventHub->markSupportedKeyCodes(mId, numCodes, keyCodes, outFlags);
252 }
253 inline bool hasScanCode(int32_t scanCode) const {
254 return mEventHub->hasScanCode(mId, scanCode);
255 }
256 inline bool hasLed(int32_t led) const { return mEventHub->hasLed(mId, led); }
257 inline void setLedState(int32_t led, bool on) { return mEventHub->setLedState(mId, led, on); }
258 inline void getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
259 return mEventHub->getVirtualKeyDefinitions(mId, outVirtualKeys);
260 }
261 inline sp<KeyCharacterMap> getKeyCharacterMap() const {
262 return mEventHub->getKeyCharacterMap(mId);
263 }
264 inline bool setKeyboardLayoutOverlay(const sp<KeyCharacterMap>& map) {
265 return mEventHub->setKeyboardLayoutOverlay(mId, map);
266 }
267 inline void vibrate(nsecs_t duration) { return mEventHub->vibrate(mId, duration); }
268 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