blob: 49673a2f4e70ff0b87be05cadbb2474036d6700e [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 "InputMapper.h"
20
21namespace android {
22
23class JoystickInputMapper : public InputMapper {
24public:
Arpit Singh8e6fb252023-04-06 11:49:17 +000025 explicit JoystickInputMapper(InputDeviceContext& deviceContext,
26 const InputReaderConfiguration& readerConfig);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070027 virtual ~JoystickInputMapper();
28
Philip Junker4af3b3d2021-12-14 10:36:55 +010029 virtual uint32_t getSources() const override;
Harry Cuttsd02ea102023-03-17 18:21:30 +000030 virtual void populateDeviceInfo(InputDeviceInfo& deviceInfo) override;
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070031 virtual void dump(std::string& dump) override;
Arpit Singh4be4eef2023-03-28 14:26:01 +000032 [[nodiscard]] std::list<NotifyArgs> reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +000033 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000034 ConfigurationChanges changes) override;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070035 [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override;
36 [[nodiscard]] std::list<NotifyArgs> process(const RawEvent* rawEvent) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070037
38private:
39 struct Axis {
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -050040 explicit Axis(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
41 bool explicitlyMapped, float scale, float offset, float highScale,
42 float highOffset, float min, float max, float flat, float fuzz,
43 float resolution, float filter)
44 : rawAxisInfo(rawAxisInfo),
45 axisInfo(axisInfo),
46 explicitlyMapped(explicitlyMapped),
47 scale(scale),
48 offset(offset),
49 highScale(highScale),
50 highOffset(highOffset),
51 min(min),
52 max(max),
53 flat(flat),
54 fuzz(fuzz),
55 resolution(resolution),
56 filter(filter) {
57 resetValue();
58 }
59
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070060 RawAbsoluteAxisInfo rawAxisInfo;
61 AxisInfo axisInfo;
62
63 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
64
65 float scale; // scale factor from raw to normalized values
66 float offset; // offset to add after scaling for normalization
67 float highScale; // scale factor from raw to normalized values of high split
68 float highOffset; // offset to add after scaling for normalization of high split
69
70 float min; // normalized inclusive minimum
71 float max; // normalized inclusive maximum
72 float flat; // normalized flat region size
73 float fuzz; // normalized error tolerance
74 float resolution; // normalized resolution in units/mm
75
76 float filter; // filter out small variations of this size
77 float currentValue; // current value
78 float newValue; // most recent value
79 float highCurrentValue; // current value of high split
80 float highNewValue; // most recent value of high split
81
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070082 void resetValue() {
83 this->currentValue = 0;
84 this->newValue = 0;
85 this->highCurrentValue = 0;
86 this->highNewValue = 0;
87 }
88 };
89
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -050090 static Axis createAxis(const AxisInfo& AxisInfo, const RawAbsoluteAxisInfo& rawAxisInfo,
91 bool explicitlyMapped);
92
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070093 // Axes indexed by raw ABS_* axis index.
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -050094 std::unordered_map<int32_t, Axis> mAxes;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070095
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070096 [[nodiscard]] std::list<NotifyArgs> sync(nsecs_t when, nsecs_t readTime, bool force);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070097
98 bool haveAxis(int32_t axisId);
99 void pruneAxes(bool ignoreExplicitlyMappedAxes);
100 bool filterAxes(bool force);
101
102 static bool hasValueChangedSignificantly(float filter, float newValue, float currentValue,
103 float min, float max);
104 static bool hasMovedNearerToValueWithinFilteredRange(float filter, float newValue,
105 float currentValue, float thresholdValue);
106
107 static bool isCenteredAxis(int32_t axis);
108 static int32_t getCompatAxis(int32_t axis);
109
Harry Cuttsd02ea102023-03-17 18:21:30 +0000110 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo& info);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700111 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis, float value);
112};
113
114} // namespace android