blob: e0023970de01cf65c66ae4aecdd0b50a3800fee0 [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;
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070029 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) override;
30 virtual void dump(std::string& dump) override;
31 virtual void configure(nsecs_t when, const InputReaderConfiguration* config,
32 uint32_t changes) override;
33 virtual void reset(nsecs_t when) override;
34 virtual void process(const RawEvent* rawEvent) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070035
36private:
37 struct Axis {
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -050038 explicit Axis(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
39 bool explicitlyMapped, float scale, float offset, float highScale,
40 float highOffset, float min, float max, float flat, float fuzz,
41 float resolution, float filter)
42 : rawAxisInfo(rawAxisInfo),
43 axisInfo(axisInfo),
44 explicitlyMapped(explicitlyMapped),
45 scale(scale),
46 offset(offset),
47 highScale(highScale),
48 highOffset(highOffset),
49 min(min),
50 max(max),
51 flat(flat),
52 fuzz(fuzz),
53 resolution(resolution),
54 filter(filter) {
55 resetValue();
56 }
57
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070058 RawAbsoluteAxisInfo rawAxisInfo;
59 AxisInfo axisInfo;
60
61 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
62
63 float scale; // scale factor from raw to normalized values
64 float offset; // offset to add after scaling for normalization
65 float highScale; // scale factor from raw to normalized values of high split
66 float highOffset; // offset to add after scaling for normalization of high split
67
68 float min; // normalized inclusive minimum
69 float max; // normalized inclusive maximum
70 float flat; // normalized flat region size
71 float fuzz; // normalized error tolerance
72 float resolution; // normalized resolution in units/mm
73
74 float filter; // filter out small variations of this size
75 float currentValue; // current value
76 float newValue; // most recent value
77 float highCurrentValue; // current value of high split
78 float highNewValue; // most recent value of high split
79
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070080 void resetValue() {
81 this->currentValue = 0;
82 this->newValue = 0;
83 this->highCurrentValue = 0;
84 this->highNewValue = 0;
85 }
86 };
87
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -050088 static Axis createAxis(const AxisInfo& AxisInfo, const RawAbsoluteAxisInfo& rawAxisInfo,
89 bool explicitlyMapped);
90
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070091 // Axes indexed by raw ABS_* axis index.
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -050092 std::unordered_map<int32_t, Axis> mAxes;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070093
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000094 void sync(nsecs_t when, nsecs_t readTime, bool force);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070095
96 bool haveAxis(int32_t axisId);
97 void pruneAxes(bool ignoreExplicitlyMappedAxes);
98 bool filterAxes(bool force);
99
100 static bool hasValueChangedSignificantly(float filter, float newValue, float currentValue,
101 float min, float max);
102 static bool hasMovedNearerToValueWithinFilteredRange(float filter, float newValue,
103 float currentValue, float thresholdValue);
104
105 static bool isCenteredAxis(int32_t axis);
106 static int32_t getCompatAxis(int32_t axis);
107
108 static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
109 static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis, float value);
110};
111
112} // namespace android