[virt-tools-list] [PATCH virt-viewer 3/3] remote-viewer: support spice foreign menu

Marc-André Lureau marcandre.lureau at gmail.com
Wed Feb 29 16:03:09 UTC 2012


From: Marc-Andre Lureau <marcandre.lureau at redhat.com>

---
 configure.ac        |    2 +-
 src/remote-viewer.c |  211 ++++++++++++++++++++++++++++++++++++++------------
 2 files changed, 161 insertions(+), 52 deletions(-)

diff --git a/configure.ac b/configure.ac
index 6cf01c3..ac81689 100644
--- a/configure.ac
+++ b/configure.ac
@@ -17,7 +17,7 @@ GTK2_REQUIRED="2.18.0"
 GTK3_REQUIRED="3.0"
 GTK_VNC1_REQUIRED="0.3.8"
 GTK_VNC2_REQUIRED="0.4.0"
-SPICE_GTK_REQUIRED="0.9.11"
+SPICE_GTK_REQUIRED="0.10.6"
 
 AC_PROG_CC
 AM_PROG_CC_C_O
diff --git a/src/remote-viewer.c b/src/remote-viewer.c
index 1aa28b0..12bbed2 100644
--- a/src/remote-viewer.c
+++ b/src/remote-viewer.c
@@ -40,8 +40,10 @@
 struct _RemoteViewerPrivate {
 #ifdef HAVE_SPICE_GTK
     SpiceCtrlController *controller;
+    SpiceCtrlForeignMenu *ctrl_foreign_menu;
 #endif
     GtkWidget *controller_menu;
+    GtkWidget *foreign_menu;
 };
 
 G_DEFINE_TYPE (RemoteViewer, remote_viewer, VIRT_VIEWER_TYPE_APP)
@@ -52,14 +54,15 @@ G_DEFINE_TYPE (RemoteViewer, remote_viewer, VIRT_VIEWER_TYPE_APP)
 enum {
     PROP_0,
     PROP_CONTROLLER,
+    PROP_CTRL_FOREIGN_MENU,
 };
 #endif
 
 static gboolean remote_viewer_start(VirtViewerApp *self);
 #if HAVE_SPICE_GTK
 static int remote_viewer_activate(VirtViewerApp *self);
-#endif
 static void remote_viewer_window_added(VirtViewerApp *self, VirtViewerWindow *win);
+#endif
 
 #if HAVE_SPICE_GTK
 static void
@@ -73,6 +76,9 @@ remote_viewer_get_property (GObject *object, guint property_id,
     case PROP_CONTROLLER:
         g_value_set_object(value, priv->controller);
         break;
+    case PROP_CTRL_FOREIGN_MENU:
+        g_value_set_object(value, priv->ctrl_foreign_menu);
+        break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     }
@@ -90,6 +96,10 @@ remote_viewer_set_property (GObject *object, guint property_id,
         g_return_if_fail(priv->controller == NULL);
         priv->controller = g_value_dup_object(value);
         break;
+    case PROP_CTRL_FOREIGN_MENU:
+        g_return_if_fail(priv->ctrl_foreign_menu == NULL);
+        priv->ctrl_foreign_menu = g_value_dup_object(value);
+        break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     }
@@ -106,11 +116,15 @@ remote_viewer_dispose (GObject *object)
         priv->controller = NULL;
     }
 
+    if (priv->ctrl_foreign_menu) {
+        g_object_unref(priv->ctrl_foreign_menu);
+        priv->ctrl_foreign_menu = NULL;
+    }
+
     G_OBJECT_CLASS(remote_viewer_parent_class)->dispose (object);
 }
 #endif
 
