[PATCH 1/2] Virt-viewer: Abstract viewer_start entrypoint

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch adjusts src/main.c so that there is an entrypoint called viewer_start which will be used by the plugin.

Note the header file ("viewer.h") and the function is private and internal to virt-viewer.

Rich.

--
Emerging Technologies, Red Hat - http://et.redhat.com/~rjones/
Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod
Street, Windsor, Berkshire, SL4 1TE, United Kingdom.  Registered in
England and Wales under Company Registration No. 03798903
diff -r fe1efb558b4b src/Makefile.am
--- a/src/Makefile.am	Fri Jan 11 17:43:30 2008 -0500
+++ b/src/Makefile.am	Fri Jan 25 16:05:40 2008 +0000
@@ -1,6 +1,6 @@
 
 bin_PROGRAMS = virt-viewer
 
-virt_viewer_SOURCES = main.c
+virt_viewer_SOURCES = main.c viewer.h
 virt_viewer_LDADD = @GTKVNC_LIBS@ @GTK2_LIBS@ @LIBXML2_LIBS@ @LIBVIRT_LIBS@
 virt_viewer_CFLAGS = @GTKVNC_CFLAGS@ @GTK2_CFLAGS@ @LIBXML2_CFLAGS@ @LIBVIRT_CFLAGS@ @WARN_CFLAGS@
diff -r fe1efb558b4b src/main.c
--- a/src/main.c	Fri Jan 11 17:43:30 2008 -0500
+++ b/src/main.c	Fri Jan 25 16:05:40 2008 +0000
@@ -35,6 +35,8 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 
+#include "viewer.h"
+
 // #define DEBUG 1
 #ifdef DEBUG
 #define DEBUG_LOG(s, ...) fprintf(stderr, (s), ## __VA_ARGS__)
@@ -67,7 +69,7 @@ static const struct keyComboDef keyCombo
 	{ { GDK_Print }, 1, "_PrintScreen"},
 };
 
-static void viewer_set_title(VncDisplay *vnc, GtkWidget *window, gboolean grabbed)
+static void viewer_set_title(VncDisplay *vnc G_GNUC_UNUSED, GtkWidget *window, gboolean grabbed)
 {
 	char title[1024];
 	const char *subtitle;
@@ -406,20 +406,39 @@ static GtkWidget *viewer_build_menu(VncD
 	return menubar;
 }
 
-static GtkWidget *viewer_build_window(VncDisplay *vnc)
+static GtkWidget *viewer_get_toplevel (void *data G_GNUC_UNUSED)
+{
+	GtkWidget *window;
+
+	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+	gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
+	return window;
+}
+
+static GtkWidget *viewer_build_window(VncDisplay *vnc,
+				      GtkWidget *(*get_toplevel)(void *),
+				      void *data,
+				      int with_menubar)
 {
 	GtkWidget *window;
 	GtkWidget *menubar;
 	GtkWidget *layout;
 
-	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-	layout = gtk_vbox_new(FALSE, 3);
-	menubar = viewer_build_menu(vnc);
-
-	gtk_container_add(GTK_CONTAINER(window), layout);
-	gtk_container_add(GTK_CONTAINER(layout), menubar);
-	gtk_container_add(GTK_CONTAINER(layout), GTK_WIDGET(vnc));
-	gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
+	/* In the standalone program, calls viewer_get_toplevel above
+	 * to make a window.  In the browser plugin this calls a function
+	 * in the plugin which returns the GtkPlug that we live inside.
+	 * In both cases they are GTK_CONTAINERs and NOT resizable.
+	 */
+	window = get_toplevel (data);
+
+	if (with_menubar) {
+		layout = gtk_vbox_new(FALSE, 3);
+		menubar = viewer_build_menu(vnc);
+		gtk_container_add(GTK_CONTAINER(window), layout);
+		gtk_container_add(GTK_CONTAINER(layout), menubar);
+		gtk_container_add(GTK_CONTAINER(layout), GTK_WIDGET(vnc));
+	} else
+		gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(vnc));
 
 	gtk_signal_connect(GTK_OBJECT(vnc), "vnc-pointer-grab",
 			   GTK_SIGNAL_FUNC(viewer_grab), window);
@@ -440,29 +459,6 @@ static GtkWidget *viewer_build_window(Vn
 			 GTK_SIGNAL_FUNC(viewer_credential), NULL);
 
 	return window;
-}
-
-
-static void viewer_version(FILE *out)
-{
-	fprintf(out, "%s version %s\n", PACKAGE, VERSION);
-}
-
-static void viewer_help(FILE *out, const char *app)
-{
-	fprintf(out, "\n");
-	fprintf(out, "syntax: %s [OPTIONS] DOMAIN-NAME|ID|UUID\n", app);
-	fprintf(out, "\n");
-	viewer_version(out);
-	fprintf(out, "\n");
-	fprintf(out, "Options:\n\n");
-	fprintf(out, "  -h, --help              display command line help\n");
-	fprintf(out, "  -v, --verbose           display verbose information\n");
-	fprintf(out, "  -V, --version           display version information\n");
-	fprintf(out, "  -d, --direct            direct connection with no automatic tunnels\n");
-	fprintf(out, "  -c URI, --connect URI   connect to hypervisor URI\n");
-	fprintf(out, "  -w, --wait              wait for domain to start\n");
-	fprintf(out, "\n");
 }
 
 static int viewer_parse_uuid(const char *name, unsigned char *uuid)
