• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #pragma once
18 
19 #include <ftl/flags.h>
20 #include <input/DisplayViewport.h>
21 #include <input/InputDevice.h>
22 #include <input/PropertyMap.h>
23 
24 #include <cstdint>
25 #include <optional>
26 #include <unordered_map>
27 #include <vector>
28 
29 #include "EventHub.h"
30 #include "InputReaderBase.h"
31 #include "InputReaderContext.h"
32 #include "NotifyArgs.h"
33 
34 namespace android {
35 
36 class PeripheralController;
37 class PeripheralControllerInterface;
38 class InputDeviceContext;
39 class InputMapper;
40 
41 /* Represents the state of a single input device. */
42 class InputDevice {
43 public:
44     InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
45                 const InputDeviceIdentifier& identifier);
46     ~InputDevice();
47 
getContext()48     inline InputReaderContext* getContext() { return mContext; }
getId()49     inline int32_t getId() const { return mId; }
getControllerNumber()50     inline int32_t getControllerNumber() const { return mControllerNumber; }
getGeneration()51     inline int32_t getGeneration() const { return mGeneration; }
getName()52     inline const std::string getName() const { return mIdentifier.name; }
getDescriptor()53     inline const std::string getDescriptor() { return mIdentifier.descriptor; }
getBluetoothAddress()54     inline std::optional<std::string> getBluetoothAddress() const {
55         return mIdentifier.bluetoothAddress;
56     }
getLocation()57     inline const std::string getLocation() const { return mIdentifier.location; }
getClasses()58     inline ftl::Flags<InputDeviceClass> getClasses() const { return mClasses; }
getSources()59     inline uint32_t getSources() const { return mSources; }
hasEventHubDevices()60     inline bool hasEventHubDevices() const { return !mDevices.empty(); }
61 
isExternal()62     inline bool isExternal() { return mIsExternal; }
getAssociatedDisplayPort()63     inline std::optional<uint8_t> getAssociatedDisplayPort() const {
64         return mAssociatedDisplayPort;
65     }
getAssociatedDisplayUniqueId()66     inline std::optional<std::string> getAssociatedDisplayUniqueId() const {
67         return mAssociatedDisplayUniqueId;
68     }
getDeviceTypeAssociation()69     inline std::optional<std::string> getDeviceTypeAssociation() const {
70         return mAssociatedDeviceType;
71     }
getAssociatedViewport()72     inline std::optional<DisplayViewport> getAssociatedViewport() const {
73         return mAssociatedViewport;
74     }
hasMic()75     inline bool hasMic() const { return mHasMic; }
76 
isIgnored()77     inline bool isIgnored() { return !getMapperCount() && !mController; }
78 
79     bool isEnabled();
80     [[nodiscard]] std::list<NotifyArgs> setEnabled(bool enabled, nsecs_t when);
81 
82     void dump(std::string& dump, const std::string& eventHubDevStr);
83     void addEmptyEventHubDevice(int32_t eventHubId);
84     void addEventHubDevice(int32_t eventHubId, const InputReaderConfiguration& readerConfig);
85     void removeEventHubDevice(int32_t eventHubId);
86     [[nodiscard]] std::list<NotifyArgs> configure(nsecs_t when,
87                                                   const InputReaderConfiguration& readerConfig,
88                                                   ConfigurationChanges changes);
89     [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when);
90     [[nodiscard]] std::list<NotifyArgs> process(const RawEvent* rawEvents, size_t count);
91     [[nodiscard]] std::list<NotifyArgs> timeoutExpired(nsecs_t when);
92     [[nodiscard]] std::list<NotifyArgs> updateExternalStylusState(const StylusState& state);
93 
94     InputDeviceInfo getDeviceInfo();
95     int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
96     int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
97     int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
98     int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const;
99     bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
100                                uint8_t* outFlags);
101     [[nodiscard]] std::list<NotifyArgs> vibrate(const VibrationSequence& sequence, ssize_t repeat,
102                                                 int32_t token);
103     [[nodiscard]] std::list<NotifyArgs> cancelVibrate(int32_t token);
104     bool isVibrating();
105     std::vector<int32_t> getVibratorIds();
106     [[nodiscard]] std::list<NotifyArgs> cancelTouch(nsecs_t when, nsecs_t readTime);
107     bool enableSensor(InputDeviceSensorType sensorType, std::chrono::microseconds samplingPeriod,
108                       std::chrono::microseconds maxBatchReportLatency);
109     void disableSensor(InputDeviceSensorType sensorType);
110     void flushSensor(InputDeviceSensorType sensorType);
111 
112     std::optional<int32_t> getBatteryEventHubId() const;
113 
114     bool setLightColor(int32_t lightId, int32_t color);
115     bool setLightPlayerId(int32_t lightId, int32_t playerId);
116     std::optional<int32_t> getLightColor(int32_t lightId);
117     std::optional<int32_t> getLightPlayerId(int32_t lightId);
118 
119     int32_t getMetaState();
120     void updateMetaState(int32_t keyCode);
121 
122     void addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode);
123 
124     void bumpGeneration();
125 
126     [[nodiscard]] NotifyDeviceResetArgs notifyReset(nsecs_t when);
127 
getConfiguration()128     inline const PropertyMap& getConfiguration() { return mConfiguration; }
getEventHub()129     inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
130 
131     std::optional<int32_t> getAssociatedDisplayId();
132 
133     void updateLedState(bool reset);
134 
135     size_t getMapperCount();
136 
137     // construct and add a mapper to the input device
138     template <class T, typename... Args>
addMapper(int32_t eventHubId,Args...args)139     T& addMapper(int32_t eventHubId, Args... args) {
140         // ensure a device entry exists for this eventHubId
141         addEmptyEventHubDevice(eventHubId);
142 
143         // create mapper
144         auto& devicePair = mDevices[eventHubId];
145         auto& deviceContext = devicePair.first;
146         auto& mappers = devicePair.second;
147         T* mapper = new T(*deviceContext, args...);
148         mappers.emplace_back(mapper);
149         return *mapper;
150     }
151 
152     template <class T, typename... Args>
constructAndAddMapper(int32_t eventHubId,Args...args)153     T& constructAndAddMapper(int32_t eventHubId, Args... args) {
154         // create mapper
155         auto& devicePair = mDevices[eventHubId];
156         auto& deviceContext = devicePair.first;
157         auto& mappers = devicePair.second;
158         mappers.push_back(createInputMapper<T>(*deviceContext, args...));
159         return static_cast<T&>(*mappers.back());
160     }
161 
162     // construct and add a controller to the input device
163     template <class T>
addController(int32_t eventHubId)164     T& addController(int32_t eventHubId) {
165         // ensure a device entry exists for this eventHubId
166         addEmptyEventHubDevice(eventHubId);
167 
168         // create controller
169         auto& devicePair = mDevices[eventHubId];
170         auto& deviceContext = devicePair.first;
171 
172         mController = std::make_unique<T>(*deviceContext);
173         return *(reinterpret_cast<T*>(mController.get()));
174     }
175 
176 private:
177     InputReaderContext* mContext;
178     int32_t mId;
179     int32_t mGeneration;
180     int32_t mControllerNumber;
181     InputDeviceIdentifier mIdentifier;
182     std::string mAlias;
183     ftl::Flags<InputDeviceClass> mClasses;
184 
185     // map from eventHubId to device context and mappers
186     using MapperVector = std::vector<std::unique_ptr<InputMapper>>;
187     using DevicePair = std::pair<std::unique_ptr<InputDeviceContext>, MapperVector>;
188     // Map from EventHub ID to pair of device context and vector of mapper.
189     std::unordered_map<int32_t, DevicePair> mDevices;
190     // Misc devices controller for lights, battery, etc.
191     std::unique_ptr<PeripheralControllerInterface> mController;
192 
193     uint32_t mSources;
194     bool mIsExternal;
195     std::optional<uint8_t> mAssociatedDisplayPort;
196     std::optional<std::string> mAssociatedDisplayUniqueId;
197     std::optional<std::string> mAssociatedDeviceType;
198     std::optional<DisplayViewport> mAssociatedViewport;
199     bool mHasMic;
200     bool mDropUntilNextSync;
201 
202     typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
203     int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
204 
205     std::vector<std::unique_ptr<InputMapper>> createMappers(
206             InputDeviceContext& contextPtr, const InputReaderConfiguration& readerConfig);
207 
208     PropertyMap mConfiguration;
209 
210     // helpers to interate over the devices collection
211     // run a function against every mapper on every subdevice
for_each_mapper(std::function<void (InputMapper &)> f)212     inline void for_each_mapper(std::function<void(InputMapper&)> f) {
213         for (auto& deviceEntry : mDevices) {
214             auto& devicePair = deviceEntry.second;
215             auto& mappers = devicePair.second;
216             for (auto& mapperPtr : mappers) {
217                 f(*mapperPtr);
218             }
219         }
220     }
221 
222     // run a function against every mapper on a specific subdevice
for_each_mapper_in_subdevice(int32_t eventHubDevice,std::function<void (InputMapper &)> f)223     inline void for_each_mapper_in_subdevice(int32_t eventHubDevice,
224                                              std::function<void(InputMapper&)> f) {
225         auto deviceIt = mDevices.find(eventHubDevice);
226         if (deviceIt != mDevices.end()) {
227             auto& devicePair = deviceIt->second;
228             auto& mappers = devicePair.second;
229             for (auto& mapperPtr : mappers) {
230                 f(*mapperPtr);
231             }
232         }
233     }
234 
235     // run a function against every subdevice
for_each_subdevice(std::function<void (InputDeviceContext &)> f)236     inline void for_each_subdevice(std::function<void(InputDeviceContext&)> f) {
237         for (auto& deviceEntry : mDevices) {
238             auto& devicePair = deviceEntry.second;
239             auto& contextPtr = devicePair.first;
240             f(*contextPtr);
241         }
242     }
243 
244     // return the first value returned by a function over every mapper.
245     // if all mappers return nullopt, return nullopt.
246     template <typename T>
first_in_mappers(std::function<std::optional<T> (InputMapper &)> f)247     inline std::optional<T> first_in_mappers(
248             std::function<std::optional<T>(InputMapper&)> f) const {
249         for (auto& deviceEntry : mDevices) {
250             auto& devicePair = deviceEntry.second;
251             auto& mappers = devicePair.second;
252             for (auto& mapperPtr : mappers) {
253                 std::optional<T> ret = f(*mapperPtr);
254                 if (ret) {
255                     return ret;
256                 }
257             }
258         }
259         return std::nullopt;
260     }
261 };
262 
263 /* Provides access to EventHub methods, but limits access to the current InputDevice.
264  * Essentially an implementation of EventHubInterface, but for a specific device id.
265  * Helps hide implementation details of InputDevice and EventHub. Used by mappers to
266  * check the status of the associated hardware device
267  */
268 class InputDeviceContext {
269 public:
270     InputDeviceContext(InputDevice& device, int32_t eventHubId);
271     ~InputDeviceContext();
272 
getContext()273     inline InputReaderContext* getContext() { return mContext; }
getId()274     inline int32_t getId() { return mDeviceId; }
getEventHubId()275     inline int32_t getEventHubId() { return mId; }
276 
getDeviceClasses()277     inline ftl::Flags<InputDeviceClass> getDeviceClasses() const {
278         return mEventHub->getDeviceClasses(mId);
279     }
getDeviceIdentifier()280     inline InputDeviceIdentifier getDeviceIdentifier() const {
281         return mEventHub->getDeviceIdentifier(mId);
282     }
getDeviceControllerNumber()283     inline int32_t getDeviceControllerNumber() const {
284         return mEventHub->getDeviceControllerNumber(mId);
285     }
getAbsoluteAxisInfo(int32_t code,RawAbsoluteAxisInfo * axisInfo)286     inline status_t getAbsoluteAxisInfo(int32_t code, RawAbsoluteAxisInfo* axisInfo) const {
287         return mEventHub->getAbsoluteAxisInfo(mId, code, axisInfo);
288     }
hasRelativeAxis(int32_t code)289     inline bool hasRelativeAxis(int32_t code) const {
290         return mEventHub->hasRelativeAxis(mId, code);
291     }
hasInputProperty(int32_t property)292     inline bool hasInputProperty(int32_t property) const {
293         return mEventHub->hasInputProperty(mId, property);
294     }
295 
hasMscEvent(int mscEvent)296     inline bool hasMscEvent(int mscEvent) const { return mEventHub->hasMscEvent(mId, mscEvent); }
297 
addKeyRemapping(int32_t fromKeyCode,int32_t toKeyCode)298     inline void addKeyRemapping(int32_t fromKeyCode, int32_t toKeyCode) const {
299         mEventHub->addKeyRemapping(mId, fromKeyCode, toKeyCode);
300     }
301 
mapKey(int32_t scanCode,int32_t usageCode,int32_t metaState,int32_t * outKeycode,int32_t * outMetaState,uint32_t * outFlags)302     inline status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t metaState,
303                            int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const {
304         return mEventHub->mapKey(mId, scanCode, usageCode, metaState, outKeycode, outMetaState,
305                                  outFlags);
306     }
mapAxis(int32_t scanCode,AxisInfo * outAxisInfo)307     inline status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
308         return mEventHub->mapAxis(mId, scanCode, outAxisInfo);
309     }
mapSensor(int32_t absCode)310     inline base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t absCode) {
311         return mEventHub->mapSensor(mId, absCode);
312     }
313 
getRawLightIds()314     inline const std::vector<int32_t> getRawLightIds() { return mEventHub->getRawLightIds(mId); }
315 
getRawLightInfo(int32_t lightId)316     inline std::optional<RawLightInfo> getRawLightInfo(int32_t lightId) {
317         return mEventHub->getRawLightInfo(mId, lightId);
318     }
319 
getLightBrightness(int32_t lightId)320     inline std::optional<int32_t> getLightBrightness(int32_t lightId) {
321         return mEventHub->getLightBrightness(mId, lightId);
322     }
323 
setLightBrightness(int32_t lightId,int32_t brightness)324     inline void setLightBrightness(int32_t lightId, int32_t brightness) {
325         return mEventHub->setLightBrightness(mId, lightId, brightness);
326     }
327 
getLightIntensities(int32_t lightId)328     inline std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
329             int32_t lightId) {
330         return mEventHub->getLightIntensities(mId, lightId);
331     }
332 
setLightIntensities(int32_t lightId,std::unordered_map<LightColor,int32_t> intensities)333     inline void setLightIntensities(int32_t lightId,
334                                     std::unordered_map<LightColor, int32_t> intensities) {
335         return mEventHub->setLightIntensities(mId, lightId, intensities);
336     }
337 
getVideoFrames()338     inline std::vector<TouchVideoFrame> getVideoFrames() { return mEventHub->getVideoFrames(mId); }
getScanCodeState(int32_t scanCode)339     inline int32_t getScanCodeState(int32_t scanCode) const {
340         return mEventHub->getScanCodeState(mId, scanCode);
341     }
getKeyCodeState(int32_t keyCode)342     inline int32_t getKeyCodeState(int32_t keyCode) const {
343         return mEventHub->getKeyCodeState(mId, keyCode);
344     }
getKeyCodeForKeyLocation(int32_t locationKeyCode)345     inline int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const {
346         return mEventHub->getKeyCodeForKeyLocation(mId, locationKeyCode);
347     }
getSwitchState(int32_t sw)348     inline int32_t getSwitchState(int32_t sw) const { return mEventHub->getSwitchState(mId, sw); }
getAbsoluteAxisValue(int32_t code,int32_t * outValue)349     inline status_t getAbsoluteAxisValue(int32_t code, int32_t* outValue) const {
350         return mEventHub->getAbsoluteAxisValue(mId, code, outValue);
351     }
markSupportedKeyCodes(const std::vector<int32_t> & keyCodes,uint8_t * outFlags)352     inline bool markSupportedKeyCodes(const std::vector<int32_t>& keyCodes,
353                                       uint8_t* outFlags) const {
354         return mEventHub->markSupportedKeyCodes(mId, keyCodes, outFlags);
355     }
hasScanCode(int32_t scanCode)356     inline bool hasScanCode(int32_t scanCode) const {
357         return mEventHub->hasScanCode(mId, scanCode);
358     }
hasKeyCode(int32_t keyCode)359     inline bool hasKeyCode(int32_t keyCode) const { return mEventHub->hasKeyCode(mId, keyCode); }
hasLed(int32_t led)360     inline bool hasLed(int32_t led) const { return mEventHub->hasLed(mId, led); }
setLedState(int32_t led,bool on)361     inline void setLedState(int32_t led, bool on) { return mEventHub->setLedState(mId, led, on); }
getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition> & outVirtualKeys)362     inline void getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
363         return mEventHub->getVirtualKeyDefinitions(mId, outVirtualKeys);
364     }
getKeyCharacterMap()365     inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const {
366         return mEventHub->getKeyCharacterMap(mId);
367     }
setKeyboardLayoutOverlay(std::shared_ptr<KeyCharacterMap> map)368     inline bool setKeyboardLayoutOverlay(std::shared_ptr<KeyCharacterMap> map) {
369         return mEventHub->setKeyboardLayoutOverlay(mId, map);
370     }
getRawLayoutInfo()371     inline const std::optional<RawLayoutInfo> getRawLayoutInfo() {
372         return mEventHub->getRawLayoutInfo(mId);
373     }
vibrate(const VibrationElement & element)374     inline void vibrate(const VibrationElement& element) {
375         return mEventHub->vibrate(mId, element);
376     }
cancelVibrate()377     inline void cancelVibrate() { return mEventHub->cancelVibrate(mId); }
378 
getVibratorIds()379     inline std::vector<int32_t> getVibratorIds() { return mEventHub->getVibratorIds(mId); }
380 
getRawBatteryIds()381     inline const std::vector<int32_t> getRawBatteryIds() {
382         return mEventHub->getRawBatteryIds(mId);
383     }
384 
getRawBatteryInfo(int32_t batteryId)385     inline std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t batteryId) {
386         return mEventHub->getRawBatteryInfo(mId, batteryId);
387     }
388 
getBatteryCapacity(int32_t batteryId)389     inline std::optional<int32_t> getBatteryCapacity(int32_t batteryId) {
390         return mEventHub->getBatteryCapacity(mId, batteryId);
391     }
392 
getBatteryStatus(int32_t batteryId)393     inline std::optional<int32_t> getBatteryStatus(int32_t batteryId) {
394         return mEventHub->getBatteryStatus(mId, batteryId);
395     }
396 
hasAbsoluteAxis(int32_t code)397     inline bool hasAbsoluteAxis(int32_t code) const {
398         RawAbsoluteAxisInfo info;
399         mEventHub->getAbsoluteAxisInfo(mId, code, &info);
400         return info.valid;
401     }
isKeyPressed(int32_t scanCode)402     inline bool isKeyPressed(int32_t scanCode) const {
403         return mEventHub->getScanCodeState(mId, scanCode) == AKEY_STATE_DOWN;
404     }
isKeyCodePressed(int32_t keyCode)405     inline bool isKeyCodePressed(int32_t keyCode) const {
406         return mEventHub->getKeyCodeState(mId, keyCode) == AKEY_STATE_DOWN;
407     }
getAbsoluteAxisValue(int32_t code)408     inline int32_t getAbsoluteAxisValue(int32_t code) const {
409         int32_t value;
410         mEventHub->getAbsoluteAxisValue(mId, code, &value);
411         return value;
412     }
isDeviceEnabled()413     inline bool isDeviceEnabled() { return mEventHub->isDeviceEnabled(mId); }
enableDevice()414     inline status_t enableDevice() { return mEventHub->enableDevice(mId); }
disableDevice()415     inline status_t disableDevice() { return mEventHub->disableDevice(mId); }
416 
getName()417     inline const std::string getName() const { return mDevice.getName(); }
getDescriptor()418     inline const std::string getDescriptor() { return mDevice.getDescriptor(); }
getLocation()419     inline const std::string getLocation() { return mDevice.getLocation(); }
isExternal()420     inline bool isExternal() const { return mDevice.isExternal(); }
getAssociatedDisplayPort()421     inline std::optional<uint8_t> getAssociatedDisplayPort() const {
422         return mDevice.getAssociatedDisplayPort();
423     }
getAssociatedDisplayUniqueId()424     inline std::optional<std::string> getAssociatedDisplayUniqueId() const {
425         return mDevice.getAssociatedDisplayUniqueId();
426     }
getDeviceTypeAssociation()427     inline std::optional<std::string> getDeviceTypeAssociation() const {
428         return mDevice.getDeviceTypeAssociation();
429     }
getAssociatedViewport()430     inline std::optional<DisplayViewport> getAssociatedViewport() const {
431         return mDevice.getAssociatedViewport();
432     }
cancelTouch(nsecs_t when,nsecs_t readTime)433     [[nodiscard]] inline std::list<NotifyArgs> cancelTouch(nsecs_t when, nsecs_t readTime) {
434         return mDevice.cancelTouch(when, readTime);
435     }
bumpGeneration()436     inline void bumpGeneration() { mDevice.bumpGeneration(); }
getConfiguration()437     inline const PropertyMap& getConfiguration() const { return mDevice.getConfiguration(); }
438 
439 private:
440     InputDevice& mDevice;
441     InputReaderContext* mContext;
442     EventHubInterface* mEventHub;
443     int32_t mId;
444     int32_t mDeviceId;
445 };
446 
447 } // namespace android
448