1 /*
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 
17 #ifndef _UI_INPUTREADER_JOYSTICK_INPUT_MAPPER_H
18 #define _UI_INPUTREADER_JOYSTICK_INPUT_MAPPER_H
19 
20 #include "InputMapper.h"
21 
22 namespace android {
23 
24 class JoystickInputMapper : public InputMapper {
25 public:
26     explicit JoystickInputMapper(InputDeviceContext& deviceContext);
27     virtual ~JoystickInputMapper();
28 
29     virtual uint32_t getSources() override;
30     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) override;
31     virtual void dump(std::string& dump) override;
32     virtual void configure(nsecs_t when, const InputReaderConfiguration* config,
33                            uint32_t changes) override;
34     virtual void reset(nsecs_t when) override;
35     virtual void process(const RawEvent* rawEvent) override;
36 
37 private:
38     struct Axis {
39         RawAbsoluteAxisInfo rawAxisInfo;
40         AxisInfo axisInfo;
41 
42         bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
43 
44         float scale;      // scale factor from raw to normalized values
45         float offset;     // offset to add after scaling for normalization
46         float highScale;  // scale factor from raw to normalized values of high split
47         float highOffset; // offset to add after scaling for normalization of high split
48 
49         float min;        // normalized inclusive minimum
50         float max;        // normalized inclusive maximum
51         float flat;       // normalized flat region size
52         float fuzz;       // normalized error tolerance
53         float resolution; // normalized resolution in units/mm
54 
55         float filter;           // filter out small variations of this size
56         float currentValue;     // current value
57         float newValue;         // most recent value
58         float highCurrentValue; // current value of high split
59         float highNewValue;     // most recent value of high split
60 
initializeAxis61         void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
62                         bool explicitlyMapped, float scale, float offset, float highScale,
63                         float highOffset, float min, float max, float flat, float fuzz,
64                         float resolution) {
65             this->rawAxisInfo = rawAxisInfo;
66             this->axisInfo = axisInfo;
67             this->explicitlyMapped = explicitlyMapped;
68             this->scale = scale;
69             this->offset = offset;
70             this->highScale = highScale;
71             this->highOffset = highOffset;
72             this->min = min;
73             this->max = max;
74             this->flat = flat;
75             this->fuzz = fuzz;
76             this->resolution = resolution;
77             this->filter = 0;
78             resetValue();
79         }
80 
resetValueAxis81         void resetValue() {
82             this->currentValue = 0;
83             this->newValue = 0;
84             this->highCurrentValue = 0;
85             this->highNewValue = 0;
86         }
87     };
88 
89     // Axes indexed by raw ABS_* axis index.
90     KeyedVector<int32_t, Axis> mAxes;
91 
92     void sync(nsecs_t when, bool force);
93 
94     bool haveAxis(int32_t axisId);
95     void pruneAxes(bool ignoreExplicitlyMappedAxes);
96     bool filterAxes(bool force);
97 
98     static bool hasValueChangedSignificantly(float filter, float newValue, float currentValue,
99                                              float min, float max);
100     static bool hasMovedNearerToValueWithinFilteredRange(float filter, float newValue,
101                                                          float currentValue, float thresholdValue);
102 
103     static bool isCenteredAxis(int32_t axis);
104     static int32_t getCompatAxis(int32_t axis);
105 
106     static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info);
107     static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis, float value);
108 };
109 
110 } // namespace android
111 
112 #endif // _UI_INPUTREADER_JOYSTICK_INPUT_MAPPER_H