[Spice-devel] [PATCH spice-gtk 2/5] UsbDeviceManager: Add a redirect-on-connect property

Hans de Goede hdegoede at redhat.com
Fri Sep 21 08:32:22 PDT 2012


Signed-off-by: Hans de Goede <hdegoede at redhat.com>
---
 gtk/spice-option.c       |  9 +++++++++
 gtk/usb-device-manager.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/gtk/spice-option.c b/gtk/spice-option.c
index 1333766..85c9e55 100644
--- a/gtk/spice-option.c
+++ b/gtk/spice-option.c
@@ -33,6 +33,7 @@ static char *host_subject = NULL;
 static char *smartcard_db = NULL;
 static char *smartcard_certificates = NULL;
 static char *usbredir_auto_redirect_filter = NULL;
+static char *usbredir_redirect_on_connect = NULL;
 static gboolean smartcard = FALSE;
 static gboolean disable_audio = FALSE;
 static gboolean disable_usbredir = FALSE;
@@ -133,6 +134,8 @@ GOptionGroup* spice_get_option_group(void)
           N_("Disable USB redirection support"), NULL },
         { "spice-usbredir-auto-redirect-filter", '\0', 0, G_OPTION_ARG_STRING, &usbredir_auto_redirect_filter,
           N_("Filter selecting USB devices to be auto-redirected when plugged in"), N_("<filter-string>") },
+        { "spice-usbredir-redirect-on-connect", '\0', 0, G_OPTION_ARG_STRING, &usbredir_redirect_on_connect,
+          N_("Filter selecting USB devices to redirect on connect"), N_("<filter-string>") },
         { "spice-cache-size", '\0', 0, G_OPTION_ARG_INT, &cache_size,
           N_("Image cache size"), N_("<bytes>") },
         { "spice-glz-window-size", '\0', 0, G_OPTION_ARG_INT, &glz_window_size,
@@ -201,6 +204,12 @@ void spice_set_session_option(SpiceSession *session)
             g_object_set(m, "auto-connect-filter",
                          usbredir_auto_redirect_filter, NULL);
     }
+    if (usbredir_redirect_on_connect) {
+        SpiceUsbDeviceManager *m = spice_usb_device_manager_get(session, NULL);
+        if (m)
+            g_object_set(m, "redirect-on-connect",
+                         usbredir_redirect_on_connect, NULL);
+    }
     if (disable_usbredir)
         g_object_set(session, "enable-usbredir", FALSE, NULL);
     if (disable_audio)
diff --git a/gtk/usb-device-manager.c b/gtk/usb-device-manager.c
index 6358da8..5ed76a1 100644
--- a/gtk/usb-device-manager.c
+++ b/gtk/usb-device-manager.c
@@ -81,6 +81,7 @@ enum {
     PROP_SESSION,
     PROP_AUTO_CONNECT,
     PROP_AUTO_CONNECT_FILTER,
+    PROP_REDIRECT_ON_CONNECT,
 };
 
 enum
@@ -96,6 +97,7 @@ struct _SpiceUsbDeviceManagerPrivate {
     SpiceSession *session;
     gboolean auto_connect;
     gchar *auto_connect_filter;
+    gchar *redirect_on_connect;
 #ifdef USE_USBREDIR
     libusb_context *context;
     GUdevClient *udev;
@@ -104,7 +106,9 @@ struct _SpiceUsbDeviceManagerPrivate {
     gboolean event_thread_run;
     libusb_device **coldplug_list; /* Avoid needless reprobing during init */
     struct usbredirfilter_rule *auto_conn_filter_rules;
+    struct usbredirfilter_rule *redirect_on_connect_rules;
     int auto_conn_filter_rules_count;
+    int redirect_on_connect_rules_count;
 #endif
     GPtrArray *devices;
     GPtrArray *channels;
@@ -331,6 +335,9 @@ static void spice_usb_device_manager_get_property(GObject     *gobject,
     case PROP_AUTO_CONNECT_FILTER:
         g_value_set_string(value, priv->auto_connect_filter);
         break;
+    case PROP_REDIRECT_ON_CONNECT:
+        g_value_set_string(value, priv->redirect_on_connect);
+        break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
         break;
@@ -374,6 +381,30 @@ static void spice_usb_device_manager_set_property(GObject       *gobject,
         priv->auto_connect_filter = g_strdup(filter);
         break;
     }
+    case PROP_REDIRECT_ON_CONNECT: {
+        const gchar *filter = g_value_get_string(value);
+#ifdef USE_USBREDIR
+        struct usbredirfilter_rule *rules = NULL;
+        int r = 0, count = 0;
+
+        if (filter)
+            r = usbredirfilter_string_to_rules(filter, ",", "|",
+                                               &rules, &count);
+        if (r) {
+            if (r == -ENOMEM)
+                g_error("Failed to allocate memory for redirect-on-connect");
+            g_warning("Error parsing redirect-on-connect string, keeping old filter\n");
+            break;
+        }
+
+        free(priv->redirect_on_connect_rules);
+        priv->redirect_on_connect_rules = rules;
+        priv->redirect_on_connect_rules_count = count;
+#endif
+        g_free(priv->redirect_on_connect);
+        priv->redirect_on_connect = g_strdup(filter);
+        break;
+    }
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
         break;
@@ -449,6 +480,21 @@ static void spice_usb_device_manager_class_init(SpiceUsbDeviceManagerClass *klas
                                     pspec);
 
     /**
+     * SpiceUsbDeviceManager:redirect-on-connect:
+     *
+     * Set a string specifying a filter selecting USB devices to automatically
+     * redirect after a Spice connection has been established.
+     *
+     * See SpiceUsbDeviceManager:auto-connect-filter: for the filter string
+     * format.
+     */
+    pspec = g_param_spec_string("redirect-on-connect", "Redirect on connect",
+               "Filter selecting USB devices to redirect on connect", NULL,
+               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+    g_object_class_install_property(gobject_class, PROP_REDIRECT_ON_CONNECT,
+                                    pspec);
+
+    /**
      * SpiceUsbDeviceManager::device-added:
      * @manager: the #SpiceUsbDeviceManager that emitted the signal
      * @device: #SpiceUsbDevice boxed object corresponding to the added device
-- 
1.7.12



More information about the Spice-devel mailing list