本文主要是针对spice的基础上对virt-viewer进行分析,使用这款软件的主要命令就是remote-viewer这个命令,因此本文在此主要分析的文件就是remote-viewer-main.c文件,spice编程方式基本上都是使用c语言实现面向对象的编程方式
安装好软件后使用remote-viewer命令,然后在对话框中的输入:spice://172.16.1.134?port=9004;
然后点击连接就会链接到服务器端了实现远程登录,如下图所示:
#include <config.h>//主要是程序中的一些开关的定义,通过#define 来定义实现某些功能的裁剪,这个文件在virt-viewer源码下
#include <locale.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <stdlib.h>
#ifdef G_OS_WIN32
#include <windows.h>
#include <io.h>
#endif
#ifdef HAVE_GTK_VNC
#include <vncdisplay.h>
#endif
#ifdef HAVE_SPICE_GTK //spice-gtk的使能在config.h中
#include <spice-option.h>//主要调用spice的操作选项spice_set_session_option这个函数 设置spice session选项,一些功能的使能,比如usbredir
#endif
#ifdef HAVE_OVIRT
#include <govirt/ovirt-options.h>
#endif
#include "remote-viewer.h"
#include "virt-viewer-app.h"
#include "virt-viewer-session.h"
static void
remote_viewer_version(void)//显示本系统版本好,在config.h中是有定义的,本文件是2.0 版本
{
g_print(_("remote-viewer version %s\n"), VERSION BUILDID);
exit(EXIT_SUCCESS);
}
static void
recent_add(gchar *uri, const gchar *mime_type)
{
GtkRecentManager *recent;
GtkRecentData meta = {
.app_name = (char*)"remote-viewer",
.app_exec = (char*)"remote-viewer %u",
.mime_type = (char*)mime_type,
};
if (uri == NULL)
return;
recent = gtk_recent_manager_get_default();
meta.display_name = uri;
if (!gtk_recent_manager_add_full(recent, uri, &meta))
g_warning("Recent item couldn't be added");
}
static void connected(VirtViewerSession *session,
VirtViewerApp *self G_GNUC_UNUSED)
{
gchar *uri = virt_viewer_session_get_uri(session);
const gchar *mime = virt_viewer_session_mime_type(session);
recent_add(uri, mime);
g_free(uri);
}
int
main(int argc, char **argv)
{
GOptionContext *context;//这是glib中对输入参数的解析,并没有直接利用判断
GError *error = NULL;
int ret = 1;
gchar **args = NULL;
gchar *uri = NULL;
char *title = NULL;
RemoteViewer *viewer = NULL;
#ifdef HAVE_SPICE_GTK //同样检测spice-gtk的时能情况,在config.h中有定义
gboolean controller = FALSE;//关闭controller
#endif
VirtViewerApp *app;
const GOptionEntry options [] = {//GOptionEntry就是GLib提供一个方便我们设置输入参数的结构的,例如 cd /home
{ "version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
remote_viewer_version, N_("Display version information"), NULL },//显示参数-V的作用即 virt-viewer -V,显示相关的信息
{ "title", 't', 0, G_OPTION_ARG_STRING, &title,
N_("Set window title"), NULL },
#ifdef HAVE_SPICE_GTK
{ "spice-controller", '\0', 0, G_OPTION_ARG_NONE, &controller,
N_("Open connection using Spice controller communication"), NULL },
#endif
{ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &args,
NULL, "URI|VV-FILE" },
{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
};
GOptionGroup *app_options = NULL;
virt_viewer_util_init(_("Remote Viewer"));//初始化相关的remote选项
/* Setup command line options */
context = g_option_context_new (NULL);//重设命令行
g_option_context_set_summary(context, _("Remote viewer client"));
app_options = virt_viewer_app_get_option_group();
g_option_group_add_entries (app_options, options);
g_option_context_set_main_group (context, app_options);
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 ());
#endif
#ifdef HAVE_SPICE_GTK
g_option_context_add_group (context, spice_get_option_group ());
#endif
#ifdef HAVE_OVIRT
g_option_context_add_group (context, ovirt_get_option_group ());
#endif
g_option_context_parse (context, &argc, &argv, &error);//获取context内的参数
if (error) {
char *base_name;
base_name = g_path_get_basename(argv[0]);
g_printerr(_("%s\nRun '%s --help' to see a full list of available command line options\n"),
error->message, base_name);
g_free(base_name);
g_error_free(error);
goto cleanup;
}
g_option_context_free(context);
#ifdef HAVE_SPICE_GTK
if (controller) {
if (args) {
g_printerr(_("Error: extra arguments given while using Spice controller\n"));
goto cleanup;
}
} else
#endif
if (args) {
if (g_strv_length(args) > 1) {
g_printerr(_("Error: can't handle multiple URIs\n"));
goto cleanup;
} else if (g_strv_length(args) == 1) {
uri = g_strdup(args[0]);
}
}
#ifdef HAVE_SPICE_GTK
if (controller) {
viewer = remote_viewer_new_with_controller();
g_object_set(viewer, "guest-name", "defined by Spice controller", NULL);
} else {
#endif
viewer = remote_viewer_new(uri);
if (title)
g_object_set(viewer, "title", title, NULL);
#ifdef HAVE_SPICE_GTK
}
#endif
if (viewer == NULL)
goto cleanup;
app = VIRT_VIEWER_APP(viewer);
if (!virt_viewer_app_start(app))
goto cleanup;
g_signal_connect(virt_viewer_app_get_session(app), "session-connected",
G_CALLBACK(connected), app);
gtk_main();
ret = 0;
cleanup:
g_free(uri);
if (viewer)
g_object_unref(viewer);
g_strfreev(args);
return ret;
}