Harry Cutts | e78184b | 2024-01-08 15:54:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2024 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 | #include <input/AccelerationCurve.h> |
| 18 | |
| 19 | #include <array> |
| 20 | #include <limits> |
| 21 | |
| 22 | #include <log/log_main.h> |
| 23 | |
| 24 | #define LOG_TAG "AccelerationCurve" |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | // The last segment must have an infinite maximum speed, so that all speeds are covered. |
| 31 | constexpr std::array<AccelerationCurveSegment, 4> kSegments = {{ |
| 32 | {32.002, 3.19, 0}, |
| 33 | {52.83, 4.79, -51.254}, |
| 34 | {119.124, 7.28, -182.737}, |
| 35 | {std::numeric_limits<double>::infinity(), 15.04, -1107.556}, |
| 36 | }}; |
| 37 | |
| 38 | static_assert(kSegments.back().maxPointerSpeedMmPerS == std::numeric_limits<double>::infinity()); |
| 39 | |
| 40 | constexpr std::array<double, 15> kSensitivityFactors = {1, 2, 4, 6, 7, 8, 9, 10, |
| 41 | 11, 12, 13, 14, 16, 18, 20}; |
| 42 | |
Michael Checo | 6d2e24f | 2024-12-16 23:59:53 +0000 | [diff] [blame] | 43 | // Calculates the base gain for a given pointer sensitivity value. |
| 44 | // |
| 45 | // The base gain is a scaling factor that is applied to the pointer movement. |
| 46 | // Higher sensitivity values result in larger base gains, which in turn result |
| 47 | // in faster pointer movements. |
| 48 | // |
| 49 | // The base gain is calculated using a linear mapping function that maps the |
Michael Checo | 0698ecd | 2025-01-24 19:13:40 +0000 | [diff] [blame^] | 50 | // sensitivity range [-7, 7] to a base gain range [1.0, 3.5]. |
Michael Checo | 6d2e24f | 2024-12-16 23:59:53 +0000 | [diff] [blame] | 51 | double calculateBaseGain(int32_t sensitivity) { |
Michael Checo | 0698ecd | 2025-01-24 19:13:40 +0000 | [diff] [blame^] | 52 | return 1.0 + (sensitivity + 7) * (3.5 - 1.0) / (7 + 7); |
Michael Checo | 6d2e24f | 2024-12-16 23:59:53 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Harry Cutts | e78184b | 2024-01-08 15:54:58 +0000 | [diff] [blame] | 55 | } // namespace |
| 56 | |
| 57 | std::vector<AccelerationCurveSegment> createAccelerationCurveForPointerSensitivity( |
| 58 | int32_t sensitivity) { |
| 59 | LOG_ALWAYS_FATAL_IF(sensitivity < -7 || sensitivity > 7, "Invalid pointer sensitivity value"); |
| 60 | std::vector<AccelerationCurveSegment> output; |
| 61 | output.reserve(kSegments.size()); |
| 62 | |
| 63 | // The curves we want to produce for different sensitivity values are actually the same curve, |
| 64 | // just scaled in the Y (gain) axis by a sensitivity factor and a couple of constants. |
| 65 | double commonFactor = 0.64 * kSensitivityFactors[sensitivity + 7] / 10; |
| 66 | for (AccelerationCurveSegment seg : kSegments) { |
| 67 | output.push_back(AccelerationCurveSegment{seg.maxPointerSpeedMmPerS, |
| 68 | commonFactor * seg.baseGain, |
| 69 | commonFactor * seg.reciprocal}); |
| 70 | } |
| 71 | |
| 72 | return output; |
| 73 | } |
| 74 | |
Michael Checo | 6d2e24f | 2024-12-16 23:59:53 +0000 | [diff] [blame] | 75 | std::vector<AccelerationCurveSegment> createFlatAccelerationCurve(int32_t sensitivity) { |
| 76 | LOG_ALWAYS_FATAL_IF(sensitivity < -7 || sensitivity > 7, "Invalid pointer sensitivity value"); |
| 77 | std::vector<AccelerationCurveSegment> output = { |
| 78 | AccelerationCurveSegment{std::numeric_limits<double>::infinity(), |
| 79 | calculateBaseGain(sensitivity), |
| 80 | /* reciprocal = */ 0}}; |
| 81 | return output; |
| 82 | } |
| 83 | |
Harry Cutts | e78184b | 2024-01-08 15:54:58 +0000 | [diff] [blame] | 84 | } // namespace android |