blob: 5d56f5a2ef3bf154781c0ea656557e8990963cfd [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
Chris Yee2b1e5c2021-03-10 22:45:12 -080036class InputController;
37class InputControllerInterface;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080038class InputDeviceContext;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070039class InputMapper;
40
41/* Represents the state of a single input device. */
42class InputDevice {
43public:
44 InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080045 const InputDeviceIdentifier& identifier);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070046 ~InputDevice();
47
48 inline InputReaderContext* getContext() { return mContext; }
49 inline int32_t getId() const { return mId; }
50 inline int32_t getControllerNumber() const { return mControllerNumber; }
51 inline int32_t getGeneration() const { return mGeneration; }
52 inline const std::string getName() const { return mIdentifier.name; }
53 inline const std::string getDescriptor() { return mIdentifier.descriptor; }
Chris Ye1b0c7342020-07-28 21:57:03 -070054 inline Flags<InputDeviceClass> getClasses() const { return mClasses; }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070055 inline uint32_t getSources() const { return mSources; }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080056 inline bool hasEventHubDevices() const { return !mDevices.empty(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070057
58 inline bool isExternal() { return mIsExternal; }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070059 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
60 return mAssociatedDisplayPort;
61 }
62 inline std::optional<DisplayViewport> getAssociatedViewport() const {
63 return mAssociatedViewport;
64 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070065 inline bool hasMic() const { return mHasMic; }
66
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080067 inline bool isIgnored() { return !getMapperCount(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070068
69 bool isEnabled();
70 void setEnabled(bool enabled, nsecs_t when);
71
Chris Yee7310032020-09-22 15:36:28 -070072 void dump(std::string& dump, const std::string& eventHubDevStr);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080073 void addEventHubDevice(int32_t eventHubId, bool populateMappers = true);
74 void removeEventHubDevice(int32_t eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070075 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
76 void reset(nsecs_t when);
77 void process(const RawEvent* rawEvents, size_t count);
78 void timeoutExpired(nsecs_t when);
79 void updateExternalStylusState(const StylusState& state);
80
81 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
82 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
83 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
84 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
85 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, const int32_t* keyCodes,
86 uint8_t* outFlags);
Chris Ye87143712020-11-10 05:05:58 +000087 void vibrate(const VibrationSequence& sequence, ssize_t repeat, int32_t token);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070088 void cancelVibrate(int32_t token);
Chris Ye87143712020-11-10 05:05:58 +000089 bool isVibrating();
90 std::vector<int32_t> getVibratorIds();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000091 void cancelTouch(nsecs_t when, nsecs_t readTime);
Chris Yef59a2f42020-10-16 12:55:26 -070092 bool enableSensor(InputDeviceSensorType sensorType, std::chrono::microseconds samplingPeriod,
93 std::chrono::microseconds maxBatchReportLatency);
94 void disableSensor(InputDeviceSensorType sensorType);
95 void flushSensor(InputDeviceSensorType sensorType);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070096
Kim Low03ea0352020-11-06 12:45:07 -080097 std::optional<int32_t> getBatteryCapacity();
98 std::optional<int32_t> getBatteryStatus();
99
Chris Ye3fdbfef2021-01-06 18:45:18 -0800100 bool setLightColor(int32_t lightId, int32_t color);
101 bool setLightPlayerId(int32_t lightId, int32_t playerId);
102 std::optional<int32_t> getLightColor(int32_t lightId);
103 std::optional<int32_t> getLightPlayerId(int32_t lightId);
104
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700105 int32_t getMetaState();
106 void updateMetaState(int32_t keyCode);
107
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700108 void bumpGeneration();
109
110 void notifyReset(nsecs_t when);
111
112 inline const PropertyMap& getConfiguration() { return mConfiguration; }
113 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
114
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700115 std::optional<int32_t> getAssociatedDisplayId();
116
arthurhungc903df12020-08-11 15:08:42 +0800117 void updateLedState(bool reset);
118
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800119 size_t getMapperCount();
120
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800121 // construct and add a mapper to the input device
122 template <class T, typename... Args>
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800123 T& addMapper(int32_t eventHubId, Args... args) {
124 // ensure a device entry exists for this eventHubId
125 addEventHubDevice(eventHubId, false);
126
127 // create mapper
128 auto& devicePair = mDevices[eventHubId];
129 auto& deviceContext = devicePair.first;
130 auto& mappers = devicePair.second;
131 T* mapper = new T(*deviceContext, args...);
132 mappers.emplace_back(mapper);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800133 return *mapper;
134 }
135
Chris Yee2b1e5c2021-03-10 22:45:12 -0800136 // construct and add a controller to the input device
137 template <class T>
138 T& addController(int32_t eventHubId) {
139 // ensure a device entry exists for this eventHubId
140 addEventHubDevice(eventHubId, false);
141
142 // create controller
143 auto& devicePair = mDevices[eventHubId];
144 auto& deviceContext = devicePair.first;
145
146 mController = std::make_unique<T>(*deviceContext);
147 return *(reinterpret_cast<T*>(mController.get()));
148 }
149
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700150private:
151 InputReaderContext* mContext;
152 int32_t mId;
153 int32_t mGeneration;
154 int32_t mControllerNumber;
155 InputDeviceIdentifier mIdentifier;
156 std::string mAlias;
Chris Ye1b0c7342020-07-28 21:57:03 -0700157 Flags<InputDeviceClass> mClasses;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700158
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800159 // map from eventHubId to device context and mappers
160 using MapperVector = std::vector<std::unique_ptr<InputMapper>>;
161 using DevicePair = std::pair<std::unique_ptr<InputDeviceContext>, MapperVector>;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800162 // Map from EventHub ID to pair of device context and vector of mapper.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800163 std::unordered_map<int32_t, DevicePair> mDevices;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800164 // Misc devices controller for lights, battery, etc.
165 std::unique_ptr<InputControllerInterface> mController;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700166
167 uint32_t mSources;
168 bool mIsExternal;
169 std::optional<uint8_t> mAssociatedDisplayPort;
170 std::optional<DisplayViewport> mAssociatedViewport;
171 bool mHasMic;
172 bool mDropUntilNextSync;
173
174 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
175 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
176
177 PropertyMap mConfiguration;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800178
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800179 // helpers to interate over the devices collection
180 // run a function against every mapper on every subdevice
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800181 inline void for_each_mapper(std::function<void(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 f(*mapperPtr);
187 }
188 }
189 }
190
191 // run a function against every mapper on a specific subdevice
192 inline void for_each_mapper_in_subdevice(int32_t eventHubDevice,
193 std::function<void(InputMapper&)> f) {
194 auto deviceIt = mDevices.find(eventHubDevice);
195 if (deviceIt != mDevices.end()) {
196 auto& devicePair = deviceIt->second;
197 auto& mappers = devicePair.second;
198 for (auto& mapperPtr : mappers) {
199 f(*mapperPtr);
200 }
201 }
202 }
203
204 // run a function against every subdevice
205 inline void for_each_subdevice(std::function<void(InputDeviceContext&)> f) {
206 for (auto& deviceEntry : mDevices) {
207 auto& devicePair = deviceEntry.second;
208 auto& contextPtr = devicePair.first;
209 f(*contextPtr);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800210 }
211 }
212
213 // return the first value returned by a function over every mapper.
214 // if all mappers return nullopt, return nullopt.
215 template <typename T>
216 inline std::optional<T> first_in_mappers(std::function<std::optional<T>(InputMapper&)> f) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800217 for (auto& deviceEntry : mDevices) {
218 auto& devicePair = deviceEntry.second;
219 auto& mappers = devicePair.second;
220 for (auto& mapperPtr : mappers) {
221 std::optional<T> ret = f(*mapperPtr);
222 if (ret) {
223 return ret;
224 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800225 }
226 }
227 return std::nullopt;
228 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700229};
230
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800231/* Provides access to EventHub methods, but limits access to the current InputDevice.
232 * Essentially an implementation of EventHubInterface, but for a specific device id.
233 * Helps hide implementation details of InputDevice and EventHub. Used by mappers to
234 * check the status of the associated hardware device
235 */
236class InputDeviceContext {
237public:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800238 InputDeviceContext(InputDevice& device, int32_t eventHubId);
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800239 ~InputDeviceContext();
240
241 inline InputReaderContext* getContext() { return mContext; }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800242 inline int32_t getId() { return mDeviceId; }
243 inline int32_t getEventHubId() { return mId; }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800244
Chris Ye1b0c7342020-07-28 21:57:03 -0700245 inline Flags<InputDeviceClass> getDeviceClasses() const {
246 return mEventHub->getDeviceClasses(mId);
247 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800248 inline InputDeviceIdentifier getDeviceIdentifier() const {
249 return mEventHub->getDeviceIdentifier(mId);
250 }
251 inline int32_t getDeviceControllerNumber() const {
252 return mEventHub->getDeviceControllerNumber(mId);
253 }
254 inline void getConfiguration(PropertyMap* outConfiguration) const {
255 return mEventHub->getConfiguration(mId, outConfiguration);
256 }
257 inline status_t getAbsoluteAxisInfo(int32_t code, RawAbsoluteAxisInfo* axisInfo) const {
258 return mEventHub->getAbsoluteAxisInfo(mId, code, axisInfo);
259 }
260 inline bool hasRelativeAxis(int32_t code) const {
261 return mEventHub->hasRelativeAxis(mId, code);
262 }
Chris Yef59a2f42020-10-16 12:55:26 -0700263 inline bool hasInputProperty(int32_t property) const {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800264 return mEventHub->hasInputProperty(mId, property);
265 }
Chris Yef59a2f42020-10-16 12:55:26 -0700266
267 inline bool hasMscEvent(int mscEvent) const { return mEventHub->hasMscEvent(mId, mscEvent); }
268
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800269 inline status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t metaState,
270 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const {
271 return mEventHub->mapKey(mId, scanCode, usageCode, metaState, outKeycode, outMetaState,
272 outFlags);
273 }
274 inline status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
275 return mEventHub->mapAxis(mId, scanCode, outAxisInfo);
276 }
Chris Yef59a2f42020-10-16 12:55:26 -0700277 inline base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t absCode) {
278 return mEventHub->mapSensor(mId, absCode);
279 }
280
Chris Ye3fdbfef2021-01-06 18:45:18 -0800281 inline const std::vector<int32_t> getRawLightIds() { return mEventHub->getRawLightIds(mId); }
282
283 inline std::optional<RawLightInfo> getRawLightInfo(int32_t lightId) {
284 return mEventHub->getRawLightInfo(mId, lightId);
285 }
286
287 inline std::optional<int32_t> getLightBrightness(int32_t lightId) {
288 return mEventHub->getLightBrightness(mId, lightId);
289 }
290
291 inline void setLightBrightness(int32_t lightId, int32_t brightness) {
292 return mEventHub->setLightBrightness(mId, lightId, brightness);
293 }
294
295 inline std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
296 int32_t lightId) {
297 return mEventHub->getLightIntensities(mId, lightId);
298 }
299
300 inline void setLightIntensities(int32_t lightId,
301 std::unordered_map<LightColor, int32_t> intensities) {
302 return mEventHub->setLightIntensities(mId, lightId, intensities);
303 }
304
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800305 inline std::vector<TouchVideoFrame> getVideoFrames() { return mEventHub->getVideoFrames(mId); }
306 inline int32_t getScanCodeState(int32_t scanCode) const {
307 return mEventHub->getScanCodeState(mId, scanCode);
308 }
309 inline int32_t getKeyCodeState(int32_t keyCode) const {
310 return mEventHub->getKeyCodeState(mId, keyCode);
311 }
312 inline int32_t getSwitchState(int32_t sw) const { return mEventHub->getSwitchState(mId, sw); }
313 inline status_t getAbsoluteAxisValue(int32_t code, int32_t* outValue) const {
314 return mEventHub->getAbsoluteAxisValue(mId, code, outValue);
315 }
316 inline bool markSupportedKeyCodes(size_t numCodes, const int32_t* keyCodes,
317 uint8_t* outFlags) const {
318 return mEventHub->markSupportedKeyCodes(mId, numCodes, keyCodes, outFlags);
319 }
320 inline bool hasScanCode(int32_t scanCode) const {
321 return mEventHub->hasScanCode(mId, scanCode);
322 }
323 inline bool hasLed(int32_t led) const { return mEventHub->hasLed(mId, led); }
324 inline void setLedState(int32_t led, bool on) { return mEventHub->setLedState(mId, led, on); }
325 inline void getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
326 return mEventHub->getVirtualKeyDefinitions(mId, outVirtualKeys);
327 }
Chris Ye3a1e4462020-08-12 10:13:15 -0700328 inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800329 return mEventHub->getKeyCharacterMap(mId);
330 }
Chris Ye3a1e4462020-08-12 10:13:15 -0700331 inline bool setKeyboardLayoutOverlay(std::shared_ptr<KeyCharacterMap> map) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800332 return mEventHub->setKeyboardLayoutOverlay(mId, map);
333 }
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000334 inline void vibrate(const VibrationElement& element) {
335 return mEventHub->vibrate(mId, element);
336 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800337 inline void cancelVibrate() { return mEventHub->cancelVibrate(mId); }
338
Chris Ye87143712020-11-10 05:05:58 +0000339 inline std::vector<int32_t> getVibratorIds() { return mEventHub->getVibratorIds(mId); }
340
Chris Yee2b1e5c2021-03-10 22:45:12 -0800341 inline const std::vector<int32_t> getRawBatteryIds() {
342 return mEventHub->getRawBatteryIds(mId);
Kim Low03ea0352020-11-06 12:45:07 -0800343 }
344
Chris Yee2b1e5c2021-03-10 22:45:12 -0800345 inline std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t batteryId) {
346 return mEventHub->getRawBatteryInfo(mId, batteryId);
347 }
348
349 inline std::optional<int32_t> getBatteryCapacity(int32_t batteryId) {
350 return mEventHub->getBatteryCapacity(mId, batteryId);
351 }
352
353 inline std::optional<int32_t> getBatteryStatus(int32_t batteryId) {
354 return mEventHub->getBatteryStatus(mId, batteryId);
355 }
Kim Low03ea0352020-11-06 12:45:07 -0800356
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800357 inline bool hasAbsoluteAxis(int32_t code) const {
358 RawAbsoluteAxisInfo info;
359 mEventHub->getAbsoluteAxisInfo(mId, code, &info);
360 return info.valid;
361 }
362 inline bool isKeyPressed(int32_t code) const {
363 return mEventHub->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
364 }
365 inline int32_t getAbsoluteAxisValue(int32_t code) const {
366 int32_t value;
367 mEventHub->getAbsoluteAxisValue(mId, code, &value);
368 return value;
369 }
370 inline bool isDeviceEnabled() { return mEventHub->isDeviceEnabled(mId); }
371 inline status_t enableDevice() { return mEventHub->enableDevice(mId); }
372 inline status_t disableDevice() { return mEventHub->disableDevice(mId); }
373
374 inline const std::string getName() { return mDevice.getName(); }
375 inline const std::string getDescriptor() { return mDevice.getDescriptor(); }
376 inline bool isExternal() { return mDevice.isExternal(); }
377 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
378 return mDevice.getAssociatedDisplayPort();
379 }
380 inline std::optional<DisplayViewport> getAssociatedViewport() const {
381 return mDevice.getAssociatedViewport();
382 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000383 inline void cancelTouch(nsecs_t when, nsecs_t readTime) { mDevice.cancelTouch(when, readTime); }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800384 inline void bumpGeneration() { mDevice.bumpGeneration(); }
385 inline const PropertyMap& getConfiguration() { return mDevice.getConfiguration(); }
386
387private:
388 InputDevice& mDevice;
389 InputReaderContext* mContext;
390 EventHubInterface* mEventHub;
391 int32_t mId;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800392 int32_t mDeviceId;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800393};
394
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700395} // namespace android
396
397#endif //_UI_INPUTREADER_INPUT_DEVICE_H