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 
43 } // namespace
44 
createAccelerationCurveForPointerSensitivity(int32_t sensitivity)45 std::vector<AccelerationCurveSegment> createAccelerationCurveForPointerSensitivity(
46         int32_t sensitivity) {
47     LOG_ALWAYS_FATAL_IF(sensitivity < -7 || sensitivity > 7, "Invalid pointer sensitivity value");
48     std::vector<AccelerationCurveSegment> output;
49     output.reserve(kSegments.size());
50 
51     // The curves we want to produce for different sensitivity values are actually the same curve,
52     // just scaled in the Y (gain) axis by a sensitivity factor and a couple of constants.
53     double commonFactor = 0.64 * kSensitivityFactors[sensitivity + 7] / 10;
54     for (AccelerationCurveSegment seg : kSegments) {
55         output.push_back(AccelerationCurveSegment{seg.maxPointerSpeedMmPerS,
56                                                   commonFactor * seg.baseGain,
57                                                   commonFactor * seg.reciprocal});
58     }
59 
60     return output;
61 }
62 
63 } // namespace android