-
 static void
 remote_viewer_class_init (RemoteViewerClass *klass)
 {
@@ -130,8 +144,8 @@ remote_viewer_class_init (RemoteViewerClass *klass)
     app_class->start = remote_viewer_start;
 #if HAVE_SPICE_GTK
     app_class->activate = remote_viewer_activate;
-#endif
     app_class->window_added = remote_viewer_window_added;
+#endif
 
 #if HAVE_SPICE_GTK
     g_object_class_install_property(object_class,
@@ -143,6 +157,15 @@ remote_viewer_class_init (RemoteViewerClass *klass)
                                                         G_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT_ONLY |
                                                         G_PARAM_STATIC_STRINGS));
+    g_object_class_install_property(object_class,
+                                    PROP_CTRL_FOREIGN_MENU,
+                                    g_param_spec_object("foreign-menu",
+                                                        "Foreign Menu",
+                                                        "Spice foreign menu",
+                                                        SPICE_CTRL_TYPE_FOREIGN_MENU,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_CONSTRUCT_ONLY |
+                                                        G_PARAM_STATIC_STRINGS));
 #endif
 }
 
@@ -167,9 +190,11 @@ remote_viewer_new_with_controller(gboolean verbose)
 {
     RemoteViewer *self;
     SpiceCtrlController *ctrl = spice_ctrl_controller_new();
+    SpiceCtrlForeignMenu *menu = spice_ctrl_foreign_menu_new();
 
     self =  g_object_new(REMOTE_VIEWER_TYPE,
                          "controller", ctrl,
+                         "foreign-menu", menu,
                          "verbose", verbose,
                          NULL);
     g_object_unref(ctrl);
@@ -199,7 +224,7 @@ spice_ctrl_hide(SpiceCtrlController *ctrl G_GNUC_UNUSED, RemoteViewer *self)
 }
 
 static void
-spice_menuitem_activate_cb(GtkMenuItem *mi, RemoteViewer *self)
+spice_menuitem_activate_cb(GtkMenuItem *mi, GObject *ctrl)
 {
     SpiceCtrlMenuItem *menuitem = g_object_get_data(G_OBJECT(mi), "spice-menuitem");
 
@@ -207,11 +232,14 @@ spice_menuitem_activate_cb(GtkMenuItem *mi, RemoteViewer *self)
     if (gtk_menu_item_get_submenu(mi))
         return;
 
-    spice_ctrl_controller_menu_item_click_msg(self->priv->controller, menuitem->id);
+    if (SPICE_CTRL_IS_CONTROLLER(ctrl))
+        spice_ctrl_controller_menu_item_click_msg(SPICE_CTRL_CONTROLLER(ctrl), menuitem->id);
+    else if (SPICE_CTRL_IS_FOREIGN_MENU(ctrl))
+        spice_ctrl_foreign_menu_menu_item_click_msg(SPICE_CTRL_FOREIGN_MENU(ctrl), menuitem->id);
 }
 
 static GtkWidget *
-ctrlmenu_to_gtkmenu (RemoteViewer *self, SpiceCtrlMenu *ctrlmenu)
+ctrlmenu_to_gtkmenu (RemoteViewer *self, SpiceCtrlMenu *ctrlmenu, GObject *ctrl)
 {
     GList *l;
     GtkWidget *menu = gtk_menu_new();
@@ -230,21 +258,27 @@ ctrlmenu_to_gtkmenu (RemoteViewer *self, SpiceCtrlMenu *ctrlmenu)
             if (*s == '&')
                 *s = '_';
 
-        if (g_str_equal(menuitem->text, "-")){
+        if (g_str_equal(menuitem->text, "-")) {
             item = gtk_separator_menu_item_new();
+        } else if (menuitem->flags & CONTROLLER_MENU_FLAGS_CHECKED) {
+            item = gtk_check_menu_item_new_with_mnemonic(menuitem->text);
+            g_object_set(item, "active", TRUE, NULL);
         } else {
             item = gtk_menu_item_new_with_mnemonic(menuitem->text);
         }
 
+        if (menuitem->flags & (CONTROLLER_MENU_FLAGS_GRAYED | CONTROLLER_MENU_FLAGS_DISABLED))
+            gtk_widget_set_sensitive(item, FALSE);
+
         g_object_set_data_full(G_OBJECT(item), "spice-menuitem",
                                g_object_ref(menuitem), g_object_unref);
-        g_signal_connect(item, "activate", G_CALLBACK(spice_menuitem_activate_cb), self);
+        g_signal_connect(item, "activate", G_CALLBACK(spice_menuitem_activate_cb), ctrl);
         gtk_menu_attach(GTK_MENU (menu), item, 0, 1, n, n + 1);
         n += 1;
 
         if (menuitem->submenu) {
             gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
-                                      ctrlmenu_to_gtkmenu(self, menuitem->submenu));
+                                      ctrlmenu_to_gtkmenu(self, menuitem->submenu, ctrl));
         }
     }
 
