# # Updated but not checked in: # (will commit) # # modified: dix/Makefile.am # modified: dix/devices.c # modified: dix/getevents.c # new file: dix/ptrveloc.c # new file: dix/ptrveloc.h # modified: hw/xfree86/common/xf86Xinput.c # modified: include/inputstr.h # diff --git a/dix/Makefile.am b/dix/Makefile.am index d0d6e87..f5afba6 100644 --- a/dix/Makefile.am +++ b/dix/Makefile.am @@ -26,6 +26,7 @@ libdix_la_SOURCES = \ pixmap.c \ privates.c \ property.c \ + ptrveloc.c \ resource.c \ swaprep.c \ swapreq.c \ diff --git a/dix/devices.c b/dix/devices.c index aa04862..cf6d8e9 100644 --- a/dix/devices.c +++ b/dix/devices.c @@ -817,6 +817,7 @@ InitValuatorClassDeviceStruct(DeviceIntP valc->lasty = 0; valc->dxremaind = 0; valc->dyremaind = 0; + valc->accelScheme = 0; dev->valuator = valc; /* biggest hack ever. */ diff --git a/dix/getevents.c b/dix/getevents.c index b19a73f..0225724 100644 --- a/dix/getevents.c +++ b/dix/getevents.c @@ -63,12 +63,18 @@ #include "exevents.h" #include "exglobals.h" #include "extnsionst.h" +#include "ptrveloc.h" + /* Maximum number of valuators, divided by six, rounded up. */ #define MAX_VALUATOR_EVENTS 6 /* Number of motion history events to store. */ #define MOTION_HISTORY_SIZE 256 + + + + /** * Returns the maximum number of events GetKeyboardEvents, * GetKeyboardValuatorEvents, and GetPointerEvents will ever return. @@ -234,17 +240,95 @@ #endif return numEvents; } + /* Originally a part of xf86PostMotionEvent; modifies valuators * in-place. */ static void -acceleratePointer(DeviceIntPtr pDev, int first_valuator, int num_valuators, - int *valuators) +acceleratePointerOriginal(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 * 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); + } + } + } +} + +/** Modifies valuators in-place. +This version employs a velocity guessing algorithm, +enabling fine-grained acceleration profiles. +*/ +static void +acceleratePointer(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; + MouseVelocityPtr velocitydata = (MouseVelocityPtr) pDev->valuator->accelData; + float fdx, fdy; //no need to init + + if (!num_valuators || !valuators || !velocitydata) return; if (first_valuator == 0) { @@ -258,53 +342,53 @@ acceleratePointer(DeviceIntPtr pDev, int if (!dx && !dy) return; + + /* reset nonvisible state? */ + if (ProcessVelocityData(velocitydata, dx , dy, current)) { + /* round by pre-adding 0.5 once */ + 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) { - /* 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); - } - } + /* invoke acceleration profile and determine acceleration */ + mult = velocitydata->profile(velocitydata, + pDev->ptrfeed->ctrl.threshold, + (float)(pDev->ptrfeed->ctrl.num) / + (float)(pDev->ptrfeed->ctrl.den)); + + /* enforce min_acceleration */ + if (mult < 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; + } + + DBG(6, ErrorF("acceleratePointer v=(%d, %d) vel=%.3f acc=%.2f + "appliedv=(%.1f, %.1f)\n", + valuator[0], valuator[1], + velocitydata->velocity, mult, fdx, fdy)); + } } + /* remember last motion delta (for softening) */ + velocitydata->last_dx = dx; + velocitydata->last_dy = dy; } /** @@ -492,6 +576,7 @@ GetPointerEvents(xEvent *events, DeviceI deviceKeyButtonPointer *kbp = NULL; Bool sendValuators = (type == MotionNotify || flags & POINTER_ABSOLUTE); DeviceIntPtr cp = inputInfo.pointer; + AcceleratePointerFunc accFn = NULL; int x = 0, y = 0; /* Sanity checks. */ @@ -550,9 +635,20 @@ GetPointerEvents(xEvent *events, DeviceI } } else { - if (flags & POINTER_ACCELERATE) - acceleratePointer(pDev, first_valuator, num_valuators, - valuators); + if (flags & POINTER_ACCELERATE){ + switch(pDev->valuator->accelScheme){ /* select algorithm */ + case 0: + acceleratePointerOriginal(pDev, first_valuator, + num_valuators, valuators, ms); + break; + case 1: + acceleratePointer(pDev, first_valuator, + num_valuators, valuators, ms); + break; + default: + break; //unknown scheme? do nothing is ok + } + } 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..84e9675 --- /dev/null +++ b/dix/ptrveloc.c @@ -0,0 +1,210 @@ +#include +#include "prtveloc.h" + + + +/****************************************************************************** + * Mouse softening fns + * + * Serves 3 complementary functions: + * 1) provide a sophisticated ballistic velocity guess to give better + acceleration + * 2) slightly soften out mouse data when acceleration is applied + * 3) decelerate by two means (constant and adaptive) if enabled + *****************************************************************************/ + +/* +Init struct so it should match the average case +*/ +_X_EXPORT void +InitVelocityData(MouseVelocityPtr s, float rdecay) +{ + int x; + s->lrm_time = 0; + s->velocity = 0; + s->lrm_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 = 200; + s->last_dx = 0; + s->last_dy = 0; + s->use_softening = 1; + s->min_acceleration = 1.0; + s->use_coupling = 1; + s->coupling = 0.3; + s->profile = ClassicProfile; + s->fading_lut_size = 0; + InitFadingLookup(s, rdecay, 40); +} + +/* +Runtime-adjust weighting decay; pretty but not 100% thread safe +*/ +_X_EXPORT void +InitFadingLookup(MouseVelocityPtr s, float rdecay, int lutsize) +{ + int x; + float *newlut = xalloc (sizeof(float)* lutsize); + float *oldlut; + + for(x = 0; x < lutsize); x++) + newlut[x] = pow(0.5, ((float)x) * rdecay); + oldlut = s->fading_lut; + s->fading_lut = newlut; + s->fading_lut_size = lutsize; + s->rdecay = rdecay; + if(oldlut != NULL) + xfree(oldlut); +} + + + +/* Perform velocity guessing + return true if non-visible state reset is suggested */ +_X_EXPORT short +ProcessVelocityData(MouseVelocityPtr s, int dx, int dy, int time) +{ + int diff = time - s->lrm_time; + /* not a real velocity yet, more a motion delta */ + float cvelocity = (float)sqrt(dx*dx + dy*dy) * s->const_acceleration; + float fade, div; + short reset = FALSE; + + 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 (diff >= s->reset_time) { + /* no movement for some time, suggest to reset nonvisible state */ + diff = s->reset_time; + reset = TRUE; + } + + 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-mouse adjustable factor) */ + cvelocity = cvelocity * s->corr_mul / (float)diff; + s->lrm_velocity = cvelocity; + + /* 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 InitVelocityData */ + if(diff < ARRAY_SIZE(s->fading_lut)) + fade = s->fading_lut[diff]; + else + fade = pow(0.5, ((float)diff) * s->rdecay); + + s->velocity *= fade; /* fade out old velocity info */ + 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 */ + s->velocity = cvelocity; + } + } + /* xf86Msg(X_CONFIG, "ProcessVelocityData acceleration guessing: vel=%.3f + diff=%d curw=%.4f, fade=%.4f\n", s->velocity, diff, (1.0- fade), fade); */ + return reset; +} + + +/* this flattens significant values 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; +} + + +_X_EXPORT void +ApplySofteningAndConstantDeceleration( + MouseVelocityPtr 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 function similar to Classic, introducting a 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. The function is given the whole +MouseVelocityPtr as param to be able to adapt the function so it holds +property (4) as specified in doc (bug#8583) +The code below doesn't and needs only the velocity field. +*/ +_X_EXPORT float +ClassicProfile( + MouseVelocityPtr pVel, + float threshold, + float acc){ + + if (threshold) { + mult = SimpleAccelerationFunc (pVel->velocity, + threshold, + acc); + } + else { + mult = PolynomialAccelerationFunc(pVel->velocity, + 0, + acc); + } +} diff --git a/dix/ptrveloc.h b/dix/ptrveloc.h new file mode 100644 index 0000000..b5925c2 --- /dev/null +++ b/dix/ptrveloc.h @@ -0,0 +1,73 @@ +#ifndef POINTERVELOCITY_H +#define POINTERVELOCITY_H + + +/* return acceleration for velocity/threshold (profile) */ +typedef float (*AccelerationProfileFunc) + (MouseVelocityPtr /*pVel*/, + float /*threshold*/, float /*max_acc*/); + +/* Contains all Data needed to implement mouse ballistics */ + +typedef struct _MouseVelocityData { + /* time the last motion event was processed */ + int lrm_time; + /* velocity as guessed by algo (read: weighted) */ + float velocity; + /* velocity as from last movement event */ + float lrm_velocity; + /* last motions delta (for cheap softening algo) */ + int last_dx, last_dy; + /* lookup for fadeout values; 40 saves 99% of cases, bigger won't help + * much, maximum sane size is reset_time+1, yielding 100% hit rate */ + float* fading_lut; + int fading_lut_size; + + /* config: multiply this into velocity; can be used to adujust behaviour, + * about 2 to 25 is sensible */ + float corr_mul; + /* config: reciprocal decay ms are between full and half weight */ + float rdecay; + /* config: USED FOR CONSTANT DECELERATION ! but stored reciprocal + for fmul is usually faster */ + float const_acceleration; + /* config: minimum acceleration; normally 1, set lower to enable subpixel + precision */ + float min_acceleration; + /* config: reset non-visible state after this number of miliseconds + inactivity, -1 means disable velocity guessing */ + short reset_time; + /* config: use softening of mouse values */ + short use_softening; + /* config: use coupling of weighted and current velocity */ + short use_coupling; + /* selected acceleration profile (only one right now) */ + AccelerationProfileFunc profile; + /* config: maximum divergence to allow weighted velocity to be preferred*/ + float coupling; +} MouseVelocityData, *MouseVelocityPtr; + + +export void +InitVelocityData(MouseVelocityPtr s, float rdecay); + +export void +InitFadingLookup(MouseVelocityPtr s, float rdecay, int lutsize); + +export short +ProcessVelocityData(MouseVelocityPtr s, int dx, int dy, int time); + +export void +ApplySofteningAndConstantDeceleration( + MouseVelocityPtr s, + int dx, + int dy, + float* fdx, + float* fdy, + short do_soften); + +export float ClassicProfile(MouseVelocityPtr /*pVel*/, + float /*threshold*/, float /*max_acc*/); + + +#endif /* POINTERVELOCITY_H */ \ No newline at end of file diff --git a/hw/xfree86/common/xf86Xinput.c b/hw/xfree86/common/xf86Xinput.c index be3368d..5a76d9a 100644 --- a/hw/xfree86/common/xf86Xinput.c +++ b/hw/xfree86/common/xf86Xinput.c @@ -90,6 +90,8 @@ #include "xf86_OSproc.h" /* sigio stuff #include "mi.h" +#include /* DIX acceleration code */ + /****************************************************************************** * debugging macro *****************************************************************************/ @@ -113,6 +115,66 @@ xf86SendDragEvents(DeviceIntPtr device) return (TRUE); } +/** + Eval config and init MouseVelocityData accordingly +*/ +static void +ProcessVelocityConfiguration(MouseVelocityPtr s, char* devname, pointer list){ + float tempf; + + /* FIXME These should be reachable by API/xset */ + tempf = xf86SetRealOption(list, "WeightingDecay", 15); + xf86Msg(X_CONFIG, "%s: weighting decay %.1f ms\n", devname, tempf); + if(tempf > 0) + tempf = 1.0 / tempf; /* set reciprocal if possible */ + else + tempf = 10000; /* else set fairly high */ + + InitVelocityData(s, tempf); + + 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->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", 200); + + s->corr_mul = xf86SetRealOption(list, "VelocityScale", 10); + + + if (xf86SetBoolOption(list, "VelocityGuessing", 1) == 0) { + s->reset_time = -1; /* Disable */ + s->use_softening = 0; + xf86Msg(X_CONFIG, "%s: Disabled velocity guessing\n", devname); + } +} + /*********************************************************************** * * xf86ProcessCommonOptions -- @@ -138,6 +200,16 @@ xf86ProcessCommonOptions(LocalDevicePtr } else { xf86Msg(X_CONFIG, "%s: doesn't report drag events\n", local->name); } + + /* Enable improved pointer acceleration */ + /* TODO: find a better place for this + and clean up properly */ + local->dev->valuator->accelData = xalloc(sizeof(MouseVelocityData)); + ProcessVelocityConfiguration( + (MouseVelocityPtr)local->dev->valuator->accelData, + local->name, + list); + local->dev->valuator->accelScheme = 1; } void diff --git a/include/inputstr.h b/include/inputstr.h index ada94e6..e39a1fd 100644 --- a/include/inputstr.h +++ b/include/inputstr.h @@ -159,6 +159,8 @@ typedef struct _ValuatorClassRec { int lastx, lasty; /* last event recorded, not posted to * client; see dix/devices.c */ int dxremaind, dyremaind; /* for acceleration */ + int accelScheme; /*0 = classic, 1 = shiny new */ + void *accelData; /* at disposal of accelScheme */ CARD8 mode; } ValuatorClassRec, *ValuatorClassPtr;