blob: 8b14b062cdd0b9977ce4d28824e4f526e04a7c9f [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>
Chris Ye1b0c7342020-07-28 21:57:03 -070021#include <input/Flags.h>
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080022#include <input/InputDevice.h>
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070023#include <input/PropertyMap.h>
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080024#include <stdint.h>
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080025
26#include <optional>
27#include <unordered_map>
28#include <vector>
29
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070030#include "EventHub.h"
31#include "InputReaderBase.h"
32#include "InputReaderContext.h"
33
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070034namespace android {
35
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080036class InputDeviceContext;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070037class InputMapper;
38
39/* Represents the state of a single input device. */
40class InputDevice {
41public:
42 InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080043 const InputDeviceIdentifier& identifier);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070044 ~InputDevice();
45
46 inline InputReaderContext* getContext() { return mContext; }
47 inline int32_t getId() const { return mId; }
48 inline int32_t getControllerNumber() const { return mControllerNumber; }
49 inline int32_t getGeneration() const { return mGeneration; }
50 inline const std::string getName() const { return mIdentifier.name; }
51 inline const std::string getDescriptor() { return mIdentifier.descriptor; }
Chris Ye1b0c7342020-07-28 21:57:03 -070052 inline Flags<InputDeviceClass> getClasses() const { return mClasses; }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070053 inline uint32_t getSources() const { return mSources; }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080054 inline bool hasEventHubDevices() const { return !mDevices.empty(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070055
56 inline bool isExternal() { return mIsExternal; }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070057 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
58 return mAssociatedDisplayPort;
59 }
60 inline std::optional<DisplayViewport> getAssociatedViewport() const {
61 return mAssociatedViewport;
62 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070063 inline bool hasMic() const { return mHasMic; }
64
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080065 inline bool isIgnored() { return !getMapperCount(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070066
67 bool isEnabled();
68 void setEnabled(bool enabled, nsecs_t when);
69
Chris Yee7310032020-09-22 15:36:28 -070070 void dump(std::string& dump, const std::string& eventHubDevStr);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080071 void addEventHubDevice(int32_t eventHubId, bool populateMappers = true);
72 void removeEventHubDevice(int32_t eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070073 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
74 void reset(nsecs_t when);
75 void process(const RawEvent* rawEvents, size_t count);
76 void timeoutExpired(nsecs_t when);
77 void updateExternalStylusState(const StylusState& state);
78
79 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
80 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
81 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
82 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
83 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, const int32_t* keyCodes,
84 uint8_t* outFlags);
Chris Ye87143712020-11-10 05:05:58 +000085 void vibrate(const VibrationSequence& sequence, ssize_t repeat, int32_t token);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070086 void cancelVibrate(int32_t token);
Chris Ye87143712020-11-10 05:05:58 +000087 bool isVibrating();
88 std::vector<int32_t> getVibratorIds();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089 void cancelTouch(nsecs_t when);
90
91 int32_t getMetaState();
92 void updateMetaState(int32_t keyCode);
93
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094 void bumpGeneration();
95
96 void notifyReset(nsecs_t when);
97
98 inline const PropertyMap& getConfiguration() { return mConfiguration; }
99 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
100
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700101 std::optional<int32_t> getAssociatedDisplayId();
102
arthurhungc903df12020-08-11 15:08:42 +0800103 void updateLedState(bool reset);
104
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800105 size_t getMapperCount();
106
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800107 // construct and add a mapper to the input device
108 template <class T, typename... Args>
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800109 T& addMapper(int32_t eventHubId, Args... args) {
110 // ensure a device entry exists for this eventHubId
111 addEventHubDevice(eventHubId, false);
112
113 // create mapper
114 auto& devicePair = mDevices[eventHubId];
115 auto& deviceContext = devicePair.first;
116 auto& mappers = devicePair.second;
117 T* mapper = new T(*deviceContext, args...);
118 mappers.emplace_back(mapper);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800119 return *mapper;
120 }
121
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700122private:
123 InputReaderContext* mContext;
124 int32_t mId;
125 int32_t mGeneration;
126 int32_t mControllerNumber;
127 InputDeviceIdentifier mIdentifier;
128 std::string mAlias;
Chris Ye1b0c7342020-07-28 21:57:03 -0700129 Flags<InputDeviceClass> mClasses;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700130
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800131 // map from eventHubId to device context and mappers
132 using MapperVector = std::vector<std::unique_ptr<InputMapper>>;
133 using DevicePair = std::pair<std::unique_ptr<InputDeviceContext>, MapperVector>;
134 std::unordered_map<int32_t, DevicePair> mDevices;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700135
136 uint32_t mSources;
137 bool mIsExternal;
138 std::optional<uint8_t> mAssociatedDisplayPort;
139 std::optional<DisplayViewport> mAssociatedViewport;
140 bool mHasMic;
141 bool mDropUntilNextSync;
142
143 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
144 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
145
146 PropertyMap mConfiguration;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800147
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800148 // helpers to interate over the devices collection
149 // run a function against every mapper on every subdevice
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800150 inline void for_each_mapper(std::function<void(InputMapper&)> f) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800151 for (auto& deviceEntry : mDevices) {
152 auto& devicePair = deviceEntry.second;
153 auto& mappers = devicePair.second;
154 for (auto& mapperPtr : mappers) {
155 f(*mapperPtr);
156 }
157 }
158 }
159
160 // run a function against every mapper on a specific subdevice
161 inline void for_each_mapper_in_subdevice(int32_t eventHubDevice,
162 std::function<void(InputMapper&)> f) {
163 auto deviceIt = mDevices.find(eventHubDevice);
164 if (deviceIt != mDevices.end()) {
165 auto& devicePair = deviceIt->second;
166 auto& mappers = devicePair.second;
167 for (auto& mapperPtr : mappers) {
168 f(*mapperPtr);
169 }
170 }
171 }
172
173 // run a function against every subdevice
174 inline void for_each_subdevice(std::function<void(InputDeviceContext&)> f) {
175 for (auto& deviceEntry : mDevices) {
176 auto& devicePair = deviceEntry.second;
177 auto& contextPtr = devicePair.first;
178 f(*contextPtr);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800179 }
180 }
181
182 // return the first value returned by a function over every mapper.
183 // if all mappers return nullopt, return nullopt.
184 template <typename T>
185 inline std::optional<T> first_in_mappers(std::function<std::optional<T>(InputMapper&)> f) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800186 for (auto& deviceEntry : mDevices) {
187 auto& devicePair = deviceEntry.second;
188 auto& mappers = devicePair.second;
189 for (auto& mapperPtr : mappers) {
190 std::optional<T> ret = f(*mapperPtr);
191 if (ret) {
192 return ret;
193 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800194 }
195 }
196 return std::nullopt;
197 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700198};
199
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800200/* Provides access to EventHub methods, but limits access to the current InputDevice.
201 * Essentially an implementation of EventHubInterface, but for a specific device id.
202 * Helps hide implementation details of InputDevice and EventHub. Used by mappers to
203 * check the status of the associated hardware device
204 */
205class InputDeviceContext {
206public:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800207 InputDeviceContext(InputDevice& device, int32_t eventHubId);
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800208 ~InputDeviceContext();
209
210 inline InputReaderContext* getContext() { return mContext; }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800211 inline int32_t getId() { return mDeviceId; }
212 inline int32_t getEventHubId() { return mId; }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800213
Chris Ye1b0c7342020-07-28 21:57:03 -0700214 inline Flags<InputDeviceClass> getDeviceClasses() const {
215 return mEventHub->getDeviceClasses(mId);
216 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800217 inline InputDeviceIdentifier getDeviceIdentifier() const {
218 return mEventHub->getDeviceIdentifier(mId);
219 }
220 inline int32_t getDeviceControllerNumber() const {
221 return mEventHub->getDeviceControllerNumber(mId);
222 }
223 inline void getConfiguration(PropertyMap* outConfiguration) const {
224 return mEventHub->getConfiguration(mId, outConfiguration);
225 }
226 inline status_t getAbsoluteAxisInfo(int32_t code, RawAbsoluteAxisInfo* axisInfo) const {
227 return mEventHub->getAbsoluteAxisInfo(mId, code, axisInfo);
228 }
229 inline bool hasRelativeAxis(int32_t code) const {
230 return mEventHub->hasRelativeAxis(mId, code);
231 }
232 inline bool hasInputProperty(int property) const {
233 return mEventHub->hasInputProperty(mId, property);
234 }
235 inline status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t metaState,
236 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const {
237 return mEventHub->mapKey(mId, scanCode, usageCode, metaState, outKeycode, outMetaState,
238 outFlags);
239 }
240 inline status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
241 return mEventHub->mapAxis(mId, scanCode, outAxisInfo);
242 }
243 inline std::vector<TouchVideoFrame> getVideoFrames() { return mEventHub->getVideoFrames(mId); }
244 inline int32_t getScanCodeState(int32_t scanCode) const {
245 return mEventHub->getScanCodeState(mId, scanCode);
246 }
247 inline int32_t getKeyCodeState(int32_t keyCode) const {
248 return mEventHub->getKeyCodeState(mId, keyCode);
249 }
250 inline int32_t getSwitchState(int32_t sw) const { return mEventHub->getSwitchState(mId, sw); }
251 inline status_t getAbsoluteAxisValue(int32_t code, int32_t* outValue) const {
252 return mEventHub->getAbsoluteAxisValue(mId, code, outValue);
253 }
254 inline bool markSupportedKeyCodes(size_t numCodes, const int32_t* keyCodes,
255 uint8_t* outFlags) const {
256 return mEventHub->markSupportedKeyCodes(mId, numCodes, keyCodes, outFlags);
257 }
258 inline bool hasScanCode(int32_t scanCode) const {
259 return mEventHub->hasScanCode(mId, scanCode);
260 }
261 inline bool hasLed(int32_t led) const { return mEventHub->hasLed(mId, led); }
262 inline void setLedState(int32_t led, bool on) { return mEventHub->setLedState(mId, led, on); }
263 inline void getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
264 return mEventHub->getVirtualKeyDefinitions(mId, outVirtualKeys);
265 }
Chris Ye3a1e4462020-08-12 10:13:15 -0700266 inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800267 return mEventHub->getKeyCharacterMap(mId);
268 }
Chris Ye3a1e4462020-08-12 10:13:15 -0700269 inline bool setKeyboardLayoutOverlay(std::shared_ptr<KeyCharacterMap> map) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800270 return mEventHub->setKeyboardLayoutOverlay(mId, map);
271 }
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000272 inline void vibrate(const VibrationElement& element) {
273 return mEventHub->vibrate(mId, element);
274 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800275 inline void cancelVibrate() { return mEventHub->cancelVibrate(mId); }
276
Chris Ye87143712020-11-10 05:05:58 +0000277 inline std::vector<int32_t> getVibratorIds() { return mEventHub->getVibratorIds(mId); }
278
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800279 inline bool hasAbsoluteAxis(int32_t code) const {
280 RawAbsoluteAxisInfo info;
281 mEventHub->getAbsoluteAxisInfo(mId, code, &info);
282 return info.valid;
283 }
284 inline bool isKeyPressed(int32_t code) const {
285 return mEventHub->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
286 }
287 inline int32_t getAbsoluteAxisValue(int32_t code) const {
288 int32_t value;
289 mEventHub->getAbsoluteAxisValue(mId, code, &value);
290 return value;
291 }
292 inline bool isDeviceEnabled() { return mEventHub->isDeviceEnabled(mId); }
293 inline status_t enableDevice() { return mEventHub->enableDevice(mId); }
294 inline status_t disableDevice() { return mEventHub->disableDevice(mId); }
295
296 inline const std::string getName() { return mDevice.getName(); }
297 inline const std::string getDescriptor() { return mDevice.getDescriptor(); }
298 inline bool isExternal() { return mDevice.isExternal(); }
299 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
300 return mDevice.getAssociatedDisplayPort();
301 }
302 inline std::optional<DisplayViewport> getAssociatedViewport() const {
303 return mDevice.getAssociatedViewport();
304 }
305 inline void cancelTouch(nsecs_t when) { mDevice.cancelTouch(when); }
306 inline void bumpGeneration() { mDevice.bumpGeneration(); }
307 inline const PropertyMap& getConfiguration() { return mDevice.getConfiguration(); }
308
309private:
310 InputDevice& mDevice;
311 InputReaderContext* mContext;
312 EventHubInterface* mEventHub;
313 int32_t mId;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800314 int32_t mDeviceId;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800315};
316
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700317} // namespace android
318
319#endif //_UI_INPUTREADER_INPUT_DEVICE_H