@@ -259,62 +293,100 @@ ctrlmenu_to_gtkmenu (RemoteViewer *self, SpiceCtrlMenu *ctrlmenu)
 }
 
 static void
-spice_menu_set_visible(gpointer key G_GNUC_UNUSED,
+spice_menu_update(RemoteViewer *self, VirtViewerWindow *win)
+{
+    GtkWidget *menuitem = g_object_get_data(G_OBJECT(win), "spice-menu");
+    SpiceCtrlMenu *menu;
+
+    if (self->priv->controller == NULL)
+        return;
+
+    if (menuitem != NULL)
+        gtk_widget_destroy(menuitem);
+
+    {
+        GtkMenuShell *shell = GTK_MENU_SHELL(gtk_builder_get_object(virt_viewer_window_get_builder(win), "top-menu"));
+        menuitem = gtk_menu_item_new_with_label("Spice");
+        gtk_menu_shell_append(shell, menuitem);
+        g_object_set_data(G_OBJECT(win), "spice-menu", menuitem);
+    }
+
+    g_object_get(self->priv->controller, "menu", &menu, NULL);
+    if (menu == NULL || g_list_length(menu->items) == 0) {
+        gtk_widget_set_visible(menuitem, FALSE);
+    } else {
+        gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
+            ctrlmenu_to_gtkmenu(self, menu, G_OBJECT(self->priv->controller)));
+        gtk_widget_set_visible(menuitem, TRUE);
+    }
+    g_object_unref(menu);
+}
+
+static void
+spice_menu_update_each(gpointer key G_GNUC_UNUSED,
                        gpointer value,
                        gpointer user_data)
 {
-    gboolean visible = GPOINTER_TO_INT(user_data);
-    GtkWidget *menu = g_object_get_data(value, "spice-menu");
-
-    gtk_widget_set_visible(menu, visible);
+    spice_menu_update(REMOTE_VIEWER(user_data), VIRT_VIEWER_WINDOW(value));
 }
 
 static void
-remote_viewer_window_spice_menu_set_visible(RemoteViewer *self,
-                                            gboolean visible)
+spice_ctrl_menu_updated(RemoteViewer *self)
 {
     GHashTable *windows = virt_viewer_app_get_windows(VIRT_VIEWER_APP(self));
 
-    g_hash_table_foreach(windows, spice_menu_set_visible, GINT_TO_POINTER(visible));
+    DEBUG_LOG("Spice controller menu updated");
+
+    g_hash_table_foreach(windows, spice_menu_update_each, self);
 }
 
 static void
