[gnome-settings-daemon] common: provide a helper function to close an XDevice safely



commit a800b3333c8a4501fa501aa918c4d2f88c20e9fd
Author: Peter Hutterer <peter hutterer who-t net>
Date:   Thu Feb 20 17:21:21 2014 +1000

    common: provide a helper function to close an XDevice safely
    
    XCloseDevice may cause a BadDevice error if the device disappeared before we
    close it.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=724789

 plugins/common/gsd-device-mapper.c |    2 +-
 plugins/common/gsd-input-helper.c  |   15 ++++++++++-----
 plugins/common/gsd-input-helper.h  |    1 +
 plugins/mouse/gsd-mouse-manager.c  |   30 +++++++++++++++---------------
 plugins/wacom/gsd-wacom-device.c   |    7 ++++---
 plugins/wacom/gsd-wacom-manager.c  |    8 ++++----
 6 files changed, 35 insertions(+), 28 deletions(-)
---
diff --git a/plugins/common/gsd-device-mapper.c b/plugins/common/gsd-device-mapper.c
index 82a4d3d..5f777f3 100644
--- a/plugins/common/gsd-device-mapper.c
+++ b/plugins/common/gsd-device-mapper.c
@@ -157,7 +157,7 @@ device_apply_property (GdkDevice      *device,
                return FALSE;
 
        retval = device_set_property (xdev, gdk_device_get_name (device), property);
-       XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdev);
+       xdevice_close (xdev);
        return retval;
 }
 
diff --git a/plugins/common/gsd-input-helper.c b/plugins/common/gsd-input-helper.c
index d359e87..f7077cc 100644
--- a/plugins/common/gsd-input-helper.c
+++ b/plugins/common/gsd-input-helper.c
@@ -264,12 +264,9 @@ device_type_is_present (InfoIdentifyFunc info_func,
                         continue;
 
                 retval = (device_func) (device);
-                if (retval) {
-                        XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), device);
+                xdevice_close (device);
+                if (retval)
                         break;
-                }
-
-                XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), device);
         }
         XFreeDeviceList (device_info);
 
@@ -610,3 +607,11 @@ xdevice_get_wacom_tool_type (int deviceid)
 
         return ret;
 }
