blob: 75cc4bb410f5865983cfe532e7576eaa366f04b0 [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
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 Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070018
Prabir Pradhan132f21c2024-07-25 16:48:30 +000019#include <optional>
20
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070021#include "EventHub.h"
22#include "InputDevice.h"
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +000023#include "InputListener.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070024#include "InputReaderContext.h"
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070025#include "NotifyArgs.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070026#include "StylusState.h"
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000027#include "VibrationElement.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070028
29namespace android {
Arpit Singha8c236b2023-04-25 13:56:05 +000030/**
31 * This is the factory method that must be used to create any InputMapper
32 */
33template <class T, class... Args>
34std::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}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070044
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:
Arpit Singh8e6fb252023-04-06 11:49:17 +000050 * - create and configure with 0 changes
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070051 * - reset
Arpit Singh7c6712d2023-04-11 14:27:13 +000052 * - process, process, process (may occasionally reconfigure or reset)
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070053 * - reset
54 * - destroy
55 */
56class InputMapper {
57public:
Arpit Singha8c236b2023-04-25 13:56:05 +000058 /**
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
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070067 virtual ~InputMapper();
68
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080069 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
70 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Philip Junker4af3b3d2021-12-14 10:36:55 +010071 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; };
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -080072 inline const std::string getDeviceName() const { return mDeviceContext.getName(); }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080073 inline InputReaderContext* getContext() { return mDeviceContext.getContext(); }
74 inline InputReaderPolicyInterface* getPolicy() { return getContext()->getPolicy(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070075
Philip Junker4af3b3d2021-12-14 10:36:55 +010076 virtual uint32_t getSources() const = 0;
Harry Cuttsd02ea102023-03-17 18:21:30 +000077 virtual void populateDeviceInfo(InputDeviceInfo& deviceInfo);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070078 virtual void dump(std::string& dump);
Arpit Singh4be4eef2023-03-28 14:26:01 +000079 [[nodiscard]] virtual std::list<NotifyArgs> reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +000080 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000081 ConfigurationChanges changes);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070082 [[nodiscard]] virtual std::list<NotifyArgs> reset(nsecs_t when);
Harry Cuttsa32a1192024-06-04 15:10:31 +000083 [[nodiscard]] virtual std::list<NotifyArgs> process(const RawEvent& rawEvent) = 0;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070084 [[nodiscard]] virtual std::list<NotifyArgs> timeoutExpired(nsecs_t when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070085
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);
Philip Junker4af3b3d2021-12-14 10:36:55 +010089 virtual int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const;
90
Siarhei Vishniakou74007942022-06-13 13:57:47 -070091 virtual bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
92 uint8_t* outFlags);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070093 [[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);
Chris Ye87143712020-11-10 05:05:58 +000096 virtual bool isVibrating();
97 virtual std::vector<int32_t> getVibratorIds();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070098 [[nodiscard]] virtual std::list<NotifyArgs> cancelTouch(nsecs_t when, nsecs_t readTime);
Chris Yef59a2f42020-10-16 12:55:26 -070099 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);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700104
Kim Low03ea0352020-11-06 12:45:07 -0800105 virtual std::optional<int32_t> getBatteryCapacity() { return std::nullopt; }
106 virtual std::optional<int32_t> getBatteryStatus() { return std::nullopt; }
107
Chris Ye3fdbfef2021-01-06 18:45:18 -0800108 virtual bool setLightColor(int32_t lightId, int32_t color) { return true; }
109 virtual bool setLightPlayerId(int32_t lightId, int32_t playerId) { return true; }
110 virtual std::optional<int32_t> getLightColor(int32_t lightId) { return std::nullopt; }
111 virtual std::optional<int32_t> getLightPlayerId(int32_t lightId) { return std::nullopt; }
112
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700113 virtual int32_t getMetaState();
Arthur Hungcb40a002021-08-03 14:31:01 +0000114 /**
115 * Process the meta key and update the global meta state when changed.
116 * Return true if the meta key could be handled by the InputMapper.
117 */
118 virtual bool updateMetaState(int32_t keyCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700119
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700120 [[nodiscard]] virtual std::list<NotifyArgs> updateExternalStylusState(const StylusState& state);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700121
Linnan Li13bf76a2024-05-05 19:18:02 +0800122 virtual std::optional<ui::LogicalDisplayId> getAssociatedDisplayId() { return std::nullopt; }
arthurhungc903df12020-08-11 15:08:42 +0800123 virtual void updateLedState(bool reset) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700124
Omar Abdelmonem5e70e962024-08-06 09:38:42 +0000125 virtual std::optional<HardwareProperties> getTouchpadHardwareProperties();
126
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700127protected:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800128 InputDeviceContext& mDeviceContext;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700129
Arpit Singha8c236b2023-04-25 13:56:05 +0000130 explicit InputMapper(InputDeviceContext& deviceContext,
131 const InputReaderConfiguration& readerConfig);
132
Prabir Pradhan132f21c2024-07-25 16:48:30 +0000133 std::optional<RawAbsoluteAxisInfo> getAbsoluteAxisInfo(int32_t axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700134 void bumpGeneration();
135
Prabir Pradhan132f21c2024-07-25 16:48:30 +0000136 static void dumpRawAbsoluteAxisInfo(std::string& dump,
137 const std::optional<RawAbsoluteAxisInfo>& axis,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700138 const char* name);
139 static void dumpStylusState(std::string& dump, const StylusState& state);
140};
141
142} // namespace android