[virt-tools-list] [PATCH virt-viewer 5/6] Use a common early init() function

Marc-André Lureau marcandre.lureau at gmail.com
Fri Oct 19 22:57:30 UTC 2012


There is a number of things both virt-viewer and remote-viewer need to
do early during execution. Do it only in one place.
---
 src/remote-viewer-main.c | 22 +---------------------
 src/virt-viewer-main.c   | 12 +-----------
 src/virt-viewer-util.c   | 32 ++++++++++++++++++++++++++++++++
 src/virt-viewer-util.h   |  2 ++
 4 files changed, 36 insertions(+), 32 deletions(-)

diff --git a/src/remote-viewer-main.c b/src/remote-viewer-main.c
index 2a8973d..704c615 100644
--- a/src/remote-viewer-main.c
+++ b/src/remote-viewer-main.c
@@ -229,27 +229,7 @@ main(int argc, char **argv)
         { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
     };
 
-#ifdef G_OS_WIN32
-    if (AttachConsole(ATTACH_PARENT_PROCESS) != 0) {
-        freopen("CONIN$", "r", stdin);
-        freopen("CONOUT$", "w", stdout);
-        freopen("CONOUT$", "w", stderr);
-        dup2(fileno(stdin), STDIN_FILENO);
-        dup2(fileno(stdout), STDOUT_FILENO);
-        dup2(fileno(stderr), STDERR_FILENO);
-    }
-#endif
-
-#if !GLIB_CHECK_VERSION(2,31,0)
-    g_thread_init(NULL);
-#endif
-
-    setlocale(LC_ALL, "");
-    bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
-    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
-    textdomain(GETTEXT_PACKAGE);
-
-    g_set_application_name(_("Remote Viewer"));
+    virt_viewer_util_init(_("Remote Viewer"));
 
     /* Setup command line options */
     context = g_option_context_new (_("- Remote viewer client"));
diff --git a/src/virt-viewer-main.c b/src/virt-viewer-main.c
index bf938c4..8dca48b 100644
--- a/src/virt-viewer-main.c
+++ b/src/virt-viewer-main.c
@@ -85,17 +85,7 @@ int main(int argc, char **argv)
         { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
     };
 
-#if !GLIB_CHECK_VERSION(2,31,0)
-    g_thread_init(NULL);
-#endif
-
-    setlocale(LC_ALL, "");
-    bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
-    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
-    textdomain(GETTEXT_PACKAGE);
-
-    g_set_application_name(_("Virt Viewer"));
-
+    virt_viewer_util_init(_("Virt Viewer"));
 
     basename = g_path_get_basename(argv[0]);
     help_msg = g_strdup_printf(_("Run '%s --help' to see a full list of available command line options"),
diff --git a/src/virt-viewer-util.c b/src/virt-viewer-util.c
index 9a163fa..556638c 100644
--- a/src/virt-viewer-util.c
+++ b/src/virt-viewer-util.c
@@ -23,6 +23,14 @@
 
 #include <config.h>
 
+#include <glib.h>
+#include <glib/gi18n.h>
+
+#ifdef G_OS_WIN32
+#include <windows.h>
+#include <io.h>
+#endif
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -252,6 +260,30 @@ gulong virt_viewer_signal_connect_object(gpointer instance,
     return ctx->handler_id;
 }
 
+void virt_viewer_util_init(const gchar *appname)
+{
+#ifdef G_OS_WIN32
+    if (AttachConsole(ATTACH_PARENT_PROCESS) != 0) {
+        freopen("CONIN$", "r", stdin);
+        freopen("CONOUT$", "w", stdout);
+        freopen("CONOUT$", "w", stderr);
+        dup2(fileno(stdin), STDIN_FILENO);
+        dup2(fileno(stdout), STDOUT_FILENO);
+        dup2(fileno(stderr), STDERR_FILENO);
+    }
+#endif
+
+#if !GLIB_CHECK_VERSION(2,31,0)
+    g_thread_init(NULL);
+#endif
+
+    setlocale(LC_ALL, "");
+    bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
+    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
+    textdomain(GETTEXT_PACKAGE);
+
+    g_set_application_name(appname);
+}
 
 /*
  * Local variables:
diff --git a/src/virt-viewer-util.h b/src/virt-viewer-util.h
index 38c8078..181d67a 100644
--- a/src/virt-viewer-util.h
+++ b/src/virt-viewer-util.h
@@ -32,6 +32,8 @@ extern gboolean doDebug;
 #define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
 
 
+void virt_viewer_util_init(const gchar *appname);
+
 GtkBuilder *virt_viewer_util_load_ui(const char *name);
 int virt_viewer_util_extract_host(const char *uristr,
                                   char **scheme,
-- 
1.7.11.7




More information about the virt-tools-list mailing list