blob: 9ca4176cff44bd6c921a6dbf0b9eafb6cecf92fd [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:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080025 explicit JoystickInputMapper(InputDeviceContext& deviceContext);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070026 virtual ~JoystickInputMapper();
27
Philip Junker4af3b3d2021-12-14 10:36:55 +010028 virtual uint32_t getSources() const override;
Harry Cuttsd02ea102023-03-17 18:21:30 +000029 virtual void populateDeviceInfo(InputDeviceInfo& deviceInfo) override;
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070030 virtual void dump(std::string& dump) override;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070031 [[nodiscard]] std::list<NotifyArgs> configure(nsecs_t when,
32 const InputReaderConfiguration* config,
33 uint32_t changes) override;
34 [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override;
35 [[nodiscard]] std::list<NotifyArgs> process(const RawEvent* rawEvent) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070036
37private:
38 struct Axis {
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -050039 explicit Axis(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
40 bool explicitlyMapped, float scale, float offset, float highScale,
41 float highOffset, float min, float max, float flat, float fuzz,
42 float resolution, float filter)
43 : rawAxisInfo(rawAxisInfo),
44 axisInfo(axisInfo),
45 explicitlyMapped(explicitlyMapped),
46 scale(scale),
47 offset(offset),
48 highScale(highScale),
49 highOffset(highOffset),
50 min(min),
51 max(max),
52 flat(flat),
53 fuzz(fuzz),
54 resolution(resolution),
55 filter(filter) {
56 resetValue();
57 }
58
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070059 RawAbsoluteAxisInfo rawAxisInfo;
60 AxisInfo axisInfo;
61
62 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
63
64 float scale; // scale factor from raw to normalized values
65 float offset; // offset to add after scaling for normalization
66 float highScale; // scale factor from raw to normalized values of high split
67 float highOffset; // offset to add after scaling for normalization of high split
68
69 float min; // normalized inclusive minimum
70 float max; // normalized inclusive maximum
71 float flat; // normalized flat region size
72 float fuzz; // normalized error tolerance
73 float resolution; // normalized resolution in units/mm
74
75 float filter; // filter out small variations of this size
76 float currentValue; // current value
77 float newValue; // most recent value
78 float highCurrentValue; // current value of high split
79 float highNewValue; // most recent value of high split
80
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070081 void resetValue() {
82 this->currentValue = 0;
83 this->newValue = 0;
84 this->highCurrentValue = 0;
85 this->highNewValue = 0;
86 }
87 };
88
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -050089 static Axis createAxis(const AxisInfo& AxisInfo, const RawAbsoluteAxisInfo& rawAxisInfo,
90 bool explicitlyMapped);
91
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070092 // Axes indexed by raw ABS_* axis index.
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -050093 std::unordered_map<int32_t, Axis> mAxes;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070095 [[nodiscard]] std::list<NotifyArgs> sync(nsecs_t when, nsecs_t readTime, bool force);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070096
97 bool haveAxis(int32_t axisId);
98 void pruneAxes(bool ignoreExplicitlyMappedAxes);
99 bool filterAxes(bool force);
100
101 static bool hasValueChangedSignificantly(float filter, float newValue, float currentValue,
102 float min, float max);
103 static bool hasMovedNearerToValueWithinFilteredRange(float filter, float newValue,
104 float currentValue, float thresholdValue);
105
106 static bool isCenteredAxis(int32_t axis);
107 static int32_t getCompatAxis(int32_t axis);
108
Harry Cuttsd02ea102023-03-17 18:21:30 +0000109 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo& info);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700110 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis, float value);
111};
112
113} // namespace android