@@ -683,12 +679,113 @@ static int viewer_open_tunnel_ssh(const 
 	return viewer_open_tunnel(cmd);
 }
 
-
-int main(int argc, char **argv)
+int
+viewer_start (const char *uri, const char *name,
+	      int direct, int waitvnc, int set_verbose,
+	      GtkWidget *(*get_toplevel)(void *), void *data,
+	      int with_menubar)
 {
 	GtkWidget *window;
 	GtkWidget *vnc;
+	virConnectPtr conn = NULL;
+	virDomainPtr dom = NULL;
+	char *host = NULL;
+	char *vncport = NULL;
+	char *transport = NULL;
+	char *user = NULL;
+	const char *tmpname = NULL;
+	int port = 0;
+	int fd = -1;
+
+	verbose = set_verbose;
+
+	conn = virConnectOpenReadOnly(uri);
+	if (!conn) {
+		fprintf(stderr, "unable to connect to libvirt %s\n",
+			uri ? uri : "xen");
+		return 2;
+	}
+
+	do {
+		dom = viewer_lookup_domain(conn, name);
+		if (!dom && !waitvnc) {
+			fprintf(stderr, "unable to lookup domain %s\n", name);
+			return 3;
+		}
+		if (!dom)
+			usleep(500*1000);
+	} while (!dom);
+
+	do {
+		viewer_extract_vnc_graphics(dom, &vncport);
+		if (!vncport && !waitvnc) {
+			fprintf(stderr, "unable to find vnc graphics for %s\n", name);
+			return 4;
+		}
+		if (!vncport)
+			usleep(300*1000);
+	} while (!vncport);
+	tmpname = virDomainGetName(dom);
+	if (tmpname != NULL) {
+		domname = strdup(tmpname);
+	}
+	virDomainFree(dom);
+	virConnectClose(conn);
+
+	if (viewer_extract_host(uri, &host, &transport, &user, &port) < 0) {
+		fprintf(stderr, "unable to determine hostname for URI %s\n", uri);
+		return 5;
+	}
+	DEBUG_LOG("Remote host is %s and transport %s user %s\n", host, transport ? transport : "", user ? user : "");
+
+	if (transport && strcasecmp(transport, "ssh") == 0 && !direct)
+		fd = viewer_open_tunnel_ssh(host, port, user, vncport);
+
+	vnc = vnc_display_new();
+	window = viewer_build_window (VNC_DISPLAY(vnc),
+				      get_toplevel, data, with_menubar);
+	gtk_widget_realize(vnc);
+
+	vnc_display_set_keyboard_grab(VNC_DISPLAY(vnc), TRUE);
+	vnc_display_set_pointer_grab(VNC_DISPLAY(vnc), TRUE);
+
+	if (fd >= 0)
+		vnc_display_open_fd(VNC_DISPLAY(vnc), fd);
+	else
+		vnc_display_open_host(VNC_DISPLAY(vnc), host, vncport);
+
+	return 0;
+}
+
+#ifndef PLUGIN
+/* Standalone program. */
+
+static void viewer_version(FILE *out)
+{
+	fprintf(out, "%s version %s\n", PACKAGE, VERSION);
+}
+
+static void viewer_help(FILE *out, const char *app)
+{
+	fprintf(out, "\n");
+	fprintf(out, "syntax: %s [OPTIONS] DOMAIN-NAME|ID|UUID\n", app);
+	fprintf(out, "\n");
+	viewer_version(out);
+	fprintf(out, "\n");
+	fprintf(out, "Options:\n\n");
+	fprintf(out, "  -h, --help              display command line help\n");
+	fprintf(out, "  -v, --verbose           display verbose information\n");
+	fprintf(out, "  -V, --version           display version information\n");
+	fprintf(out, "  -d, --direct            direct connection with no automatic tunnels\n");
+	fprintf(out, "  -c URI, --connect URI   connect to hypervisor URI\n");
+	fprintf(out, "  -w, --wait              wait for domain to start\n");
+	fprintf(out, "\n");
+}
+
+int main(int argc, char **argv)
+{
 	char *uri = NULL;
+	char *name = NULL;
 	int opt_ind;
 	const char *sopts = "hVc:";
 	static const struct option lopts[] = {
@@ -701,17 +798,10 @@ int main(int argc, char **argv)
 		{ 0, 0, 0, 0 }
 	};
 	int ch;
+	int direct = 0;
 	int waitvnc = 0;
-	virConnectPtr conn = NULL;
-	virDomainPtr dom = NULL;
-	char *host = NULL;
-	char *vncport = NULL;
-	char *transport = NULL;
-	char *user = NULL;
-	const char *tmpname = NULL;
-	int port = 0;
-	int fd = -1;
-	int direct = 0;
+	int set_verbose = 0;
+	int ret;
 
 	while ((ch = getopt_long(argc, argv, sopts, lopts, &opt_ind)) != -1) {
 		switch (ch) {
@@ -722,7 +812,7 @@ int main(int argc, char **argv)
 			viewer_version(stdout);
 			return 0;
 		case 'v':
-			verbose = 1;
+			set_verbose = 1;
 			break;
 		case 'c':
 			uri = strdup(optarg);
@@ -739,7 +829,6 @@ int main(int argc, char **argv)
 		}
 	}
 
-
 	if (argc != (optind+1)) {
 		viewer_help(stderr, argv[0]);
 		return 1;
@@ -747,64 +836,16 @@ int main(int argc, char **argv)
 
 	gtk_init(&argc, &argv);
 
-	conn = virConnectOpenReadOnly(uri);
-	if (!conn) {
-		fprintf(stderr, "unable to connect to libvirt %s\n",
-			uri ? uri : "xen");
-		return 2;
-	}
-
-	do {
-		dom = viewer_lookup_domain(conn, argv[optind]);
-		if (!dom && !waitvnc) {
-			fprintf(stderr, "unable to lookup domain %s\n", argv[optind]);
-			return 3;
-		}
-		if (!dom)
-			usleep(500*1000);
-	} while (!dom);
-
-	do {
-		viewer_extract_vnc_graphics(dom, &vncport);
-		if (!vncport && !waitvnc) {
-			fprintf(stderr, "unable to find vnc graphics for %s\n", argv[optind]);
-			return 4;
-		}
-		if (!vncport)
-			usleep(300*1000);
-	} while (!vncport);
-	tmpname = virDomainGetName(dom);
-	if (tmpname != NULL) {
-		domname = strdup(tmpname);
-	}
-	virDomainFree(dom);
-	virConnectClose(conn);
-
-	if (viewer_extract_host(uri, &host, &transport, &user, &port) < 0) {
-		fprintf(stderr, "unable to determine hostname for URI %s\n", uri);
-		return 5;
-	}
-	DEBUG_LOG("Remote host is %s and transport %s user %s\n", host, transport ? transport : "", user ? user : "");
-
-	if (transport && strcasecmp(transport, "ssh") == 0 && !direct)
-		fd = viewer_open_tunnel_ssh(host, port, user, vncport);
-
-	vnc = vnc_display_new();
-	window = viewer_build_window(VNC_DISPLAY(vnc));
-	gtk_widget_realize(vnc);
-
-	vnc_display_set_keyboard_grab(VNC_DISPLAY(vnc), TRUE);
-	vnc_display_set_pointer_grab(VNC_DISPLAY(vnc), TRUE);
-
-	if (fd >= 0)
-		vnc_display_open_fd(VNC_DISPLAY(vnc), fd);
-	else
-		vnc_display_open_host(VNC_DISPLAY(vnc), host, vncport);
+	name = argv[optind];
+	ret = viewer_start (uri, name, direct, waitvnc, set_verbose,
+			    viewer_get_toplevel, NULL, 1);
+	if (ret != 0) return ret;
 
 	gtk_main();
 
 	return 0;
 }
+#endif /* !PLUGIN */
 
 /*
  * Local variables:
diff -r fe1efb558b4b src/viewer.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/viewer.h	Fri Jan 25 16:05:40 2008 +0000
@@ -0,0 +1,28 @@
+/*
+ * Virt Viewer: A virtual machine console viewer
+ *
+ * Copyright (C) 2007 Red Hat,
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Daniel P. Berrange <berrange@xxxxxxxxxx>
+ */
+
+#ifndef VIEWER_H
+#define VIEWER_H
+
+extern int viewer_start (const char *uri, const char *name, int direct, int waitvnc, int set_verbose, GtkWidget *(*get_toplevel)(void *), void *data, int with_menubar);
+
+#endif /* VIEWER_H */

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

_______________________________________________
et-mgmt-tools mailing list
et-mgmt-tools@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/et-mgmt-tools

[Index of Archives]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux