From b1fe1b9b727f2018ced6e90f7f6e492c20069703 Mon Sep 17 00:00:00 2001
From: Simon Thum <simon.thum@gmx.de>
Date: Sat, 4 Sep 2010 16:31:24 +0200
Subject: [PATCH 1/4] dix: refactor scheme init

Signed-off-by: Simon Thum <simon.thum@gmx.de>
---
 dix/devices.c      |   53 +++++++++++++++------------------------------------
 dix/ptrveloc.c     |   34 +++++++++++++++++++++++++-------
 include/input.h    |    5 ++++
 include/inputstr.h |    1 +
 include/ptrveloc.h |    4 +++
 5 files changed, 52 insertions(+), 45 deletions(-)

diff --git a/dix/devices.c b/dix/devices.c
index 6c0dc42..052813a 100644
--- a/dix/devices.c
+++ b/dix/devices.c
@@ -1279,10 +1279,11 @@ InitValuatorClassDeviceStruct(DeviceIntPtr dev, int numAxes, Atom *labels,
 
 /* global list of acceleration schemes */
 ValuatorAccelerationRec pointerAccelerationScheme[] = {
-    {PtrAccelNoOp,        NULL, NULL, NULL},
-    {PtrAccelPredictable, acceleratePointerPredictable, NULL, AccelerationDefaultCleanup},
-    {PtrAccelLightweight, acceleratePointerLightweight, NULL, NULL},
-    {-1, NULL, NULL, NULL} /* terminator */
+    {PtrAccelNoOp, NULL, NULL, NULL, NULL},
+    {PtrAccelPredictable, acceleratePointerPredictable, NULL,
+	InitPredictableAccelerationScheme, AccelerationDefaultCleanup},
+    {PtrAccelLightweight, acceleratePointerLightweight, NULL, NULL, NULL},
+    {-1, NULL, NULL, NULL, NULL} /* terminator */
 };
 
 /**
@@ -1294,59 +1295,37 @@ InitPointerAccelerationScheme(DeviceIntPtr dev,
                               int scheme)
 {
     int x, i = -1;
-    void* data = NULL;
     ValuatorClassPtr val;
 
     val = dev->valuator;
 
-    if(!val)
+    if (!val)
 	return FALSE;
 
-    if(IsMaster(dev) && scheme != PtrAccelNoOp)
+    if (IsMaster(dev) && scheme != PtrAccelNoOp)
         return FALSE;
 
-    for(x = 0; pointerAccelerationScheme[x].number >= 0; x++) {
+    for (x = 0; pointerAccelerationScheme[x].number >= 0; x++) {
         if(pointerAccelerationScheme[x].number == scheme){
             i = x;
             break;
         }
     }
 
-    if(-1 == i)
+    if (-1 == i)
         return FALSE;
 
     if (val->accelScheme.AccelCleanupProc)
         val->accelScheme.AccelCleanupProc(dev);
 
-    /* init scheme-specific data */
-    switch(scheme){
-        case PtrAccelPredictable:
-        {
-            DeviceVelocityPtr s;
-            s = malloc(sizeof(DeviceVelocityRec));
-            if(!s)
-        	return FALSE;
-            InitVelocityData(s);
-            data = s;
-            break;
-        }
-        default:
-            break;
-    }
-
-    val->accelScheme = pointerAccelerationScheme[i];
-    val->accelScheme.accelData = data;
-
-    /* post-init scheme */
-    switch(scheme){
-        case PtrAccelPredictable:
-            InitializePredictableAccelerationProperties(dev);
-            break;
-
-        default:
-            break;
+    if (pointerAccelerationScheme[i].AccelInitProc) {
+	if (!pointerAccelerationScheme[i].AccelInitProc(dev,
+					&pointerAccelerationScheme[i])) {
+	    return FALSE;
+	}
+    } else {
+	val->accelScheme = pointerAccelerationScheme[i];
     }
-
     return TRUE;
 }
 
diff --git a/dix/ptrveloc.c b/dix/ptrveloc.c
index 8f03321..829cbf4 100644
--- a/dix/ptrveloc.c
+++ b/dix/ptrveloc.c
@@ -85,7 +85,7 @@ GetAccelerationProfile(DeviceVelocityPtr vel, int profile_num);
 
 
 /**
- * Init struct so it should match the average case
+ * Init DeviceVelocity struct so it should match the average case
  */
 void
 InitVelocityData(DeviceVelocityPtr vel)
@@ -107,7 +107,7 @@ InitVelocityData(DeviceVelocityPtr vel)
 
 
 /**
- * Clean up
+ * Clean up DeviceVelocityRec
  */
 void
 FreeVelocityData(DeviceVelocityPtr vel){
@@ -116,8 +116,28 @@ FreeVelocityData(DeviceVelocityPtr vel){
 }
 
 
-/*
- *  dix uninit helper, called through scheme
+/**
+ * Init predictable scheme
+ */
+Bool
+InitPredictableAccelerationScheme(DeviceIntPtr dev,
+				  ValuatorAccelerationPtr protoScheme) {
+    DeviceVelocityPtr vel;
+    ValuatorAccelerationRec scheme;
+    scheme = *protoScheme;
+    vel = calloc(1, sizeof(DeviceVelocityRec));
+    if (!vel)
+	return FALSE;
+    InitVelocityData(vel);
+    scheme.accelData = vel;
+    dev->valuator->accelScheme = scheme;
+    InitializePredictableAccelerationProperties(dev);
+    return TRUE;
+}
+
+
+/**
+ *  Uninit scheme
  */
 void
 AccelerationDefaultCleanup(DeviceIntPtr dev)
@@ -1024,12 +1044,10 @@ acceleratePointerPredictable(
     int *valuators,
     int evtime)
 {
-    float mult = 0.0;
+    float fdx, fdy, tmp, mult; /* no need to init */
     int dx = 0, dy = 0;
     int *px = NULL, *py = NULL;
-    DeviceVelocityPtr velocitydata =
-	(DeviceVelocityPtr) dev->valuator->accelScheme.accelData;
-    float fdx, fdy, tmp; /* no need to init */
+    DeviceVelocityPtr velocitydata = GetDevicePredictableAccelData(dev);
     Bool soften = TRUE;
 
     if (!num_valuators || !valuators || !velocitydata)
diff --git a/include/input.h b/include/input.h
index f96a0a9..ab58a15 100644
--- a/include/input.h
+++ b/include/input.h
@@ -150,6 +150,11 @@ typedef void (*PointerAccelSchemeProc)(
 typedef void (*DeviceCallbackProc)(
               DeviceIntPtr /*pDev*/);
 
+struct _ValuatorAccelerationRec;
+typedef Bool (*PointerAccelSchemeInitProc)(
+              DeviceIntPtr /*dev*/,
+              struct _ValuatorAccelerationRec* /*protoScheme*/);
+
 typedef struct _DeviceRec {
     pointer	devicePrivate;
     ProcessInputProc processInputProc;	/* current */
diff --git a/include/inputstr.h b/include/inputstr.h
index b74ee04..65b9ef9 100644
--- a/include/inputstr.h
+++ b/include/inputstr.h
@@ -266,6 +266,7 @@ typedef struct _ValuatorAccelerationRec {
     int                         number;
     PointerAccelSchemeProc      AccelSchemeProc;
     void                       *accelData; /* at disposal of AccelScheme */
+    PointerAccelSchemeInitProc  AccelInitProc;
     DeviceCallbackProc          AccelCleanupProc;
 } ValuatorAccelerationRec, *ValuatorAccelerationPtr;
 
diff --git a/include/ptrveloc.h b/include/ptrveloc.h
index 6f999a8..8d7f037 100644
--- a/include/ptrveloc.h
+++ b/include/ptrveloc.h
@@ -129,6 +129,10 @@ SetDeviceSpecificAccelerationProfile(DeviceVelocityPtr vel,
 extern _X_INTERNAL void
 AccelerationDefaultCleanup(DeviceIntPtr dev);
 
+extern _X_INTERNAL Bool
+InitPredictableAccelerationScheme(DeviceIntPtr dev,
+				  struct _ValuatorAccelerationRec* protoScheme);
+
 extern _X_INTERNAL void
 acceleratePointerPredictable(DeviceIntPtr dev, int first_valuator,
                              int num_valuators, int *valuators, int evtime);
-- 
1.7.3.4
