From cb4e5c90835e218247d3787c9df310b1fbdc7726 Mon Sep 17 00:00:00 2001
From: Emmanuele Bassi <ebassi@linux.intel.com>
Date: Fri, 19 Jun 2009 15:12:32 +0100
Subject: [PATCH] [x11] Simplify the XInput support

Instead of using a specific function to check whether the X
server supports the XInput extension we can use the generic
Xlib function XQueryExtension(). This cuts down the extra
checks inside the configure.ac and simplifies the code inside
clutter_x11_register_xinput().
---
 clutter/x11/clutter-backend-x11.c | 56 ++++++++++++++-----------------
 configure.ac                      | 41 ----------------------
 2 files changed, 26 insertions(+), 71 deletions(-)

diff --git a/clutter/x11/clutter-backend-x11.c b/clutter/x11/clutter-backend-x11.c
index 768b48a5e..222fa10ef 100644
--- a/clutter/x11/clutter-backend-x11.c
+++ b/clutter/x11/clutter-backend-x11.c
@@ -644,8 +644,9 @@ _clutter_x11_register_xinput ()
   XDevice *xdevice = NULL;
 
   XInputClassInfo *xclass_info = NULL;
-  XExtensionVersion *ext;
 
+  gint opcode, event, error;
+  gint res;
   gint num_devices = 0;
   gint num_events = 0;
   gint i = 0, j = 0;
@@ -672,21 +673,15 @@ _clutter_x11_register_xinput ()
 
   context = _clutter_context_get_default ();
 
-  backend_singleton->have_xinput = TRUE;
+  backend_singleton->have_xinput = FALSE;
 
-#if defined(HAVE_XQUERY_INPUT_VERSION)
-  ext = XQueryInputVersion (backend_singleton->xdpy, XI_2_Major, XI_2_Minor);
-#elif defined(HAVE_XGET_EXTENSION_VERSION)
-  ext = XGetExtensionVersion (backend_singleton->xdpy, INAME);
-#else
-  g_critical ("XInput does not have XGetExtensionVersion nor "
-              "XQueryInputVersion");
-  return;
-#endif
-
-  if (!ext || (ext == (XExtensionVersion*) NoSuchExtension))
+  /* is the XInput extension available? */
+  res = XQueryExtension (backend_singleton->xdpy, "XInputExtension",
+                         &opcode, &event,
+                         &error);
+  if (!res)
     {
-      backend_singleton->have_xinput = FALSE;
+      CLUTTER_NOTE (BACKEND, "X Input extension not available");
       return;
     }
 
@@ -697,15 +692,12 @@ _clutter_x11_register_xinput ()
   CLUTTER_NOTE (BACKEND, "%d XINPUT devices found", num_devices);
 
   if (num_devices == 0)
