blob: 064e00d1d0c79fde966b1facce5e21c4f48d8ee4 [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();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000089 void cancelTouch(nsecs_t when, nsecs_t readTime);
Chris Yef59a2f42020-10-16 12:55:26 -070090 bool enableSensor(InputDeviceSensorType sensorType, std::chrono::microseconds samplingPeriod,
91 std::chrono::microseconds maxBatchReportLatency);
92 void disableSensor(InputDeviceSensorType sensorType);
93 void flushSensor(InputDeviceSensorType sensorType);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094
Kim Low03ea0352020-11-06 12:45:07 -080095 std::optional<int32_t> getBatteryCapacity();
96 std::optional<int32_t> getBatteryStatus();
97
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098 int32_t getMetaState();
99 void updateMetaState(int32_t keyCode);
100
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700101 void bumpGeneration();
102
103 void notifyReset(nsecs_t when);
104
105 inline const PropertyMap& getConfiguration() { return mConfiguration; }
106 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
107
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700108 std::optional<int32_t> getAssociatedDisplayId();
109
arthurhungc903df12020-08-11 15:08:42 +0800110 void updateLedState(bool reset);
111
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800112 size_t getMapperCount();
113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800114 // construct and add a mapper to the input device
115 template <class T, typename... Args>
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800116 T& addMapper(int32_t eventHubId, Args... args) {
117 // ensure a device entry exists for this eventHubId
118 addEventHubDevice(eventHubId, false);
119
120 // create mapper
121 auto& devicePair = mDevices[eventHubId];
122 auto& deviceContext = devicePair.first;
123 auto& mappers = devicePair.second;
124 T* mapper = new T(*deviceContext, args...);
125 mappers.emplace_back(mapper);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800126 return *mapper;
127 }
128
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700129private:
130 InputReaderContext* mContext;
131 int32_t mId;
132 int32_t mGeneration;
133 int32_t mControllerNumber;
134 InputDeviceIdentifier mIdentifier;
135 std::string mAlias;
Chris Ye1b0c7342020-07-28 21:57:03 -0700136 Flags<InputDeviceClass> mClasses;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700137
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800138 // map from eventHubId to device context and mappers
139 using MapperVector = std::vector<std::unique_ptr<InputMapper>>;
140 using DevicePair = std::pair<std::unique_ptr<InputDeviceContext>, MapperVector>;
141 std::unordered_map<int32_t, DevicePair> mDevices;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700142
143 uint32_t mSources;
144 bool mIsExternal;
145 std::optional<uint8_t> mAssociatedDisplayPort;
146 std::optional<DisplayViewport> mAssociatedViewport;
147 bool mHasMic;
148 bool mDropUntilNextSync;
149
150 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
151 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
152
153 PropertyMap mConfiguration;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800154
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800155 // helpers to interate over the devices collection
156 // run a function against every mapper on every subdevice
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800157 inline void for_each_mapper(std::function<void(InputMapper&)> f) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800158 for (auto& deviceEntry : mDevices) {
159 auto& devicePair = deviceEntry.second;
160 auto& mappers = devicePair.second;
161 for (auto& mapperPtr : mappers) {
162 f(*mapperPtr);
163 }
164 }
165 }
166
167 // run a function against every mapper on a specific subdevice
168 inline void for_each_mapper_in_subdevice(int32_t eventHubDevice,
169 std::function<void(InputMapper&)> f) {
170 auto deviceIt = mDevices.find(eventHubDevice);
171 if (deviceIt != mDevices.end()) {
172 auto& devicePair = deviceIt->second;
173 auto& mappers = devicePair.second;
174 for (auto& mapperPtr : mappers) {
175 f(*mapperPtr);
176 }
177 }
178 }
179
180 // run a function against every subdevice
181 inline void for_each_subdevice(std::function<void(InputDeviceContext&)> f) {
182 for (auto& deviceEntry : mDevices) {
183 auto& devicePair = deviceEntry.second;
184 auto& contextPtr = devicePair.first;
185 f(*contextPtr);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800186 }
187 }
188
189 // return the first value returned by a function over every mapper.
190 // if all mappers return nullopt, return nullopt.
191 template <typename T>
192 inline std::optional<T> first_in_mappers(std::function<std::optional<T>(InputMapper&)> f) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800193 for (auto& deviceEntry : mDevices) {
194 auto& devicePair = deviceEntry.second;
195 auto& mappers = devicePair.second;
196 for (auto& mapperPtr : mappers) {
197 std::optional<T> ret = f(*mapperPtr);
198 if (ret) {
199 return ret;
200 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800201 }
202 }
203 return std::nullopt;
204 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700205};
206
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800207/* Provides access to EventHub methods, but limits access to the current InputDevice.
208 * Essentially an implementation of EventHubInterface, but for a specific device id.
209 * Helps hide implementation details of InputDevice and EventHub. Used by mappers to
210 * check the status of the associated hardware device
211 */
212class InputDeviceContext {
213public:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800214 InputDeviceContext(InputDevice& device, int32_t eventHubId);
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800215 ~InputDeviceContext();
216
217 inline InputReaderContext* getContext() { return mContext; }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800218 inline int32_t getId() { return mDeviceId; }
219 inline int32_t getEventHubId() { return mId; }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800220
Chris Ye1b0c7342020-07-28 21:57:03 -0700221 inline Flags<InputDeviceClass> getDeviceClasses() const {
222 return mEventHub->getDeviceClasses(mId);
223 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800224 inline InputDeviceIdentifier getDeviceIdentifier() const {
225 return mEventHub->getDeviceIdentifier(mId);
226 }
227 inline int32_t getDeviceControllerNumber() const {
228 return mEventHub->getDeviceControllerNumber(mId);
229 }
230 inline void getConfiguration(PropertyMap* outConfiguration) const {
231 return mEventHub->getConfiguration(mId, outConfiguration);
232 }
233 inline status_t getAbsoluteAxisInfo(int32_t code, RawAbsoluteAxisInfo* axisInfo) const {
234 return mEventHub->getAbsoluteAxisInfo(mId, code, axisInfo);
235 }
236 inline bool hasRelativeAxis(int32_t code) const {
237 return mEventHub->hasRelativeAxis(mId, code);
238 }
Chris Yef59a2f42020-10-16 12:55:26 -0700239 inline bool hasInputProperty(int32_t property) const {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800240 return mEventHub->hasInputProperty(mId, property);
241 }
Chris Yef59a2f42020-10-16 12:55:26 -0700242
243 inline bool hasMscEvent(int mscEvent) const { return mEventHub->hasMscEvent(mId, mscEvent); }
244
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800245 inline status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t metaState,
246 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const {
247 return mEventHub->mapKey(mId, scanCode, usageCode, metaState, outKeycode, outMetaState,
248 outFlags);
249 }
250 inline status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
251 return mEventHub->mapAxis(mId, scanCode, outAxisInfo);
252 }
Chris Yef59a2f42020-10-16 12:55:26 -0700253 inline base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t absCode) {
254 return mEventHub->mapSensor(mId, absCode);
255 }
256
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800257 inline std::vector<TouchVideoFrame> getVideoFrames() { return mEventHub->getVideoFrames(mId); }
258 inline int32_t getScanCodeState(int32_t scanCode) const {
259 return mEventHub->getScanCodeState(mId, scanCode);
260 }
261 inline int32_t getKeyCodeState(int32_t keyCode) const {
262 return mEventHub->getKeyCodeState(mId, keyCode);
263 }
264 inline int32_t getSwitchState(int32_t sw) const { return mEventHub->getSwitchState(mId, sw); }
265 inline status_t getAbsoluteAxisValue(int32_t code, int32_t* outValue) const {
266 return mEventHub->getAbsoluteAxisValue(mId, code, outValue);
267 }
268 inline bool markSupportedKeyCodes(size_t numCodes, const int32_t* keyCodes,
269 uint8_t* outFlags) const {
270 return mEventHub->markSupportedKeyCodes(mId, numCodes, keyCodes, outFlags);
271 }
272 inline bool hasScanCode(int32_t scanCode) const {
273 return mEventHub->hasScanCode(mId, scanCode);
274 }
275 inline bool hasLed(int32_t led) const { return mEventHub->hasLed(mId, led); }
276 inline void setLedState(int32_t led, bool on) { return mEventHub->setLedState(mId, led, on); }
277 inline void getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
278 return mEventHub->getVirtualKeyDefinitions(mId, outVirtualKeys);
279 }
Chris Ye3a1e4462020-08-12 10:13:15 -0700280 inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800281 return mEventHub->getKeyCharacterMap(mId);
282 }
Chris Ye3a1e4462020-08-12 10:13:15 -0700283 inline bool setKeyboardLayoutOverlay(std::shared_ptr<KeyCharacterMap> map) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800284 return mEventHub->setKeyboardLayoutOverlay(mId, map);
285 }
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000286 inline void vibrate(const VibrationElement& element) {
287 return mEventHub->vibrate(mId, element);
288 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800289 inline void cancelVibrate() { return mEventHub->cancelVibrate(mId); }
290
Chris Ye87143712020-11-10 05:05:58 +0000291 inline std::vector<int32_t> getVibratorIds() { return mEventHub->getVibratorIds(mId); }
292
Kim Low03ea0352020-11-06 12:45:07 -0800293 inline std::optional<int32_t> getBatteryCapacity() {
294 return mEventHub->getBatteryCapacity(mId);
295 }
296
297 inline std::optional<int32_t> getBatteryStatus() { return mEventHub->getBatteryStatus(mId); }
298
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800299 inline bool hasAbsoluteAxis(int32_t code) const {
300 RawAbsoluteAxisInfo info;
301 mEventHub->getAbsoluteAxisInfo(mId, code, &info);
302 return info.valid;
303 }
304 inline bool isKeyPressed(int32_t code) const {
305 return mEventHub->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
306 }
307 inline int32_t getAbsoluteAxisValue(int32_t code) const {
308 int32_t value;
309 mEventHub->getAbsoluteAxisValue(mId, code, &value);
310 return value;
311 }
312 inline bool isDeviceEnabled() { return mEventHub->isDeviceEnabled(mId); }
313 inline status_t enableDevice() { return mEventHub->enableDevice(mId); }
314 inline status_t disableDevice() { return mEventHub->disableDevice(mId); }
315
316 inline const std::string getName() { return mDevice.getName(); }
317 inline const std::string getDescriptor() { return mDevice.getDescriptor(); }
318 inline bool isExternal() { return mDevice.isExternal(); }
319 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
320 return mDevice.getAssociatedDisplayPort();
321 }
322 inline std::optional<DisplayViewport> getAssociatedViewport() const {
323 return mDevice.getAssociatedViewport();
324 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000325 inline void cancelTouch(nsecs_t when, nsecs_t readTime) { mDevice.cancelTouch(when, readTime); }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800326 inline void bumpGeneration() { mDevice.bumpGeneration(); }
327 inline const PropertyMap& getConfiguration() { return mDevice.getConfiguration(); }
328
329private:
330 InputDevice& mDevice;
331 InputReaderContext* mContext;
332 EventHubInterface* mEventHub;
333 int32_t mId;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800334 int32_t mDeviceId;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800335};
336
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700337} // namespace android
338
339#endif //_UI_INPUTREADER_INPUT_DEVICE_H