blob: b78f63e1ae444b1bd7c35181d07fb1dce02e26bb [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
Zixuan Qu79b76d12022-11-11 04:47:51 +000019#include <android-base/stringprintf.h>
Jeff Brown5912f952013-07-01 19:10:31 -070020#include <input/Input.h>
21#include <input/VelocityTracker.h>
22#include <utils/Timers.h>
23
Zixuan Qu79b76d12022-11-11 04:47:51 +000024using android::base::StringPrintf;
25
Jeff Brown5912f952013-07-01 19:10:31 -070026namespace android {
27
28/*
29 * Specifies parameters that govern pointer or wheel acceleration.
30 */
31struct VelocityControlParameters {
32 // A scale factor that is multiplied with the raw velocity deltas
33 // prior to applying any other velocity control factors. The scale
34 // factor should be used to adapt the input device resolution
35 // (eg. counts per inch) to the output device resolution (eg. pixels per inch).
36 //
37 // Must be a positive value.
38 // Default is 1.0 (no scaling).
39 float scale;
40
41 // The scaled speed at which acceleration begins to be applied.
42 // This value establishes the upper bound of a low speed regime for
43 // small precise motions that are performed without any acceleration.
44 //
45 // Must be a non-negative value.
46 // Default is 0.0 (no low threshold).
47 float lowThreshold;
48
49 // The scaled speed at which maximum acceleration is applied.
50 // The difference between highThreshold and lowThreshold controls
51 // the range of speeds over which the acceleration factor is interpolated.
52 // The wider the range, the smoother the acceleration.
53 //
54 // Must be a non-negative value greater than or equal to lowThreshold.
55 // Default is 0.0 (no high threshold).
56 float highThreshold;
57
58 // The acceleration factor.
59 // When the speed is above the low speed threshold, the velocity will scaled
60 // by an interpolated value between 1.0 and this amount.
61 //
62 // Must be a positive greater than or equal to 1.0.
63 // Default is 1.0 (no acceleration).
64 float acceleration;
65
66 VelocityControlParameters() :
67 scale(1.0f), lowThreshold(0.0f), highThreshold(0.0f), acceleration(1.0f) {
68 }
69
70 VelocityControlParameters(float scale, float lowThreshold,
71 float highThreshold, float acceleration) :
72 scale(scale), lowThreshold(lowThreshold),
73 highThreshold(highThreshold), acceleration(acceleration) {
74 }
Zixuan Qu79b76d12022-11-11 04:47:51 +000075
76 std::string dump() const {
77 return StringPrintf("scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, "
78 "acceleration=%0.3f\n",
79 scale, lowThreshold, highThreshold, acceleration);
80 }
Jeff Brown5912f952013-07-01 19:10:31 -070081};
82
83/*
84 * Implements mouse pointer and wheel speed control and acceleration.
85 */
86class VelocityControl {
87public:
88 VelocityControl();
89
Zixuan Qu79b76d12022-11-11 04:47:51 +000090 /* Gets the various parameters. */
Zixuan Quad074412022-11-23 19:20:58 +000091 const VelocityControlParameters& getParameters() const;
Zixuan Qu79b76d12022-11-11 04:47:51 +000092
Jeff Brown5912f952013-07-01 19:10:31 -070093 /* Sets the various parameters. */
94 void setParameters(const VelocityControlParameters& parameters);
95
96 /* Resets the current movement counters to zero.
97 * This has the effect of nullifying any acceleration. */
98 void reset();
99
100 /* Translates a raw movement delta into an appropriately
101 * scaled / accelerated delta based on the current velocity. */
102 void move(nsecs_t eventTime, float* deltaX, float* deltaY);
103
104private:
105 // 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
109 VelocityControlParameters mParameters;
110
111 nsecs_t mLastMovementTime;
Yeabkal Wubshit37acf6e2022-08-27 05:48:51 +0000112 float mRawPositionX, mRawPositionY;
Jeff Brown5912f952013-07-01 19:10:31 -0700113 VelocityTracker mVelocityTracker;
114};
115
116} // namespace android