blob: 5567cac6a9309023099c655cbb7bf885b39e3b2b [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
19#include "EventHub.h"
20#include "InputDevice.h"
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +000021#include "InputListener.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070022#include "InputReaderContext.h"
23#include "StylusState.h"
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000024#include "VibrationElement.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070025
26namespace android {
27
28/* An input mapper transforms raw input events into cooked event data.
29 * A single input device can have multiple associated input mappers in order to interpret
30 * different classes of events.
31 *
32 * InputMapper lifecycle:
33 * - create
34 * - configure with 0 changes
35 * - reset
36 * - process, process, process (may occasionally reconfigure with non-zero changes or reset)
37 * - reset
38 * - destroy
39 */
40class InputMapper {
41public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080042 explicit InputMapper(InputDeviceContext& deviceContext);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070043 virtual ~InputMapper();
44
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080045 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
46 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Philip Junker4af3b3d2021-12-14 10:36:55 +010047 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; };
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -080048 inline const std::string getDeviceName() const { return mDeviceContext.getName(); }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080049 inline InputReaderContext* getContext() { return mDeviceContext.getContext(); }
50 inline InputReaderPolicyInterface* getPolicy() { return getContext()->getPolicy(); }
Siarhei Vishniakou18050092021-09-01 13:32:49 -070051 inline InputListenerInterface& getListener() { return getContext()->getListener(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052
Philip Junker4af3b3d2021-12-14 10:36:55 +010053 virtual uint32_t getSources() const = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070054 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
55 virtual void dump(std::string& dump);
56 virtual void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
57 virtual void reset(nsecs_t when);
58 virtual void process(const RawEvent* rawEvent) = 0;
59 virtual void timeoutExpired(nsecs_t when);
60
61 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
62 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
63 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
Philip Junker4af3b3d2021-12-14 10:36:55 +010064 virtual int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const;
65
Siarhei Vishniakou74007942022-06-13 13:57:47 -070066 virtual bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
67 uint8_t* outFlags);
Chris Ye87143712020-11-10 05:05:58 +000068 virtual void vibrate(const VibrationSequence& sequence, ssize_t repeat, int32_t token);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070069 virtual void cancelVibrate(int32_t token);
Chris Ye87143712020-11-10 05:05:58 +000070 virtual bool isVibrating();
71 virtual std::vector<int32_t> getVibratorIds();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000072 virtual void cancelTouch(nsecs_t when, nsecs_t readTime);
Chris Yef59a2f42020-10-16 12:55:26 -070073 virtual bool enableSensor(InputDeviceSensorType sensorType,
74 std::chrono::microseconds samplingPeriod,
75 std::chrono::microseconds maxBatchReportLatency);
76 virtual void disableSensor(InputDeviceSensorType sensorType);
77 virtual void flushSensor(InputDeviceSensorType sensorType);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070078
Kim Low03ea0352020-11-06 12:45:07 -080079 virtual std::optional<int32_t> getBatteryCapacity() { return std::nullopt; }
80 virtual std::optional<int32_t> getBatteryStatus() { return std::nullopt; }
81
Chris Ye3fdbfef2021-01-06 18:45:18 -080082 virtual bool setLightColor(int32_t lightId, int32_t color) { return true; }
83 virtual bool setLightPlayerId(int32_t lightId, int32_t playerId) { return true; }
84 virtual std::optional<int32_t> getLightColor(int32_t lightId) { return std::nullopt; }
85 virtual std::optional<int32_t> getLightPlayerId(int32_t lightId) { return std::nullopt; }
86
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070087 virtual int32_t getMetaState();
Arthur Hungcb40a002021-08-03 14:31:01 +000088 /**
89 * Process the meta key and update the global meta state when changed.
90 * Return true if the meta key could be handled by the InputMapper.
91 */
92 virtual bool updateMetaState(int32_t keyCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070093
94 virtual void updateExternalStylusState(const StylusState& state);
95
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070096 virtual std::optional<int32_t> getAssociatedDisplayId() { return std::nullopt; }
arthurhungc903df12020-08-11 15:08:42 +080097 virtual void updateLedState(bool reset) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098
99protected:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800100 InputDeviceContext& mDeviceContext;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700101
102 status_t getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo);
103 void bumpGeneration();
104
105 static void dumpRawAbsoluteAxisInfo(std::string& dump, const RawAbsoluteAxisInfo& axis,
106 const char* name);
107 static void dumpStylusState(std::string& dump, const StylusState& state);
108};
109
110} // namespace android