blob: b2b23e59777c5536e403b19fecd6be109899a086 [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 {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070035// TODO b/180733860 support multiple battery in API and remove this.
36constexpr int32_t DEFAULT_BATTERY_ID = 1;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070037
Chris Ye1dd2e5c2021-04-04 23:12:41 -070038class PeripheralController;
39class PeripheralControllerInterface;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080040class InputDeviceContext;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070041class InputMapper;
42
43/* Represents the state of a single input device. */
44class InputDevice {
45public:
46 InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080047 const InputDeviceIdentifier& identifier);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070048 ~InputDevice();
49
50 inline InputReaderContext* getContext() { return mContext; }
51 inline int32_t getId() const { return mId; }
52 inline int32_t getControllerNumber() const { return mControllerNumber; }
53 inline int32_t getGeneration() const { return mGeneration; }
54 inline const std::string getName() const { return mIdentifier.name; }
55 inline const std::string getDescriptor() { return mIdentifier.descriptor; }
Chris Ye1b0c7342020-07-28 21:57:03 -070056 inline Flags<InputDeviceClass> getClasses() const { return mClasses; }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070057 inline uint32_t getSources() const { return mSources; }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080058 inline bool hasEventHubDevices() const { return !mDevices.empty(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070059
60 inline bool isExternal() { return mIsExternal; }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070061 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
62 return mAssociatedDisplayPort;
63 }
64 inline std::optional<DisplayViewport> getAssociatedViewport() const {
65 return mAssociatedViewport;
66 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070067 inline bool hasMic() const { return mHasMic; }
68
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080069 inline bool isIgnored() { return !getMapperCount(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070070
71 bool isEnabled();
72 void setEnabled(bool enabled, nsecs_t when);
73
Chris Yee7310032020-09-22 15:36:28 -070074 void dump(std::string& dump, const std::string& eventHubDevStr);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080075 void addEventHubDevice(int32_t eventHubId, bool populateMappers = true);
76 void removeEventHubDevice(int32_t eventHubId);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070077 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
78 void reset(nsecs_t when);
79 void process(const RawEvent* rawEvents, size_t count);
80 void timeoutExpired(nsecs_t when);
81 void updateExternalStylusState(const StylusState& state);
82
83 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
84 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
85 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
86 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
87 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, const int32_t* keyCodes,
88 uint8_t* outFlags);
Chris Ye87143712020-11-10 05:05:58 +000089 void vibrate(const VibrationSequence& sequence, ssize_t repeat, int32_t token);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090 void cancelVibrate(int32_t token);
Chris Ye87143712020-11-10 05:05:58 +000091 bool isVibrating();
92 std::vector<int32_t> getVibratorIds();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000093 void cancelTouch(nsecs_t when, nsecs_t readTime);
Chris Yef59a2f42020-10-16 12:55:26 -070094 bool enableSensor(InputDeviceSensorType sensorType, std::chrono::microseconds samplingPeriod,
95 std::chrono::microseconds maxBatchReportLatency);
96 void disableSensor(InputDeviceSensorType sensorType);
97 void flushSensor(InputDeviceSensorType sensorType);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098
Kim Low03ea0352020-11-06 12:45:07 -080099 std::optional<int32_t> getBatteryCapacity();
100 std::optional<int32_t> getBatteryStatus();
101
Chris Ye3fdbfef2021-01-06 18:45:18 -0800102 bool setLightColor(int32_t lightId, int32_t color);
103 bool setLightPlayerId(int32_t lightId, int32_t playerId);
104 std::optional<int32_t> getLightColor(int32_t lightId);
105 std::optional<int32_t> getLightPlayerId(int32_t lightId);
106
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700107 int32_t getMetaState();
108 void updateMetaState(int32_t keyCode);
109
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700110 void bumpGeneration();
111
112 void notifyReset(nsecs_t when);
113
114 inline const PropertyMap& getConfiguration() { return mConfiguration; }
115 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
116
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700117 std::optional<int32_t> getAssociatedDisplayId();
118
arthurhungc903df12020-08-11 15:08:42 +0800119 void updateLedState(bool reset);
120
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800121 size_t getMapperCount();
122
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800123 // construct and add a mapper to the input device
124 template <class T, typename... Args>
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800125 T& addMapper(int32_t eventHubId, Args... args) {
126 // ensure a device entry exists for this eventHubId
127 addEventHubDevice(eventHubId, false);
128
129 // create mapper
130 auto& devicePair = mDevices[eventHubId];
131 auto& deviceContext = devicePair.first;
132 auto& mappers = devicePair.second;
133 T* mapper = new T(*deviceContext, args...);
134 mappers.emplace_back(mapper);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800135 return *mapper;
136 }
137
Chris Yee2b1e5c2021-03-10 22:45:12 -0800138 // construct and add a controller to the input device
139 template <class T>
140 T& addController(int32_t eventHubId) {
141 // ensure a device entry exists for this eventHubId
142 addEventHubDevice(eventHubId, false);
143
144 // create controller
145 auto& devicePair = mDevices[eventHubId];
146 auto& deviceContext = devicePair.first;
147
148 mController = std::make_unique<T>(*deviceContext);
149 return *(reinterpret_cast<T*>(mController.get()));
150 }
151
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700152private:
153 InputReaderContext* mContext;
154 int32_t mId;
155 int32_t mGeneration;
156 int32_t mControllerNumber;
157 InputDeviceIdentifier mIdentifier;
158 std::string mAlias;
Chris Ye1b0c7342020-07-28 21:57:03 -0700159 Flags<InputDeviceClass> mClasses;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700160
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800161 // map from eventHubId to device context and mappers
162 using MapperVector = std::vector<std::unique_ptr<InputMapper>>;
163 using DevicePair = std::pair<std::unique_ptr<InputDeviceContext>, MapperVector>;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800164 // Map from EventHub ID to pair of device context and vector of mapper.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800165 std::unordered_map<int32_t, DevicePair> mDevices;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800166 // Misc devices controller for lights, battery, etc.
Chris Ye1dd2e5c2021-04-04 23:12:41 -0700167 std::unique_ptr<PeripheralControllerInterface> mController;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700168
169 uint32_t mSources;
170 bool mIsExternal;
171 std::optional<uint8_t> mAssociatedDisplayPort;
172 std::optional<DisplayViewport> mAssociatedViewport;
173 bool mHasMic;
174 bool mDropUntilNextSync;
175
176 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
177 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
178
179 PropertyMap mConfiguration;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800180
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800181 // helpers to interate over the devices collection
182 // run a function against every mapper on every subdevice
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800183 inline void for_each_mapper(std::function<void(InputMapper&)> f) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800184 for (auto& deviceEntry : mDevices) {
185 auto& devicePair = deviceEntry.second;
186 auto& mappers = devicePair.second;
187 for (auto& mapperPtr : mappers) {
188 f(*mapperPtr);
189 }
190 }
191 }
192
193 // run a function against every mapper on a specific subdevice
194 inline void for_each_mapper_in_subdevice(int32_t eventHubDevice,
195 std::function<void(InputMapper&)> f) {
196 auto deviceIt = mDevices.find(eventHubDevice);
197 if (deviceIt != mDevices.end()) {
198 auto& devicePair = deviceIt->second;
199 auto& mappers = devicePair.second;
200 for (auto& mapperPtr : mappers) {
201 f(*mapperPtr);
202 }
203 }
204 }
205
206 // run a function against every subdevice
207 inline void for_each_subdevice(std::function<void(InputDeviceContext&)> f) {
208 for (auto& deviceEntry : mDevices) {
209 auto& devicePair = deviceEntry.second;
210 auto& contextPtr = devicePair.first;
211 f(*contextPtr);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800212 }
213 }
214
215 // return the first value returned by a function over every mapper.
216 // if all mappers return nullopt, return nullopt.
217 template <typename T>
218 inline std::optional<T> first_in_mappers(std::function<std::optional<T>(InputMapper&)> f) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800219 for (auto& deviceEntry : mDevices) {
220 auto& devicePair = deviceEntry.second;
221 auto& mappers = devicePair.second;
222 for (auto& mapperPtr : mappers) {
223 std::optional<T> ret = f(*mapperPtr);
224 if (ret) {
225 return ret;
226 }
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800227 }
228 }
229 return std::nullopt;
230 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700231};
232
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800233/* Provides access to EventHub methods, but limits access to the current InputDevice.
234 * Essentially an implementation of EventHubInterface, but for a specific device id.
235 * Helps hide implementation details of InputDevice and EventHub. Used by mappers to
236 * check the status of the associated hardware device
237 */
238class InputDeviceContext {
239public:
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800240 InputDeviceContext(InputDevice& device, int32_t eventHubId);
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800241 ~InputDeviceContext();
242
243 inline InputReaderContext* getContext() { return mContext; }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800244 inline int32_t getId() { return mDeviceId; }
245 inline int32_t getEventHubId() { return mId; }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800246
Chris Ye1b0c7342020-07-28 21:57:03 -0700247 inline Flags<InputDeviceClass> getDeviceClasses() const {
248 return mEventHub->getDeviceClasses(mId);
249 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800250 inline InputDeviceIdentifier getDeviceIdentifier() const {
251 return mEventHub->getDeviceIdentifier(mId);
252 }
253 inline int32_t getDeviceControllerNumber() const {
254 return mEventHub->getDeviceControllerNumber(mId);
255 }
256 inline void getConfiguration(PropertyMap* outConfiguration) const {
257 return mEventHub->getConfiguration(mId, outConfiguration);
258 }
259 inline status_t getAbsoluteAxisInfo(int32_t code, RawAbsoluteAxisInfo* axisInfo) const {
260 return mEventHub->getAbsoluteAxisInfo(mId, code, axisInfo);
261 }
262 inline bool hasRelativeAxis(int32_t code) const {
263 return mEventHub->hasRelativeAxis(mId, code);
264 }
Chris Yef59a2f42020-10-16 12:55:26 -0700265 inline bool hasInputProperty(int32_t property) const {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800266 return mEventHub->hasInputProperty(mId, property);
267 }
Chris Yef59a2f42020-10-16 12:55:26 -0700268
269 inline bool hasMscEvent(int mscEvent) const { return mEventHub->hasMscEvent(mId, mscEvent); }
270
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800271 inline status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t metaState,
272 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const {
273 return mEventHub->mapKey(mId, scanCode, usageCode, metaState, outKeycode, outMetaState,
274 outFlags);
275 }
276 inline status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
277 return mEventHub->mapAxis(mId, scanCode, outAxisInfo);
278 }
Chris Yef59a2f42020-10-16 12:55:26 -0700279 inline base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t absCode) {
280 return mEventHub->mapSensor(mId, absCode);
281 }
282
Chris Ye3fdbfef2021-01-06 18:45:18 -0800283 inline const std::vector<int32_t> getRawLightIds() { return mEventHub->getRawLightIds(mId); }
284
285 inline std::optional<RawLightInfo> getRawLightInfo(int32_t lightId) {
286 return mEventHub->getRawLightInfo(mId, lightId);
287 }
288
289 inline std::optional<int32_t> getLightBrightness(int32_t lightId) {
290 return mEventHub->getLightBrightness(mId, lightId);
291 }
292
293 inline void setLightBrightness(int32_t lightId, int32_t brightness) {
294 return mEventHub->setLightBrightness(mId, lightId, brightness);
295 }
296
297 inline std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
298 int32_t lightId) {
299 return mEventHub->getLightIntensities(mId, lightId);
300 }
301
302 inline void setLightIntensities(int32_t lightId,
303 std::unordered_map<LightColor, int32_t> intensities) {
304 return mEventHub->setLightIntensities(mId, lightId, intensities);
305 }
306
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800307 inline std::vector<TouchVideoFrame> getVideoFrames() { return mEventHub->getVideoFrames(mId); }
308 inline int32_t getScanCodeState(int32_t scanCode) const {
309 return mEventHub->getScanCodeState(mId, scanCode);
310 }
311 inline int32_t getKeyCodeState(int32_t keyCode) const {
312 return mEventHub->getKeyCodeState(mId, keyCode);
313 }
314 inline int32_t getSwitchState(int32_t sw) const { return mEventHub->getSwitchState(mId, sw); }
315 inline status_t getAbsoluteAxisValue(int32_t code, int32_t* outValue) const {
316 return mEventHub->getAbsoluteAxisValue(mId, code, outValue);
317 }
318 inline bool markSupportedKeyCodes(size_t numCodes, const int32_t* keyCodes,
319 uint8_t* outFlags) const {
320 return mEventHub->markSupportedKeyCodes(mId, numCodes, keyCodes, outFlags);
321 }
322 inline bool hasScanCode(int32_t scanCode) const {
323 return mEventHub->hasScanCode(mId, scanCode);
324 }
325 inline bool hasLed(int32_t led) const { return mEventHub->hasLed(mId, led); }
326 inline void setLedState(int32_t led, bool on) { return mEventHub->setLedState(mId, led, on); }
327 inline void getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
328 return mEventHub->getVirtualKeyDefinitions(mId, outVirtualKeys);
329 }
Chris Ye3a1e4462020-08-12 10:13:15 -0700330 inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800331 return mEventHub->getKeyCharacterMap(mId);
332 }
Chris Ye3a1e4462020-08-12 10:13:15 -0700333 inline bool setKeyboardLayoutOverlay(std::shared_ptr<KeyCharacterMap> map) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800334 return mEventHub->setKeyboardLayoutOverlay(mId, map);
335 }
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000336 inline void vibrate(const VibrationElement& element) {
337 return mEventHub->vibrate(mId, element);
338 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800339 inline void cancelVibrate() { return mEventHub->cancelVibrate(mId); }
340
Chris Ye87143712020-11-10 05:05:58 +0000341 inline std::vector<int32_t> getVibratorIds() { return mEventHub->getVibratorIds(mId); }
342
Chris Yee2b1e5c2021-03-10 22:45:12 -0800343 inline const std::vector<int32_t> getRawBatteryIds() {
344 return mEventHub->getRawBatteryIds(mId);
Kim Low03ea0352020-11-06 12:45:07 -0800345 }
346
Chris Yee2b1e5c2021-03-10 22:45:12 -0800347 inline std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t batteryId) {
348 return mEventHub->getRawBatteryInfo(mId, batteryId);
349 }
350
351 inline std::optional<int32_t> getBatteryCapacity(int32_t batteryId) {
352 return mEventHub->getBatteryCapacity(mId, batteryId);
353 }
354
355 inline std::optional<int32_t> getBatteryStatus(int32_t batteryId) {
356 return mEventHub->getBatteryStatus(mId, batteryId);
357 }
Kim Low03ea0352020-11-06 12:45:07 -0800358
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800359 inline bool hasAbsoluteAxis(int32_t code) const {
360 RawAbsoluteAxisInfo info;
361 mEventHub->getAbsoluteAxisInfo(mId, code, &info);
362 return info.valid;
363 }
364 inline bool isKeyPressed(int32_t code) const {
365 return mEventHub->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
366 }
367 inline int32_t getAbsoluteAxisValue(int32_t code) const {
368 int32_t value;
369 mEventHub->getAbsoluteAxisValue(mId, code, &value);
370 return value;
371 }
372 inline bool isDeviceEnabled() { return mEventHub->isDeviceEnabled(mId); }
373 inline status_t enableDevice() { return mEventHub->enableDevice(mId); }
374 inline status_t disableDevice() { return mEventHub->disableDevice(mId); }
375
376 inline const std::string getName() { return mDevice.getName(); }
377 inline const std::string getDescriptor() { return mDevice.getDescriptor(); }
378 inline bool isExternal() { return mDevice.isExternal(); }
379 inline std::optional<uint8_t> getAssociatedDisplayPort() const {
380 return mDevice.getAssociatedDisplayPort();
381 }
382 inline std::optional<DisplayViewport> getAssociatedViewport() const {
383 return mDevice.getAssociatedViewport();
384 }
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000385 inline void cancelTouch(nsecs_t when, nsecs_t readTime) { mDevice.cancelTouch(when, readTime); }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800386 inline void bumpGeneration() { mDevice.bumpGeneration(); }
387 inline const PropertyMap& getConfiguration() { return mDevice.getConfiguration(); }
388
389private:
390 InputDevice& mDevice;
391 InputReaderContext* mContext;
392 EventHubInterface* mEventHub;
393 int32_t mId;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800394 int32_t mDeviceId;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800395};
396
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700397} // namespace android
398
399#endif //_UI_INPUTREADER_INPUT_DEVICE_H