• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #pragma once
18 
19 #include <android-base/stringprintf.h>
20 #include <input/Input.h>
21 #include <input/VelocityTracker.h>
22 #include <utils/Timers.h>
23 
24 using android::base::StringPrintf;
25 
26 namespace android {
27 
28 /*
29  * Specifies parameters that govern pointer or wheel acceleration.
30  */
31 struct 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 
VelocityControlParametersVelocityControlParameters66     VelocityControlParameters() :
67             scale(1.0f), lowThreshold(0.0f), highThreshold(0.0f), acceleration(1.0f) {
68     }
69 
VelocityControlParametersVelocityControlParameters70     VelocityControlParameters(float scale, float lowThreshold,
71             float highThreshold, float acceleration) :
72             scale(scale), lowThreshold(lowThreshold),
73             highThreshold(highThreshold), acceleration(acceleration) {
74     }
75 
dumpVelocityControlParameters76     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     }
81 };
82 
83 /*
84  * Implements mouse pointer and wheel speed control and acceleration.
85  */
86 class VelocityControl {
87 public:
88     VelocityControl();
89 
90     /* Gets the various parameters. */
91     VelocityControlParameters& getParameters();
92 
93     /* 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 
104 private:
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;
112     float mRawPositionX, mRawPositionY;
113     VelocityTracker mVelocityTracker;
114 };
115 
116 } // namespace android
117