[PATCH 10/10] xfree86: factor out adding/removing a device from the input device array

Peter Hutterer peter.hutterer at who-t.net
Sun Jul 3 23:09:16 PDT 2011


No functional changes, just readability improvements. This also gets rid of
the count variable. Count was just used for resizing the null-terminated
list. Since we're not in a time-critical path here at all we can afford to
loop the list multiple times instead of keeping an extra variable around.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 hw/xfree86/common/xf86Config.c |   93 ++++++++++++++++++++--------------------
 1 files changed, 47 insertions(+), 46 deletions(-)

diff --git a/hw/xfree86/common/xf86Config.c b/hw/xfree86/common/xf86Config.c
index 4a76a2d..0949d16 100644
--- a/hw/xfree86/common/xf86Config.c
+++ b/hw/xfree86/common/xf86Config.c
@@ -1068,6 +1068,46 @@ Bool xf86DRI2Enabled(void)
     return xf86Info.dri2;
 }
 
+/**
+ * Search for the pInfo in the null-terminated list given and remove (and
+ * free) it if present. All other devices are moved forward.
+ */
+static void
+freeDevice(InputInfoPtr *list, InputInfoPtr pInfo)
+{
+    InputInfoPtr *devs;
+
+    for (devs = list; devs && *devs; devs++) {
+	if (*devs == pInfo) {
+	    free(*devs);
+	    for (; devs && *devs; devs++)
+		devs[0] = devs[1];
+	    break;
+	}
+    }
+}
+
+/**
+ * Append pInfo to the null-terminated list, allocating space as necessary.
+ * pInfo is copied into the last element.
+ */
+static InputInfoPtr*
+addDevice(InputInfoPtr *list, InputInfoPtr pInfo)
+{
+    InputInfoPtr *devs;
+    int count = 1;
+
+    for (devs = list; devs && *devs; devs++)
+	count++;
+
+    list = xnfrealloc(list, (count + 1) * sizeof(InputInfoPtr));
+    list[count] = NULL;
+
+    list[count - 1] = xnfalloc(sizeof(InputInfoRec));
+    *list[count - 1] = *pInfo;
+    return list;
+}
+
 /*
  * Locate the core input devices.  These can be specified/located in
  * the following ways, in order of priority:
@@ -1094,7 +1134,6 @@ checkCoreInputDevices(serverLayoutPtr servlayoutp, Bool implicitLayout)
     InputInfoRec Pointer = {}, Keyboard = {};
     XF86ConfInputPtr confInput;
     XF86ConfInputRec defPtr, defKbd;
-    int count = 0;
     MessageType from = X_DEFAULT;
     int found = 0;
     const char *mousedrivers[] = { "mouse", "synaptics", "evdev", "vmmouse",
@@ -1119,7 +1158,6 @@ checkCoreInputDevices(serverLayoutPtr servlayoutp, Bool implicitLayout)
 		coreKeyboard = indp;
 	    }
 	}
-	count++;
     }
 
     confInput = NULL;
@@ -1139,17 +1177,9 @@ checkCoreInputDevices(serverLayoutPtr servlayoutp, Bool implicitLayout)
 	 * removed.
 	 */
 	if (corePointer) {
-	    for (devs = servlayoutp->inputs; devs && *devs; devs++) {
-		if (*devs == corePointer) {
-		    free(*devs);
-		    for (; devs && *devs; devs++)
-			devs[0] = devs[1];
-		    break;
-		}
-	    }
-	    count--;
+	    freeDevice(servlayoutp->inputs, corePointer);
+	    corePointer = NULL;
 	}
-	corePointer = NULL;
 	foundPointer = TRUE;
     }
 
@@ -1209,14 +1239,7 @@ checkCoreInputDevices(serverLayoutPtr servlayoutp, Bool implicitLayout)
         if (foundPointer) {
 	    Pointer.options = xf86addNewOption(Pointer.options,
 					       xnfstrdup("CorePointer"), "on");
-	    count++;
-	    devs = xnfrealloc(servlayoutp->inputs,
-			      (count + 1) * sizeof(InputInfoPtr));
-            devs[count - 1] = xnfalloc(sizeof(InputInfoRec));
-	    devs[count] = NULL;
-
-	    *devs[count - 1] = Pointer;
-	    servlayoutp->inputs = devs;
+	    servlayoutp->inputs = addDevice(servlayoutp->inputs, &Pointer);
 	}
     }
 
@@ -1254,14 +1277,7 @@ checkCoreInputDevices(serverLayoutPtr servlayoutp, Bool implicitLayout)
         if (foundPointer) {
 	    Pointer.options = xf86addNewOption(NULL,
 					       xnfstrdup("AlwaysCore"), "on");
-	    count++;
-	    devs = xnfrealloc(servlayoutp->inputs,
-			      (count + 1) * sizeof(InputInfoPtr));
-            devs[count - 1] = xnfalloc(sizeof(InputInfoRec));
-	    devs[count] = NULL;
-
-	    *devs[count - 1] = Pointer;
-	    servlayoutp->inputs = devs;
+	    servlayoutp->inputs = addDevice(servlayoutp->inputs, &Pointer);
 	}
     }
 
@@ -1282,17 +1298,9 @@ checkCoreInputDevices(serverLayoutPtr servlayoutp, Bool implicitLayout)
 	 * removed.
 	 */
 	if (coreKeyboard) {
-	    for (devs = servlayoutp->inputs; devs && *devs; devs++) {
-		if (*devs == coreKeyboard) {
-		    free(*devs);
-		    for (; devs && *devs; devs++)
-			devs[0] = devs[1];
-		    break;
-		}
-	    }
-	    count--;
+	    freeDevice(servlayoutp->inputs, coreKeyboard);
+	    coreKeyboard = NULL;
 	}
-	coreKeyboard = NULL;
 	foundKeyboard = TRUE;
     }
 
@@ -1350,14 +1358,7 @@ checkCoreInputDevices(serverLayoutPtr servlayoutp, Bool implicitLayout)
         if (foundKeyboard) {
 	    Keyboard.options = xf86addNewOption(Keyboard.options,
 						xnfstrdup("CoreKeyboard"), "on");
-	    count++;
-	    devs = xnfrealloc(servlayoutp->inputs,
-			      (count + 1) * sizeof(InputInfoPtr));
-            devs[count - 1] = xnfalloc(sizeof(InputInfoRec));
-	    devs[count] = NULL;
-
-	    *devs[count - 1] = Keyboard;
-	    servlayoutp->inputs = devs;
+	    servlayoutp->inputs = addDevice(servlayoutp->inputs, &Keyboard);
 	}
     }
 
-- 
1.7.5.4



More information about the xorg-devel mailing list