Code

Hopefully fix #334800 (input devices configuration not saved).
authorKrzysztof Kosiński <tweenk.pl@gmail.com>
Fri, 12 Mar 2010 00:52:36 +0000 (01:52 +0100)
committerKrzysztof Kosiński <tweenk.pl@gmail.com>
Fri, 12 Mar 2010 00:52:36 +0000 (01:52 +0100)
src/dialogs/input.cpp

index 0b4587f55aeb9a074b7e12bdcb780af16bf6e847..f2ef8afe065794cc9df77ad86059077d8fbc6216 100644 (file)
@@ -63,10 +63,24 @@ sp_input_dialog_delete (GtkObject */*object*/, GdkEvent */*event*/, gpointer /*d
 
 }
 
-static const gchar *axis_use_strings[GDK_AXIS_LAST] = {
+static gchar const *axis_use_strings[GDK_AXIS_LAST] = {
     "ignore", "x", "y", "pressure", "xtilt", "ytilt", "wheel"
 };
 
+static Glib::ustring sanitized_device_path(gchar const *str)
+{
+    // LP #334800: device names on Windows sometimes contain funny junk like
+    // \x03, \xf2, etc. We need to get rid of this to properly access the settings.
+    gchar *escaped = g_strescape(str, NULL);
+
+    for (gchar *s = escaped; *s; ++s) {
+        if (*s == '/') *s = '_';
+    }
+    Glib::ustring device_path = Glib::ustring("/devices/") + escaped;
+    g_free(escaped);
+    return device_path;
+}
+
 void
 sp_input_load_from_preferences (void)
 {
@@ -74,60 +88,59 @@ sp_input_load_from_preferences (void)
 
     for (GList *list_ptr = gdk_devices_list(); list_ptr != NULL; list_ptr = list_ptr->next) {
         GdkDevice *device = static_cast<GdkDevice *>(list_ptr->data);
-        //repr = sp_repr_lookup_child(devices, "id", device->name);
-        Glib::ustring device_path = Glib::ustring("/devices/") + device->name;
-        if (/*repr != NULL*/ 1) {
-            GdkInputMode mode;
-            Glib::ustring device_mode = prefs->getString(device_path + "/mode");
-
-            if (device_mode.empty())
-                mode = GDK_MODE_DISABLED;
-            else if (device_mode == "screen")
-                mode = GDK_MODE_SCREEN;
-            else if (device_mode == "window")
-                mode = GDK_MODE_WINDOW;
-            else
-                mode = GDK_MODE_DISABLED;
-
-            if (device->mode != mode) {
-                gdk_device_set_mode(device, mode);
-            }
 
-            Glib::ustring::size_type pos0, pos1;
-            GdkAxisUse axis_use;
-
-            //temp_ptr = repr->attribute("axes");
-            Glib::ustring const axes_str = prefs->getString(device_path + "/axes");
-            pos0 = pos1 = 0;
-            for (gint i=0; i < device->num_axes; i++) {
-                pos1 = axes_str.find(';', pos0);
-                if (pos1 == Glib::ustring::npos)
-                    break;  // Too few axis specifications
-
-                axis_use = GDK_AXIS_IGNORE;
-                for (gint j=0; j < GDK_AXIS_LAST; j++)
-                    if (!strcmp(axes_str.substr(pos0, pos1-pos0).c_str(), axis_use_strings[j])) {
-                        axis_use = static_cast<GdkAxisUse>(j);
-                        break;
-                    }
-                gdk_device_set_axis_use(device, i, axis_use);
-                pos0 = pos1 + 1;
-            }
+        Glib::ustring device_path = sanitized_device_path(device->name);
 
-            guint keyval;
-            GdkModifierType modifier;
+        GdkInputMode mode;
+        Glib::ustring device_mode = prefs->getString(device_path + "/mode");
 
-            Glib::ustring const keys_str = prefs->getString(device_path + "/keys");
-            pos0 = pos1 = 0;
-            for (gint i=0; i < device->num_keys; i++) {
-                pos1 = keys_str.find(';', pos0);
-                if (pos1 == Glib::ustring::npos)
-                    break;  // Too few key specifications
+        if (device_mode.empty())
+            mode = GDK_MODE_DISABLED;
+        else if (device_mode == "screen")
+            mode = GDK_MODE_SCREEN;
+        else if (device_mode == "window")
+            mode = GDK_MODE_WINDOW;
+        else
+            mode = GDK_MODE_DISABLED;
 
-                gtk_accelerator_parse(keys_str.substr(pos0, pos1-pos0).c_str(), &keyval, &modifier);
-                gdk_device_set_key(device, i, keyval, modifier);
-                pos0 = pos1 + 1;
-            }
+        if (device->mode != mode) {
+            gdk_device_set_mode(device, mode);
+        }
+
+        Glib::ustring::size_type pos0, pos1;
+        GdkAxisUse axis_use;
+
+        //temp_ptr = repr->attribute("axes");
+        Glib::ustring const axes_str = prefs->getString(device_path + "/axes");
+        pos0 = pos1 = 0;
+        for (gint i=0; i < device->num_axes; i++) {
+            pos1 = axes_str.find(';', pos0);
+            if (pos1 == Glib::ustring::npos)
+                break;  // Too few axis specifications
+
+            axis_use = GDK_AXIS_IGNORE;
+            for (gint j=0; j < GDK_AXIS_LAST; j++)
+                if (!strcmp(axes_str.substr(pos0, pos1-pos0).c_str(), axis_use_strings[j])) {
+                    axis_use = static_cast<GdkAxisUse>(j);
+                    break;
+                }
+            gdk_device_set_axis_use(device, i, axis_use);
+            pos0 = pos1 + 1;
+        }
+
+        guint keyval;
+        GdkModifierType modifier;
+
+        Glib::ustring const keys_str = prefs->getString(device_path + "/keys");
+        pos0 = pos1 = 0;
+        for (gint i=0; i < device->num_keys; i++) {
+            pos1 = keys_str.find(';', pos0);
+            if (pos1 == Glib::ustring::npos)
+                break;  // Too few key specifications
+
+            gtk_accelerator_parse(keys_str.substr(pos0, pos1-pos0).c_str(), &keyval, &modifier);
+            gdk_device_set_key(device, i, keyval, modifier);
+            pos0 = pos1 + 1;
         }
     }
 }
@@ -140,8 +153,7 @@ sp_input_save_to_preferences (void)
     for (GList *list_ptr = gdk_devices_list(); list_ptr != NULL; list_ptr = list_ptr->next) {
         GdkDevice *device = static_cast<GdkDevice *>(list_ptr->data);
 
-        //repr = sp_repr_lookup_child(devices, "id", device->name);
-        Glib::ustring device_path = Glib::ustring("/devices/") + device->name;
+        Glib::ustring device_path = sanitized_device_path(device->name);
 
         switch (device->mode) {
             default: