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