# On branch master # Changes to be committed: # (use "git reset HEAD ..." to unstage) # # modified: Xext/dpmsstubs.c # modified: Xi/chgdctl.c # modified: Xi/getdctl.c # modified: dix/Makefile.am # modified: dix/devices.c # modified: dix/getevents.c # new file: dix/ptrveloc.c # modified: hw/xfree86/common/xf86Xinput.c # modified: include/input.h # modified: include/inputstr.h # new file: include/ptrveloc.h # # Untracked files: # (use "git add ..." to include in what will be committed) # # Xi/getdctl.c.orig # dix/Makefile.am.orig # dix/Makefile.am.rej # dix/devices.c.orig # hw/xfree86/common/xf86Xinput.c.orig # include/input.h.orig # include/inputstr.h.orig diff --git a/Xext/dpmsstubs.c b/Xext/dpmsstubs.c index fad07bd..8b3ce4e 100644 --- a/Xext/dpmsstubs.c +++ b/Xext/dpmsstubs.c @@ -26,7 +26,7 @@ Equipment Corporation. ******************************************************************/ -typedef int Bool; +//typedef int Bool; #ifdef HAVE_DIX_CONFIG_H #include diff --git a/Xi/chgdctl.c b/Xi/chgdctl.c index e7d04a7..51ced03 100644 --- a/Xi/chgdctl.c +++ b/Xi/chgdctl.c @@ -105,6 +105,7 @@ ProcXChangeDeviceControl(ClientPtr client) xDeviceAbsAreaCtl *area; xDeviceCoreCtl *c; xDeviceEnableCtl *e; + xDeviceAccelCtl *accel; devicePresenceNotify dpn; REQUEST(xChangeDeviceControlReq); @@ -244,6 +245,46 @@ ProcXChangeDeviceControl(ClientPtr client) } break; + case DEVICE_PTRACCEL: + if(dev->valuator){ + accel = (xDeviceAccelCtl*)&stuff[1]; + if(accel->scheme != dev->valuator->accelScheme.number){ + i = dev->valuator->accelScheme.number; /* save scheme number*/ + /* exchange scheme */ + DebugF("(ptr devctrl) %s accel scheme %i -> %i\n", + dev->name, i, accel->scheme); + + if(dev->valuator->accelScheme.AccelCleanupProc){ + dev->valuator->accelScheme.AccelCleanupProc(dev); + } + status = InitPointerAccelerationScheme(dev, accel->scheme); + if(status != TRUE){ + ret = BadValue; + DebugF("(ptr devctrl) scheme %i unavailable." \ + "Trying to restore %i\n", accel->scheme, i); + InitPointerAccelerationScheme(dev, i); + break; + } + } + /* invoke scheme's devctrl handler if approriate */ + if(accel->length > sizeof(xDeviceAccelCtl) + && dev->valuator->accelScheme.HandleChangeDeviceConrol) + { + ret = dev->valuator->accelScheme.HandleChangeDeviceConrol ( + client, + dev, + accel); + DebugF("(ptr devctrl) scheme ChgDevCtrl handler: %s\n", + ret==Success?"OK":"Failure"); + }else{ + /* if we got only a scheme, this is OK */ + ret = (accel->length == sizeof(xDeviceAccelCtl))? + Success:BadMatch; + } + }else{ + ret = BadMatch; + } + break; default: ret = BadValue; } diff --git a/Xi/getdctl.c b/Xi/getdctl.c index 6e1e3ef..8be0d66 100644 --- a/Xi/getdctl.c +++ b/Xi/getdctl.c @@ -211,6 +211,40 @@ static void CopySwapDeviceEnable (ClientPtr client, DeviceIntPtr dev, char *buf) } } +static int +CopySwapDeviceAccel (ClientPtr client, + DeviceIntPtr dev, + char *buf, + int bufferlen) +{ + char n; + int rc; + xDeviceAccelState *e = (xDeviceAccelState*) buf; + + e->control = DEVICE_PTRACCEL; + e->length = bufferlen; + e->scheme = dev->valuator->accelScheme.number; + + if(bufferlen == sizeof(xDeviceAccelState)){ + rc = Success; + }else{ + if(dev->valuator->accelScheme.HandleGetDeviceConrol) + rc = dev->valuator->accelScheme.HandleGetDeviceConrol(client, dev, e); + else{ + rc = BadMatch; + ErrorF("false internal length"); + } + } + + if (client->swapped) { + swaps(&e->control, n); + swaps(&e->length, n); + swaps(&e->scheme, n); + } + return rc; +} + + /*********************************************************************** * * This procedure writes the reply for the xGetDeviceControl function, @@ -279,8 +313,20 @@ ProcXGetDeviceControl(ClientPtr client) case DEVICE_ENABLE: total_length = sizeof(xDeviceEnableCtl); break; + case DEVICE_PTRACCEL: + if (!dev->valuator) + return BadMatch; + switch(dev->valuator->accelScheme.number){ + case PtrAccelPredictable: + total_length = sizeof(xDeviceAccelPredictableState); + break; + default: + total_length = sizeof(xDeviceAccelState); + break; + } + break; default: - return BadValue; + return BadValue; } buf = (char *)xalloc(total_length); @@ -304,6 +350,13 @@ ProcXGetDeviceControl(ClientPtr client) case DEVICE_ENABLE: CopySwapDeviceEnable(client, dev, buf); break; + case DEVICE_PTRACCEL: + rc = CopySwapDeviceAccel(client, dev, buf, total_length); + if(rc != Success){ + xfree(savbuf); + return rc; + } + break; default: break; } diff --git a/dix/Makefile.am b/dix/Makefile.am index 2cf9014..0d0b217 100644 --- a/dix/Makefile.am +++ b/dix/Makefile.am @@ -27,6 +27,7 @@ libdix_la_SOURCES = \ pixmap.c \ privates.c \ property.c \ + ptrveloc.c \ registry.c \ resource.c \ swaprep.c \ diff --git a/dix/devices.c b/dix/devices.c index adf2fba..16049a1 100644 --- a/dix/devices.c +++ b/dix/devices.c @@ -62,6 +62,7 @@ SOFTWARE. #include "scrnintstr.h" #include "cursorstr.h" #include "dixstruct.h" +#include "ptrveloc.h" #include "site.h" #ifndef XKB_IN_SERVER #define XKB_IN_SERVER @@ -205,6 +206,10 @@ EnableDevice(DeviceIntPtr dev) ; *prev = dev; dev->next = NULL; + + /*if(dev->valuator && dev->valuator->AccelInitProc){ + dev->valuator->AccelInitProc(dev); + }*/ ev.type = DevicePresenceNotify; ev.time = currentTime.milliseconds; @@ -531,6 +536,10 @@ CloseDevice(DeviceIntPtr dev) } if (dev->valuator) { + /* free pointer acceleration stuff */ + if(NULL != dev->valuator->accelScheme.AccelCleanupProc){ + dev->valuator->accelScheme.AccelCleanupProc(dev); + } /* Counterpart to 'biggest hack ever' in init. */ if (dev->valuator->motion && dev->valuator->GetMotionProc == GetMotionHistory) @@ -934,6 +943,7 @@ InitValuatorClassDeviceStruct(DeviceIntPtr dev, int numAxes, valc->dxremaind = 0; valc->dyremaind = 0; dev->valuator = valc; + InitPointerAccelerationScheme(dev, 1); /* biggest hack ever. */ if (motionProc == GetMotionHistory) @@ -946,6 +956,57 @@ InitValuatorClassDeviceStruct(DeviceIntPtr dev, int numAxes, return TRUE; } +/* global list of acceleration schemes */ +ValuatorAccelerationRec pointerAccelerationScheme[] = { + {0, NULL, NULL, NULL, NULL, NULL}, + {1, acceleratePointerPredictable, NULL, HandleRequestGetAccelDevCtrl, + HandleRequestChangeAccelDevCtrl, AccelerationDefaultCleanup}, + {2, acceleratePointerClassic, NULL, NULL, NULL, NULL}, + {-1, NULL, NULL, NULL, NULL, NULL} /* terminator */ +}; + +_X_EXPORT Bool +InitPointerAccelerationScheme(DeviceIntPtr dev, + int scheme) +{ + int x, i = -1; + for(x = 0; pointerAccelerationScheme[x].number != -1; x++) { + if(pointerAccelerationScheme[x].number == scheme){ + i = x; + break; + } + } + + if(-1 == i) + return FALSE; + + void* data = NULL; + + /* init scheme-specific data */ + switch(scheme){ + case PtrAccelPredictable: + { + DeviceVelocityPtr s; + s = (DeviceVelocityPtr)xalloc(sizeof(DeviceVelocityData)); + InitVelocityData(s); + data = s; + break; + } + + default: + break; + } + + ValuatorClassPtr val = dev->valuator; + val->accelScheme = pointerAccelerationScheme[i]; + + if(NULL != data){ + val->accelScheme.accelData = data; + } + return TRUE; +} + + _X_EXPORT Bool InitAbsoluteClassDeviceStruct(DeviceIntPtr dev) { diff --git a/dix/getevents.c b/dix/getevents.c index 40fc7f2..9cc71d6 100644 --- a/dix/getevents.c +++ b/dix/getevents.c @@ -225,80 +225,6 @@ GetMaximumEventsNum(void) { } -/* Originally a part of xf86PostMotionEvent; modifies valuators - * in-place. */ -static void -acceleratePointer(DeviceIntPtr pDev, int first_valuator, int num_valuators, - int *valuators) -{ - 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; - - if (pDev->ptrfeed && pDev->ptrfeed->ctrl.num) { - /* modeled from xf86Events.c */ - if (pDev->ptrfeed->ctrl.threshold) { - if ((abs(dx) + abs(dy)) >= pDev->ptrfeed->ctrl.threshold) { - pDev->valuator->dxremaind = ((float)dx * - (float)(pDev->ptrfeed->ctrl.num)) / - (float)(pDev->ptrfeed->ctrl.den) + - pDev->valuator->dxremaind; - if (px) { - *px = (int)pDev->valuator->dxremaind; - pDev->valuator->dxremaind = pDev->valuator->dxremaind - - (float)(*px); - } - - pDev->valuator->dyremaind = ((float)dy * - (float)(pDev->ptrfeed->ctrl.num)) / - (float)(pDev->ptrfeed->ctrl.den) + - pDev->valuator->dyremaind; - if (py) { - *py = (int)pDev->valuator->dyremaind; - pDev->valuator->dyremaind = pDev->valuator->dyremaind - - (float)(*py); - } - } - } - else { - mult = pow((float)dx * (float)dx + (float)dy * (float)dy, - ((float)(pDev->ptrfeed->ctrl.num) / - (float)(pDev->ptrfeed->ctrl.den) - 1.0) / - 2.0) / 2.0; - if (dx) { - pDev->valuator->dxremaind = mult * (float)dx + - pDev->valuator->dxremaind; - *px = (int)pDev->valuator->dxremaind; - pDev->valuator->dxremaind = pDev->valuator->dxremaind - - (float)(*px); - } - if (dy) { - pDev->valuator->dyremaind = mult * (float)dy + - pDev->valuator->dyremaind; - *py = (int)pDev->valuator->dyremaind; - pDev->valuator->dyremaind = pDev->valuator->dyremaind - - (float)(*py); - } - } - } -} - - /** * Clip an axis to its bounds, which are declared in the call to * InitValuatorAxisClassStruct. @@ -591,9 +517,14 @@ GetPointerEvents(xEvent *events, DeviceIntPtr pDev, int type, int buttons, } } else { - if (flags & POINTER_ACCELERATE) - acceleratePointer(pDev, first_valuator, num_valuators, - valuators); + if ((flags & POINTER_ACCELERATE) && + pDev->valuator->accelScheme.AccelSchemeProc) { + pDev->valuator->accelScheme.AccelSchemeProc(pDev, + first_valuator, + num_valuators, + valuators, + ms); + } if (pDev->coreEvents) { if (first_valuator == 0 && num_valuators >= 1) diff --git a/dix/ptrveloc.c b/dix/ptrveloc.c new file mode 100644 index 0000000..b20fee8 --- /dev/null +++ b/dix/ptrveloc.c @@ -0,0 +1,893 @@ + +#ifdef HAVE_DIX_CONFIG_H +#include +#endif + +#include +#include +#include +#include + + +/* needed fwds */ +void +AccelerationDefaultCleanup(DeviceIntPtr pDev); + +int +SetAccelerationProfile(DeviceVelocityPtr s, int profile_num); + +void +InitFadingLookup(DeviceVelocityPtr s, float rdecay, int lutsize); + +/***************************************************************************** + * Predictable pointer ballistics + * + * 2006-2008 by Simon Thum (simon [dot] thum [at] gmx de) + * + * 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 (classic/predictable, + * the latter is the stuff herein) + * - Profile + * which selects or excutes the + * - Function, + * which returns an acceleration for any given velocity. + * + * The Profile is an intermediary intended to cleanly perform old-style + * function selection (threshold =/!= 0) + * However, custom functions should be implemented as a profile, + * for which there are selection mechanisms. + * A function can be identical with a profile. + * + ****************************************************************************/ + + + + +/******************************** + * Init/Uninit etc + *******************************/ + +/** + * Init struct so it should match the average case + */ +void +InitVelocityData(DeviceVelocityPtr s) +{ + s->lrm_time = 0; + s->velocity = 0; + s->corr_mul = 10.0; /* dots per 10 milisecond should be usable */ + s->const_acceleration = 1.0; /* no acceleration/deceleration */ + s->reset_time = 300; + s->last_dx = 0; + s->last_dy = 0; + s->use_softening = 1; + s->min_acceleration = 1.0; /* don't decelerate */ + s->use_coupling = 1; + s->coupling = 0.2; + s->fading_lut_size = 0; + s->fading_lut = NULL; + s->profile_private = NULL; + memset(&s->statistics, 0, sizeof(s->statistics)); + SetAccelerationProfile(s, 0); + InitFadingLookup(s, (float)1.0/20.0, 40); /* 20 ms halflive */ +} + +/** + * Clean up + */ +static void +FreeVelocityData(DeviceVelocityPtr s){ + s->fading_lut_size = 0; + xfree(s->fading_lut); + s->fading_lut = NULL; + SetAccelerationProfile(s, -1); +} + +/** + * Adjust weighting decay and lut in sync + * The fn is designed so its integral 0->inf is unity, so we end + * up with a stable (basically IIR) filter. It always draws + * towards its more current input values, which have more weight the older + * the last input value is. + */ +void +InitFadingLookup(DeviceVelocityPtr s, float rdecay, int lutsize) +{ + int x; + float *newlut; + float *oldlut; + + if(lutsize > 0){ + newlut = xalloc (sizeof(float)* lutsize); + if(!newlut) + return; + for(x = 0; x < lutsize; x++) + newlut[x] = pow(0.5, ((float)x) * rdecay); + }else{ + newlut = NULL; + } + oldlut = s->fading_lut; + s->fading_lut = newlut; + s->rdecay = rdecay; + s->fading_lut_size = lutsize; + if(oldlut != NULL) + xfree(oldlut); +} + + +/************************************* + * dix/ddx uninit + ************************************/ + +void +AccelerationDefaultCleanup(DeviceIntPtr pDev){ + /*sanity check*/ + if( pDev->valuator->accelScheme.AccelSchemeProc == acceleratePointerPredictable + && pDev->valuator->accelScheme.accelData != NULL){ + pDev->valuator->accelScheme.AccelSchemeProc = NULL; + FreeVelocityData(pDev->valuator->accelScheme.accelData); + xfree(pDev->valuator->accelScheme.accelData); + pDev->valuator->accelScheme.accelData = NULL; + } +} + +/************************************* + * Get/ChangeDeviceControl + ************************************/ + +static void +FloatToControl(CARD32* num, int den, float val){ + *num = val * (float)den + 0.5f; + *(num+1) = den; +} + +int +HandleRequestGetAccelDevCtrl(ClientPtr client, + DeviceIntPtr pDev, + void* _ctl) +{ + xDeviceAccelPredictableState* ctl = (xDeviceAccelPredictableState*)_ctl; + + if(pDev->valuator->accelScheme.AccelSchemeProc != acceleratePointerPredictable + || ctl->length != sizeof(xDeviceAccelPredictableState)) { + DebugF("(ptr devctrl) something went _very_ wrong"); + return BadMatch; + } + + DeviceVelocityPtr pVel = + (DeviceVelocityPtr)pDev->valuator->accelScheme.accelData; + + ctl->valid_set = 255; + ctl->profile = pVel->statistics.profile_number; + ctl->const_deceleration = (int)(1.0f/pVel->const_acceleration + 0.5f); + FloatToControl(&ctl->adaptive_deceleration_num, 1000, pVel->min_acceleration); + FloatToControl(&ctl->scale_num, 1000, pVel->corr_mul); + if(pVel->use_coupling) + FloatToControl(&ctl->coupling_num, 1000, pVel->coupling); + else{ + ctl->coupling_num = ctl->coupling_den = 0; + ctl->valid_set &= ~PredictableAccelCoupling; + } + ctl->reset_ms = pVel->reset_time; + ctl->halflife_ms = (int)(1.0f/pVel->rdecay + 0.5f); + ctl->softening = pVel->use_softening; + + /* handle swapped clients; scheme-specific part */ + if(client->swapped){ + char n; + CARD32 *f, *end = &ctl->softening; + for(f = &ctl->valid_set; f <= end; f++){ + swapl(f, n); + } + } + return Success; +} + +/* only used to evalute, thus not in header */ +typedef enum { + PM_IGNORE=0, + PM_SET_DEFAULT, + PM_SET_VALUE, + PM_ERROR +} ParameterMode; + +static ParameterMode +GetParameterMode(int mask, xDeviceAccelPredictableCtl* s){ + Bool v = mask & s->valid_set; + Bool d = mask & s->default_set; + if(!v && !d) + return PM_IGNORE; + if(v && !d) + return PM_SET_VALUE; + if(!v && d) + return PM_SET_DEFAULT; + return PM_ERROR; +} + +static float +ControlToFloat(CARD32* num, Bool* success, Bool reciprocal){ + if(!reciprocal){ + *success = (*(num+1) != 0); + if(success) + return ((float)*num / (float)*(num+1)); + else + return 0; + }else{ + *success = (*num != 0); + if(success) + return ((float)*(num+1) / (float)*num); + else + return 0; + } +} + +static float +ChooseFloat(CARD32* num, + Bool reciprocal, + ParameterMode mode, + float defVal, + Bool* nonDefault) +{ + float res = ControlToFloat(num, nonDefault, reciprocal); + if(mode == PM_SET_DEFAULT || !(*nonDefault)){ + res = defVal; + *nonDefault = FALSE; + } + DebugF("(ptr devctrl) read float %.4f\n", res); + return res; +} + +static int +ChooseInteger(CARD32 _integer, + ParameterMode mode, + int defVal) +{ + int res = _integer; + if(mode == PM_SET_DEFAULT) + res = defVal; + DebugF("(ptr devctrl) read int %i\n", res); + return res; +} + +int +HandleRequestChangeAccelDevCtrl(ClientPtr client, + DeviceIntPtr pDev, + void* _ctl) +{ + xDeviceAccelPredictableCtl* ctl = (xDeviceAccelPredictableCtl*)_ctl; + int i, mask; + ParameterMode mode; + Bool p_rational_default; + float p_value; + + /* should handle struct versioning better */ + if(pDev->valuator->accelScheme.AccelSchemeProc != acceleratePointerPredictable + || ctl->length != sizeof(xDeviceAccelPredictableCtl)){ + DebugF("(ptr devctrl) fundamental mismatch\n"); + return BadMatch; + } + + /* handle swapped clients; scheme-specific part */ + if(client->swapped){ + char n; + CARD32 *f, *end = &ctl->softening; + for(f = &ctl->valid_set; f <= end; f++){ + swapl(f, n); + } + } + + if((ctl->valid_set & ctl->default_set) != 0){ + DebugF("(ptr devctrl) can't set and reset.\n"); + return BadValue; + } + + DeviceVelocityPtr pVel = + (DeviceVelocityPtr)pDev->valuator->accelScheme.accelData; + + for(mask = 1; mask <= 128; mask <<= 1 ){ + mode = GetParameterMode(mask, ctl); + if (mode == PM_ERROR) { /* this is normally caught above */ + DebugF("(ptr devctrl) Shouldn't happen\n"); + return BadValue; + } + if (mode == PM_IGNORE) + continue; + + DebugF("(ptr devctrl) diverting to %i mode %s\n", + mask, mode == PM_SET_VALUE?"VALUE":"DEFAULT"); + switch (mask) { + case PredictableAccelProfile : + i = ChooseInteger(ctl->profile, mode, 0); + if(i >= 0){ + if(SetAccelerationProfile(pVel, i)) + break; + } + return BadValue; + + case PredictableAccelHalflife : + i = ChooseInteger(ctl->halflife_ms, mode, 20); + if(i > 0){ + InitFadingLookup(pVel, 1.0f/i, 40); + break; + } + if(i == 0){ /* nearly-disable averaging */ + InitFadingLookup(pVel, 1000, 40); + break; + } + return BadValue; + + case PredictableAccelConstDec : + i = ChooseInteger(ctl->const_deceleration, mode, 1); + if(i > 0){ + pVel->const_acceleration = 1.0f/i; + break; + } + return BadValue; + + case PredictableAccelAdaptiveDec : + p_value = ChooseFloat(&ctl->adaptive_deceleration_num, + TRUE, /*struct bears deceleration (>1)*/ + mode, + 1.0f, + &p_rational_default); + if(p_value <= 1.0f && p_value >= 0.0f){ + pVel->min_acceleration = p_value; + break; + } + return BadValue; + + case PredictableAccelScale : + p_value = ChooseFloat(&ctl->scale_num, + FALSE, + mode, + 10.0f, + &p_rational_default); + if(p_value > 0.0f && p_value < 10000.0f){ + pVel->corr_mul = p_value; + break; + } + return BadValue; + + case PredictableAccelCoupling : + p_value = ChooseFloat(&ctl->coupling_num, + FALSE, + mode, + 0.2f, + &p_rational_default); + if(p_value >= 0.0f && p_value <= 1.0f){ + pVel->coupling = p_value; + pVel->use_coupling = p_value > 0.0f; + break; + } + return BadValue; + + case PredictableAccelSoftening : + i = ChooseInteger(ctl->softening, mode, 1); + if(i >= 0 && i <= 1){ + pVel->use_softening = i; + break; + } + return BadValue; + + default: + return BadMatch; + } + } + return Success; +} + + +/******************************** + * velocity computation + *******************************/ + +/** + * return the axis if mickey is insignificant and axis-aligned + * or -1 otherwise + * 1 for x-axis + * 2 for y-axis + */ +static inline short +GetAxis(int dx, int dy){ + if(dx == 0 || dy == 0){ + if(dx == 1 || dx == -1) + return 1; + if(dy == 1 || dy == -1) + return 2; + return -1; + }else{ + return -1; + } +} + +/** + * Perform velocity approximation + * return true if non-visible state reset is suggested + */ +static short +ProcessVelocityData(DeviceVelocityPtr s, int dx, int dy, int time) +{ + float cvelocity, fade, div; + int diff = time - s->lrm_time; + int cur_ax = GetAxis(dx, dy); + int last_ax = GetAxis(s->last_dx, s->last_dy); + short reset = (diff >= s->reset_time); + if(cur_ax != last_ax && cur_ax != -1 && last_ax != -1 && !reset){ + /* correct for the error induced when diagonal movements are + reported as alternating axis mickeys */ + dx += s->last_dx; + dy += s->last_dy; + diff += s->last_diff; + s->last_diff = time - s->lrm_time; /* prevent repeating add-up */ + DebugF("(dix ptracc) axial correction\n"); + }else{ + s->last_diff = diff; + } + + /* + * cvelocity is not a real velocity yet, more a motion delta. contant + * acceleration is multiplied here to make the velocity an on-screen + * velocity (px/t as opposed to [insert unit]/t). This is intended to + * make multiple devices with widely varying ConstantDecelerations behave + * more similar. + */ + cvelocity = (float)sqrt(dx*dx + dy*dy) * s->const_acceleration; + + s->lrm_time = time; + + if (s->reset_time < 0 || diff < 0) { /* disabled or timer overrun? */ + /* simply set velocity from current movement, no reset. */ + s->velocity = cvelocity; + return 0; + } + + if (reset) { + /* no movement for some time, suggest to reset nonvisible state */ + reset = TRUE; + DebugF("(dix ptracc) nv-reset\n"); + } + + if (diff == 0) + diff = 1; /* prevent div-by-zero, though it shouldn't happen anyway*/ + + /* translate velocity to dots/ms (somewhat untractable in integers, + so we multiply by some per-device adjustable factor) */ + cvelocity = cvelocity * s->corr_mul / (float)diff; + + /* short-circuit: when nv-reset the rest can be skipped */ + if(reset == TRUE){ + s->velocity = cvelocity; + return TRUE; + } + + /* fading factor; crucial it matches definition in InitFadingLookup */ + if(diff < s->fading_lut_size) + fade = s->fading_lut[diff]; + else + fade = pow(0.5, ((float)diff) * s->rdecay); + + s->velocity *= fade; /* fade out old velocity */ + s->velocity += cvelocity * (1.0f - fade); /* and add up current */ + + /* perform coupling */ + if(s->use_coupling && cvelocity > 0 && fabs(s->velocity-cvelocity) > 1.0){ + div = fabs(cvelocity - s->velocity) / (s->velocity + cvelocity); + if(div > s->coupling){ /* if divergence is too high... */ + /* set current velocity as weighted to catch up quickly */ + DebugF("(dix ptracc) coupled velocity: %.3f instead of %.3f\n", + cvelocity, s->velocity); + s->velocity = cvelocity; + + /* for the records, so a user may check if coupling is adjusted + * sensibly + */ + s->statistics.coupled ++; + } + } + DebugF("(dix ptracc) guessing: vel=%.3f diff=%d curw=%.3f\n", + s->velocity, diff, (1.0- fade)); + return reset; +} + + +/** + * 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( + DeviceVelocityPtr s, + int dx, + int dy, + float* fdx, + float* fdy, + short do_soften) +{ + if (do_soften && s->use_softening) { + *fdx = ApplySimpleSoftening(s->last_dx, dx); + *fdy = ApplySimpleSoftening(s->last_dy, dy); + } else { + *fdx = dx; + *fdy = dy; + } + + *fdx *= s->const_acceleration; + *fdy *= s->const_acceleration; +} + + + +/***************************************** + * Acceleration functions and profiles + ****************************************/ + +/** + * acceleration function similar to classic accelerated/unaccelerated, + * introducing some transition range + */ +static float +SimpleAccelerationFunc(float velocity, float threshold, float acc) +{ + if(velocity < 1.0) + return velocity; + + if (velocity <= threshold) + return 1; + + velocity /= threshold; + + if (velocity >= acc) + return acc; + else + return velocity; + +} + +/** + * Polynomial function similar previous one, but with f(1) = 1 + */ +static float +PolynomialAccelerationFunc(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( + DeviceVelocityPtr pVel, + float threshold, + float acc) +{ + + if (threshold) { + return SimpleAccelerationFunc (pVel->velocity, + threshold, + acc); + } else { + return PolynomialAccelerationFunc(pVel->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( + DeviceVelocityPtr pVel, + float threshold, + float acc) +{ + float vel_dist; + + acc = (acc-1.0) * 0.1f + 1.0; /* without this, acc of 2 is unuseable */ + + if (pVel->velocity <= threshold) + return pVel->min_acceleration; + vel_dist = pVel->velocity - threshold; + return (pow(acc, vel_dist)) * pVel->min_acceleration; +} + + +/** + * just a smooth function in [0..1] -> [0..1] + * - point symmetry at 0.5 + * - f'(0) = f'(1) = 0 + * - starts faster than sinoids, C1 (Cinf if you dare to ignore endpoints) + */ +static 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( + DeviceVelocityPtr pVel, + float threshold, + float acc) +{ + float velocity = pVel->velocity; + 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( + DeviceVelocityPtr pVel, + float threshold, + float acc) +{ + if(acc > 1.0f) + acc -= 1.0f; /*this is so acc = 1 is no acceleration */ + else + return 1.0f; + + float nv = (pVel->velocity - threshold) * acc * 0.5f; + float res; + 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 */ + } + res += pVel->min_acceleration; + return res; +} + +/** + * Set the profile by number. + * Intended to make profiles exchangeable at runtime. + * If you created a profile, give it a number here to make it selectable. + * In case some profile-specific init is needed, here would be a good place, + * since FreeVelocityData() also calls this with -1. + * returns FALSE (0) if profile number is unknown. + */ +int +SetAccelerationProfile( + DeviceVelocityPtr s, + int profile_num) +{ + PointerAccelerationProfileFunc profile; + switch(profile_num){ + default: + return FALSE; + case -1: + profile = NULL; /* Special case to uninit properly */ + break; + case 0: + profile = ClassicProfile; + break; + case 1: + profile = SmoothLinearProfile; + break; + case 2: + profile = SimpleSmoothProfile; + break; + case 3: + profile = PowerProfile; + break; + } + if(s->profile_private != NULL){ + /* Here one could free old profile-private data */ + xfree(s->profile_private); + s->profile_private = NULL; + } + /* Here one could init profile-private data */ + s->Profile = profile; + s->statistics.profile_number = profile_num; + return TRUE; +} + + + + +/******************************** + * acceleration schemes + *******************************/ + +/** + * Modifies valuators in-place. + * This version employs a velocity approximation algorithm to + * enable fine-grained acceleration profiles. + */ +void +acceleratePointerPredictable(DeviceIntPtr pDev, 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 = (DeviceVelocityPtr) pDev->valuator->accelScheme.accelData; + float fdx, fdy; /* no need to init */ + + if (!num_valuators || !valuators || !velocitydata) + 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){ + /* reset nonvisible state? */ + if (ProcessVelocityData(velocitydata, dx , dy, evtime)) { + /* set to center of pixel */ + pDev->valuator->dxremaind = pDev->valuator->dyremaind = 0.5f; + /* prevent softening (somewhat quirky solution, + as it depends on the algorithm) */ + velocitydata->last_dx = dx; + velocitydata->last_dy = dy; + } + + if (pDev->ptrfeed && pDev->ptrfeed->ctrl.num) { + /* invoke acceleration profile to determine acceleration */ + mult = velocitydata->Profile(velocitydata, + pDev->ptrfeed->ctrl.threshold, + (float)(pDev->ptrfeed->ctrl.num) / + (float)(pDev->ptrfeed->ctrl.den)); + + DebugF("(dix ptracc) resulting speed multiplier : %.3f\n", mult); + /* enforce min_acceleration */ + if (mult < velocitydata->min_acceleration) { + DebugF("(dix ptracc) enforced min multiplier : %.3f\n", + velocitydata->min_acceleration); + mult = velocitydata->min_acceleration; + } + + if(mult != 1.0 || velocitydata->const_acceleration != 1.0) { + ApplySofteningAndConstantDeceleration( velocitydata, + dx, dy, + &fdx, &fdy, + mult > 1.0); + if (dx) { + pDev->valuator->dxremaind = mult * fdx + pDev->valuator->dxremaind; + *px = (int)pDev->valuator->dxremaind; + pDev->valuator->dxremaind = pDev->valuator->dxremaind - (float)*px; + } + if (dy) { + pDev->valuator->dyremaind = mult * fdy + pDev->valuator->dyremaind; + *py = (int)pDev->valuator->dyremaind; + pDev->valuator->dyremaind = pDev->valuator->dyremaind - (float)*py; + } + } + } + } + /* 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. + */ +void +acceleratePointerClassic(DeviceIntPtr pDev, int first_valuator, int num_valuators, + int *valuators, int ignore) +{ + 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; + + if (pDev->ptrfeed && pDev->ptrfeed->ctrl.num) { + /* modeled from xf86Events.c */ + if (pDev->ptrfeed->ctrl.threshold) { + if ((abs(dx) + abs(dy)) >= pDev->ptrfeed->ctrl.threshold) { + pDev->valuator->dxremaind = ((float)dx * + (float)(pDev->ptrfeed->ctrl.num)) / + (float)(pDev->ptrfeed->ctrl.den) + + pDev->valuator->dxremaind; + if (px) { + *px = (int)pDev->valuator->dxremaind; + pDev->valuator->dxremaind = pDev->valuator->dxremaind - + (float)(*px); + } + + pDev->valuator->dyremaind = ((float)dy * + (float)(pDev->ptrfeed->ctrl.num)) / + (float)(pDev->ptrfeed->ctrl.den) + + pDev->valuator->dyremaind; + if (py) { + *py = (int)pDev->valuator->dyremaind; + pDev->valuator->dyremaind = pDev->valuator->dyremaind - + (float)(*py); + } + } + } + else { + mult = pow((float)(dx * dx + dy * dy), + ((float)(pDev->ptrfeed->ctrl.num) / + (float)(pDev->ptrfeed->ctrl.den) - 1.0) / + 2.0) / 2.0; + if (dx) { + pDev->valuator->dxremaind = mult * (float)dx + + pDev->valuator->dxremaind; + *px = (int)pDev->valuator->dxremaind; + pDev->valuator->dxremaind = pDev->valuator->dxremaind - + (float)(*px); + } + if (dy) { + pDev->valuator->dyremaind = mult * (float)dy + + pDev->valuator->dyremaind; + *py = (int)pDev->valuator->dyremaind; + pDev->valuator->dyremaind = pDev->valuator->dyremaind - + (float)(*py); + } + } + } +} diff --git a/hw/xfree86/common/xf86Xinput.c b/hw/xfree86/common/xf86Xinput.c index cd0c30a..1291b6e 100644 --- a/hw/xfree86/common/xf86Xinput.c +++ b/hw/xfree86/common/xf86Xinput.c @@ -86,12 +86,120 @@ #include "mi.h" +#include /* dix pointer acceleration */ + + #ifdef XFreeXDGA #include "dgaproc.h" #endif xEvent *xf86Events = NULL; + +/** + * Eval config and init DeviceVelocityData accordingly + */ +static void +ProcessVelocityConfiguration(char* devname, pointer list, DeviceVelocityPtr s){ + int tempi; + float tempf; + + if(!s) + return; + + tempf = xf86SetRealOption(list, "WeightingHalflife", 20); + xf86Msg(X_CONFIG, "%s: weighting halflife %.1f ms\n", devname, tempf); + if(tempf > 0) + tempf = 1.0 / tempf; /* set reciprocal if possible */ + else + tempf = 10000; /* else set fairly high */ + + InitFadingLookup(s, tempf, 40); + + tempf = xf86SetIntOption(list, "ConstantDeceleration", 1); + if(tempf > 1.0){ + xf86Msg(X_CONFIG, "%s: constant deceleration by %.1f\n", devname, + tempf); + s->const_acceleration = 1.0 / tempf; /* set reciprocal deceleration + alias acceleration */ + } + + tempf = xf86SetIntOption(list, "AdaptiveDeceleration", 1); + if(tempf > 1.0){ + xf86Msg(X_CONFIG, "%s: adaptive deceleration by %.1f\n", devname, + tempf); + s->min_acceleration = 1.0 / tempf; /* set minimum acceleration */ + } + + tempf = xf86SetRealOption(list, "VelocityCoupling", 0.2); + if(tempf > 0.0){ + xf86Msg(X_CONFIG, "%s: velocity coupling is %.1f%%\n", devname, + tempf*100.0); + s->coupling = tempf; + s->use_coupling = 1; + }else{ + s->use_coupling = 0; + xf86Msg(X_CONFIG, "%s: disabled velocity coupling\n", devname); + } + + /* Configure softening. If const deceleration is used, this is expected + * to provide better subpixel information so we enable + * softening by default only if ConstantDeceleration is not used + */ + s->use_softening = xf86SetBoolOption(list, "Softening", + s->const_acceleration == 1.0); + + s->reset_time = xf86SetIntOption(list, "VelocityReset", 300); + + tempf = xf86SetRealOption(list, "ExpectedRate", 0); + if(tempf > 0){ + s->corr_mul = 1000.0 / tempf; + }else{ + s->corr_mul = xf86SetRealOption(list, "VelocityScale", 10); + } + + /* select profile by number */ + tempi= xf86SetIntOption(list, "AccelerationProfile", 0); + if(SetAccelerationProfile(s, tempi)){ + xf86Msg(X_CONFIG, "%s: set acceleration profile %i\n", devname, tempi); + }else{ + xf86Msg(X_CONFIG, "%s: acceleration profile %i is unknown\n", + devname, tempi); + } +} + +static void +ApplyExtendedValuatorSettings(DeviceIntPtr dev){ + int scheme; + DeviceVelocityPtr pVel; + LocalDevicePtr local = (LocalDevicePtr)dev->public.devicePrivate; + + if(dev->valuator){ + scheme = xf86SetIntOption(local->options, "AccelerationScheme", 1); + + /* reinit if needed */ + if(dev->valuator->accelScheme.number != scheme){ + if(dev->valuator->accelScheme.AccelCleanupProc){ + dev->valuator->accelScheme.AccelCleanupProc(dev); + } + + xf86Msg(X_CONFIG, "%s: init scheme %i\n", local->name, scheme); + InitPointerAccelerationScheme(dev, scheme); + }else{ + xf86Msg(X_CONFIG, "%s: keeping scheme %i\n", local->name, scheme); + } + + /* process special configuration */ + switch(scheme){ + case 1: + pVel = (DeviceVelocityPtr) dev->valuator->accelScheme.accelData; + ProcessVelocityConfiguration (local->name, local->options, + pVel); + break; + } + } +} + static Bool xf86SendDragEvents(DeviceIntPtr device) { @@ -772,6 +880,9 @@ xf86InitValuatorAxisStruct(DeviceIntPtr dev, int axnum, int minval, int maxval, InitValuatorAxisStruct(dev, axnum, minval, maxval, resolution, min_res, max_res); + + if(axnum == 0) /* prevent double invocation */ + ApplyExtendedValuatorSettings(dev); } /* diff --git a/include/input.h b/include/input.h index ca67cfa..d5af85d 100644 --- a/include/input.h +++ b/include/input.h @@ -105,6 +105,22 @@ typedef void (*DeviceUnwrapProc)( void* /*data*/ ); +/* pointer acceleration handling */ +typedef void (*PointerAccelSchemeProc)( + DeviceIntPtr /*pDev*/, + int /*first_valuator*/, + int /*num_valuators*/, + int* /*valuators*/, + int /*evtime*/); + +typedef void (*DeviceCallbackProc)( + DeviceIntPtr /*pDev*/); + +typedef int (*HandleDevCtrlRequest)(ClientPtr /*client*/, + DeviceIntPtr /*pDev*/, + void* /*ctl*/); + + typedef struct _DeviceRec { pointer devicePrivate; ProcessInputProc processInputProc; /* current */ @@ -235,6 +251,10 @@ extern Bool InitValuatorClassDeviceStruct( int /*numMotionEvents*/, int /*mode*/); +extern Bool InitPointerAccelerationScheme( + DeviceIntPtr /*dev*/, + int /*scheme*/); + extern Bool InitAbsoluteClassDeviceStruct( DeviceIntPtr /*device*/); diff --git a/include/inputstr.h b/include/inputstr.h index 6a1d5c9..c6b81e5 100644 --- a/include/inputstr.h +++ b/include/inputstr.h @@ -146,6 +146,15 @@ typedef struct _AxisInfo { int max_value; } AxisInfo, *AxisInfoPtr; +typedef struct _ValuatorAccelerationRec { + int number; + PointerAccelSchemeProc AccelSchemeProc; + void *accelData; /* at disposal of AccelScheme */ + HandleDevCtrlRequest HandleGetDeviceConrol; + HandleDevCtrlRequest HandleChangeDeviceConrol; + DeviceCallbackProc AccelCleanupProc; +} ValuatorAccelerationRec, *ValuatorAccelerationPtr; + typedef struct _ValuatorClassRec { ValuatorMotionProcPtr GetMotionProc; int numMotionEvents; @@ -160,7 +169,8 @@ typedef struct _ValuatorClassRec { int *axisVal; int lastx, lasty; /* last event recorded, not posted to * client; see dix/devices.c */ - int dxremaind, dyremaind; /* for acceleration */ + float dxremaind, dyremaind; /* for acceleration */ + ValuatorAccelerationRec accelScheme; CARD8 mode; } ValuatorClassRec, *ValuatorClassPtr; diff --git a/include/ptrveloc.h b/include/ptrveloc.h new file mode 100644 index 0000000..e807641 --- /dev/null +++ b/include/ptrveloc.h @@ -0,0 +1,80 @@ +/* +* 2006-2008 by Simon Thum +*/ + +#ifndef POINTERVELOCITY_H +#define POINTERVELOCITY_H + +#include +#include +#include + + +struct _DeviceVelocityData; + +/** + * profile + * returns actual acceleration for velocity, acceleration control,... + */ +typedef float (*PointerAccelerationProfileFunc) + (struct _DeviceVelocityData* /*pVel*/, + float /*threshold*/, float /*acc*/); + + +/** + * Contains all data needed to implement mouse ballistics + */ +typedef struct _DeviceVelocityData { + int lrm_time; /* time the last motion event was processed */ + float velocity; /* velocity as guessed by algorithm */ + int last_dx, last_dy; /* last motion delta */ + int last_diff; /* last time-diff */ + float* fading_lut; /* lookup for IIR filter */ + int fading_lut_size; /* size of lookup table */ + float corr_mul; /* config: multiply this into velocity */ + float rdecay; /* config: (reciprocal) weighting halflife in ms */ + float const_acceleration; /* config: (reciprocal) constant deceleration */ + float min_acceleration; /* config: minimum acceleration */ + short reset_time; /* config: reset non-visible state after # ms */ + short use_softening; /* config: use softening of mouse values */ + short use_coupling; /* config: use coupling */ + float coupling; /* config: max. divergence before coupling happens*/ + PointerAccelerationProfileFunc Profile; + void* profile_private;/* extended data, see SetAccelerationProfile() */ + struct { /* used only to be able to query this information */ + int profile_number; + int coupled; + float last_acceleration; + } statistics; +} DeviceVelocityData, *DeviceVelocityPtr; + + +extern void +InitVelocityData(DeviceVelocityPtr s); + +extern void +InitFadingLookup(DeviceVelocityPtr s, float rdecay, int lutsize); + +extern int +SetAccelerationProfile(DeviceVelocityPtr s, int profile_num); + +extern int +HandleRequestGetAccelDevCtrl(ClientPtr client, DeviceIntPtr pDev, + void* ctl); + +extern int +HandleRequestChangeAccelDevCtrl(ClientPtr client, DeviceIntPtr pDev, + void* ctl); + +extern void +AccelerationDefaultCleanup(DeviceIntPtr pDev); + +extern void +acceleratePointerPredictable(DeviceIntPtr pDev, int first_valuator, + int num_valuators, int *valuators, int evtime); + +extern void +acceleratePointerClassic(DeviceIntPtr pDev, int first_valuator, + int num_valuators, int *valuators, int ignore); + +#endif /* POINTERVELOCITY_H */