blob: 7c58c87f8b4ab9e0b08e62e6247418d2c778b0bf [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
2 * Copyright (C) 2012 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 Pradhanc08b0db2022-09-10 00:57:15 +000017#pragma once
Jeff Brown5912f952013-07-01 19:10:31 -070018
Harry Cuttse78184b2024-01-08 15:54:58 +000019#include <vector>
20
Zixuan Qu79b76d12022-11-11 04:47:51 +000021#include <android-base/stringprintf.h>
Harry Cuttse78184b2024-01-08 15:54:58 +000022#include <input/AccelerationCurve.h>
Jeff Brown5912f952013-07-01 19:10:31 -070023#include <input/Input.h>
24#include <input/VelocityTracker.h>
25#include <utils/Timers.h>
26
Zixuan Qu79b76d12022-11-11 04:47:51 +000027using android::base::StringPrintf;
28
Jeff Brown5912f952013-07-01 19:10:31 -070029namespace android {
30
31/*
32 * Specifies parameters that govern pointer or wheel acceleration.
33 */
34struct VelocityControlParameters {
35 // A scale factor that is multiplied with the raw velocity deltas
36 // prior to applying any other velocity control factors. The scale
37 // factor should be used to adapt the input device resolution
38 // (eg. counts per inch) to the output device resolution (eg. pixels per inch).
39 //
40 // Must be a positive value.
41 // Default is 1.0 (no scaling).
42 float scale;
43
44 // The scaled speed at which acceleration begins to be applied.
45 // This value establishes the upper bound of a low speed regime for
46 // small precise motions that are performed without any acceleration.
47 //
48 // Must be a non-negative value.
49 // Default is 0.0 (no low threshold).
50 float lowThreshold;
51
52 // The scaled speed at which maximum acceleration is applied.
53 // The difference between highThreshold and lowThreshold controls
54 // the range of speeds over which the acceleration factor is interpolated.
55 // The wider the range, the smoother the acceleration.
56 //
57 // Must be a non-negative value greater than or equal to lowThreshold.
58 // Default is 0.0 (no high threshold).
59 float highThreshold;
60
61 // The acceleration factor.
62 // When the speed is above the low speed threshold, the velocity will scaled
63 // by an interpolated value between 1.0 and this amount.
64 //
65 // Must be a positive greater than or equal to 1.0.
66 // Default is 1.0 (no acceleration).
67 float acceleration;
68
69 VelocityControlParameters() :
70 scale(1.0f), lowThreshold(0.0f), highThreshold(0.0f), acceleration(1.0f) {
71 }
72
73 VelocityControlParameters(float scale, float lowThreshold,
74 float highThreshold, float acceleration) :
75 scale(scale), lowThreshold(lowThreshold),
76 highThreshold(highThreshold), acceleration(acceleration) {
77 }
Zixuan Qu79b76d12022-11-11 04:47:51 +000078
79 std::string dump() const {
80 return StringPrintf("scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
81 "acceleration=%0.3f\n",
82 scale, lowThreshold, highThreshold, acceleration);
83 }
Jeff Brown5912f952013-07-01 19:10:31 -070084};
85
86/*
87 * Implements mouse pointer and wheel speed control and acceleration.
88 */
89class VelocityControl {
90public:
91 VelocityControl();
Harry Cuttse78184b2024-01-08 15:54:58 +000092 virtual ~VelocityControl() {}
Jeff Brown5912f952013-07-01 19:10:31 -070093
94 /* Resets the current movement counters to zero.
95 * This has the effect of nullifying any acceleration. */
96 void reset();
97
98 /* Translates a raw movement delta into an appropriately
99 * scaled / accelerated delta based on the current velocity. */
100 void move(nsecs_t eventTime, float* deltaX, float* deltaY);
101
Harry Cuttse78184b2024-01-08 15:54:58 +0000102protected:
103 virtual void scaleDeltas(float* deltaX, float* deltaY) = 0;
104
Jeff Brown5912f952013-07-01 19:10:31 -0700105 // If no movements are received within this amount of time,
106 // we assume the movement has stopped and reset the movement counters.
107 static const nsecs_t STOP_TIME = 500 * 1000000; // 500 ms
108
Jeff Brown5912f952013-07-01 19:10:31 -0700109 nsecs_t mLastMovementTime;
Yeabkal Wubshit37acf6e2022-08-27 05:48:51 +0000110 float mRawPositionX, mRawPositionY;
Jeff Brown5912f952013-07-01 19:10:31 -0700111 VelocityTracker mVelocityTracker;
112};
113
Harry Cuttse78184b2024-01-08 15:54:58 +0000114/**
115 * Velocity control using a simple acceleration curve where the acceleration factor increases
116 * linearly with movement speed, subject to minimum and maximum values.
117 */
118class SimpleVelocityControl : public VelocityControl {
119public:
120 /** Gets the various parameters. */
121 const VelocityControlParameters& getParameters() const;
122
123 /** Sets the various parameters. */
124 void setParameters(const VelocityControlParameters& parameters);
125
126protected:
127 virtual void scaleDeltas(float* deltaX, float* deltaY) override;
128
129private:
130 VelocityControlParameters mParameters;
131};
132
133/** Velocity control using a curve made up of multiple reciprocal segments. */
134class CurvedVelocityControl : public VelocityControl {
135public:
136 CurvedVelocityControl();
137
138 /** Sets the curve to be used for acceleration. */
139 void setCurve(const std::vector<AccelerationCurveSegment>& curve);
140
141 void setAccelerationEnabled(bool enabled);
142
143protected:
144 virtual void scaleDeltas(float* deltaX, float* deltaY) override;
145
146private:
147 const AccelerationCurveSegment& segmentForSpeed(float speedMmPerS);
148
149 bool mAccelerationEnabled = true;
150 std::vector<AccelerationCurveSegment> mCurveSegments;
151};
152
Jeff Brown5912f952013-07-01 19:10:31 -0700153} // namespace android