Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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 | |
Prabir Pradhan | 4810866 | 2022-09-09 21:22:04 +0000 | [diff] [blame] | 17 | #pragma once |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 18 | |
| 19 | #include "EventHub.h" |
| 20 | #include "InputDevice.h" |
Siarhei Vishniakou | f2f073b | 2021-02-09 21:59:56 +0000 | [diff] [blame] | 21 | #include "InputListener.h" |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 22 | #include "InputReaderContext.h" |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 23 | #include "NotifyArgs.h" |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 24 | #include "StylusState.h" |
Nathaniel R. Lewis | cacd69a | 2019-08-12 22:07:00 +0000 | [diff] [blame] | 25 | #include "VibrationElement.h" |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
Arpit Singh | a8c236b | 2023-04-25 13:56:05 +0000 | [diff] [blame] | 28 | /** |
| 29 | * This is the factory method that must be used to create any InputMapper |
| 30 | */ |
| 31 | template <class T, class... 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 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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: |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 48 | * - create and configure with 0 changes |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 49 | * - reset |
Arpit Singh | 7c6712d | 2023-04-11 14:27:13 +0000 | [diff] [blame] | 50 | * - process, process, process (may occasionally reconfigure or reset) |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 51 | * - reset |
| 52 | * - destroy |
| 53 | */ |
| 54 | class InputMapper { |
| 55 | public: |
Arpit Singh | a8c236b | 2023-04-25 13:56:05 +0000 | [diff] [blame] | 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 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 65 | virtual ~InputMapper(); |
| 66 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 67 | inline int32_t getDeviceId() { return mDeviceContext.getId(); } |
| 68 | inline InputDeviceContext& getDeviceContext() { return mDeviceContext; } |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 69 | inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }; |
Siarhei Vishniakou | 12c0fcb | 2021-12-17 13:40:44 -0800 | [diff] [blame] | 70 | inline const std::string getDeviceName() const { return mDeviceContext.getName(); } |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 71 | inline InputReaderContext* getContext() { return mDeviceContext.getContext(); } |
| 72 | inline InputReaderPolicyInterface* getPolicy() { return getContext()->getPolicy(); } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 73 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 74 | virtual uint32_t getSources() const = 0; |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 75 | virtual void populateDeviceInfo(InputDeviceInfo& deviceInfo); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 76 | virtual void dump(std::string& dump); |
Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 77 | [[nodiscard]] virtual std::list<NotifyArgs> reconfigure(nsecs_t when, |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 78 | const InputReaderConfiguration& config, |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 79 | ConfigurationChanges changes); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 80 | [[nodiscard]] virtual std::list<NotifyArgs> reset(nsecs_t when); |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 81 | [[nodiscard]] virtual std::list<NotifyArgs> process(const RawEvent& rawEvent) = 0; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 82 | [[nodiscard]] virtual std::list<NotifyArgs> timeoutExpired(nsecs_t when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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); |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 87 | virtual int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const; |
| 88 | |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 89 | virtual bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes, |
| 90 | uint8_t* outFlags); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 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); |
Chris Ye | 8714371 | 2020-11-10 05:05:58 +0000 | [diff] [blame] | 94 | virtual bool isVibrating(); |
| 95 | virtual std::vector<int32_t> getVibratorIds(); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 96 | [[nodiscard]] virtual std::list<NotifyArgs> cancelTouch(nsecs_t when, nsecs_t readTime); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 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); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 102 | |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame] | 103 | virtual std::optional<int32_t> getBatteryCapacity() { return std::nullopt; } |
| 104 | virtual std::optional<int32_t> getBatteryStatus() { return std::nullopt; } |
| 105 | |
Chris Ye | 3fdbfef | 2021-01-06 18:45:18 -0800 | [diff] [blame] | 106 | virtual bool setLightColor(int32_t lightId, int32_t color) { return true; } |
| 107 | virtual bool setLightPlayerId(int32_t lightId, int32_t playerId) { return true; } |
| 108 | virtual std::optional<int32_t> getLightColor(int32_t lightId) { return std::nullopt; } |
| 109 | virtual std::optional<int32_t> getLightPlayerId(int32_t lightId) { return std::nullopt; } |
| 110 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 111 | virtual int32_t getMetaState(); |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 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); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 117 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 118 | [[nodiscard]] virtual std::list<NotifyArgs> updateExternalStylusState(const StylusState& state); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 119 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 120 | virtual std::optional<ui::LogicalDisplayId> getAssociatedDisplayId() { return std::nullopt; } |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 121 | virtual void updateLedState(bool reset) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 122 | |
| 123 | protected: |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 124 | InputDeviceContext& mDeviceContext; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 125 | |
Arpit Singh | a8c236b | 2023-04-25 13:56:05 +0000 | [diff] [blame] | 126 | explicit InputMapper(InputDeviceContext& deviceContext, |
| 127 | const InputReaderConfiguration& readerConfig); |
| 128 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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 |