[PATCH v2 15/28] Input: Convert acceleration code to using ValuatorMask

Daniel Stone daniel at fooishbar.org
Thu Jun 9 10:17:23 PDT 2011


Instead of passing a set of int* to the acceleration code, pass it a
mask instead, which avoids an unfortunate loss of precision.

Signed-off-by: Daniel Stone <daniel at fooishbar.org>
Reviewed-by: Peter Hutterer <peter.hutterer at who-t.net>
Reviewed-by: Simon Thum <simon.thum at gmx.de>
---
 dix/getevents.c |   18 ++++++++----
 dix/ptrveloc.c  |   77 +++++++++++++++++-------------------------------------
 2 files changed, 36 insertions(+), 59 deletions(-)

v2: Remove hunks which crept in from changing moveAbsolute and moveRelative.

diff --git a/dix/getevents.c b/dix/getevents.c
index f02b833..87a84d2 100644
--- a/dix/getevents.c
+++ b/dix/getevents.c
@@ -1200,16 +1200,22 @@ GetPointerEvents(InternalEvent *events, DeviceIntPtr pDev, int type, int buttons
         transformAbsolute(pDev, &mask);
         moveAbsolute(pDev, &x, &y, &mask);
     } else {
-        if (flags & POINTER_ACCELERATE) {
+        if (flags & POINTER_ACCELERATE)
             accelPointer(pDev, &mask, ms);
-            /* The pointer acceleration code modifies the fractional part
-             * in-place, so we need to extract this information first */
-            x_frac = pDev->last.remainder[0];
-            y_frac = pDev->last.remainder[1];
-        }
         moveRelative(pDev, &x, &y, &mask);
     }
 
+    if (valuator_mask_isset(&mask, 0))
+    {
+        x_frac = valuator_mask_get_double(&mask, 0);
+        x_frac -= trunc(x_frac);
+    }
+    if (valuator_mask_isset(&mask, 1))
+    {
+        y_frac = valuator_mask_get_double(&mask, 1);
+        y_frac -= trunc(y_frac);
+    }
+
     set_raw_valuators(raw, &mask, raw->valuators.data,
                       raw->valuators.data_frac);
 
diff --git a/dix/ptrveloc.c b/dix/ptrveloc.c
index 55056e6..91e0885 100644
--- a/dix/ptrveloc.c
+++ b/dix/ptrveloc.c
@@ -1115,11 +1115,10 @@ acceleratePointerPredictable(
     CARD32 evtime)
 {
     double dx = 0, dy = 0;
-    int tmpi;
     DeviceVelocityPtr velocitydata = GetDevicePredictableAccelData(dev);
     Bool soften = TRUE;
 
-    if (!velocitydata)
+    if (valuator_mask_num_valuators(val) == 0 || !velocitydata)
         return;
 
     if (velocitydata->statistics.profile_number == AccelProfileNone &&
@@ -1128,11 +1127,11 @@ acceleratePointerPredictable(
     }
 
     if (valuator_mask_isset(val, 0)) {
-        dx = valuator_mask_get(val, 0);
+        dx = valuator_mask_get_double(val, 0);
     }
 
     if (valuator_mask_isset(val, 1)) {
-        dy = valuator_mask_get(val, 1);
+        dy = valuator_mask_get_double(val, 1);
     }
 
     if (dx || dy){
@@ -1155,24 +1154,12 @@ acceleratePointerPredictable(
                     ApplySoftening(velocitydata, &dx, &dy);
                 ApplyConstantDeceleration(velocitydata, &dx, &dy);
 
-                /* Calculate the new delta (with accel) and drop it back
-                 * into the valuator masks */
-                if (dx) {
-                    double tmp;
-                    tmp = mult * dx + dev->last.remainder[0];
-                    tmpi = trunc(tmp);
-                    valuator_mask_set(val, 0, tmpi);
-                    dev->last.remainder[0] = tmp - (double)tmpi;
-                }
-                if (dy) {
-                    double tmp;
-                    tmp = mult * dy + dev->last.remainder[1];
-                    tmpi = trunc(tmp);
-                    valuator_mask_set(val, 1, tmpi);
-                    dev->last.remainder[1] = tmp - (double)tmpi;
-                }
-                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], dx, dy);
+                if (dx)
+                    valuator_mask_set_double(val, 0, mult * dx);
+                if (dy)
+                    valuator_mask_set_double(val, 1, mult * dy);
+                DebugAccelF("pos (%i | %i) delta x:%.3f y:%.3f\n", mult * dx,
+                            mult * dy);
             }
         }
     }
