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