[PATCH 3/3] refactor predictable scheme initialization

Simon Thum simon.thum at gmx.de
Sun Sep 5 09:10:42 PDT 2010


This intends to clean up the accel struct and streamline initialization.

Signed-off-by: Simon Thum <simon.thum at gmx.de>
---
 dix/devices.c      |   14 +++++-----
 dix/ptrveloc.c     |   68 +++++++++++++++++++++++++++++++++++----------------
 include/ptrveloc.h |   22 +++++++++-------
 3 files changed, 65 insertions(+), 39 deletions(-)

diff --git a/dix/devices.c b/dix/devices.c
index acde30c..37e3b79 100644
--- a/dix/devices.c
+++ b/dix/devices.c
@@ -1288,7 +1288,7 @@ InitValuatorClassDeviceStruct(DeviceIntPtr dev, int numAxes, Atom *labels,
 ValuatorAccelerationRec pointerAccelerationScheme[] = {
     {PtrAccelNoOp, NULL, NULL, NULL, NULL},
     {PtrAccelPredictable, acceleratePointerPredictable, NULL,
-	InitPredictableAccelerationScheme, AccelerationDefaultCleanup},
+        InitPredictableAccelerationScheme, AccelerationDefaultCleanup},
     {PtrAccelLightweight, acceleratePointerLightweight, NULL, NULL, NULL},
     {-1, NULL, NULL, NULL, NULL} /* terminator */
 };
@@ -1308,7 +1308,7 @@ InitPointerAccelerationScheme(DeviceIntPtr dev,
     val = dev->valuator;
 
     if (!val)
-	return FALSE;
+        return FALSE;
 
     if (IsMaster(dev) && scheme != PtrAccelNoOp)
         return FALSE;
@@ -1327,12 +1327,12 @@ InitPointerAccelerationScheme(DeviceIntPtr dev,
         val->accelScheme.AccelCleanupProc(dev);
 
     if (pointerAccelerationScheme[i].AccelInitProc) {
-	schemeRec = pointerAccelerationScheme[i].AccelInitProc(dev);
-	if (!schemeRec)
-	    return FALSE;
-	val->accelScheme = *schemeRec;
+        schemeRec = pointerAccelerationScheme[i].AccelInitProc(dev);
+        if (!schemeRec)
+            return FALSE;
+        val->accelScheme = *schemeRec;
     } else {
-	val->accelScheme = pointerAccelerationScheme[i];
+        val->accelScheme = pointerAccelerationScheme[i];
     }
     return TRUE;
 }
diff --git a/dix/ptrveloc.c b/dix/ptrveloc.c
index 990bfe9..b0da768 100644
--- a/dix/ptrveloc.c
+++ b/dix/ptrveloc.c
@@ -30,6 +30,7 @@
 #include <ptrveloc.h>
 #include <exevents.h>
 #include <X11/Xatom.h>
+#include <os.h>
 
 #include <xserver-properties.h>
 
@@ -67,6 +68,10 @@ SimpleSmoothProfile(DeviceIntPtr dev, DeviceVelocityPtr vel, float velocity,
                     float threshold, float acc);
 static PointerAccelerationProfileFunc
 GetAccelerationProfile(DeviceVelocityPtr vel, int profile_num);
+static BOOL
+InitializePredictableAccelerationProperties(DeviceIntPtr, long*);
+static BOOL
+DeletePredictableAccelerationProperties(DeviceIntPtr, long*);
 
 /*#define PTRACCEL_DEBUGGING*/
 
@@ -123,13 +128,18 @@ ValuatorAccelerationPtr
 InitPredictableAccelerationScheme(DeviceIntPtr dev) {
     DeviceVelocityPtr vel;
     ValuatorAccelerationPtr scheme;
+    PredictableAccelSchemePtr schemeData;
     scheme = calloc(1, sizeof(ValuatorAccelerationRec));
     vel = calloc(1, sizeof(DeviceVelocityRec));
-    if (!vel || !scheme)
-	return NULL;
+    schemeData = calloc(1, sizeof(PredictableAccelSchemeRec));
+    if (!vel || !scheme || !schemeData)
+        return NULL;
     InitVelocityData(vel);
-    scheme->accelData = vel;
-    InitializePredictableAccelerationProperties(dev);
+    schemeData->vel = vel;
+    schemeData->prop_handlers = calloc(NPROPS_PREDICTABLE_ACCEL,
+                                      sizeof(long));
+    InitializePredictableAccelerationProperties(dev, schemeData->prop_handlers);
+    scheme->accelData = schemeData;
     return scheme;
 }
 
@@ -140,14 +150,24 @@ InitPredictableAccelerationScheme(DeviceIntPtr dev) {
 void
 AccelerationDefaultCleanup(DeviceIntPtr dev)
 {
-    /*sanity check*/
-    if( dev->valuator->accelScheme.AccelSchemeProc == acceleratePointerPredictable
-            && dev->valuator->accelScheme.accelData != NULL){
+    DeviceVelocityPtr vel = GetDevicePredictableAccelData(dev);
+    long* prop_handlers;
+    if (vel) {
+        /* the proper guarantee would be that we're not inside of
+         * AccelSchemeProc(), but that seems impossible. Schemes don't get
+         * schwitched often anyway.
+         */
+        OsBlockSignals();
         dev->valuator->accelScheme.AccelSchemeProc = NULL;
-        FreeVelocityData(dev->valuator->accelScheme.accelData);
+        FreeVelocityData(vel);
+        free(vel);
+        prop_handlers = ((PredictableAccelSchemePtr)
+            dev->valuator->accelScheme.accelData)->prop_handlers;
+        DeletePredictableAccelerationProperties(dev, prop_handlers);
+        free(prop_handlers);
         free(dev->valuator->accelScheme.accelData);
         dev->valuator->accelScheme.accelData = NULL;
-        DeletePredictableAccelerationProperties(dev);
+        OsReleaseSignals();
     }
 }
 
@@ -339,26 +359,30 @@ AccelInitScaleProperty(DeviceIntPtr dev, DeviceVelocityPtr vel)
     return XIRegisterPropertyHandler(dev, AccelSetScaleProperty, NULL, NULL);
 }
 
-BOOL
-InitializePredictableAccelerationProperties(DeviceIntPtr dev)
+static BOOL
+InitializePredictableAccelerationProperties(
+    DeviceIntPtr dev,
+    long* prop_handlers)
 {
     DeviceVelocityPtr  vel = GetDevicePredictableAccelData(dev);
 
     if(!vel)
 	return FALSE;
 
-    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);
+    prop_handlers[0] = AccelInitProfileProperty(dev, vel);
+    prop_handlers[1] = AccelInitDecelProperty(dev, vel);
+    prop_handlers[2] = AccelInitAdaptDecelProperty(dev, vel);
+    prop_handlers[3] = AccelInitScaleProperty(dev, vel);
 
     return TRUE;
 }
 
 BOOL
-DeletePredictableAccelerationProperties(DeviceIntPtr dev)
+DeletePredictableAccelerationProperties(
+    DeviceIntPtr dev,
+    long* prop_handlers)
 {
-    DeviceVelocityPtr  vel;
+    DeviceVelocityPtr vel;
     Atom prop;
     int i;
 
@@ -373,8 +397,8 @@ DeletePredictableAccelerationProperties(DeviceIntPtr dev)
 
     vel = GetDevicePredictableAccelData(dev);
     for (i = 0; vel && i < NPROPS_PREDICTABLE_ACCEL; i++)
-	if (vel->prop_handlers[i])
-	    XIUnregisterPropertyHandler(dev, vel->prop_handlers[i]);
+        if (prop_handlers[i])
+            XIUnregisterPropertyHandler(dev, prop_handlers[i]);
 
     return TRUE;
 }
@@ -391,8 +415,7 @@ InitTrackers(DeviceVelocityPtr vel, int ntracker)
 	return;
     }
     free(vel->tracker);
