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() { 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(); }
getPolicy()74 inline InputReaderPolicyInterface* getPolicy() { return getContext()->getPolicy(); }
75
76 virtual uint32_t getSources() const = 0;
77 virtual void populateDeviceInfo(InputDeviceInfo& deviceInfo);
78 virtual void dump(std::string& dump);
79 [[nodiscard]] virtual std::list<NotifyArgs> reconfigure(nsecs_t when,
80 const InputReaderConfiguration& config,
81 ConfigurationChanges changes);
82 [[nodiscard]] virtual std::list<NotifyArgs> reset(nsecs_t when);
83 [[nodiscard]] virtual std::list<NotifyArgs> process(const RawEvent& rawEvent) = 0;
84 [[nodiscard]] virtual std::list<NotifyArgs> timeoutExpired(nsecs_t when);
85
86 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
87 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
88 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
89 virtual int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const;
90
91 virtual bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
92 uint8_t* outFlags);
93 [[nodiscard]] virtual std::list<NotifyArgs> vibrate(const VibrationSequence& sequence,
94 ssize_t repeat, int32_t token);
95 [[nodiscard]] virtual std::list<NotifyArgs> cancelVibrate(int32_t token);
96 virtual bool isVibrating();
97 virtual std::vector<int32_t> getVibratorIds();
98 [[nodiscard]] virtual std::list<NotifyArgs> cancelTouch(nsecs_t when, nsecs_t readTime);
99 virtual bool enableSensor(InputDeviceSensorType sensorType,
100 std::chrono::microseconds samplingPeriod,
101 std::chrono::microseconds maxBatchReportLatency);
102 virtual void disableSensor(InputDeviceSensorType sensorType);
103 virtual void flushSensor(InputDeviceSensorType sensorType);
104
getBatteryCapacity()105 virtual std::optional<int32_t> getBatteryCapacity() { return std::nullopt; }
getBatteryStatus()106 virtual std::optional<int32_t> getBatteryStatus() { return std::nullopt; }
107
setLightColor(int32_t lightId,int32_t color)108 virtual bool setLightColor(int32_t lightId, int32_t color) { return true; }
setLightPlayerId(int32_t lightId,int32_t playerId)109 virtual bool setLightPlayerId(int32_t lightId, int32_t playerId) { return true; }
getLightColor(int32_t lightId)110 virtual std::optional<int32_t> getLightColor(int32_t lightId) { return std::nullopt; }
getLightPlayerId(int32_t lightId)111 virtual std::optional<int32_t> getLightPlayerId(int32_t lightId) { return std::nullopt; }
112
113 virtual int32_t getMetaState();
114
115 [[nodiscard]] virtual std::list<NotifyArgs> updateExternalStylusState(const StylusState& state);
116
getAssociatedDisplayId()117 virtual std::optional<ui::LogicalDisplayId> getAssociatedDisplayId() { return std::nullopt; }
updateLedState(bool reset)118 virtual void updateLedState(bool reset) {}
119
120 virtual std::optional<HardwareProperties> getTouchpadHardwareProperties();
121
122 protected:
123 InputDeviceContext& mDeviceContext;
124
125 explicit InputMapper(InputDeviceContext& deviceContext,
126 const InputReaderConfiguration& readerConfig);
127
128 std::optional<RawAbsoluteAxisInfo> getAbsoluteAxisInfo(int32_t axis);
129 void bumpGeneration();
130
131 static void dumpRawAbsoluteAxisInfo(std::string& dump,
132 const std::optional<RawAbsoluteAxisInfo>& axis,
133 const char* name);
134 static void dumpStylusState(std::string& dump, const StylusState& state);
135 };
136
137 } // namespace android
138