• 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 #ifndef _UI_INPUTREADER_INPUT_MAPPER_H
18 #define _UI_INPUTREADER_INPUT_MAPPER_H
19 
20 #include "EventHub.h"
21 #include "InputDevice.h"
22 #include "InputListener.h"
23 #include "InputReaderContext.h"
24 #include "StylusState.h"
25 #include "VibrationElement.h"
26 
27 namespace android {
28 
29 /* An input mapper transforms raw input events into cooked event data.
30  * A single input device can have multiple associated input mappers in order to interpret
31  * different classes of events.
32  *
33  * InputMapper lifecycle:
34  * - create
35  * - configure with 0 changes
36  * - reset
37  * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
38  * - reset
39  * - destroy
40  */
41 class InputMapper {
42 public:
43     explicit InputMapper(InputDeviceContext& deviceContext);
44     virtual ~InputMapper();
45 
getDeviceId()46     inline int32_t getDeviceId() { return mDeviceContext.getId(); }
getDeviceContext()47     inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
getDeviceContext()48     inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; };
getDeviceName()49     inline const std::string getDeviceName() const { return mDeviceContext.getName(); }
getContext()50     inline InputReaderContext* getContext() { return mDeviceContext.getContext(); }
getPolicy()51     inline InputReaderPolicyInterface* getPolicy() { return getContext()->getPolicy(); }
getListener()52     inline InputListenerInterface& getListener() { return getContext()->getListener(); }
53 
54     virtual uint32_t getSources() const = 0;
55     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
56     virtual void dump(std::string& dump);
57     virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
58     virtual void reset(nsecs_t when);
59     virtual void process(const RawEvent* rawEvent) = 0;
60     virtual void timeoutExpired(nsecs_t when);
61 
62     virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
63     virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
64     virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
65     virtual int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const;
66 
67     virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
68                                        const int32_t* keyCodes, uint8_t* outFlags);
69     virtual void vibrate(const VibrationSequence& sequence, ssize_t repeat, int32_t token);
70     virtual void cancelVibrate(int32_t token);
71     virtual bool isVibrating();
72     virtual std::vector<int32_t> getVibratorIds();
73     virtual void cancelTouch(nsecs_t when, nsecs_t readTime);
74     virtual bool enableSensor(InputDeviceSensorType sensorType,
75                               std::chrono::microseconds samplingPeriod,
76                               std::chrono::microseconds maxBatchReportLatency);
77     virtual void disableSensor(InputDeviceSensorType sensorType);
78     virtual void flushSensor(InputDeviceSensorType sensorType);
79 
getBatteryCapacity()80     virtual std::optional<int32_t> getBatteryCapacity() { return std::nullopt; }
getBatteryStatus()81     virtual std::optional<int32_t> getBatteryStatus() { return std::nullopt; }
82 
setLightColor(int32_t lightId,int32_t color)83     virtual bool setLightColor(int32_t lightId, int32_t color) { return true; }
setLightPlayerId(int32_t lightId,int32_t playerId)84     virtual bool setLightPlayerId(int32_t lightId, int32_t playerId) { return true; }
getLightColor(int32_t lightId)85     virtual std::optional<int32_t> getLightColor(int32_t lightId) { return std::nullopt; }
getLightPlayerId(int32_t lightId)86     virtual std::optional<int32_t> getLightPlayerId(int32_t lightId) { return std::nullopt; }
87 
88     virtual int32_t getMetaState();
89     /**
90      * Process the meta key and update the global meta state when changed.
91      * Return true if the meta key could be handled by the InputMapper.
92      */
93     virtual bool updateMetaState(int32_t keyCode);
94 
95     virtual void updateExternalStylusState(const StylusState& state);
96 
getAssociatedDisplayId()97     virtual std::optional<int32_t> getAssociatedDisplayId() { return std::nullopt; }
updateLedState(bool reset)98     virtual void updateLedState(bool reset) {}
99 
100 protected:
101     InputDeviceContext& mDeviceContext;
102 
103     status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
104     void bumpGeneration();
105 
106     static void dumpRawAbsoluteAxisInfo(std::string& dump, const RawAbsoluteAxisInfo& axis,
107                                         const char* name);
108     static void dumpStylusState(std::string& dump, const StylusState& state);
109 };
110 
111 } // namespace android
112 
113 #endif // _UI_INPUTREADER_INPUT_MAPPER_H
114