18a90e6e3174083f274538567d851f98478fc83e9Jeff Brown/* 28a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * Copyright (C) 2012 The Android Open Source Project 38a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * 48a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * Licensed under the Apache License, Version 2.0 (the "License"); 58a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * you may not use this file except in compliance with the License. 68a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * You may obtain a copy of the License at 78a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * 88a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * http://www.apache.org/licenses/LICENSE-2.0 98a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * 108a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * Unless required by applicable law or agreed to in writing, software 118a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * distributed under the License is distributed on an "AS IS" BASIS, 128a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * See the License for the specific language governing permissions and 148a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * limitations under the License. 158a90e6e3174083f274538567d851f98478fc83e9Jeff Brown */ 168a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 178a90e6e3174083f274538567d851f98478fc83e9Jeff Brown#ifndef _ANDROIDFW_VELOCITY_CONTROL_H 188a90e6e3174083f274538567d851f98478fc83e9Jeff Brown#define _ANDROIDFW_VELOCITY_CONTROL_H 198a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 208a90e6e3174083f274538567d851f98478fc83e9Jeff Brown#include <androidfw/Input.h> 218a90e6e3174083f274538567d851f98478fc83e9Jeff Brown#include <androidfw/VelocityTracker.h> 228a90e6e3174083f274538567d851f98478fc83e9Jeff Brown#include <utils/Timers.h> 238a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 248a90e6e3174083f274538567d851f98478fc83e9Jeff Brownnamespace android { 258a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 268a90e6e3174083f274538567d851f98478fc83e9Jeff Brown/* 278a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * Specifies parameters that govern pointer or wheel acceleration. 288a90e6e3174083f274538567d851f98478fc83e9Jeff Brown */ 298a90e6e3174083f274538567d851f98478fc83e9Jeff Brownstruct VelocityControlParameters { 308a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // A scale factor that is multiplied with the raw velocity deltas 318a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // prior to applying any other velocity control factors. The scale 328a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // factor should be used to adapt the input device resolution 338a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // (eg. counts per inch) to the output device resolution (eg. pixels per inch). 348a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // 358a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // Must be a positive value. 368a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // Default is 1.0 (no scaling). 378a90e6e3174083f274538567d851f98478fc83e9Jeff Brown float scale; 388a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 398a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // The scaled speed at which acceleration begins to be applied. 408a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // This value establishes the upper bound of a low speed regime for 418a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // small precise motions that are performed without any acceleration. 428a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // 438a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // Must be a non-negative value. 448a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // Default is 0.0 (no low threshold). 458a90e6e3174083f274538567d851f98478fc83e9Jeff Brown float lowThreshold; 468a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 478a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // The scaled speed at which maximum acceleration is applied. 488a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // The difference between highThreshold and lowThreshold controls 498a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // the range of speeds over which the acceleration factor is interpolated. 508a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // The wider the range, the smoother the acceleration. 518a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // 528a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // Must be a non-negative value greater than or equal to lowThreshold. 538a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // Default is 0.0 (no high threshold). 548a90e6e3174083f274538567d851f98478fc83e9Jeff Brown float highThreshold; 558a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 568a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // The acceleration factor. 578a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // When the speed is above the low speed threshold, the velocity will scaled 588a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // by an interpolated value between 1.0 and this amount. 598a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // 608a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // Must be a positive greater than or equal to 1.0. 618a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // Default is 1.0 (no acceleration). 628a90e6e3174083f274538567d851f98478fc83e9Jeff Brown float acceleration; 638a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 648a90e6e3174083f274538567d851f98478fc83e9Jeff Brown VelocityControlParameters() : 658a90e6e3174083f274538567d851f98478fc83e9Jeff Brown scale(1.0f), lowThreshold(0.0f), highThreshold(0.0f), acceleration(1.0f) { 668a90e6e3174083f274538567d851f98478fc83e9Jeff Brown } 678a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 688a90e6e3174083f274538567d851f98478fc83e9Jeff Brown VelocityControlParameters(float scale, float lowThreshold, 698a90e6e3174083f274538567d851f98478fc83e9Jeff Brown float highThreshold, float acceleration) : 708a90e6e3174083f274538567d851f98478fc83e9Jeff Brown scale(scale), lowThreshold(lowThreshold), 718a90e6e3174083f274538567d851f98478fc83e9Jeff Brown highThreshold(highThreshold), acceleration(acceleration) { 728a90e6e3174083f274538567d851f98478fc83e9Jeff Brown } 738a90e6e3174083f274538567d851f98478fc83e9Jeff Brown}; 748a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 758a90e6e3174083f274538567d851f98478fc83e9Jeff Brown/* 768a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * Implements mouse pointer and wheel speed control and acceleration. 778a90e6e3174083f274538567d851f98478fc83e9Jeff Brown */ 788a90e6e3174083f274538567d851f98478fc83e9Jeff Brownclass VelocityControl { 798a90e6e3174083f274538567d851f98478fc83e9Jeff Brownpublic: 808a90e6e3174083f274538567d851f98478fc83e9Jeff Brown VelocityControl(); 818a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 828a90e6e3174083f274538567d851f98478fc83e9Jeff Brown /* Sets the various parameters. */ 838a90e6e3174083f274538567d851f98478fc83e9Jeff Brown void setParameters(const VelocityControlParameters& parameters); 848a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 858a90e6e3174083f274538567d851f98478fc83e9Jeff Brown /* Resets the current movement counters to zero. 868a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * This has the effect of nullifying any acceleration. */ 878a90e6e3174083f274538567d851f98478fc83e9Jeff Brown void reset(); 888a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 898a90e6e3174083f274538567d851f98478fc83e9Jeff Brown /* Translates a raw movement delta into an appropriately 908a90e6e3174083f274538567d851f98478fc83e9Jeff Brown * scaled / accelerated delta based on the current velocity. */ 918a90e6e3174083f274538567d851f98478fc83e9Jeff Brown void move(nsecs_t eventTime, float* deltaX, float* deltaY); 928a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 938a90e6e3174083f274538567d851f98478fc83e9Jeff Brownprivate: 948a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // If no movements are received within this amount of time, 958a90e6e3174083f274538567d851f98478fc83e9Jeff Brown // we assume the movement has stopped and reset the movement counters. 968a90e6e3174083f274538567d851f98478fc83e9Jeff Brown static const nsecs_t STOP_TIME = 500 * 1000000; // 500 ms 978a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 988a90e6e3174083f274538567d851f98478fc83e9Jeff Brown VelocityControlParameters mParameters; 998a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 1008a90e6e3174083f274538567d851f98478fc83e9Jeff Brown nsecs_t mLastMovementTime; 1018a90e6e3174083f274538567d851f98478fc83e9Jeff Brown VelocityTracker::Position mRawPosition; 1028a90e6e3174083f274538567d851f98478fc83e9Jeff Brown VelocityTracker mVelocityTracker; 1038a90e6e3174083f274538567d851f98478fc83e9Jeff Brown}; 1048a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 1058a90e6e3174083f274538567d851f98478fc83e9Jeff Brown} // namespace android 1068a90e6e3174083f274538567d851f98478fc83e9Jeff Brown 1078a90e6e3174083f274538567d851f98478fc83e9Jeff Brown#endif // _ANDROIDFW_VELOCITY_CONTROL_H 108