@@ -1195,7 +1182,6 @@ acceleratePointerLightweight(
 {
     double mult = 0.0, tmpf;
     double dx = 0.0, dy = 0.0;
-    int tmpi;
 
     if (valuator_mask_isset(val, 0)) {
         dx = valuator_mask_get(val, 0);
@@ -1205,53 +1191,38 @@ acceleratePointerLightweight(
         dy = valuator_mask_get(val, 1);
     }
 
+    if (valuator_mask_num_valuators(val) == 0)
+        return;
+
     if (!dx && !dy)
         return;
 
     if (dev->ptrfeed && dev->ptrfeed->ctrl.num) {
         /* modeled from xf86Events.c */
         if (dev->ptrfeed->ctrl.threshold) {
-            if ((abs(dx) + abs(dy)) >= dev->ptrfeed->ctrl.threshold) {
-                tmpf = ((double)dx *
-                        (double)(dev->ptrfeed->ctrl.num)) /
-                       (double)(dev->ptrfeed->ctrl.den) +
-                       dev->last.remainder[0];
+            if ((fabs(dx) + fabs(dy)) >= dev->ptrfeed->ctrl.threshold) {
                 if (dx) {
-                    tmpi = (int) tmpf;
-                    valuator_mask_set(val, 0, tmpi);
-                    dev->last.remainder[0] = tmpf - (double)tmpi;
+                    tmpf = (dx * (double)(dev->ptrfeed->ctrl.num)) /
+                           (double)(dev->ptrfeed->ctrl.den);
+                    valuator_mask_set_double(val, 0, tmpf);
                 }
 
-                tmpf = ((double)dy *
-                        (double)(dev->ptrfeed->ctrl.num)) /
-                       (double)(dev->ptrfeed->ctrl.den) +
-                       dev->last.remainder[1];
                 if (dy) {
-                    tmpi = (int) tmpf;
-                    valuator_mask_set(val, 1, tmpi);
-                    dev->last.remainder[1] = tmpf - (double)tmpi;
+                    tmpf = (dy * (double)(dev->ptrfeed->ctrl.num)) /
+                           (double)(dev->ptrfeed->ctrl.den);
+                    valuator_mask_set_double(val, 1, tmpf);
                 }
             }
         }
         else {
-	    mult = pow((double)dx * (double)dx + (double)dy * (double)dy,
+	    mult = pow(dx * dx + dy * dy,
                        ((double)(dev->ptrfeed->ctrl.num) /
                         (double)(dev->ptrfeed->ctrl.den) - 1.0) /
                        2.0) / 2.0;
-            if (dx) {
-                tmpf = mult * (double)dx +
-                       dev->last.remainder[0];
-                tmpi = (int) tmpf;
-                valuator_mask_set(val, 0, tmpi);
-                dev->last.remainder[0] = tmpf - (double)tmpi;
-            }
-            if (dy) {
-                tmpf = mult * (double)dy +
-                       dev->last.remainder[1];
-                tmpi = (int)tmpf;
-                valuator_mask_set(val, 1, tmpi);
-                dev->last.remainder[1] = tmpf - (double)tmpi;
-            }
+            if (dx)
+                valuator_mask_set_double(val, 0, mult * dx);
+            if (dy)
+                valuator_mask_set_double(val, 1, mult * dy);
         }
     }
 }
-- 
1.7.5.3



More information about the xorg-devel mailing list