-    vel->tracker = (MotionTrackerPtr)malloc(ntracker * sizeof(MotionTracker));
-    memset(vel->tracker, 0, ntracker * sizeof(MotionTracker));
+    vel->tracker = (MotionTrackerPtr)calloc(ntracker, sizeof(MotionTracker));
     vel->num_tracker = ntracker;
 }
 
@@ -1022,7 +1045,8 @@ GetDevicePredictableAccelData(
 	    acceleratePointerPredictable &&
 	dev->valuator->accelScheme.accelData != NULL){
 
-	return (DeviceVelocityPtr)dev->valuator->accelScheme.accelData;
+	return ((PredictableAccelSchemePtr)
+		dev->valuator->accelScheme.accelData)->vel;
     }
     return NULL;
 }
diff --git a/include/ptrveloc.h b/include/ptrveloc.h
index db511be..07258eb 100644
--- a/include/ptrveloc.h
+++ b/include/ptrveloc.h
@@ -62,9 +62,6 @@ typedef struct _MotionTracker {
     int dir;        /* initial direction bitfield */
 } MotionTracker, *MotionTrackerPtr;
 
-/* number of properties for predictable acceleration */
-#define NPROPS_PREDICTABLE_ACCEL 4
-
 /**
  * Contains all data needed to implement mouse ballistics
  */
@@ -91,9 +88,20 @@ typedef struct _DeviceVelocityRec {
     struct {   /* to be able to query this information */
         int     profile_number;
     } statistics;
-    long    prop_handlers[NPROPS_PREDICTABLE_ACCEL];
 } DeviceVelocityRec, *DeviceVelocityPtr;
 
+/* number of properties for predictable acceleration */
+#define NPROPS_PREDICTABLE_ACCEL 4
+
+/**
+ * contains the run-time data for the predictable scheme, that is, a
+ * DeviceVelocityPtr and the property handlers.
+ */
+typedef struct _PredictableAccelSchemeRec {
+    DeviceVelocityPtr vel;
+    long* prop_handlers;
+} PredictableAccelSchemeRec, *PredictableAccelSchemePtr;
+
 extern _X_EXPORT void
 InitVelocityData(DeviceVelocityPtr vel);
 
@@ -110,12 +118,6 @@ BasicComputeAcceleration(DeviceIntPtr dev, DeviceVelocityPtr vel,
 extern _X_EXPORT void
 FreeVelocityData(DeviceVelocityPtr vel);
 
-extern _X_INTERNAL BOOL
-InitializePredictableAccelerationProperties(DeviceIntPtr dev);
-
-extern _X_INTERNAL BOOL
-DeletePredictableAccelerationProperties(DeviceIntPtr dev);
-
 extern _X_EXPORT int
 SetAccelerationProfile(DeviceVelocityPtr vel, int profile_num);
 
-- 
1.7.1


--------------070002080801000903070402--


More information about the xorg-devel mailing list