Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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 | |
Prabir Pradhan | 4810866 | 2022-09-09 21:22:04 +0000 | [diff] [blame] | 17 | #pragma once |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 18 | |
| 19 | #include "InputMapper.h" |
| 20 | |
| 21 | namespace android { |
| 22 | |
| 23 | class JoystickInputMapper : public InputMapper { |
| 24 | public: |
Arpit Singh | 2be4a36 | 2023-04-26 14:16:50 +0000 | [diff] [blame^] | 25 | template <class T, class... Args> |
| 26 | friend std::unique_ptr<T> createInputMapper(InputDeviceContext& deviceContext, |
| 27 | const InputReaderConfiguration& readerConfig, |
| 28 | Args... args); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 29 | virtual ~JoystickInputMapper(); |
| 30 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 31 | virtual uint32_t getSources() const override; |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 32 | virtual void populateDeviceInfo(InputDeviceInfo& deviceInfo) override; |
Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 33 | virtual void dump(std::string& dump) override; |
Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 34 | [[nodiscard]] std::list<NotifyArgs> reconfigure(nsecs_t when, |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 35 | const InputReaderConfiguration& config, |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 36 | ConfigurationChanges changes) override; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 37 | [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override; |
| 38 | [[nodiscard]] std::list<NotifyArgs> process(const RawEvent* rawEvent) override; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 39 | |
| 40 | private: |
| 41 | struct Axis { |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 42 | explicit Axis(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo, |
| 43 | bool explicitlyMapped, float scale, float offset, float highScale, |
| 44 | float highOffset, float min, float max, float flat, float fuzz, |
| 45 | float resolution, float filter) |
| 46 | : rawAxisInfo(rawAxisInfo), |
| 47 | axisInfo(axisInfo), |
| 48 | explicitlyMapped(explicitlyMapped), |
| 49 | scale(scale), |
| 50 | offset(offset), |
| 51 | highScale(highScale), |
| 52 | highOffset(highOffset), |
| 53 | min(min), |
| 54 | max(max), |
| 55 | flat(flat), |
| 56 | fuzz(fuzz), |
| 57 | resolution(resolution), |
| 58 | filter(filter) { |
| 59 | resetValue(); |
| 60 | } |
| 61 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 62 | RawAbsoluteAxisInfo rawAxisInfo; |
| 63 | AxisInfo axisInfo; |
| 64 | |
| 65 | bool explicitlyMapped; // true if the axis was explicitly assigned an axis id |
| 66 | |
| 67 | float scale; // scale factor from raw to normalized values |
| 68 | float offset; // offset to add after scaling for normalization |
| 69 | float highScale; // scale factor from raw to normalized values of high split |
| 70 | float highOffset; // offset to add after scaling for normalization of high split |
| 71 | |
| 72 | float min; // normalized inclusive minimum |
| 73 | float max; // normalized inclusive maximum |
| 74 | float flat; // normalized flat region size |
| 75 | float fuzz; // normalized error tolerance |
| 76 | float resolution; // normalized resolution in units/mm |
| 77 | |
| 78 | float filter; // filter out small variations of this size |
| 79 | float currentValue; // current value |
| 80 | float newValue; // most recent value |
| 81 | float highCurrentValue; // current value of high split |
| 82 | float highNewValue; // most recent value of high split |
| 83 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 84 | void resetValue() { |
| 85 | this->currentValue = 0; |
| 86 | this->newValue = 0; |
| 87 | this->highCurrentValue = 0; |
| 88 | this->highNewValue = 0; |
| 89 | } |
| 90 | }; |
| 91 | |
Arpit Singh | 2be4a36 | 2023-04-26 14:16:50 +0000 | [diff] [blame^] | 92 | explicit JoystickInputMapper(InputDeviceContext& deviceContext, |
| 93 | const InputReaderConfiguration& readerConfig); |
| 94 | |
Siarhei Vishniakou | 690f8be | 2020-06-30 04:31:25 -0500 | [diff] [blame] | 95 | static Axis createAxis(const AxisInfo& AxisInfo, const RawAbsoluteAxisInfo& rawAxisInfo, |
| 96 | bool explicitlyMapped); |
| 97 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 98 | // Axes indexed by raw ABS_* axis index. |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 99 | std::unordered_map<int32_t, Axis> mAxes; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 100 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 101 | [[nodiscard]] std::list<NotifyArgs> sync(nsecs_t when, nsecs_t readTime, bool force); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 102 | |
| 103 | bool haveAxis(int32_t axisId); |
| 104 | void pruneAxes(bool ignoreExplicitlyMappedAxes); |
| 105 | bool filterAxes(bool force); |
| 106 | |
| 107 | static bool hasValueChangedSignificantly(float filter, float newValue, float currentValue, |
| 108 | float min, float max); |
| 109 | static bool hasMovedNearerToValueWithinFilteredRange(float filter, float newValue, |
| 110 | float currentValue, float thresholdValue); |
| 111 | |
| 112 | static bool isCenteredAxis(int32_t axis); |
| 113 | static int32_t getCompatAxis(int32_t axis); |
| 114 | |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 115 | static void addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo& info); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 116 | static void setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis, float value); |
| 117 | }; |
| 118 | |
| 119 | } // namespace android |