2009-09-06 13:44:18 -06:00
|
|
|
/*
|
|
|
|
*
|
2010-07-27 13:02:24 -06:00
|
|
|
* Copyright © 2006-2009 Simon Thum simon dot thum at gmx dot de
|
2009-09-06 13:44:18 -06:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_DIX_CONFIG_H
|
|
|
|
#include <dix-config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <ptrveloc.h>
|
2010-07-27 13:02:24 -06:00
|
|
|
#include <exevents.h>
|
|
|
|
#include <X11/Xatom.h>
|
|
|
|
|
|
|
|
#include <xserver-properties.h>
|
2009-09-06 13:44:18 -06:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Predictable pointer acceleration
|
|
|
|
*
|
2010-07-27 13:02:24 -06:00
|
|
|
* 2006-2009 by Simon Thum (simon [dot] thum [at] gmx de)
|
2009-09-06 13:44:18 -06:00
|
|
|
*
|
|
|
|
* Serves 3 complementary functions:
|
|
|
|
* 1) provide a sophisticated ballistic velocity estimate to improve
|
|
|
|
* the relation between velocity (of the device) and acceleration
|
|
|
|
* 2) make arbitrary acceleration profiles possible
|
|
|
|
* 3) decelerate by two means (constant and adaptive) if enabled
|
|
|
|
*
|
|
|
|
* Important concepts are the
|
|
|
|
*
|
|
|
|
* - Scheme
|
|
|
|
* which selects the basic algorithm
|
|
|
|
* (see devices.c/InitPointerAccelerationScheme)
|
|
|
|
* - Profile
|
|
|
|
* which returns an acceleration
|
|
|
|
* for a given velocity
|
|
|
|
*
|
2010-07-27 13:02:24 -06:00
|
|
|
* The profile can be selected by the user at runtime.
|
|
|
|
* The classic profile is intended to cleanly perform old-style
|
2009-09-06 13:44:18 -06:00
|
|
|
* function selection (threshold =/!= 0)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* fwds */
|
|
|
|
int
|
2010-07-27 13:02:24 -06:00
|
|
|
SetAccelerationProfile(DeviceVelocityPtr vel, int profile_num);
|
2009-09-06 13:44:18 -06:00
|
|
|
static float
|
2010-07-27 13:02:24 -06:00
|
|
|
SimpleSmoothProfile(DeviceIntPtr dev, DeviceVelocityPtr vel, float velocity,
|
2009-09-06 13:44:18 -06:00
|
|
|
float threshold, float acc);
|
2010-07-27 13:02:24 -06:00
|
|
|
static PointerAccelerationProfileFunc
|
|
|
|
GetAccelerationProfile(DeviceVelocityPtr vel, int profile_num);
|
2009-09-06 13:44:18 -06:00
|
|
|
|
|
|
|
/*#define PTRACCEL_DEBUGGING*/
|
|
|
|
|
|
|
|
#ifdef PTRACCEL_DEBUGGING
|
|
|
|
#define DebugAccelF ErrorF
|
|
|
|
#else
|
|
|
|
#define DebugAccelF(...) /* */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/********************************
|
2010-07-27 13:02:24 -06:00
|
|
|
* Init/Uninit
|
2009-09-06 13:44:18 -06:00
|
|
|
*******************************/
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
/* some int which is not a profile number */
|
|
|
|
#define PROFILE_UNINITIALIZE (-100)
|
|
|
|
|
|
|
|
|
2009-09-06 13:44:18 -06:00
|
|
|
/**
|
|
|
|
* Init struct so it should match the average case
|
|
|
|
*/
|
|
|
|
void
|
2010-07-27 13:02:24 -06:00
|
|
|
InitVelocityData(DeviceVelocityPtr vel)
|
2009-09-06 13:44:18 -06:00
|
|
|
{
|
2010-07-27 13:02:24 -06:00
|
|
|
memset(vel, 0, sizeof(DeviceVelocityRec));
|
|
|
|
|
|
|
|
vel->corr_mul = 10.0; /* dots per 10 milisecond should be usable */
|
|
|
|
vel->const_acceleration = 1.0; /* no acceleration/deceleration */
|
|
|
|
vel->reset_time = 300;
|
|
|
|
vel->use_softening = 1;
|
|
|
|
vel->min_acceleration = 1.0; /* don't decelerate */
|
|
|
|
vel->max_rel_diff = 0.2;
|
|
|
|
vel->max_diff = 1.0;
|
|
|
|
vel->initial_range = 2;
|
|
|
|
vel->average_accel = TRUE;
|
|
|
|
SetAccelerationProfile(vel, AccelProfileClassic);
|
|
|
|
InitTrackers(vel, 16);
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up
|
|
|
|
*/
|
2010-07-27 13:02:24 -06:00
|
|
|
void
|
|
|
|
FreeVelocityData(DeviceVelocityPtr vel){
|
2010-12-05 08:36:02 -07:00
|
|
|
free(vel->tracker);
|
2010-07-27 13:02:24 -06:00
|
|
|
SetAccelerationProfile(vel, PROFILE_UNINITIALIZE);
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dix uninit helper, called through scheme
|
|
|
|
*/
|
|
|
|
void
|
2010-07-27 13:02:24 -06:00
|
|
|
AccelerationDefaultCleanup(DeviceIntPtr dev)
|
2009-09-06 13:44:18 -06:00
|
|
|
{
|
|
|
|
/*sanity check*/
|
2010-07-27 13:02:24 -06:00
|
|
|
if( dev->valuator->accelScheme.AccelSchemeProc == acceleratePointerPredictable
|
|
|
|
&& dev->valuator->accelScheme.accelData != NULL){
|
|
|
|
dev->valuator->accelScheme.AccelSchemeProc = NULL;
|
|
|
|
FreeVelocityData(dev->valuator->accelScheme.accelData);
|
2010-12-05 08:36:02 -07:00
|
|
|
free(dev->valuator->accelScheme.accelData);
|
2010-07-27 13:02:24 -06:00
|
|
|
dev->valuator->accelScheme.accelData = NULL;
|
|
|
|
DeletePredictableAccelerationProperties(dev);
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
|
|
|
|
/*************************
|
|
|
|
* Input property support
|
|
|
|
************************/
|
2009-09-06 13:44:18 -06:00
|
|
|
|
|
|
|
/**
|
2010-07-27 13:02:24 -06:00
|
|
|
* choose profile
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
AccelSetProfileProperty(DeviceIntPtr dev, Atom atom,
|
|
|
|
XIPropertyValuePtr val, BOOL checkOnly)
|
2009-09-06 13:44:18 -06:00
|
|
|
{
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceVelocityPtr vel;
|
|
|
|
int profile, *ptr = &profile;
|
|
|
|
int rc;
|
|
|
|
int nelem = 1;
|
|
|
|
|
|
|
|
if (atom != XIGetKnownProperty(ACCEL_PROP_PROFILE_NUMBER))
|
|
|
|
return Success;
|
|
|
|
|
|
|
|
vel = GetDevicePredictableAccelData(dev);
|
|
|
|
if (!vel)
|
|
|
|
return BadValue;
|
|
|
|
rc = XIPropToInt(val, &nelem, &ptr);
|
|
|
|
|
|
|
|
if(checkOnly)
|
|
|
|
{
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
if (GetAccelerationProfile(vel, profile) == NULL)
|
|
|
|
return BadValue;
|
|
|
|
} else
|
|
|
|
SetAccelerationProfile(vel, profile);
|
|
|
|
|
|
|
|
return Success;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
static long
|
|
|
|
AccelInitProfileProperty(DeviceIntPtr dev, DeviceVelocityPtr vel)
|
2009-09-06 13:44:18 -06:00
|
|
|
{
|
2010-07-27 13:02:24 -06:00
|
|
|
int profile = vel->statistics.profile_number;
|
|
|
|
Atom prop_profile_number = XIGetKnownProperty(ACCEL_PROP_PROFILE_NUMBER);
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
XIChangeDeviceProperty(dev, prop_profile_number, XA_INTEGER, 32,
|
|
|
|
PropModeReplace, 1, &profile, FALSE);
|
|
|
|
XISetDevicePropertyDeletable(dev, prop_profile_number, FALSE);
|
|
|
|
return XIRegisterPropertyHandler(dev, AccelSetProfileProperty, NULL, NULL);
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
/**
|
|
|
|
* constant deceleration
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
AccelSetDecelProperty(DeviceIntPtr dev, Atom atom,
|
|
|
|
XIPropertyValuePtr val, BOOL checkOnly)
|
2009-09-06 13:44:18 -06:00
|
|
|
{
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceVelocityPtr vel;
|
|
|
|
float v, *ptr = &v;
|
|
|
|
int rc;
|
|
|
|
int nelem = 1;
|
|
|
|
|
|
|
|
if (atom != XIGetKnownProperty(ACCEL_PROP_CONSTANT_DECELERATION))
|
|
|
|
return Success;
|
|
|
|
|
|
|
|
vel = GetDevicePredictableAccelData(dev);
|
|
|
|
if (!vel)
|
|
|
|
return BadValue;
|
|
|
|
rc = XIPropToFloat(val, &nelem, &ptr);
|
|
|
|
|
|
|
|
if(checkOnly)
|
|
|
|
{
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
return (v >= 1.0f) ? Success : BadValue;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
2010-07-27 13:02:24 -06:00
|
|
|
|
|
|
|
if(v >= 1.0f)
|
|
|
|
vel->const_acceleration = 1/v;
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
AccelInitDecelProperty(DeviceIntPtr dev, DeviceVelocityPtr vel)
|
|
|
|
{
|
|
|
|
float fval = 1.0/vel->const_acceleration;
|
|
|
|
Atom prop_const_decel = XIGetKnownProperty(ACCEL_PROP_CONSTANT_DECELERATION);
|
|
|
|
XIChangeDeviceProperty(dev, prop_const_decel,
|
|
|
|
XIGetKnownProperty(XATOM_FLOAT), 32,
|
|
|
|
PropModeReplace, 1, &fval, FALSE);
|
|
|
|
XISetDevicePropertyDeletable(dev, prop_const_decel, FALSE);
|
|
|
|
return XIRegisterPropertyHandler(dev, AccelSetDecelProperty, NULL, NULL);
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-07-27 13:02:24 -06:00
|
|
|
* adaptive deceleration
|
2009-09-06 13:44:18 -06:00
|
|
|
*/
|
2010-07-27 13:02:24 -06:00
|
|
|
static int
|
|
|
|
AccelSetAdaptDecelProperty(DeviceIntPtr dev, Atom atom,
|
|
|
|
XIPropertyValuePtr val, BOOL checkOnly)
|
2009-09-06 13:44:18 -06:00
|
|
|
{
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceVelocityPtr veloc;
|
|
|
|
float v, *ptr = &v;
|
|
|
|
int rc;
|
|
|
|
int nelem = 1;
|
|
|
|
|
|
|
|
if (atom != XIGetKnownProperty(ACCEL_PROP_ADAPTIVE_DECELERATION))
|
|
|
|
return Success;
|
|
|
|
|
|
|
|
veloc = GetDevicePredictableAccelData(dev);
|
|
|
|
if (!veloc)
|
|
|
|
return BadValue;
|
|
|
|
rc = XIPropToFloat(val, &nelem, &ptr);
|
|
|
|
|
|
|
|
if(checkOnly)
|
|
|
|
{
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
return (v >= 1.0f) ? Success : BadValue;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
2010-07-27 13:02:24 -06:00
|
|
|
|
|
|
|
if(v >= 1.0f)
|
|
|
|
veloc->min_acceleration = 1/v;
|
|
|
|
|
|
|
|
return Success;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
static long
|
|
|
|
AccelInitAdaptDecelProperty(DeviceIntPtr dev, DeviceVelocityPtr vel)
|
|
|
|
{
|
|
|
|
float fval = 1.0/vel->min_acceleration;
|
|
|
|
Atom prop_adapt_decel = XIGetKnownProperty(ACCEL_PROP_ADAPTIVE_DECELERATION);
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
XIChangeDeviceProperty(dev, prop_adapt_decel, XIGetKnownProperty(XATOM_FLOAT), 32,
|
|
|
|
PropModeReplace, 1, &fval, FALSE);
|
|
|
|
XISetDevicePropertyDeletable(dev, prop_adapt_decel, FALSE);
|
|
|
|
return XIRegisterPropertyHandler(dev, AccelSetAdaptDecelProperty, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* velocity scaling
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
AccelSetScaleProperty(DeviceIntPtr dev, Atom atom,
|
|
|
|
XIPropertyValuePtr val, BOOL checkOnly)
|
2009-09-06 13:44:18 -06:00
|
|
|
{
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceVelocityPtr vel;
|
|
|
|
float v, *ptr = &v;
|
|
|
|
int rc;
|
|
|
|
int nelem = 1;
|
|
|
|
|
|
|
|
if (atom != XIGetKnownProperty(ACCEL_PROP_VELOCITY_SCALING))
|
|
|
|
return Success;
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
vel = GetDevicePredictableAccelData(dev);
|
|
|
|
if (!vel)
|
|
|
|
return BadValue;
|
|
|
|
rc = XIPropToFloat(val, &nelem, &ptr);
|
|
|
|
|
|
|
|
if (checkOnly)
|
|
|
|
{
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
return (v > 0) ? Success : BadValue;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
2010-07-27 13:02:24 -06:00
|
|
|
|
|
|
|
if(v > 0)
|
|
|
|
vel->corr_mul = v;
|
|
|
|
|
|
|
|
return Success;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
static long
|
|
|
|
AccelInitScaleProperty(DeviceIntPtr dev, DeviceVelocityPtr vel)
|
|
|
|
{
|
|
|
|
float fval = vel->corr_mul;
|
|
|
|
Atom prop_velo_scale = XIGetKnownProperty(ACCEL_PROP_VELOCITY_SCALING);
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
XIChangeDeviceProperty(dev, prop_velo_scale, XIGetKnownProperty(XATOM_FLOAT), 32,
|
|
|
|
PropModeReplace, 1, &fval, FALSE);
|
|
|
|
XISetDevicePropertyDeletable(dev, prop_velo_scale, FALSE);
|
|
|
|
return XIRegisterPropertyHandler(dev, AccelSetScaleProperty, NULL, NULL);
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
BOOL
|
|
|
|
InitializePredictableAccelerationProperties(DeviceIntPtr dev)
|
2009-09-06 13:44:18 -06:00
|
|
|
{
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceVelocityPtr vel = GetDevicePredictableAccelData(dev);
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
if(!vel)
|
|
|
|
return FALSE;
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-12-05 08:36:02 -07:00
|
|
|
vel->prop_handlers[0] = AccelInitProfileProperty(dev, vel);
|
|
|
|
vel->prop_handlers[1] = AccelInitDecelProperty(dev, vel);
|
|
|
|
vel->prop_handlers[2] = AccelInitAdaptDecelProperty(dev, vel);
|
|
|
|
vel->prop_handlers[3] = AccelInitScaleProperty(dev, vel);
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
return TRUE;
|
|
|
|
}
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
BOOL
|
|
|
|
DeletePredictableAccelerationProperties(DeviceIntPtr dev)
|
|
|
|
{
|
2010-12-05 08:36:02 -07:00
|
|
|
DeviceVelocityPtr vel;
|
2010-07-27 13:02:24 -06:00
|
|
|
Atom prop;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
prop = XIGetKnownProperty(ACCEL_PROP_VELOCITY_SCALING);
|
|
|
|
XIDeleteDeviceProperty(dev, prop, FALSE);
|
|
|
|
prop = XIGetKnownProperty(ACCEL_PROP_ADAPTIVE_DECELERATION);
|
|
|
|
XIDeleteDeviceProperty(dev, prop, FALSE);
|
|
|
|
prop = XIGetKnownProperty(ACCEL_PROP_CONSTANT_DECELERATION);
|
|
|
|
XIDeleteDeviceProperty(dev, prop, FALSE);
|
|
|
|
prop = XIGetKnownProperty(ACCEL_PROP_PROFILE_NUMBER);
|
|
|
|
XIDeleteDeviceProperty(dev, prop, FALSE);
|
|
|
|
|
2010-12-05 08:36:02 -07:00
|
|
|
vel = GetDevicePredictableAccelData(dev);
|
|
|
|
for (i = 0; vel && i < NPROPS_PREDICTABLE_ACCEL; i++)
|
|
|
|
if (vel->prop_handlers[i])
|
|
|
|
XIUnregisterPropertyHandler(dev, vel->prop_handlers[i]);
|
2010-07-27 13:02:24 -06:00
|
|
|
|
|
|
|
return TRUE;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
/*********************
|
|
|
|
* Tracking logic
|
|
|
|
********************/
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
void
|
|
|
|
InitTrackers(DeviceVelocityPtr vel, int ntracker)
|
|
|
|
{
|
|
|
|
if(ntracker < 1){
|
|
|
|
ErrorF("(dix ptracc) invalid number of trackers\n");
|
|
|
|
return;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
2010-12-05 08:36:02 -07:00
|
|
|
free(vel->tracker);
|
|
|
|
vel->tracker = (MotionTrackerPtr)malloc(ntracker * sizeof(MotionTracker));
|
2010-07-27 13:02:24 -06:00
|
|
|
memset(vel->tracker, 0, ntracker * sizeof(MotionTracker));
|
|
|
|
vel->num_tracker = ntracker;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-27 13:02:24 -06:00
|
|
|
* return a bit field of possible directions.
|
|
|
|
* 0 = N, 2 = E, 4 = S, 6 = W, in-between is as you guess.
|
|
|
|
* There's no reason against widening to more precise directions (<45 degrees),
|
|
|
|
* should it not perform well. All this is needed for is sort out non-linear
|
|
|
|
* motion, so precision isn't paramount. However, one should not flag direction
|
|
|
|
* too narrow, since it would then cut the linear segment to zero size way too
|
|
|
|
* often.
|
2009-09-06 13:44:18 -06:00
|
|
|
*/
|
2010-07-27 13:02:24 -06:00
|
|
|
static int
|
|
|
|
DoGetDirection(int dx, int dy){
|
|
|
|
float r;
|
|
|
|
int i1, i2;
|
|
|
|
/* on insignificant mickeys, flag 135 degrees */
|
|
|
|
if(abs(dx) < 2 && abs(dy < 2)){
|
|
|
|
/* first check diagonal cases */
|
|
|
|
if(dx > 0 && dy > 0)
|
|
|
|
return 4+8+16;
|
|
|
|
if(dx > 0 && dy < 0)
|
|
|
|
return 1+2+4;
|
|
|
|
if(dx < 0 && dy < 0)
|
|
|
|
return 1+128+64;
|
|
|
|
if(dx < 0 && dy > 0)
|
|
|
|
return 16+32+64;
|
|
|
|
/* check axis-aligned directions */
|
|
|
|
if(dx > 0)
|
|
|
|
return 2+4+8; /*E*/
|
|
|
|
if(dx < 0)
|
|
|
|
return 128+64+32; /*W*/
|
|
|
|
if(dy > 0)
|
|
|
|
return 32+16+8; /*S*/
|
|
|
|
if(dy < 0)
|
|
|
|
return 128+1+2; /*N*/
|
|
|
|
return 255; /* shouldn't happen */
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
2010-07-27 13:02:24 -06:00
|
|
|
/* else, compute angle and set appropriate flags */
|
|
|
|
#ifdef _ISOC99_SOURCE
|
|
|
|
r = atan2f(dy, dx);
|
|
|
|
#else
|
|
|
|
r = atan2(dy, dx);
|
|
|
|
#endif
|
|
|
|
/* find direction. We avoid r to become negative,
|
|
|
|
* since C has no well-defined modulo for such cases. */
|
|
|
|
r = (r+(M_PI*2.5))/(M_PI/4);
|
|
|
|
/* this intends to flag 2 directions (90 degrees),
|
|
|
|
* except on very well-aligned mickeys. */
|
|
|
|
i1 = (int)(r+0.1) % 8;
|
|
|
|
i2 = (int)(r+0.9) % 8;
|
|
|
|
if(i1 < 0 || i1 > 7 || i2 < 0 || i2 > 7)
|
|
|
|
return 255; /* shouldn't happen */
|
|
|
|
return 1 << i1 | 1 << i2;
|
|
|
|
}
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
#define DIRECTION_CACHE_RANGE 5
|
|
|
|
#define DIRECTION_CACHE_SIZE (DIRECTION_CACHE_RANGE*2+1)
|
|
|
|
|
|
|
|
/* cache DoGetDirection(). */
|
|
|
|
static int
|
|
|
|
GetDirection(int dx, int dy){
|
|
|
|
static int cache[DIRECTION_CACHE_SIZE][DIRECTION_CACHE_SIZE];
|
|
|
|
int i;
|
|
|
|
if (abs(dx) <= DIRECTION_CACHE_RANGE &&
|
|
|
|
abs(dy) <= DIRECTION_CACHE_RANGE) {
|
|
|
|
/* cacheable */
|
|
|
|
i = cache[DIRECTION_CACHE_RANGE+dx][DIRECTION_CACHE_RANGE+dy];
|
|
|
|
if(i != 0){
|
|
|
|
return i;
|
|
|
|
}else{
|
|
|
|
i = DoGetDirection(dx, dy);
|
|
|
|
cache[DIRECTION_CACHE_RANGE+dx][DIRECTION_CACHE_RANGE+dy] = i;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
/* non-cacheable */
|
|
|
|
return DoGetDirection(dx, dy);
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
2010-07-27 13:02:24 -06:00
|
|
|
}
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
#undef DIRECTION_CACHE_RANGE
|
|
|
|
#undef DIRECTION_CACHE_SIZE
|
2009-09-06 13:44:18 -06:00
|
|
|
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
/* convert offset (age) to array index */
|
|
|
|
#define TRACKER_INDEX(s, d) (((s)->num_tracker + (s)->cur_tracker - (d)) % (s)->num_tracker)
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
FeedTrackers(DeviceVelocityPtr vel, int dx, int dy, int cur_t)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
for(n = 0; n < vel->num_tracker; n++){
|
|
|
|
vel->tracker[n].dx += dx;
|
|
|
|
vel->tracker[n].dy += dy;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
2010-07-27 13:02:24 -06:00
|
|
|
n = (vel->cur_tracker + 1) % vel->num_tracker;
|
|
|
|
vel->tracker[n].dx = 0;
|
|
|
|
vel->tracker[n].dy = 0;
|
|
|
|
vel->tracker[n].time = cur_t;
|
|
|
|
vel->tracker[n].dir = GetDirection(dx, dy);
|
|
|
|
DebugAccelF("(dix prtacc) motion [dx: %i dy: %i dir:%i diff: %i]\n",
|
|
|
|
dx, dy, vel->tracker[n].dir,
|
|
|
|
cur_t - vel->tracker[vel->cur_tracker].time);
|
|
|
|
vel->cur_tracker = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* calc velocity for given tracker, with
|
|
|
|
* velocity scaling.
|
|
|
|
* This assumes linear motion.
|
|
|
|
*/
|
|
|
|
static float
|
|
|
|
CalcTracker(DeviceVelocityPtr vel, int offset, int cur_t){
|
|
|
|
int index = TRACKER_INDEX(vel, offset);
|
|
|
|
float dist = sqrt( vel->tracker[index].dx * vel->tracker[index].dx
|
|
|
|
+ vel->tracker[index].dy * vel->tracker[index].dy);
|
|
|
|
int dtime = cur_t - vel->tracker[index].time;
|
|
|
|
if(dtime > 0)
|
2010-12-05 08:36:02 -07:00
|
|
|
return dist / dtime;
|
2010-07-27 13:02:24 -06:00
|
|
|
else
|
|
|
|
return 0;/* synonymous for NaN, since we're not C99 */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the most plausible velocity. That is, the most distant
|
|
|
|
* (in time) tracker which isn't too old, beyond a linear partition,
|
|
|
|
* or simply too much off initial velocity.
|
|
|
|
*
|
|
|
|
* May return 0.
|
|
|
|
*/
|
|
|
|
static float
|
|
|
|
QueryTrackers(DeviceVelocityPtr vel, int cur_t){
|
|
|
|
int n, offset, dir = 255, i = -1, age_ms;
|
|
|
|
/* initial velocity: a low-offset, valid velocity */
|
|
|
|
float iveloc = 0, res = 0, tmp, vdiff;
|
|
|
|
float vfac = vel->corr_mul * vel->const_acceleration; /* premultiply */
|
|
|
|
/* loop from current to older data */
|
|
|
|
for(offset = 1; offset < vel->num_tracker; offset++){
|
|
|
|
n = TRACKER_INDEX(vel, offset);
|
|
|
|
|
|
|
|
age_ms = cur_t - vel->tracker[n].time;
|
|
|
|
|
|
|
|
/* bail out if data is too old and protect from overrun */
|
|
|
|
if (age_ms >= vel->reset_time || age_ms < 0) {
|
|
|
|
DebugAccelF("(dix prtacc) query: tracker too old\n");
|
|
|
|
break;
|
|
|
|
}
|
2009-09-06 13:44:18 -06:00
|
|
|
|
|
|
|
/*
|
2010-07-27 13:02:24 -06:00
|
|
|
* this heuristic avoids using the linear-motion velocity formula
|
|
|
|
* in CalcTracker() on motion that isn't exactly linear. So to get
|
|
|
|
* even more precision we could subdivide as a final step, so possible
|
|
|
|
* non-linearities are accounted for.
|
2009-09-06 13:44:18 -06:00
|
|
|
*/
|
2010-07-27 13:02:24 -06:00
|
|
|
dir &= vel->tracker[n].dir;
|
|
|
|
if(dir == 0){
|
|
|
|
DebugAccelF("(dix prtacc) query: no longer linear\n");
|
|
|
|
/* instead of breaking it we might also inspect the partition after,
|
|
|
|
* but actual improvement with this is probably rare. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = CalcTracker(vel, offset, cur_t) * vfac;
|
|
|
|
|
|
|
|
if ((iveloc == 0 || offset <= vel->initial_range) && tmp != 0) {
|
|
|
|
/* set initial velocity and result */
|
|
|
|
res = iveloc = tmp;
|
|
|
|
i = offset;
|
|
|
|
} else if (iveloc != 0 && tmp != 0) {
|
|
|
|
vdiff = fabs(iveloc - tmp);
|
|
|
|
if (vdiff <= vel->max_diff ||
|
|
|
|
vdiff/(iveloc + tmp) < vel->max_rel_diff) {
|
|
|
|
/* we're in range with the initial velocity,
|
|
|
|
* so this result is likely better
|
|
|
|
* (it contains more information). */
|
|
|
|
res = tmp;
|
|
|
|
i = offset;
|
|
|
|
}else{
|
|
|
|
/* we're not in range, quit - it won't get better. */
|
|
|
|
DebugAccelF("(dix prtacc) query: tracker too different:"
|
|
|
|
" old %2.2f initial %2.2f diff: %2.2f\n",
|
|
|
|
tmp, iveloc, vdiff);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(offset == vel->num_tracker){
|
|
|
|
DebugAccelF("(dix prtacc) query: last tracker in effect\n");
|
|
|
|
i = vel->num_tracker-1;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
2010-07-27 13:02:24 -06:00
|
|
|
if(i>=0){
|
|
|
|
n = TRACKER_INDEX(vel, i);
|
|
|
|
DebugAccelF("(dix prtacc) result: offset %i [dx: %i dy: %i diff: %i]\n",
|
|
|
|
i,
|
|
|
|
vel->tracker[n].dx,
|
|
|
|
vel->tracker[n].dy,
|
|
|
|
cur_t - vel->tracker[n].time);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef TRACKER_INDEX
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform velocity approximation based on 2D 'mickeys' (mouse motion delta).
|
|
|
|
* return true if non-visible state reset is suggested
|
|
|
|
*/
|
|
|
|
short
|
|
|
|
ProcessVelocityData2D(
|
|
|
|
DeviceVelocityPtr vel,
|
|
|
|
int dx,
|
|
|
|
int dy,
|
|
|
|
int time)
|
|
|
|
{
|
|
|
|
float velocity;
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
vel->last_velocity = vel->velocity;
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
FeedTrackers(vel, dx, dy, time);
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
velocity = QueryTrackers(vel, time);
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
vel->velocity = velocity;
|
|
|
|
return velocity == 0;
|
|
|
|
}
|
2009-09-06 13:44:18 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* this flattens significant ( > 1) mickeys a little bit for more steady
|
|
|
|
* constant-velocity response
|
|
|
|
*/
|
|
|
|
static inline float
|
|
|
|
ApplySimpleSoftening(int od, int d)
|
|
|
|
{
|
|
|
|
float res = d;
|
|
|
|
if (d <= 1 && d >= -1)
|
|
|
|
return res;
|
|
|
|
if (d > od)
|
|
|
|
res -= 0.5;
|
|
|
|
else if (d < od)
|
|
|
|
res += 0.5;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ApplySofteningAndConstantDeceleration(
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceVelocityPtr vel,
|
2009-09-06 13:44:18 -06:00
|
|
|
int dx,
|
|
|
|
int dy,
|
|
|
|
float* fdx,
|
|
|
|
float* fdy,
|
|
|
|
short do_soften)
|
|
|
|
{
|
2010-07-27 13:02:24 -06:00
|
|
|
if (do_soften && vel->use_softening) {
|
|
|
|
*fdx = ApplySimpleSoftening(vel->last_dx, dx);
|
|
|
|
*fdy = ApplySimpleSoftening(vel->last_dy, dy);
|
2009-09-06 13:44:18 -06:00
|
|
|
} else {
|
|
|
|
*fdx = dx;
|
|
|
|
*fdy = dy;
|
|
|
|
}
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
*fdx *= vel->const_acceleration;
|
|
|
|
*fdy *= vel->const_acceleration;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compute the acceleration for given velocity and enforce min_acceleartion
|
|
|
|
*/
|
2010-07-27 13:02:24 -06:00
|
|
|
float
|
2009-09-06 13:44:18 -06:00
|
|
|
BasicComputeAcceleration(
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceIntPtr dev,
|
|
|
|
DeviceVelocityPtr vel,
|
2009-09-06 13:44:18 -06:00
|
|
|
float velocity,
|
|
|
|
float threshold,
|
|
|
|
float acc){
|
|
|
|
|
|
|
|
float result;
|
2010-07-27 13:02:24 -06:00
|
|
|
result = vel->Profile(dev, vel, velocity, threshold, acc);
|
2009-09-06 13:44:18 -06:00
|
|
|
|
|
|
|
/* enforce min_acceleration */
|
2010-07-27 13:02:24 -06:00
|
|
|
if (result < vel->min_acceleration)
|
|
|
|
result = vel->min_acceleration;
|
2009-09-06 13:44:18 -06:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute acceleration. Takes into account averaging, nv-reset, etc.
|
|
|
|
*/
|
|
|
|
static float
|
|
|
|
ComputeAcceleration(
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceIntPtr dev,
|
2009-09-06 13:44:18 -06:00
|
|
|
DeviceVelocityPtr vel,
|
|
|
|
float threshold,
|
|
|
|
float acc){
|
|
|
|
float res;
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
if(vel->velocity <= 0){
|
2009-09-06 13:44:18 -06:00
|
|
|
DebugAccelF("(dix ptracc) profile skipped\n");
|
|
|
|
/*
|
2010-07-27 13:02:24 -06:00
|
|
|
* If we have no idea about device velocity, don't pretend it.
|
2009-09-06 13:44:18 -06:00
|
|
|
*/
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(vel->average_accel && vel->velocity != vel->last_velocity){
|
|
|
|
/* use simpson's rule to average acceleration between
|
|
|
|
* current and previous velocity.
|
|
|
|
* Though being the more natural choice, it causes a minor delay
|
|
|
|
* in comparison, so it can be disabled. */
|
2010-07-27 13:02:24 -06:00
|
|
|
res = BasicComputeAcceleration(
|
|
|
|
dev, vel, vel->velocity, threshold, acc);
|
|
|
|
res += BasicComputeAcceleration(
|
|
|
|
dev, vel, vel->last_velocity, threshold, acc);
|
|
|
|
res += 4.0f * BasicComputeAcceleration(dev, vel,
|
2009-09-06 13:44:18 -06:00
|
|
|
(vel->last_velocity + vel->velocity) / 2,
|
|
|
|
threshold, acc);
|
|
|
|
res /= 6.0f;
|
|
|
|
DebugAccelF("(dix ptracc) profile average [%.2f ... %.2f] is %.3f\n",
|
|
|
|
vel->velocity, vel->last_velocity, res);
|
|
|
|
return res;
|
|
|
|
}else{
|
2010-07-27 13:02:24 -06:00
|
|
|
res = BasicComputeAcceleration(dev, vel,
|
|
|
|
vel->velocity, threshold, acc);
|
2009-09-06 13:44:18 -06:00
|
|
|
DebugAccelF("(dix ptracc) profile sample [%.2f] is %.3f\n",
|
|
|
|
vel->velocity, res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************
|
|
|
|
* Acceleration functions and profiles
|
|
|
|
****************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Polynomial function similar previous one, but with f(1) = 1
|
|
|
|
*/
|
|
|
|
static float
|
|
|
|
PolynomialAccelerationProfile(
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceIntPtr dev,
|
|
|
|
DeviceVelocityPtr vel,
|
2009-09-06 13:44:18 -06:00
|
|
|
float velocity,
|
|
|
|
float ignored,
|
|
|
|
float acc)
|
|
|
|
{
|
|
|
|
return pow(velocity, (acc - 1.0) * 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns acceleration for velocity.
|
|
|
|
* This profile selects the two functions like the old scheme did
|
|
|
|
*/
|
|
|
|
static float
|
|
|
|
ClassicProfile(
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceIntPtr dev,
|
|
|
|
DeviceVelocityPtr vel,
|
2009-09-06 13:44:18 -06:00
|
|
|
float velocity,
|
|
|
|
float threshold,
|
|
|
|
float acc)
|
|
|
|
{
|
2010-07-27 13:02:24 -06:00
|
|
|
if (threshold > 0) {
|
|
|
|
return SimpleSmoothProfile (dev,
|
|
|
|
vel,
|
2009-09-06 13:44:18 -06:00
|
|
|
velocity,
|
|
|
|
threshold,
|
|
|
|
acc);
|
|
|
|
} else {
|
2010-07-27 13:02:24 -06:00
|
|
|
return PolynomialAccelerationProfile (dev,
|
|
|
|
vel,
|
2009-09-06 13:44:18 -06:00
|
|
|
velocity,
|
|
|
|
0,
|
|
|
|
acc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Power profile
|
|
|
|
* This has a completely smooth transition curve, i.e. no jumps in the
|
|
|
|
* derivatives.
|
|
|
|
*
|
|
|
|
* This has the expense of overall response dependency on min-acceleration.
|
|
|
|
* In effect, min_acceleration mimics const_acceleration in this profile.
|
|
|
|
*/
|
|
|
|
static float
|
|
|
|
PowerProfile(
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceIntPtr dev,
|
|
|
|
DeviceVelocityPtr vel,
|
2009-09-06 13:44:18 -06:00
|
|
|
float velocity,
|
|
|
|
float threshold,
|
|
|
|
float acc)
|
|
|
|
{
|
|
|
|
float vel_dist;
|
|
|
|
|
|
|
|
acc = (acc-1.0) * 0.1f + 1.0; /* without this, acc of 2 is unuseable */
|
|
|
|
|
|
|
|
if (velocity <= threshold)
|
2010-07-27 13:02:24 -06:00
|
|
|
return vel->min_acceleration;
|
2009-09-06 13:44:18 -06:00
|
|
|
vel_dist = velocity - threshold;
|
2010-07-27 13:02:24 -06:00
|
|
|
return (pow(acc, vel_dist)) * vel->min_acceleration;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* just a smooth function in [0..1] -> [0..1]
|
|
|
|
* - point symmetry at 0.5
|
|
|
|
* - f'(0) = f'(1) = 0
|
|
|
|
* - starts faster than a sinoid
|
|
|
|
* - smoothness C1 (Cinf if you dare to ignore endpoints)
|
|
|
|
*/
|
|
|
|
static inline float
|
|
|
|
CalcPenumbralGradient(float x){
|
|
|
|
x *= 2.0f;
|
|
|
|
x -= 1.0f;
|
|
|
|
return 0.5f + (x * sqrt(1.0f - x*x) + asin(x))/M_PI;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* acceleration function similar to classic accelerated/unaccelerated,
|
|
|
|
* but with smooth transition in between (and towards zero for adaptive dec.).
|
|
|
|
*/
|
|
|
|
static float
|
|
|
|
SimpleSmoothProfile(
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceIntPtr dev,
|
|
|
|
DeviceVelocityPtr vel,
|
2009-09-06 13:44:18 -06:00
|
|
|
float velocity,
|
|
|
|
float threshold,
|
|
|
|
float acc)
|
|
|
|
{
|
|
|
|
if(velocity < 1.0f)
|
|
|
|
return CalcPenumbralGradient(0.5 + velocity*0.5) * 2.0f - 1.0f;
|
|
|
|
if(threshold < 1.0f)
|
|
|
|
threshold = 1.0f;
|
|
|
|
if (velocity <= threshold)
|
|
|
|
return 1;
|
|
|
|
velocity /= threshold;
|
|
|
|
if (velocity >= acc)
|
|
|
|
return acc;
|
|
|
|
else
|
|
|
|
return 1.0f + (CalcPenumbralGradient(velocity/acc) * (acc - 1.0f));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This profile uses the first half of the penumbral gradient as a start
|
|
|
|
* and then scales linearly.
|
|
|
|
*/
|
|
|
|
static float
|
|
|
|
SmoothLinearProfile(
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceIntPtr dev,
|
|
|
|
DeviceVelocityPtr vel,
|
2009-09-06 13:44:18 -06:00
|
|
|
float velocity,
|
|
|
|
float threshold,
|
|
|
|
float acc)
|
|
|
|
{
|
|
|
|
float res, nv;
|
|
|
|
|
|
|
|
if(acc > 1.0f)
|
|
|
|
acc -= 1.0f; /*this is so acc = 1 is no acceleration */
|
|
|
|
else
|
|
|
|
return 1.0f;
|
|
|
|
|
|
|
|
nv = (velocity - threshold) * acc * 0.5f;
|
|
|
|
|
|
|
|
if(nv < 0){
|
|
|
|
res = 0;
|
|
|
|
}else if(nv < 2){
|
|
|
|
res = CalcPenumbralGradient(nv*0.25f)*2.0f;
|
|
|
|
}else{
|
|
|
|
nv -= 2.0f;
|
|
|
|
res = nv * 2.0f / M_PI /* steepness of gradient at 0.5 */
|
|
|
|
+ 1.0f; /* gradient crosses 2|1 */
|
|
|
|
}
|
2010-07-27 13:02:24 -06:00
|
|
|
res += vel->min_acceleration;
|
2009-09-06 13:44:18 -06:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
/**
|
|
|
|
* From 0 to threshold, the response graduates smoothly from min_accel to
|
|
|
|
* acceleration. Beyond threshold it is exactly the specified acceleration.
|
|
|
|
*/
|
|
|
|
static float
|
|
|
|
SmoothLimitedProfile(
|
|
|
|
DeviceIntPtr dev,
|
|
|
|
DeviceVelocityPtr vel,
|
|
|
|
float velocity,
|
|
|
|
float threshold,
|
|
|
|
float acc)
|
|
|
|
{
|
|
|
|
float res;
|
|
|
|
|
|
|
|
if(velocity >= threshold || threshold == 0.0f)
|
|
|
|
return acc;
|
|
|
|
|
|
|
|
velocity /= threshold; /* should be [0..1[ now */
|
|
|
|
|
|
|
|
res = CalcPenumbralGradient(velocity) * (acc - vel->min_acceleration);
|
|
|
|
|
|
|
|
return vel->min_acceleration + res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-06 13:44:18 -06:00
|
|
|
static float
|
|
|
|
LinearProfile(
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceIntPtr dev,
|
|
|
|
DeviceVelocityPtr vel,
|
2009-09-06 13:44:18 -06:00
|
|
|
float velocity,
|
|
|
|
float threshold,
|
|
|
|
float acc)
|
|
|
|
{
|
|
|
|
return acc * velocity;
|
|
|
|
}
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
static float
|
|
|
|
NoProfile(
|
|
|
|
DeviceIntPtr dev,
|
|
|
|
DeviceVelocityPtr vel,
|
|
|
|
float velocity,
|
|
|
|
float threshold,
|
|
|
|
float acc)
|
|
|
|
{
|
|
|
|
return 1.0f;
|
|
|
|
}
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
static PointerAccelerationProfileFunc
|
|
|
|
GetAccelerationProfile(
|
|
|
|
DeviceVelocityPtr vel,
|
2009-09-06 13:44:18 -06:00
|
|
|
int profile_num)
|
|
|
|
{
|
|
|
|
switch(profile_num){
|
|
|
|
case AccelProfileClassic:
|
2010-07-27 13:02:24 -06:00
|
|
|
return ClassicProfile;
|
2009-09-06 13:44:18 -06:00
|
|
|
case AccelProfileDeviceSpecific:
|
2010-07-27 13:02:24 -06:00
|
|
|
return vel->deviceSpecificProfile;
|
2009-09-06 13:44:18 -06:00
|
|
|
case AccelProfilePolynomial:
|
2010-07-27 13:02:24 -06:00
|
|
|
return PolynomialAccelerationProfile;
|
2009-09-06 13:44:18 -06:00
|
|
|
case AccelProfileSmoothLinear:
|
2010-07-27 13:02:24 -06:00
|
|
|
return SmoothLinearProfile;
|
2009-09-06 13:44:18 -06:00
|
|
|
case AccelProfileSimple:
|
2010-07-27 13:02:24 -06:00
|
|
|
return SimpleSmoothProfile;
|
2009-09-06 13:44:18 -06:00
|
|
|
case AccelProfilePower:
|
2010-07-27 13:02:24 -06:00
|
|
|
return PowerProfile;
|
2009-09-06 13:44:18 -06:00
|
|
|
case AccelProfileLinear:
|
2010-07-27 13:02:24 -06:00
|
|
|
return LinearProfile;
|
|
|
|
case AccelProfileSmoothLimited:
|
|
|
|
return SmoothLimitedProfile;
|
|
|
|
case AccelProfileNone:
|
|
|
|
return NoProfile;
|
2009-09-06 13:44:18 -06:00
|
|
|
default:
|
2010-07-27 13:02:24 -06:00
|
|
|
return NULL;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
2010-07-27 13:02:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the profile by number.
|
|
|
|
* Intended to make profiles exchangeable at runtime.
|
|
|
|
* If you created a profile, give it a number here and in the header to
|
|
|
|
* make it selectable. In case some profile-specific init is needed, here
|
|
|
|
* would be a good place, since FreeVelocityData() also calls this with
|
|
|
|
* PROFILE_UNINITIALIZE.
|
|
|
|
*
|
|
|
|
* returns FALSE if profile number is unavailable, TRUE otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
SetAccelerationProfile(
|
|
|
|
DeviceVelocityPtr vel,
|
|
|
|
int profile_num)
|
|
|
|
{
|
|
|
|
PointerAccelerationProfileFunc profile;
|
|
|
|
profile = GetAccelerationProfile(vel, profile_num);
|
|
|
|
|
|
|
|
if(profile == NULL && profile_num != PROFILE_UNINITIALIZE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if(vel->profile_private != NULL){
|
2009-09-06 13:44:18 -06:00
|
|
|
/* Here one could free old profile-private data */
|
2010-12-05 08:36:02 -07:00
|
|
|
free(vel->profile_private);
|
2010-07-27 13:02:24 -06:00
|
|
|
vel->profile_private = NULL;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
/* Here one could init profile-private data */
|
2010-07-27 13:02:24 -06:00
|
|
|
vel->Profile = profile;
|
|
|
|
vel->statistics.profile_number = profile_num;
|
2009-09-06 13:44:18 -06:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************
|
|
|
|
* driver interaction
|
|
|
|
**********************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* device-specific profile
|
|
|
|
*
|
|
|
|
* The device-specific profile is intended as a hook for a driver
|
|
|
|
* which may want to provide an own acceleration profile.
|
|
|
|
* It should not rely on profile-private data, instead
|
|
|
|
* it should do init/uninit in the driver (ie. with DEVICE_INIT and friends).
|
|
|
|
* Users may override or choose it.
|
|
|
|
*/
|
2010-07-27 13:02:24 -06:00
|
|
|
void
|
2009-09-06 13:44:18 -06:00
|
|
|
SetDeviceSpecificAccelerationProfile(
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceVelocityPtr vel,
|
2009-09-06 13:44:18 -06:00
|
|
|
PointerAccelerationProfileFunc profile)
|
|
|
|
{
|
2010-07-27 13:02:24 -06:00
|
|
|
if(vel)
|
|
|
|
vel->deviceSpecificProfile = profile;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use this function to obtain a DeviceVelocityPtr for a device. Will return NULL if
|
|
|
|
* the predictable acceleration scheme is not in effect.
|
|
|
|
*/
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceVelocityPtr
|
2009-09-06 13:44:18 -06:00
|
|
|
GetDevicePredictableAccelData(
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceIntPtr dev)
|
2009-09-06 13:44:18 -06:00
|
|
|
{
|
|
|
|
/*sanity check*/
|
2010-07-27 13:02:24 -06:00
|
|
|
if(!dev){
|
2009-09-06 13:44:18 -06:00
|
|
|
ErrorF("[dix] accel: DeviceIntPtr was NULL");
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-07-27 13:02:24 -06:00
|
|
|
if( dev->valuator &&
|
|
|
|
dev->valuator->accelScheme.AccelSchemeProc ==
|
2009-09-06 13:44:18 -06:00
|
|
|
acceleratePointerPredictable &&
|
2010-07-27 13:02:24 -06:00
|
|
|
dev->valuator->accelScheme.accelData != NULL){
|
2009-09-06 13:44:18 -06:00
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
return (DeviceVelocityPtr)dev->valuator->accelScheme.accelData;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************
|
|
|
|
* acceleration schemes
|
|
|
|
*******************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modifies valuators in-place.
|
|
|
|
* This version employs a velocity approximation algorithm to
|
|
|
|
* enable fine-grained predictable acceleration profiles.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
acceleratePointerPredictable(
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceIntPtr dev,
|
2009-09-06 13:44:18 -06:00
|
|
|
int first_valuator,
|
|
|
|
int num_valuators,
|
|
|
|
int *valuators,
|
|
|
|
int evtime)
|
|
|
|
{
|
|
|
|
float mult = 0.0;
|
|
|
|
int dx = 0, dy = 0;
|
|
|
|
int *px = NULL, *py = NULL;
|
|
|
|
DeviceVelocityPtr velocitydata =
|
2010-07-27 13:02:24 -06:00
|
|
|
(DeviceVelocityPtr) dev->valuator->accelScheme.accelData;
|
|
|
|
float fdx, fdy, tmp; /* no need to init */
|
|
|
|
Bool soften = TRUE;
|
2009-09-06 13:44:18 -06:00
|
|
|
|
|
|
|
if (!num_valuators || !valuators || !velocitydata)
|
|
|
|
return;
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
if (velocitydata->statistics.profile_number == AccelProfileNone &&
|
|
|
|
velocitydata->const_acceleration == 1.0f) {
|
|
|
|
return; /*we're inactive anyway, so skip the whole thing.*/
|
|
|
|
}
|
|
|
|
|
2009-09-06 13:44:18 -06:00
|
|
|
if (first_valuator == 0) {
|
|
|
|
dx = valuators[0];
|
|
|
|
px = &valuators[0];
|
|
|
|
}
|
|
|
|
if (first_valuator <= 1 && num_valuators >= (2 - first_valuator)) {
|
|
|
|
dy = valuators[1 - first_valuator];
|
|
|
|
py = &valuators[1 - first_valuator];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dx || dy){
|
2010-07-27 13:02:24 -06:00
|
|
|
/* reset non-visible state? */
|
|
|
|
if (ProcessVelocityData2D(velocitydata, dx , dy, evtime)) {
|
|
|
|
soften = FALSE;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
if (dev->ptrfeed && dev->ptrfeed->ctrl.num) {
|
2009-09-06 13:44:18 -06:00
|
|
|
/* invoke acceleration profile to determine acceleration */
|
2010-07-27 13:02:24 -06:00
|
|
|
mult = ComputeAcceleration (dev, velocitydata,
|
|
|
|
dev->ptrfeed->ctrl.threshold,
|
|
|
|
(float)dev->ptrfeed->ctrl.num /
|
|
|
|
(float)dev->ptrfeed->ctrl.den);
|
2009-09-06 13:44:18 -06:00
|
|
|
|
|
|
|
if(mult != 1.0 || velocitydata->const_acceleration != 1.0) {
|
|
|
|
ApplySofteningAndConstantDeceleration( velocitydata,
|
2010-07-27 13:02:24 -06:00
|
|
|
dx, dy,
|
|
|
|
&fdx, &fdy,
|
|
|
|
(mult > 1.0) && soften);
|
|
|
|
|
2009-09-06 13:44:18 -06:00
|
|
|
if (dx) {
|
2010-07-27 13:02:24 -06:00
|
|
|
tmp = mult * fdx + dev->last.remainder[0];
|
|
|
|
/* Since it may not be apparent: lrintf() does not offer
|
|
|
|
* strong statements about rounding; however because we
|
|
|
|
* process each axis conditionally, there's no danger
|
|
|
|
* of a toggling remainder. Its lack of guarantees likely
|
|
|
|
* makes it faster on the average target. */
|
|
|
|
*px = lrintf(tmp);
|
|
|
|
dev->last.remainder[0] = tmp - (float)*px;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
if (dy) {
|
2010-07-27 13:02:24 -06:00
|
|
|
tmp = mult * fdy + dev->last.remainder[1];
|
|
|
|
*py = lrintf(tmp);
|
|
|
|
dev->last.remainder[1] = tmp - (float)*py;
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
2010-07-27 13:02:24 -06:00
|
|
|
DebugAccelF("pos (%i | %i) remainders x: %.3f y: %.3f delta x:%.3f y:%.3f\n",
|
|
|
|
*px, *py, dev->last.remainder[0], dev->last.remainder[1], fdx, fdy);
|
2009-09-06 13:44:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* remember last motion delta (for softening/slow movement treatment) */
|
|
|
|
velocitydata->last_dx = dx;
|
|
|
|
velocitydata->last_dy = dy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Originally a part of xf86PostMotionEvent; modifies valuators
|
|
|
|
* in-place. Retained mostly for embedded scenarios.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
acceleratePointerLightweight(
|
2010-07-27 13:02:24 -06:00
|
|
|
DeviceIntPtr dev,
|
2009-09-06 13:44:18 -06:00
|
|
|
int first_valuator,
|
|
|
|
int num_valuators,
|
|
|
|
int *valuators,
|
|
|
|
int ignored)
|
|
|
|
{
|
|
|
|
float mult = 0.0;
|
|
|
|
int dx = 0, dy = 0;
|
|
|
|
int *px = NULL, *py = NULL;
|
|
|
|
|
|
|
|
if (!num_valuators || !valuators)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (first_valuator == 0) {
|
|
|
|
dx = valuators[0];
|
|
|
|
px = &valuators[0];
|
|
|
|
}
|
|
|
|
if (first_valuator <= 1 && num_valuators >= (2 - first_valuator)) {
|
|
|
|
dy = valuators[1 - first_valuator];
|
|
|
|
py = &valuators[1 - first_valuator];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dx && !dy)
|
|
|
|
return;
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
if (dev->ptrfeed && dev->ptrfeed->ctrl.num) {
|
2009-09-06 13:44:18 -06:00
|
|
|
/* modeled from xf86Events.c */
|
2010-07-27 13:02:24 -06:00
|
|
|
if (dev->ptrfeed->ctrl.threshold) {
|
|
|
|
if ((abs(dx) + abs(dy)) >= dev->ptrfeed->ctrl.threshold) {
|
|
|
|
dev->last.remainder[0] = ((float)dx *
|
|
|
|
(float)(dev->ptrfeed->ctrl.num)) /
|
|
|
|
(float)(dev->ptrfeed->ctrl.den) +
|
|
|
|
dev->last.remainder[0];
|
2009-09-06 13:44:18 -06:00
|
|
|
if (px) {
|
2010-07-27 13:02:24 -06:00
|
|
|
*px = (int)dev->last.remainder[0];
|
|
|
|
dev->last.remainder[0] = dev->last.remainder[0] -
|
2009-09-06 13:44:18 -06:00
|
|
|
(float)(*px);
|
|
|
|
}
|
|
|
|
|
2010-07-27 13:02:24 -06:00
|
|
|
dev->last.remainder[1] = ((float)dy *
|
|
|
|
(float)(dev->ptrfeed->ctrl.num)) /
|
|
|
|
(float)(dev->ptrfeed->ctrl.den) +
|
|
|
|
dev->last.remainder[1];
|
2009-09-06 13:44:18 -06:00
|
|
|
if (py) {
|
2010-07-27 13:02:24 -06:00
|
|
|
*py = (int)dev->last.remainder[1];
|
|
|
|
dev->last.remainder[1] = dev->last.remainder[1] -
|
2009-09-06 13:44:18 -06:00
|
|
|
(float)(*py);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mult = pow((float)dx * (float)dx + (float)dy * (float)dy,
|
2010-07-27 13:02:24 -06:00
|
|
|
((float)(dev->ptrfeed->ctrl.num) /
|
|
|
|
(float)(dev->ptrfeed->ctrl.den) - 1.0) /
|
2009-09-06 13:44:18 -06:00
|
|
|
2.0) / 2.0;
|
|
|
|
if (dx) {
|
2010-07-27 13:02:24 -06:00
|
|
|
dev->last.remainder[0] = mult * (float)dx +
|
|
|
|
dev->last.remainder[0];
|
|
|
|
*px = (int)dev->last.remainder[0];
|
|
|
|
dev->last.remainder[0] = dev->last.remainder[0] -
|
2009-09-06 13:44:18 -06:00
|
|
|
(float)(*px);
|
|
|
|
}
|
|
|
|
if (dy) {
|
2010-07-27 13:02:24 -06:00
|
|
|
dev->last.remainder[1] = mult * (float)dy +
|
|
|
|
dev->last.remainder[1];
|
|
|
|
*py = (int)dev->last.remainder[1];
|
|
|
|
dev->last.remainder[1] = dev->last.remainder[1] -
|
2009-09-06 13:44:18 -06:00
|
|
|
(float)(*py);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|