-    {
-      backend_singleton->have_xinput = FALSE;
-      return;
-    }
+    return;
 
   for (i = 0; i < num_devices; i++)
     {
-      num_events = 0;
       info = xdevices + i;
+      num_events = 0;
 
       CLUTTER_NOTE (BACKEND, "Considering %li with type %d",
                     info->id,
@@ -713,11 +705,11 @@ _clutter_x11_register_xinput ()
 
       /* Only want 'raw' devices themselves not virtual ones */
       if (info->use == IsXExtensionPointer ||
-          /*info->use == IsXExtensionKeyboard || XInput is broken */
+        /*info->use == IsXExtensionKeyboard || XInput 1.x is broken */
           info->use == IsXExtensionDevice)
         {
           clutter_x11_trap_x_errors ();
-          xdevice = XOpenDevice (x11b->xdpy, info->id);
+          xdevice = XOpenDevice (backend_singleton->xdpy, info->id);
           if (clutter_x11_untrap_x_errors () || xdevice == NULL)
             continue;
 
@@ -734,11 +726,12 @@ _clutter_x11_register_xinput ()
               break;
 
 #if 0
-            /* XInput is broken for keyboards: */
+            /* XInput 1.x is broken for keyboards: */
             case IsXExtensionKeyboard:
               device->device.type = CLUTTER_KEYBOARD_DEVICE;
               break;
 #endif
+
             case IsXExtensionDevice:
               device->device.device_type = CLUTTER_EXTENSION_DEVICE;
               break;
@@ -751,8 +744,8 @@ _clutter_x11_register_xinput ()
           device->device.previous_y = -1;
           device->device.previous_button_number = -1;
 
-          device->xdevice = xdevice;
           device->num_events = 0;
+          device->xdevice = xdevice;
 
           CLUTTER_NOTE (BACKEND, "Registering XINPUT device with XID: %li",
                         xdevice->device_id);
@@ -769,7 +762,7 @@ _clutter_x11_register_xinput ()
               switch (xclass_info->input_class)
                 {
 #if 0
-                /* XInput is broken for keyboards: */
+                /* XInput 1.x is broken for keyboards: */
                 case KeyClass:
                   DeviceKeyPress (xdevice,
                                   x11b->event_types[CLUTTER_X11_XINPUT_KEY_PRESS_EVENT],
@@ -809,7 +802,7 @@ _clutter_x11_register_xinput ()
 
           device->num_events  = num_events;
 
-          input_devices = g_slist_append (input_devices, device);
+          input_devices = g_slist_prepend (input_devices, device);
         }
     }
 
@@ -824,17 +817,20 @@ _clutter_x11_register_xinput ()
       */
       g_warning ("No usuable XInput pointing devices found");
 
-      backend_singleton->have_xinput = FALSE;
-
       for (l = input_devices; l != NULL; l = l->next)
         g_slice_free (ClutterX11XInputDevice, l->data);
 
       g_slist_free (input_devices);
       context->input_devices = NULL;
-    }
-  else
-    context->input_devices = input_devices;
 
+      return;
+    }
+
+  /* store the list of input devices */
+  context->input_devices = g_slist_reverse (input_devices);
+
+  /* why yes, we are awesome */
+  backend_singleton->have_xinput = TRUE;
 #endif /* HAVE_XINPUT */
 }
 
diff --git a/configure.ac b/configure.ac
index b314e3480..2534e9cf1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -521,49 +521,8 @@ AS_IF([test "x$clutterbackend" = "xglx" || test "x$clutterbackend" = "xeglx"],
                 [
                   AC_DEFINE(HAVE_XINPUT, 1, Use the XINPUT X extension)
 
-                  # ugh, this is insane
-                  AC_TRY_COMPILE([#include <X11/extensions/XInput.h>],
-                              [
- XExtensionVersion *res;
- res = XGetExtensionVersion (NULL, INAME);
-                              ],
-                              [have_xget_extension_version=yes],
-                              [have_xget_extension_version=no])
-
-                  AC_TRY_COMPILE([#include <X11/extensions/XInput.h>],
-                              [
- XExtensionVersion *res;
- res = XQueryInputVersion (NULL, XI_2_Major, XI_2_Minor);
-                              ],
-                              [have_xquery_input_version=yes],
-                              [have_xquery_input_version=no])
-
-                  AS_IF([test "x$have_xget_extension_version" = "xyes"],
-                        [
-                          AC_DEFINE([HAVE_XGET_EXTENSION_VERSION],
-                                    [1],
-                                    [Define to 1 if we have XGetExtensionVersion])
-                        ])
-
-                  #Seems some distros ship with the header but not the symbol
-                  really_have_xinput_query_version=no   
-                  AS_IF([test "x$have_xquery_input_version" = "xyes"],
-                        [
-                         AC_CHECK_LIB(Xi, [XQueryInputVersion],
-                                      [really_have_xinput_query_version=yes],
-                                      [])
-                        ])
-
-                  AS_IF([test "x$really_have_xquery_input_version" = "xyes"],
-                        [
-                          AC_DEFINE([HAVE_XQUERY_INPUT_VERSION],
-                                    [1],
-                                    [Define to 1 if we have XQueryInputVersion])
-                        ])
-
                   X11_LIBS="$X11_LIBS -lXi"
                   X11_PC_FILES="$X11_PC_FILES xi"
-
                 ],
 
                 [no],