blob: da36a48118a4a932776981170a44ba48107d6405 [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>
23#include <stdint.h>
24#include <utils/PropertyMap.h>
25
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
70 void dump(std::string& dump);
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);
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000085 void vibrate(const std::vector<VibrationElement>& pattern, ssize_t repeat, int32_t token);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070086 void cancelVibrate(int32_t token);
87 void cancelTouch(nsecs_t when);
88
89 int32_t getMetaState();
90 void updateMetaState(int32_t keyCode);
91
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070092 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
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070099 std::optional<int32_t> getAssociatedDisplayId();
100
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800101 size_t getMapperCount();
102
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800103 // construct and add a mapper to the input device
104 template <class T, typename... Args>
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800105 T& addMapper(int32_t eventHubId, Args... args) {
106 // ensure a device entry exists for this eventHubId
107 addEventHubDevice(eventHubId, false);
108
109 // create mapper
110 auto& devicePair = mDevices[eventHubId];
111 auto& deviceContext = devicePair.first;
112 auto& mappers = devicePair.second;
113 T* mapper = new T(*deviceContext, args...);
114 mappers.emplace_back(mapper);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800115 return *mapper;
116 }
117
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700118private:
119 InputReaderContext* mContext;
120 int32_t mId;
121 int32_t mGeneration;
122 int32_t mControllerNumber;
123 InputDeviceIdentifier mIdentifier;
124 std::string mAlias;
Chris Ye1b0c7342020-07-28 21:57:03 -0700125 Flags<InputDeviceClass> mClasses;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700126
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800127 // map from eventHubId to device context and mappers
128 using MapperVector = std::vector<std::unique_ptr<InputMapper>>;
129 using DevicePair = std::pair<std::unique_ptr<InputDeviceContext>, MapperVector>;
130 std::unordered_map<int32_t, DevicePair> mDevices;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700131
132 uint32_t mSources;
133 bool mIsExternal;
134 std::optional<uint8_t> mAssociatedDisplayPort;
135 std::optional<DisplayViewport> mAssociatedViewport;
136 bool mHasMic;
137 bool mDropUntilNextSync;
138
139 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
140 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
141
142 PropertyMap mConfiguration;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800143
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800144 // helpers to interate over the devices collection
145 // run a function against every mapper on every subdevice
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800146 inline void for_each_mapper(std::function<void(InputMapper&)> f) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800147 for (auto& deviceEntry : mDevices) {
148 auto& devicePair = deviceEntry.second;
149 auto& mappers = devicePair.second;
150 for (auto& mapperPtr : mappers) {
151 f(*mapperPtr);
152 }
153 }
154 }
155
156 // run a function against every mapper on a specific subdevice
157 inline void for_each_mapper_in_subdevice(int32_t eventHubDevice,
158 std::function<void(InputMapper&)> f) {
159 auto deviceIt = mDevices.find(eventHubDevice);
160 if (deviceIt != mDevices.end()) {
161 auto& devicePair = deviceIt->second;
162 auto& mappers = devicePair.second;
163 for (auto& mapperPtr : mappers) {
164 f(*mapperPtr);
165 }
166 }
167 }
168
169 // run a function against every subdevice
170 inline void for_each_subdevice(std::function<void(InputDeviceContext&)> f) {
171 for (auto& deviceEntry : mDevices) {
172 auto& devicePair = deviceEntry.second;
173 auto& contextPtr = devicePair.first;
174 f(*contextPtr);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800175 }
176 }
177
178 // return the first value returned by a function over every mapper.
179 // if all mappers return nullopt, return nullopt.
180 template <typename T>
181 inline std::optional<T> first_in_mappers(std::function<std::optional<T>(InputMapper&)> f) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800182 for (auto& deviceEntry : mDevices) {
183 auto& devicePair = deviceEntry.second;
184 auto& mappers = devicePair.second;
185 for (auto& mapperPtr : mappers) {
186 std::optional<T> ret = f(*mapperPtr);
187 if (ret) {
188 return ret;
189 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800190 }
191 }
192 return std::nullopt;
193 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700194};
195
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800196/* Provides access to EventHub methods, but limits access to the current InputDevice.
197 * Essentially an implementation of EventHubInterface, but for a specific device id.
198 * Helps hide implementation details of InputDevice and EventHub. Used by mappers to
199 * check the status of the associated hardware device
200 */
201class InputDeviceContext {
202public:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800203 InputDeviceContext(InputDevice& device, int32_t eventHubId);
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800204 ~InputDeviceContext();
205
206 inline InputReaderContext* getContext() { return mContext; }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800207 inline int32_t getId() { return mDeviceId; }
208 inline int32_t getEventHubId() { return mId; }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800209
Chris Ye1b0c7342020-07-28 21:57:03 -0700210 inline Flags<InputDeviceClass> getDeviceClasses() const {
211 return mEventHub->getDeviceClasses(mId);
212 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800213 inline InputDeviceIdentifier getDeviceIdentifier() const {
214 return mEventHub->getDeviceIdentifier(mId);
215 }
216 inline int32_t getDeviceControllerNumber() const {
217 return mEventHub->getDeviceControllerNumber(mId);
218 }
219 inline void getConfiguration(PropertyMap* outConfiguration) const {
220 return mEventHub->getConfiguration(mId, outConfiguration);
221 }
222 inline status_t getAbsoluteAxisInfo(int32_t code, RawAbsoluteAxisInfo* axisInfo) const {
223 return mEventHub->getAbsoluteAxisInfo(mId, code, axisInfo);
224 }
225 inline bool hasRelativeAxis(int32_t code) const {
226 return mEventHub->hasRelativeAxis(mId, code);
227 }
228 inline bool hasInputProperty(int property) const {
229 return mEventHub->hasInputProperty(mId, property);
230 }
231 inline status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t metaState,
232 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const {
233 return mEventHub->mapKey(mId, scanCode, usageCode, metaState, outKeycode, outMetaState,
234 outFlags);
235 }
236 inline status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
237 return mEventHub->mapAxis(mId, scanCode, outAxisInfo);
238 }
239 inline std::vector<TouchVideoFrame> getVideoFrames() { return mEventHub->getVideoFrames(mId); }
240 inline int32_t getScanCodeState(int32_t scanCode) const {
241 return mEventHub->getScanCodeState(mId, scanCode);
242 }
243 inline int32_t getKeyCodeState(int32_t keyCode) const {
244 return mEventHub->getKeyCodeState(mId, keyCode);
245 }
246 inline int32_t getSwitchState(int32_t sw) const { return mEventHub->getSwitchState(mId, sw); }
247 inline status_t getAbsoluteAxisValue(int32_t code, int32_t* outValue) const {
248 return mEventHub->getAbsoluteAxisValue(mId, code, outValue);
249 }
250 inline bool markSupportedKeyCodes(size_t numCodes, const int32_t* keyCodes,
251 uint8_t* outFlags) const {
252 return mEventHub->markSupportedKeyCodes(mId, numCodes, keyCodes, outFlags);
253 }
254 inline bool hasScanCode(int32_t scanCode) const {
255 return mEventHub->hasScanCode(mId, scanCode);
256 }
257 inline bool hasLed(int32_t led) const { return mEventHub->hasLed(mId, led); }
258 inline void setLedState(int32_t led, bool on) { return mEventHub->setLedState(mId, led, on); }
259 inline void getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
260 return mEventHub->getVirtualKeyDefinitions(mId, outVirtualKeys);
261 }
262 inline sp<KeyCharacterMap> getKeyCharacterMap() const {
263 return mEventHub->getKeyCharacterMap(mId);
264 }
265 inline bool setKeyboardLayoutOverlay(const sp<KeyCharacterMap>& map) {
266 return mEventHub->setKeyboardLayoutOverlay(mId, map);
267 }
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000268 inline void vibrate(const VibrationElement& element) {
269 return mEventHub->vibrate(mId, element);
270 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800271 inline void cancelVibrate() { return mEventHub->cancelVibrate(mId); }
272
273 inline bool hasAbsoluteAxis(int32_t code) const {
274 RawAbsoluteAxisInfo info;
275 mEventHub->getAbsoluteAxisInfo(mId, code, &info);
276 return info.valid;
277 }
278 inline bool isKeyPressed(int32_t code) const {
279 return mEventHub->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
280 }
281 inline int32_t getAbsoluteAxisValue(int32_t code) const {
282 int32_t value;
283 mEventHub->getAbsoluteAxisValue(mId, code, &value);
284 return value;
285 }
286 inline bool isDeviceEnabled() { return mEventHub->isDeviceEnabled(mId); }
287 inline status_t enableDevice() { return mEventHub->enableDevice(mId); }
288 inline status_t disableDevice() { return mEventHub->disableDevice(mId); }
289
290 inline const std::string getName() { return mDevice.getName(); }
291 inline const std::string getDescriptor() { return mDevice.getDescriptor(); }
292 inline bool isExternal() { return mDevice.isExternal(); }
293 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
294 return mDevice.getAssociatedDisplayPort();
295 }
296 inline std::optional<DisplayViewport> getAssociatedViewport() const {
297 return mDevice.getAssociatedViewport();
298 }
299 inline void cancelTouch(nsecs_t when) { mDevice.cancelTouch(when); }
300 inline void bumpGeneration() { mDevice.bumpGeneration(); }
301 inline const PropertyMap& getConfiguration() { return mDevice.getConfiguration(); }
302
303private:
304 InputDevice& mDevice;
305 InputReaderContext* mContext;
306 EventHubInterface* mEventHub;
307 int32_t mId;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800308 int32_t mDeviceId;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800309};
310
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700311} // namespace android
312
313#endif //_UI_INPUTREADER_INPUT_DEVICE_H