[virt-tools-list] [PATCHv3 virt-viewer 01/12] Try to share more GOption code between r-v and v-v

Marc-André Lureau marcandre.lureau at gmail.com
Tue Aug 20 12:19:51 UTC 2013


---
 src/remote-viewer-main.c | 54 +++---------------------------------------
 src/remote-viewer.c      |  6 ++---
 src/remote-viewer.h      |  6 ++---
 src/virt-viewer-app.c    | 61 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/virt-viewer-app.h    |  1 +
 src/virt-viewer-main.c   | 29 ++---------------------
 src/virt-viewer.c        |  6 +----
 src/virt-viewer.h        |  4 +---
 8 files changed, 73 insertions(+), 94 deletions(-)

diff --git a/src/remote-viewer-main.c b/src/remote-viewer-main.c
index 19e5197..a773288 100644
--- a/src/remote-viewer-main.c
+++ b/src/remote-viewer-main.c
@@ -48,28 +48,6 @@ remote_viewer_version(void)
     exit(EXIT_SUCCESS);
 }
 
-gboolean fullscreen = FALSE;
-gboolean fullscreen_auto_conf = FALSE;
-
-static gboolean
-option_fullscreen(G_GNUC_UNUSED const gchar *option_name,
-                  const gchar *value,
-                  G_GNUC_UNUSED gpointer data, GError **error)
-{
-    fullscreen = TRUE;
-
-    if (value == NULL)
-        return TRUE;
-
-    if (g_str_equal(value, "auto-conf")) {
-        fullscreen_auto_conf = TRUE;
-        return TRUE;
-    }
-
-    g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, _("Invalid full-screen argument: %s"), value);
-    return FALSE;
-}
-
 static void
 recent_add(gchar *uri, const gchar *mime_type)
 {
@@ -105,13 +83,9 @@ main(int argc, char **argv)
     GOptionContext *context;
     GError *error = NULL;
     int ret = 1;
-    int zoom = 100;
     gchar **args = NULL;
     gchar *uri = NULL;
     char *title = NULL;
-    char *hotkeys = NULL;
-    gboolean verbose = FALSE;
-    gboolean debug = FALSE;
     RemoteViewer *viewer = NULL;
 #ifdef HAVE_SPICE_GTK
     gboolean controller = FALSE;
@@ -120,22 +94,12 @@ main(int argc, char **argv)
     const GOptionEntry options [] = {
         { "version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
           remote_viewer_version, N_("Display version information"), NULL },
-        { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
-          N_("Display verbose information"), NULL },
         { "title", 't', 0, G_OPTION_ARG_STRING, &title,
           N_("Set window title"), NULL },
-        { "zoom", 'z', 0, G_OPTION_ARG_INT, &zoom,
-          N_("Zoom level of window, in percentage"), "ZOOM" },
-        { "debug", '\0', 0, G_OPTION_ARG_NONE, &debug,
-          N_("Display debugging information"), NULL },
-        { "full-screen", 'f', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, option_fullscreen,
-          N_("Open in full screen mode (auto-conf adjusts guest resolution to fit the client's)."), N_("<auto-conf>") },
 #ifdef HAVE_SPICE_GTK
         { "spice-controller", '\0', 0, G_OPTION_ARG_NONE, &controller,
           N_("Open connection using Spice controller communication"), NULL },
 #endif
-        { "hotkeys", 'H', 0, G_OPTION_ARG_STRING, &hotkeys,
-          N_("Customise hotkeys"), NULL },
         { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &args,
           NULL, "-- URI" },
         { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
@@ -145,6 +109,7 @@ main(int argc, char **argv)
 
     /* Setup command line options */
     context = g_option_context_new (_("- Remote viewer client"));
+    g_option_context_add_main_entries (context, virt_viewer_app_get_options(), NULL);
     g_option_context_add_main_entries (context, options, NULL);
     g_option_context_add_group (context, gtk_get_option_group (TRUE));
 #ifdef HAVE_GTK_VNC
@@ -183,22 +148,13 @@ main(int argc, char **argv)
         }
     }
 
-    if (zoom < 10 || zoom > 200) {
-        g_printerr(_("Zoom level must be within 10-200\n"));
-        goto cleanup;
-    }
-
-    gtk_window_set_default_icon_name("virt-viewer");
-
-    virt_viewer_app_set_debug(debug);
-
 #ifdef HAVE_SPICE_GTK
     if (controller) {
-        viewer = remote_viewer_new_with_controller(verbose);
+        viewer = remote_viewer_new_with_controller();
         g_object_set(viewer, "guest-name", "defined by Spice controller", NULL);
     } else {
 #endif
-        viewer = remote_viewer_new(uri, title, verbose);
+        viewer = remote_viewer_new(uri, title);
         g_object_set(viewer, "guest-name", uri, NULL);
 #ifdef HAVE_SPICE_GTK
     }
@@ -207,10 +163,6 @@ main(int argc, char **argv)
         goto cleanup;
 
     app = VIRT_VIEWER_APP(viewer);
-    g_object_set(app, "fullscreen-auto-conf", fullscreen_auto_conf, NULL);
-    g_object_set(app, "fullscreen", fullscreen, NULL);
-    virt_viewer_window_set_zoom_level(virt_viewer_app_get_main_window(app), zoom);
-    virt_viewer_app_set_hotkeys(app, hotkeys);
 
     if (!virt_viewer_app_start(app))
         goto cleanup;
diff --git a/src/remote-viewer.c b/src/remote-viewer.c
index b656175..3f2801f 100644
--- a/src/remote-viewer.c
+++ b/src/remote-viewer.c
@@ -220,11 +220,10 @@ remote_viewer_init(RemoteViewer *self)
 }
 
 RemoteViewer *
