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 <vector> 20 21 #include <android-base/stringprintf.h> 22 #include <input/AccelerationCurve.h> 23 #include <input/Input.h> 24 #include <input/VelocityTracker.h> 25 #include <utils/Timers.h> 26 27 using android::base::StringPrintf; 28 29 namespace android { 30 31 /* 32 * Specifies parameters that govern pointer or wheel acceleration. 33 */ 34 struct 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 VelocityControlParametersVelocityControlParameters69 VelocityControlParameters() : 70 scale(1.0f), lowThreshold(0.0f), highThreshold(0.0f), acceleration(1.0f) { 71 } 72 VelocityControlParametersVelocityControlParameters73 VelocityControlParameters(float scale, float lowThreshold, 74 float highThreshold, float acceleration) : 75 scale(scale), lowThreshold(lowThreshold), 76 highThreshold(highThreshold), acceleration(acceleration) { 77 } 78 dumpVelocityControlParameters79 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 } 84 }; 85 86 /* 87 * Implements mouse pointer and wheel speed control and acceleration. 88 */ 89 class VelocityControl { 90 public: 91 VelocityControl(); ~VelocityControl()92 virtual ~VelocityControl() {} 93 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 102 protected: 103 virtual void scaleDeltas(float* deltaX, float* deltaY) = 0; 104 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 nsecs_t mLastMovementTime; 110 float mRawPositionX, mRawPositionY; 111 VelocityTracker mVelocityTracker; 112 }; 113 114 /** 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 */ 118 class SimpleVelocityControl : public VelocityControl { 119 public: 120 /** Gets the various parameters. */ 121 const VelocityControlParameters& getParameters() const; 122 123 /** Sets the various parameters. */ 124 void setParameters(const VelocityControlParameters& parameters); 125 126 protected: 127 virtual void scaleDeltas(float* deltaX, float* deltaY) override; 128 129 private: 130 VelocityControlParameters mParameters; 131 }; 132 133 /** Velocity control using a curve made up of multiple reciprocal segments. */ 134 class CurvedVelocityControl : public VelocityControl { 135 public: 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 143 protected: 144 virtual void scaleDeltas(float* deltaX, float* deltaY) override; 145 146 private: 147 const AccelerationCurveSegment& segmentForSpeed(float speedMmPerS); 148 149 bool mAccelerationEnabled = true; 150 std::vector<AccelerationCurveSegment> mCurveSegments; 151 }; 152 153 } // namespace android 154