-spice_menu_update(gpointer key G_GNUC_UNUSED,
-                  gpointer value,
-                  gpointer user_data)
+foreign_menu_update(RemoteViewer *self, VirtViewerWindow *win)
 {
-    RemoteViewer *self = REMOTE_VIEWER(user_data);
-    GtkWidget *menuitem = g_object_get_data(value, "spice-menu");
+    GtkWidget *menuitem = g_object_get_data(G_OBJECT(win), "foreign-menu");
     SpiceCtrlMenu *menu;
 
-    g_object_get(self->priv->controller, "menu", &menu, NULL);
-    gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), ctrlmenu_to_gtkmenu(self, menu));
+    if (self->priv->ctrl_foreign_menu == NULL)
+        return;
+
+    if (menuitem != NULL)
+        gtk_widget_destroy(menuitem);
+
+    {
+        GtkMenuShell *shell = GTK_MENU_SHELL(gtk_builder_get_object(virt_viewer_window_get_builder(win), "top-menu"));
+        const gchar *title = spice_ctrl_foreign_menu_get_title(self->priv->ctrl_foreign_menu);
+        menuitem = gtk_menu_item_new_with_label(title);
+        gtk_menu_shell_append(shell, menuitem);
+        g_object_set_data(G_OBJECT(win), "foreign-menu", menuitem);
+    }
+
+    g_object_get(self->priv->ctrl_foreign_menu, "menu", &menu, NULL);
+    if (menu == NULL || g_list_length(menu->items) == 0) {
+        gtk_widget_set_visible(menuitem, FALSE);
+    } else {
+        gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
+            ctrlmenu_to_gtkmenu(self, menu, G_OBJECT(self->priv->ctrl_foreign_menu)));
+        gtk_widget_set_visible(menuitem, TRUE);
+    }
     g_object_unref(menu);
 }
 
 static void
-spice_ctrl_menu_updated(RemoteViewer *self,
-                        SpiceCtrlMenu *menu)
+foreign_menu_update_each(gpointer key G_GNUC_UNUSED,
+                         gpointer value,
+                         gpointer user_data)
 {
-    GHashTable *windows = virt_viewer_app_get_windows(VIRT_VIEWER_APP(self));
-    RemoteViewerPrivate *priv = self->priv;
-    gboolean visible;
-
-    DEBUG_LOG("Spice controller menu updated");
-
-    if (priv->controller_menu != NULL) {
-        g_object_unref (priv->controller_menu);
-        priv->controller_menu = NULL;
-    }
+    foreign_menu_update(REMOTE_VIEWER(user_data), VIRT_VIEWER_WINDOW(value));
+}
 
-    if (menu && g_list_length(menu->items) > 0) {
-        priv->controller_menu = ctrlmenu_to_gtkmenu(self, menu);
-        g_hash_table_foreach(windows, spice_menu_update, self);
-    }
+static void
+spice_foreign_menu_updated(RemoteViewer *self)
+{
+    GHashTable *windows = virt_viewer_app_get_windows(VIRT_VIEWER_APP(self));
 
-    visible = priv->controller_menu != NULL;
+    DEBUG_LOG("Spice foreign menu updated");
 
-    remote_viewer_window_spice_menu_set_visible(self, visible);
+    g_hash_table_foreach(windows, foreign_menu_update_each, self);
 }
 
 static SpiceSession *
@@ -451,6 +523,24 @@ ctrl_key_to_gtk_accelerator(const gchar *key)
 }
 
 static void
+app_notified(VirtViewerApp *app,
+             GParamSpec *pspec,
+             RemoteViewer *self)
+{
+    GValue value = G_VALUE_INIT;
+
+    g_value_init(&value, pspec->value_type);
+    g_object_get_property(G_OBJECT(app), pspec->name, &value);
+
+    if (g_str_equal(pspec->name, "has-focus")) {
+        if (self->priv->ctrl_foreign_menu)
+            spice_ctrl_foreign_menu_app_activated_msg(self->priv->ctrl_foreign_menu, g_value_get_boolean(&value));
+    }
+
+    g_value_unset(&value);
+}
+
+static void
 spice_ctrl_notified(SpiceCtrlController *ctrl,
                     GParamSpec *pspec,
                     RemoteViewer *self)