-remote_viewer_new(const gchar *uri, const gchar *title, gboolean verbose)
+remote_viewer_new(const gchar *uri, const gchar *title)
 {
     return g_object_new(REMOTE_VIEWER_TYPE,
                         "guri", uri,
-                        "verbose", verbose,
                         "title", title,
                         "open-recent-dialog", uri == NULL,
                         NULL);
@@ -250,7 +249,7 @@ foreign_menu_title_changed(SpiceCtrlForeignMenu *menu G_GNUC_UNUSED,
 }
 
 RemoteViewer *
-remote_viewer_new_with_controller(gboolean verbose)
+remote_viewer_new_with_controller(void)
 {
     RemoteViewer *self;
     SpiceCtrlController *ctrl = spice_ctrl_controller_new();
@@ -259,7 +258,6 @@ remote_viewer_new_with_controller(gboolean verbose)
     self =  g_object_new(REMOTE_VIEWER_TYPE,
                          "controller", ctrl,
                          "foreign-menu", menu,
-                         "verbose", verbose,
                          NULL);
     g_signal_connect(menu, "notify::title",
                      G_CALLBACK(foreign_menu_title_changed),
diff --git a/src/remote-viewer.h b/src/remote-viewer.h
index de701e0..6035f94 100644
--- a/src/remote-viewer.h
+++ b/src/remote-viewer.h
@@ -48,10 +48,8 @@ typedef struct {
 
 GType remote_viewer_get_type (void);
 
-RemoteViewer* remote_viewer_new(const gchar *uri,
-                                const gchar *title,
-                                gboolean verbose);
-RemoteViewer* remote_viewer_new_with_controller(gboolean verbose);
+RemoteViewer* remote_viewer_new(const gchar *uri, const gchar *title);
+RemoteViewer* remote_viewer_new_with_controller(void);
 
 G_END_DECLS
 
diff --git a/src/virt-viewer-app.c b/src/virt-viewer-app.c
index 6d12584..637896e 100644
--- a/src/virt-viewer-app.c
+++ b/src/virt-viewer-app.c
@@ -1402,11 +1402,21 @@ gboolean virt_viewer_app_start(VirtViewerApp *self)
     return self->priv->started;
 }
 
+static int opt_zoom = 100;
+static gchar *opt_hotkeys = NULL;
+static gboolean opt_verbose = FALSE;
+static gboolean opt_debug = FALSE;
+static gboolean opt_fullscreen = FALSE;
+static gboolean opt_fullscreen_auto_conf = FALSE;
+
 static void
 virt_viewer_app_init (VirtViewerApp *self)
 {
     GError *error = NULL;
 
+    gtk_window_set_default_icon_name("virt-viewer");
+    virt_viewer_app_set_debug(opt_debug);
+
     self->priv = GET_PRIVATE(self);
     self->priv->windows = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_object_unref);
     self->priv->config = g_key_file_new();
@@ -1419,6 +1429,14 @@ virt_viewer_app_init (VirtViewerApp *self)
         g_debug("Couldn't load configuration: %s", error->message);
 
     g_clear_error(&error);
+
+    if (opt_zoom < 10 || opt_zoom > 200) {
+        g_printerr(_("Zoom level must be within 10-200\n"));
+        opt_zoom = 100;
+    }
+
+    self->priv->verbose = opt_verbose;
+    self->priv->fullscreen_auto_conf = opt_fullscreen_auto_conf;
 }
 
 static void
@@ -1495,6 +1513,10 @@ virt_viewer_app_constructor (GType gtype,
     gtk_accel_map_add_entry("<virt-viewer>/view/release-cursor", GDK_F12, GDK_SHIFT_MASK);
     gtk_accel_map_add_entry("<virt-viewer>/view/zoom-reset", GDK_0, GDK_CONTROL_MASK);
 
+    virt_viewer_window_set_zoom_level(priv->main_window, opt_zoom);
+    virt_viewer_app_set_fullscreen(self, opt_fullscreen);
+    virt_viewer_app_set_hotkeys(self, opt_hotkeys);
+
     return obj;
 }
 
@@ -2049,6 +2071,45 @@ virt_viewer_app_get_windows(VirtViewerApp *self)
     return self->priv->windows;
 }
 
+static gboolean
+option_fullscreen(G_GNUC_UNUSED const gchar *option_name,
+                  const gchar *value,
+                  G_GNUC_UNUSED gpointer data, GError **error)
+{
+    opt_fullscreen = TRUE;
+
+    if (value == NULL)
+        return TRUE;
+
+    if (g_str_equal(value, "auto-conf")) {
+        opt_fullscreen_auto_conf = TRUE;
+        return TRUE;
+    }
+
+    g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, _("Invalid full-screen argument: %s"), value);
+    return FALSE;
+}
+
+const GOptionEntry *
+virt_viewer_app_get_options(void)
+{
+    static const GOptionEntry options [] = {
+        { "zoom", 'z', 0, G_OPTION_ARG_INT, &opt_zoom,
+          N_("Zoom level of window, in percentage"), "ZOOM" },
+        { "full-screen", 'f', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, option_fullscreen,
+          N_("Open in full screen mode (auto-conf adjusts guest resolution to fit the client's)"), N_("<auto-conf>") },
+        { "hotkeys", 'H', 0, G_OPTION_ARG_STRING, &opt_hotkeys,
+          N_("Customise hotkeys"), NULL },
+        { "verbose", 'v', 0, G_OPTION_ARG_NONE, &opt_verbose,
+          N_("Display verbose information"), NULL },
+        { "debug", '\0', 0, G_OPTION_ARG_NONE, &opt_debug,
+          N_("Display debugging information"), NULL },
+        { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
+    };
+
+    return options;
+}
+
 /*
  * Local variables:
  *  c-indent-level: 4
diff --git a/src/virt-viewer-app.h b/src/virt-viewer-app.h
index 93bb988..4721fe3 100644
--- a/src/virt-viewer-app.h
+++ b/src/virt-viewer-app.h
@@ -98,6 +98,7 @@ gboolean virt_viewer_app_get_enable_accel(VirtViewerApp *self);
 VirtViewerSession* virt_viewer_app_get_session(VirtViewerApp *self);
 gboolean virt_viewer_app_get_fullscreen(VirtViewerApp *app);
 gboolean virt_viewer_app_get_fullscreen_auto_conf(VirtViewerApp *app);
+const GOptionEntry* virt_viewer_app_get_options(void);
 
 G_END_DECLS
 
diff --git a/src/virt-viewer-main.c b/src/virt-viewer-main.c
index 96e2e00..1f3f112 100644
--- a/src/virt-viewer-main.c
+++ b/src/virt-viewer-main.c
@@ -47,24 +47,17 @@ int main(int argc, char **argv)
     GError *error = NULL;
     int ret = 1;
     char *uri = NULL;
-    int zoom = 100;
     gchar **args = NULL;
-    gchar *hotkeys = NULL;
-    gboolean verbose = FALSE;
-    gboolean debug = FALSE;
     gboolean direct = FALSE;
     gboolean attach = FALSE;
     gboolean waitvm = FALSE;
     gboolean reconnect = FALSE;
-    gboolean fullscreen = FALSE;
     VirtViewer *viewer = NULL;
     char *base_name;
     char *help_msg = NULL;
     const GOptionEntry options [] = {
         { "version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
           virt_viewer_version, N_("Display version information"), NULL },
-        { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
-          N_("Display verbose information"), NULL },
         { "direct", 'd', 0, G_OPTION_ARG_NONE, &direct,
           N_("Direct connection with no automatic tunnels"), NULL },
         { "attach", 'a', 0, G_OPTION_ARG_NONE, &attach,
@@ -75,14 +68,6 @@ int main(int argc, char **argv)
           N_("Wait for domain to start"), NULL },
         { "reconnect", 'r', 0, G_OPTION_ARG_NONE, &reconnect,
           N_("Reconnect to domain upon restart"), NULL },
-        { "zoom", 'z', 0, G_OPTION_ARG_INT, &zoom,
-          N_("Zoom level of window, in percentage"), "ZOOM" },
-        { "debug", '\0', 0, G_OPTION_ARG_NONE, &debug,
-          N_("Display debugging information"), NULL },
-        { "full-screen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen,
-          N_("Open in full screen mode"), NULL },
-        { "hotkeys", 'H', 0, G_OPTION_ARG_STRING, &hotkeys,
-          N_("Customise hotkeys"), NULL },
         { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &args,
           NULL, "-- DOMAIN-NAME|ID|UUID" },
         { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
@@ -98,6 +83,7 @@ int main(int argc, char **argv)
     /* Setup command line options */
     context = g_option_context_new (_("- Virtual machine graphical console"));
     g_option_context_add_main_entries (context, options, NULL);
+    g_option_context_add_main_entries (context, virt_viewer_app_get_options(), NULL);
     g_option_context_add_group (context, gtk_get_option_group (TRUE));
 #ifdef HAVE_GTK_VNC
     g_option_context_add_group (context, vnc_display_get_option_group ());
@@ -120,21 +106,10 @@ int main(int argc, char **argv)
         goto cleanup;
     }
 
