blob: f72a1bdded5f2f09b495734b4f3a5f6171313be4 [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
19#include <input/Input.h>
20#include <input/VelocityTracker.h>
21#include <utils/Timers.h>
22
23namespace android {
24
25/*
26 * Specifies parameters that govern pointer or wheel acceleration.
27 */
28struct VelocityControlParameters {
29 // A scale factor that is multiplied with the raw velocity deltas
30 // prior to applying any other velocity control factors. The scale
31 // factor should be used to adapt the input device resolution
32 // (eg. counts per inch) to the output device resolution (eg. pixels per inch).
33 //
34 // Must be a positive value.
35 // Default is 1.0 (no scaling).
36 float scale;
37
38 // The scaled speed at which acceleration begins to be applied.
39 // This value establishes the upper bound of a low speed regime for
40 // small precise motions that are performed without any acceleration.
41 //
42 // Must be a non-negative value.
43 // Default is 0.0 (no low threshold).
44 float lowThreshold;
45
46 // The scaled speed at which maximum acceleration is applied.
47 // The difference between highThreshold and lowThreshold controls
48 // the range of speeds over which the acceleration factor is interpolated.
49 // The wider the range, the smoother the acceleration.
50 //
51 // Must be a non-negative value greater than or equal to lowThreshold.
52 // Default is 0.0 (no high threshold).
53 float highThreshold;
54
55 // The acceleration factor.
56 // When the speed is above the low speed threshold, the velocity will scaled
57 // by an interpolated value between 1.0 and this amount.
58 //
59 // Must be a positive greater than or equal to 1.0.
60 // Default is 1.0 (no acceleration).
61 float acceleration;
62
63 VelocityControlParameters() :
64 scale(1.0f), lowThreshold(0.0f), highThreshold(0.0f), acceleration(1.0f) {
65 }
66
67 VelocityControlParameters(float scale, float lowThreshold,
68 float highThreshold, float acceleration) :
69 scale(scale), lowThreshold(lowThreshold),
70 highThreshold(highThreshold), acceleration(acceleration) {
71 }
72};
73
74/*
75 * Implements mouse pointer and wheel speed control and acceleration.
76 */
77class VelocityControl {
78public:
79 VelocityControl();
80
81 /* Sets the various parameters. */
82 void setParameters(const VelocityControlParameters& parameters);
83
84 /* Resets the current movement counters to zero.
85 * This has the effect of nullifying any acceleration. */
86 void reset();
87
88 /* Translates a raw movement delta into an appropriately
89 * scaled / accelerated delta based on the current velocity. */
90 void move(nsecs_t eventTime, float* deltaX, float* deltaY);
91
92private:
93 // If no movements are received within this amount of time,
94 // we assume the movement has stopped and reset the movement counters.
95 static const nsecs_t STOP_TIME = 500 * 1000000; // 500 ms
96
97 VelocityControlParameters mParameters;
98
99 nsecs_t mLastMovementTime;
Yeabkal Wubshit37acf6e2022-08-27 05:48:51 +0000100 float mRawPositionX, mRawPositionY;
Jeff Brown5912f952013-07-01 19:10:31 -0700101 VelocityTracker mVelocityTracker;
102};
103
104} // namespace android