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 "EventHub.h"
20 #include "InputDevice.h"
21 #include "InputListener.h"
22 #include "InputReaderContext.h"
23 #include "NotifyArgs.h"
24 #include "StylusState.h"
25 #include "VibrationElement.h"
26 
27 namespace android {
28 /**
29  * This is the factory method that must be used to create any InputMapper
30  */
31 template <class T, class... Args>
createInputMapper(InputDeviceContext & deviceContext,const InputReaderConfiguration & readerConfig,Args...args)32 std::unique_ptr<T> createInputMapper(InputDeviceContext& deviceContext,
33                                      const InputReaderConfiguration& readerConfig, Args... args) {
34     // Using `new` to access non-public constructors.
35     std::unique_ptr<T> mapper(new T(deviceContext, readerConfig, args...));
36     // We need to reset and configure the mapper to ensure it is ready to process event
37     nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
38     std::list<NotifyArgs> unused = mapper->reset(now);
39     unused += mapper->reconfigure(now, readerConfig, /*changes=*/{});
40     return mapper;
41 }
42 
43 /* An input mapper transforms raw input events into cooked event data.
44  * A single input device can have multiple associated input mappers in order to interpret
45  * different classes of events.
46  *
47  * InputMapper lifecycle:
48  * - create and configure with 0 changes
49  * - reset
50  * - process, process, process (may occasionally reconfigure or reset)
51  * - reset
52  * - destroy
53  */
54 class InputMapper {
55 public:
56     /**
57      * Subclasses must either provide a public constructor
58      * or must be-friend the factory method.
59      */
60     template <class T, class... Args>
61     friend std::unique_ptr<T> createInputMapper(InputDeviceContext& deviceContext,
62                                                 const InputReaderConfiguration& readerConfig,
63                                                 Args... args);
64 
65     virtual ~InputMapper();
66 
getDeviceId()67     inline int32_t getDeviceId() { return mDeviceContext.getId(); }
getDeviceContext()68     inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
getDeviceContext()69     inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; };
getDeviceName()70     inline const std::string getDeviceName() const { return mDeviceContext.getName(); }
getContext()71     inline InputReaderContext* getContext() { return mDeviceContext.getContext(); }
getPolicy()72     inline InputReaderPolicyInterface* getPolicy() { return getContext()->getPolicy(); }
73 
74     virtual uint32_t getSources() const = 0;
75     virtual void populateDeviceInfo(InputDeviceInfo& deviceInfo);
76     virtual void dump(std::string& dump);
77     [[nodiscard]] virtual std::list<NotifyArgs> reconfigure(nsecs_t when,
78                                                             const InputReaderConfiguration& config,
79                                                             ConfigurationChanges changes);
80     [[nodiscard]] virtual std::list<NotifyArgs> reset(nsecs_t when);
81     [[nodiscard]] virtual std::list<NotifyArgs> process(const RawEvent& rawEvent) = 0;
82     [[nodiscard]] virtual std::list<NotifyArgs> timeoutExpired(nsecs_t when);
83 
84     virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
85     virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
86     virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
87     virtual int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const;
88 
89     virtual bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
90                                        uint8_t* outFlags);
91     [[nodiscard]] virtual std::list<NotifyArgs> vibrate(const VibrationSequence& sequence,
92                                                         ssize_t repeat, int32_t token);
93     [[nodiscard]] virtual std::list<NotifyArgs> cancelVibrate(int32_t token);
94     virtual bool isVibrating();
95     virtual std::vector<int32_t> getVibratorIds();
96     [[nodiscard]] virtual std::list<NotifyArgs> cancelTouch(nsecs_t when, nsecs_t readTime);
97     virtual bool enableSensor(InputDeviceSensorType sensorType,
98                               std::chrono::microseconds samplingPeriod,
99                               std::chrono::microseconds maxBatchReportLatency);
100     virtual void disableSensor(InputDeviceSensorType sensorType);
101     virtual void flushSensor(InputDeviceSensorType sensorType);
102 
getBatteryCapacity()103     virtual std::optional<int32_t> getBatteryCapacity() { return std::nullopt; }
getBatteryStatus()104     virtual std::optional<int32_t> getBatteryStatus() { return std::nullopt; }
105 
setLightColor(int32_t lightId,int32_t color)106     virtual bool setLightColor(int32_t lightId, int32_t color) { return true; }
setLightPlayerId(int32_t lightId,int32_t playerId)107     virtual bool setLightPlayerId(int32_t lightId, int32_t playerId) { return true; }
getLightColor(int32_t lightId)108     virtual std::optional<int32_t> getLightColor(int32_t lightId) { return std::nullopt; }
getLightPlayerId(int32_t lightId)109     virtual std::optional<int32_t> getLightPlayerId(int32_t lightId) { return std::nullopt; }
110 
111     virtual int32_t getMetaState();
112     /**
113      * Process the meta key and update the global meta state when changed.
114      * Return true if the meta key could be handled by the InputMapper.
115      */
116     virtual bool updateMetaState(int32_t keyCode);
117 
118     [[nodiscard]] virtual std::list<NotifyArgs> updateExternalStylusState(const StylusState& state);
119 
getAssociatedDisplayId()120     virtual std::optional<ui::LogicalDisplayId> getAssociatedDisplayId() { return std::nullopt; }
updateLedState(bool reset)121     virtual void updateLedState(bool reset) {}
122 
123 protected:
124     InputDeviceContext& mDeviceContext;
125 
126     explicit InputMapper(InputDeviceContext& deviceContext,
127                          const InputReaderConfiguration& readerConfig);
128 
129     status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
130     void bumpGeneration();
131 
132     static void dumpRawAbsoluteAxisInfo(std::string& dump, const RawAbsoluteAxisInfo& axis,
133                                         const char* name);
134     static void dumpStylusState(std::string& dump, const StylusState& state);
135 };
136 
137 } // namespace android
138