-    if (zoom < 10 || zoom > 200) {
-        g_printerr(_("Zoom level must be within 10-200\n"));
-        goto cleanup;
-    }
-
-    gtk_window_set_default_icon_name("virt-viewer");
-
-    virt_viewer_app_set_debug(debug);
-
-    viewer = virt_viewer_new(uri, args[0], zoom, direct, attach, waitvm, reconnect, verbose);
+    viewer = virt_viewer_new(uri, args[0], direct, attach, waitvm, reconnect);
     if (viewer == NULL)
         goto cleanup;
 
-    g_object_set(viewer, "fullscreen", fullscreen, NULL);
-    virt_viewer_app_set_hotkeys(VIRT_VIEWER_APP(viewer), hotkeys);
     if (!virt_viewer_app_start(VIRT_VIEWER_APP(viewer)))
         goto cleanup;
 
diff --git a/src/virt-viewer.c b/src/virt-viewer.c
index b98c252..ae25fc6 100644
--- a/src/virt-viewer.c
+++ b/src/virt-viewer.c
@@ -735,19 +735,16 @@ virt_viewer_start(VirtViewerApp *app)
 VirtViewer *
 virt_viewer_new(const char *uri,
                 const char *name,
-                gint zoom,
                 gboolean direct,
                 gboolean attach,
                 gboolean waitvm,
-                gboolean reconnect,
-                gboolean verbose)
+                gboolean reconnect)
 {
     VirtViewer *self;
     VirtViewerApp *app;
     VirtViewerPrivate *priv;
 
     self = g_object_new(VIRT_VIEWER_TYPE,
-                        "verbose", verbose,
                         "guest-name", name,
                         NULL);
     app = VIRT_VIEWER_APP(self);
@@ -757,7 +754,6 @@ virt_viewer_new(const char *uri,
      * UUID, or NAME string. To be replaced with the real guest name later
      */
     g_object_set(app, "title", name, NULL);
-    virt_viewer_window_set_zoom_level(virt_viewer_app_get_main_window(app), zoom);
     virt_viewer_app_set_direct(app, direct);
     virt_viewer_app_set_attach(app, attach);
 
diff --git a/src/virt-viewer.h b/src/virt-viewer.h
index 73d9170..c962615 100644
--- a/src/virt-viewer.h
+++ b/src/virt-viewer.h
@@ -51,12 +51,10 @@ GType virt_viewer_get_type (void);
 VirtViewer *
 virt_viewer_new(const char *uri,
                 const char *name,
-                gint zoom,
                 gboolean direct,
                 gboolean attach,
                 gboolean waitvm,
-                gboolean reconnect,
-                gboolean verbose);
+                gboolean reconnect);
 
 G_END_DECLS
 
-- 
1.8.3.rc1.49.g8d97506




More information about the virt-tools-list mailing list