blob: 0b47f3e6e7c38e928213c700f045bf12d672c529 [file] [log] [blame]
Harry Cuttse78184b2024-01-08 15:54:58 +00001/*
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
26namespace android {
27
28namespace {
29
30// The last segment must have an infinite maximum speed, so that all speeds are covered.
31constexpr 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
38static_assert(kSegments.back().maxPointerSpeedMmPerS == std::numeric_limits<double>::infinity());
39
40constexpr std::array<double, 15> kSensitivityFactors = {1, 2, 4, 6, 7, 8, 9, 10,
41 11, 12, 13, 14, 16, 18, 20};
42
Michael Checo6d2e24f2024-12-16 23:59:53 +000043// 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
50// sensitivity range [-7, 7] to a base gain range [0.5, 2.0].
51double calculateBaseGain(int32_t sensitivity) {
52 return 0.5 + (sensitivity + 7) * (2.0 - 0.5) / (7 + 7);
53}
54
Harry Cuttse78184b2024-01-08 15:54:58 +000055} // namespace
56
57std::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 Checo6d2e24f2024-12-16 23:59:53 +000075std::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 Cuttse78184b2024-01-08 15:54:58 +000084} // namespace android