2 * Copyright (C) 2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * Parts are derived from 'gvnccapture':
22 * Copyright (C) 2010 Daniel P. Berrange <dan@berrange.com>
24 * This library is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU Lesser General Public
26 * License as published by the Free Software Foundation; either
27 * version 2.0 of the License, or (at your option) any later version.
29 * This library is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * Lesser General Public License for more details.
34 * You should have received a copy of the GNU Lesser General Public
35 * License along with this library; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
46 #include <glib/gi18n.h>
47 #include <vncconnection.h>
48 #include <vncbaseframebuffer.h>
52 static void initialized (VncConnection *conn, gpointer opaque);
53 static void disconnected (VncConnection *conn, gpointer opaque);
54 static int click (struct self_t *self, int x, int y, int b);
55 static void shutdown (self_t *self);
57 static callbacks_t callbacks = {
66 static GOptionEntry vnc_entries[] = {
67 { "vnc", 0, 0, G_OPTION_ARG_STRING,
68 &vnc, "Connect to VNC server directly", NULL },
71 static GOptionGroup *vnc_group;
74 vc_vnc_cmdline_get_option_group (void)
76 if (vnc_group == NULL) {
77 vnc_group = g_option_group_new ("vnc",
79 _("Show VNC Options"),
81 g_option_group_add_entries (vnc_group, vnc_entries);
87 vc_vnc_is_selected (void)
93 vc_vnc_setup (self_t *self)
98 self->callbacks = &callbacks;
101 vnc_util_set_debug (TRUE);
104 host = g_strdup ("localhost");
107 host = g_strdup (vnc);
108 display = strchr (host, ':');
113 port = 5900 + atoi (display);
117 self->conn = vnc_connection_new ();
119 g_signal_connect (self->conn, "vnc-initialized",
120 G_CALLBACK(initialized), self);
121 g_signal_connect (self->conn, "vnc-disconnected",
122 G_CALLBACK(disconnected), self);
123 g_signal_connect (self->conn, "vnc-auth-choose-type",
124 G_CALLBACK(vc_vnc_auth_choose_type), self);
125 g_signal_connect (self->conn, "vnc-auth-choose-subtype",
126 G_CALLBACK(vc_vnc_auth_choose_type), self);
127 g_signal_connect (self->conn, "vnc-auth-credential",
128 G_CALLBACK(vc_vnc_auth_credential), self);
130 port_str = g_strdup_printf ("%d", port);
131 vnc_connection_open_host (self->conn, host, port_str);
136 initialized (VncConnection *conn,
139 self_t *self = opaque;
140 gint32 encodings[] = { VNC_CONNECTION_ENCODING_DESKTOP_RESIZE,
141 VNC_CONNECTION_ENCODING_ZRLE,
142 VNC_CONNECTION_ENCODING_HEXTILE,
143 VNC_CONNECTION_ENCODING_RRE,
144 VNC_CONNECTION_ENCODING_COPY_RECT,
145 VNC_CONNECTION_ENCODING_RAW };
150 fprintf (stderr, "Connected to %s:%d\n", host, port - 5900);
152 /* Remember that we managed to connect. */
153 self->connected = TRUE;
156 * NB: Although this step is useless, you *MUST* send encodings otherwise
157 * qemu's internal VNC server refuses to parse any other events.
159 encodingsp = encodings;
160 n_encodings = G_N_ELEMENTS(encodings);
162 if (!vnc_connection_set_encodings(conn, n_encodings, encodingsp)) {
163 self->ret = EXIT_FAILURE;
164 vnc_connection_shutdown (self->conn);
168 /* Issue the command. */
169 vc_issue_command (self);
173 disconnected (VncConnection *conn G_GNUC_UNUSED,
176 self_t *self = opaque;
178 if (self->connected) {
179 self->ret = EXIT_SUCCESS;
181 fprintf (stderr, "Disconnected from %s:%d\n",
184 fprintf (stderr, "vnc: unable to connect to %s:%d\n",
186 self->ret = EXIT_FAILURE;
189 g_main_quit(self->loop);
193 shutdown (self_t *self)
195 vnc_connection_shutdown (self->conn);
196 g_object_unref (self->conn);
201 click (struct self_t *self, int x, int y, int b)
203 unsigned mask = b > 0 ? 1 << (b-1) : 0;
205 if (!vnc_connection_pointer_event (self->conn, mask, x, y))