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, unsigned mask);
55 static void shutdown (self_t *self);
57 static callbacks_t callbacks = {
63 vc_vnc_setup (self_t *self, gchar *vnc)
69 vnc_util_set_debug (TRUE);
72 self->host = g_strdup ("localhost");
75 self->host = g_strdup (vnc);
76 display = strchr (self->host, ':');
81 self->port = 5900 + atoi (display);
85 self->conn = vnc_connection_new ();
87 g_signal_connect (self->conn, "vnc-initialized",
88 G_CALLBACK(initialized), self);
89 g_signal_connect (self->conn, "vnc-disconnected",
90 G_CALLBACK(disconnected), self);
91 g_signal_connect (self->conn, "vnc-auth-choose-type",
92 G_CALLBACK(vc_vnc_auth_choose_type), self);
93 g_signal_connect (self->conn, "vnc-auth-choose-subtype",
94 G_CALLBACK(vc_vnc_auth_choose_type), self);
95 g_signal_connect (self->conn, "vnc-auth-credential",
96 G_CALLBACK(vc_vnc_auth_credential), self);
98 port = g_strdup_printf ("%d", self->port);
99 vnc_connection_open_host (self->conn, self->host, port);
101 self->callbacks = &callbacks;
105 initialized (VncConnection *conn,
108 self_t *self = opaque;
109 gint32 encodings[] = { VNC_CONNECTION_ENCODING_DESKTOP_RESIZE,
110 VNC_CONNECTION_ENCODING_ZRLE,
111 VNC_CONNECTION_ENCODING_HEXTILE,
112 VNC_CONNECTION_ENCODING_RRE,
113 VNC_CONNECTION_ENCODING_COPY_RECT,
114 VNC_CONNECTION_ENCODING_RAW };
119 fprintf (stderr, "Connected to %s:%d\n", self->host, self->port - 5900);
121 /* Remember that we managed to connect. */
122 self->connected = TRUE;
125 * NB: Although this step is useless, you *MUST* send encodings otherwise
126 * qemu's internal VNC server refuses to parse any other events.
128 encodingsp = encodings;
129 n_encodings = G_N_ELEMENTS(encodings);
131 if (!vnc_connection_set_encodings(conn, n_encodings, encodingsp)) {
132 self->ret = EXIT_FAILURE;
133 vnc_connection_shutdown (self->conn);
137 /* Issue the command. */
138 vc_issue_command (self);
142 disconnected (VncConnection *conn G_GNUC_UNUSED,
145 self_t *self = opaque;
147 if (self->connected) {
148 self->ret = EXIT_SUCCESS;
150 fprintf (stderr, "Disconnected from %s:%d\n",
151 self->host, self->port - 5900);
153 fprintf (stderr, "vnc: unable to connect to %s:%d\n",
154 self->host, self->port - 5900);
155 self->ret = EXIT_FAILURE;
158 g_main_quit(self->loop);
162 shutdown (self_t *self)
164 vnc_connection_shutdown (self->conn);
165 g_object_unref (self->conn);
169 /* To perform a button press in VNC we have to send the button press
170 * event, wait a short period, then send a button release event (ie.
171 * no buttons pressed).
173 static gboolean click_release (gpointer opaque);
176 click (struct self_t *self, int x, int y, unsigned mask)
178 if (!vnc_connection_pointer_event (self->conn, mask, x, y))
181 g_timeout_add (100, click_release, self);
185 click_release (gpointer opaque)
187 self_t *self = opaque;
189 vnc_connection_pointer_event (self->conn, 0,
190 self->command.click.x, self->command.click.y);
192 vnc_connection_shutdown (self->conn);