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