@@ -486,7 +576,7 @@ spice_ctrl_notified(SpiceCtrlController *ctrl,
         g_debug("unimplemented resize-guest %d", auto_res);
         /* g_object_set(G_OBJECT(self), "resize-guest", auto_res, NULL); */
     } else if (g_str_equal(pspec->name, "menu")) {
-        spice_ctrl_menu_updated(self, g_value_get_object(&value));
+        spice_ctrl_menu_updated(self);
     } else if (g_str_equal(pspec->name, "hotkeys")) {
         gchar **hotkey, **hotkeys = g_strsplit(g_value_get_string(&value), ",", -1);
         if (!hotkeys || g_strv_length(hotkeys) == 0) {
@@ -536,13 +626,28 @@ end:
 }
 
 static void
+spice_ctrl_foreign_menu_notified(SpiceCtrlForeignMenu *ctrl_foreign_menu G_GNUC_UNUSED,
+                                 GParamSpec *pspec,
+                                 RemoteViewer *self)
+{
+    if (g_str_equal(pspec->name, "menu")) {
+        spice_foreign_menu_updated(self);
+    }
+}
+
+static void
 spice_ctrl_listen_async_cb(GObject *object,
                            GAsyncResult *res,
                            gpointer user_data)
 {
     GError *error = NULL;
 
-    spice_ctrl_controller_listen_finish(SPICE_CTRL_CONTROLLER(object), res, &error);
+    if (SPICE_CTRL_IS_CONTROLLER(object))
+        spice_ctrl_controller_listen_finish(SPICE_CTRL_CONTROLLER(object), res, &error);
+    else if (SPICE_CTRL_IS_FOREIGN_MENU(object))
+        spice_ctrl_foreign_menu_listen_finish(SPICE_CTRL_FOREIGN_MENU(object), res, &error);
+    else
+        g_warn_if_reached();
 
     if (error != NULL) {
         virt_viewer_app_simple_message_dialog(VIRT_VIEWER_APP(user_data),
@@ -571,18 +676,15 @@ remote_viewer_activate(VirtViewerApp *app)
 
     return ret;
 }
-#endif
 
 static void
-remote_viewer_window_added(VirtViewerApp *self G_GNUC_UNUSED,
+remote_viewer_window_added(VirtViewerApp *app G_GNUC_UNUSED,
                            VirtViewerWindow *win)
 {
-    GtkMenuShell *shell = GTK_MENU_SHELL(gtk_builder_get_object(virt_viewer_window_get_builder(win), "top-menu"));
-    GtkWidget *spice = gtk_menu_item_new_with_label("Spice");
-
-    gtk_menu_shell_append(shell, spice);
-    g_object_set_data(G_OBJECT(win), "spice-menu", spice);
+    spice_menu_update(REMOTE_VIEWER(app), win);
+    foreign_menu_update(REMOTE_VIEWER(app), win);
 }
+#endif
 
 static gboolean
 remote_viewer_start(VirtViewerApp *app)
@@ -598,6 +700,9 @@ remote_viewer_start(VirtViewerApp *app)
     gchar *type = NULL;
 
 #if HAVE_SPICE_GTK
+    g_signal_connect(app, "notify", G_CALLBACK(app_notified), self);
+    g_object_notify(G_OBJECT(app), "has-focus");
+
     if (priv->controller) {
         if (virt_viewer_app_create_session(app, "spice") < 0) {
             virt_viewer_app_simple_message_dialog(app, _("Couldn't create a Spice session"));
@@ -610,6 +715,10 @@ remote_viewer_start(VirtViewerApp *app)
         g_signal_connect(priv->controller, "hide", G_CALLBACK(spice_ctrl_hide), self);
 
         spice_ctrl_controller_listen(priv->controller, NULL, spice_ctrl_listen_async_cb, self);
+
+        g_signal_connect(priv->ctrl_foreign_menu, "notify", G_CALLBACK(spice_ctrl_foreign_menu_notified), self);
+        spice_ctrl_foreign_menu_listen(priv->ctrl_foreign_menu, NULL, spice_ctrl_listen_async_cb, self);
+
         virt_viewer_app_show_status(VIRT_VIEWER_APP(self), _("Setting up Spice session..."));
     } else {
 #endif
-- 
1.7.7.6




More information about the virt-tools-list mailing list