+
+void
+xdevice_close (XDevice *xdevice)
+{
+    gdk_error_trap_push ();
+    XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+    gdk_error_trap_pop_ignored();
+}
diff --git a/plugins/common/gsd-input-helper.h b/plugins/common/gsd-input-helper.h
index 66b2062..702a15a 100644
--- a/plugins/common/gsd-input-helper.h
+++ b/plugins/common/gsd-input-helper.h
@@ -80,6 +80,7 @@ gboolean  run_custom_command      (GdkDevice              *device,
 GList *   get_disabled_devices     (GdkDeviceManager       *manager);
 char *    xdevice_get_device_node  (int                     deviceid);
 int       xdevice_get_last_tool_id (int                     deviceid);
+void      xdevice_close      (XDevice                *xdevice);
 
 const char * xdevice_get_wacom_tool_type (int               deviceid);
 
diff --git a/plugins/mouse/gsd-mouse-manager.c b/plugins/mouse/gsd-mouse-manager.c
index e7d3c74..04b0a3c 100644
--- a/plugins/mouse/gsd-mouse-manager.c
+++ b/plugins/mouse/gsd-mouse-manager.c
@@ -370,7 +370,7 @@ set_left_handed (GsdMouseManager *manager,
         gdk_error_trap_pop_ignored ();
 
 out:
-        XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+        xdevice_close (xdevice);
         g_free (buttons);
 }
 
@@ -467,7 +467,7 @@ set_motion (GsdMouseManager *manager,
 
     out:
 
-        XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+        xdevice_close (xdevice);
 }
 
 static void
@@ -514,7 +514,7 @@ set_middle_button (GsdMouseManager *manager,
         if (rc == Success)
                 XFree (data);
 
-        XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+        xdevice_close (xdevice);
 }
 
 /* Ensure that syndaemon dies together with us, to avoid running several of
@@ -621,7 +621,7 @@ set_tap_to_click (GdkDevice *device,
                 return;
 
         if (!device_is_touchpad (xdevice)) {
-                XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+                xdevice_close (xdevice);
                 return;
         }
 
@@ -647,7 +647,7 @@ set_tap_to_click (GdkDevice *device,
         if (gdk_error_trap_pop ())
                 g_warning ("Error in setting tap to click on \"%s\"", gdk_device_get_name (device));
 
-        XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+        xdevice_close (xdevice);
 }
 
 static void
@@ -672,7 +672,7 @@ set_horiz_scroll (GdkDevice *device,
                 return;
 
         if (!device_is_touchpad (xdevice)) {
-                XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+                xdevice_close (xdevice);
                 return;
         }
 
@@ -711,7 +711,7 @@ set_horiz_scroll (GdkDevice *device,
         if (rc == Success)
                 XFree (data);
 
-        XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+        xdevice_close (xdevice);
 
 }
 
@@ -739,7 +739,7 @@ set_scroll_method (GsdMouseManager         *manager,
                 return;
 
         if (!device_is_touchpad (xdevice)) {
-                XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+                xdevice_close (xdevice);
                 return;
         }
 
@@ -792,7 +792,7 @@ set_scroll_method (GsdMouseManager         *manager,
         if (rc == Success)
                 XFree (data);
 
-        XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+        xdevice_close (xdevice);
 }
 
 static void
@@ -810,7 +810,7 @@ set_touchpad_disabled (GdkDevice *device)
                 return;
 
         if (!device_is_touchpad (xdevice)) {
-                XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+                xdevice_close (xdevice);
                 return;
         }
 
@@ -819,7 +819,7 @@ set_touchpad_disabled (GdkDevice *device)
         else
                 g_debug ("Disabled device \"%s\" (%d)", gdk_device_get_name (device), id);
 
-        XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+        xdevice_close (xdevice);
 }
 
 static void
@@ -835,7 +835,7 @@ set_touchpad_enabled (int id)
                 return;
 
         if (!device_is_touchpad (xdevice)) {
-                XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+                xdevice_close (xdevice);
                 return;
         }
 
@@ -844,7 +844,7 @@ set_touchpad_enabled (int id)
         else
                 g_debug ("Enabled device %d", id);
 
-        XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+        xdevice_close (xdevice);
 }
 
 static void
@@ -967,7 +967,7 @@ set_natural_scroll (GsdMouseManager *manager,
                 return;
 
         if (!device_is_touchpad (xdevice)) {
-                XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+                xdevice_close (xdevice);
                 return;
         }
 
@@ -1008,7 +1008,7 @@ set_natural_scroll (GsdMouseManager *manager,
         if (rc == Success)
                 XFree (data);
 
-        XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+        xdevice_close (xdevice);
 }
 
 static void
diff --git a/plugins/wacom/gsd-wacom-device.c b/plugins/wacom/gsd-wacom-device.c
index d656c2f..1b18143 100644
--- a/plugins/wacom/gsd-wacom-device.c
+++ b/plugins/wacom/gsd-wacom-device.c
@@ -500,11 +500,12 @@ get_device_type (XDeviceInfo *dev)
                                  device, prop, 0, 1, False,
                                  XA_ATOM, &realtype, &realformat, &nitems,
                                  &bytes_after, &data);
-        XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), device);
 
         if (gdk_error_trap_pop () || rc != Success || realtype == None)
                 ret = WACOM_TYPE_INVALID;
 
+        xdevice_close (device);
+
         XFree (data);
 
        return ret;
@@ -1901,7 +1902,7 @@ gsd_wacom_device_get_area (GsdWacomDevice *device)
                                 XA_INTEGER, &realtype, &realformat, &nitems,
                                 &bytes_after, &data);
        if (gdk_error_trap_pop () || rc != Success || realtype == None || bytes_after != 0 || nitems != 4) {
-               XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+               xdevice_close (xdevice);
                return NULL;
        }
 
@@ -1910,7 +1911,7 @@ gsd_wacom_device_get_area (GsdWacomDevice *device)
                device_area[i] = ((long *)data)[i];
 
        XFree (data);
-       XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
+       xdevice_close (xdevice);
 
        return device_area;
 }
diff --git a/plugins/wacom/gsd-wacom-manager.c b/plugins/wacom/gsd-wacom-manager.c
index 0744721..1670ae5 100644
--- a/plugins/wacom/gsd-wacom-manager.c
+++ b/plugins/wacom/gsd-wacom-manager.c
@@ -268,7 +268,7 @@ wacom_set_property (GsdWacomDevice *device,
 
        xdev = open_device (device);
        device_set_property (xdev, gsd_wacom_device_get_tool_name (device), property);
-       XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdev);
+       xdevice_close (xdev);
 }
 
 static void
@@ -367,7 +367,7 @@ set_absolute (GsdWacomDevice  *device,
        if (gdk_error_trap_pop ())
                g_error ("Failed to set mode \"%s\" for \"%s\".",
                         is_absolute ? "Absolute" : "Relative", gsd_wacom_device_get_tool_name (device));
-       XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdev);
+       xdevice_close (xdev);
 }
 
 static void
@@ -518,7 +518,7 @@ set_device_buttonmap (GsdWacomDevice *device,
 
        g_free (map);
 
-       XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdev);
+       xdevice_close (xdev);
 }
 
 static void
@@ -747,7 +747,7 @@ reset_pad_buttons (GsdWacomDevice *device)
        reset_touch_buttons (xdev, def_touchstrip_buttons, "Wacom Strip Buttons");
        gdk_error_trap_pop_ignored ();
 
-       XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdev);
+       xdevice_close (xdev);
 
        update_pad_leds (device);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]