summaryrefslogtreecommitdiffstats
path: root/src/virt-viewer-session-spice.c
blob: a39ea0776807a237dab2520cad0401cf61ca05bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * Virt Viewer: A virtual machine console viewer
 *
 * Copyright (C) 2007-2009 Red Hat,
 * Copyright (C) 2009 Daniel P. Berrange
 * Copyright (C) 2010 Marc-André Lureau
 *
 * 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@redhat.com>
 */

#include <spice-audio.h>
#include <glib/gi18n.h>

#include <spice-option.h>
#include <usb-device-widget.h>
#include "virt-viewer-util.h"
#include "virt-viewer-session-spice.h"
#include "virt-viewer-display-spice.h"
#include "virt-viewer-auth.h"

G_DEFINE_TYPE (VirtViewerSessionSpice, virt_viewer_session_spice, VIRT_VIEWER_TYPE_SESSION)


struct _VirtViewerSessionSpicePrivate {
	SpiceSession *session;
	SpiceGtkSession *gtk_session;
	SpiceAudio *audio;
};

#define VIRT_VIEWER_SESSION_SPICE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), VIRT_VIEWER_TYPE_SESSION_SPICE, VirtViewerSessionSpicePrivate))

enum {
	PROP_0,
	PROP_SPICE_SESSION,
};


static void virt_viewer_session_spice_close(VirtViewerSession *session);
static gboolean virt_viewer_session_spice_open_fd(VirtViewerSession *session, int fd);
static gboolean virt_viewer_session_spice_open_host(VirtViewerSession *session, char *host, char *port);
static gboolean virt_viewer_session_spice_open_uri(VirtViewerSession *session, char *uri);
static gboolean virt_viewer_session_spice_channel_open_fd(VirtViewerSession *session, VirtViewerSessionChannel *channel, int fd);
static gboolean virt_viewer_session_spice_has_usb(VirtViewerSession *session);
static void virt_viewer_session_spice_usb_device_selection(VirtViewerSession *session, GtkWindow *parent);
static void virt_viewer_session_spice_channel_new(SpiceSession *s,
						  SpiceChannel *channel,
						  VirtViewerSession *session);
static void virt_viewer_session_spice_channel_destroy(SpiceSession *s,
						      SpiceChannel *channel,
						      VirtViewerSession *session);


static void
virt_viewer_session_spice_get_property(GObject *object, guint property_id,
				       GValue *value, GParamSpec *pspec)
{
	VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(object);
	VirtViewerSessionSpicePrivate *priv = self->priv;

	switch (property_id) {
	case PROP_SPICE_SESSION:
		g_value_set_object(value, priv->session);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
virt_viewer_session_spice_set_property(GObject *object, guint property_id,
				       const GValue *value G_GNUC_UNUSED, GParamSpec *pspec)
{
	switch (property_id) {
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
virt_viewer_session_spice_dispose(GObject *obj)
{
	VirtViewerSessionSpice *spice = VIRT_VIEWER_SESSION_SPICE(obj);

	if (spice->priv->session) {
		spice_session_disconnect(spice->priv->session);
		g_object_unref(spice->priv->session);
	}
	if (spice->priv->audio)
		g_object_unref(spice->priv->audio);

	G_OBJECT_CLASS(virt_viewer_session_spice_parent_class)->finalize(obj);
}


static void
virt_viewer_session_spice_class_init(VirtViewerSessionSpiceClass *klass)
{
	VirtViewerSessionClass *dclass = VIRT_VIEWER_SESSION_CLASS(klass);
	GObjectClass *oclass = G_OBJECT_CLASS(klass);

	oclass->get_property = virt_viewer_session_spice_get_property;
	oclass->set_property = virt_viewer_session_spice_set_property;
	oclass->dispose = virt_viewer_session_spice_dispose;

	dclass->close = virt_viewer_session_spice_close;
	dclass->open_fd = virt_viewer_session_spice_open_fd;
	dclass->open_host = virt_viewer_session_spice_open_host;
	dclass->open_uri = virt_viewer_session_spice_open_uri;
	dclass->channel_open_fd = virt_viewer_session_spice_channel_open_fd;
	dclass->has_usb = virt_viewer_session_spice_has_usb;
	dclass->usb_device_selection = virt_viewer_session_spice_usb_device_selection;

	g_type_class_add_private(klass, sizeof(VirtViewerSessionSpicePrivate));

	g_object_class_install_property(oclass,
					PROP_SPICE_SESSION,
					g_param_spec_object("spice-session",
							    "Spice session",
							    "Spice session",
							    SPICE_TYPE_SESSION,
							    G_PARAM_READABLE |
							    G_PARAM_STATIC_STRINGS));
}

static void
virt_viewer_session_spice_init(VirtViewerSessionSpice *self G_GNUC_UNUSED)
{
	self->priv = VIRT_VIEWER_SESSION_SPICE_GET_PRIVATE(self);
}

static void
usb_connect_failed(GObject *object G_GNUC_UNUSED,
		   SpiceUsbDevice *device G_GNUC_UNUSED,
		   GError *error, VirtViewerSessionSpice *self)
{
	if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
		return;

	g_signal_emit_by_name(self, "session-usb-failed", error->message);
}

static void
create_spice_session(VirtViewerSessionSpice *self)
{
	SpiceUsbDeviceManager *manager;

	g_return_if_fail(self != NULL);
	g_return_if_fail(self->priv->session == NULL);

	self->priv->session = spice_session_new();
	spice_set_session_option(self->priv->session);

	self->priv->gtk_session = spice_gtk_session_get(self->priv->session);
	g_object_set(self->priv->gtk_session, "auto-clipboard", TRUE, NULL);

	g_signal_connect(self->priv->session, "channel-new",
			 G_CALLBACK(virt_viewer_session_spice_channel_new), self);
	g_signal_connect(self->priv->session, "channel-destroy",
			 G_CALLBACK(virt_viewer_session_spice_channel_destroy), self);

	manager = spice_usb_device_manager_get(self->priv->session, NULL);
	if (manager)
		g_signal_connect(manager, "auto-connect-failed",
				 G_CALLBACK(usb_connect_failed), self);

	g_object_bind_property(self, "auto-usbredir",
			       self->priv->gtk_session, "auto-usbredir",
			       G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
}

static void
virt_viewer_session_spice_close(VirtViewerSession *session)
{
	VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(session);

	g_return_if_fail(self != NULL);

	virt_viewer_session_clear_displays(session);

	if (self->priv->session) {
		spice_session_disconnect(self->priv->session);
		g_object_unref(self->priv->session);
		self->priv->session = NULL;
		self->priv->gtk_session = NULL;

		if (self->priv->audio)
			g_object_unref(self->priv->audio);
		self->priv->audio = NULL;
	}

	/* FIXME: version 0.7 of spice-gtk allows reuse of session */
	create_spice_session(self);
}

static gboolean
virt_viewer_session_spice_open_host(VirtViewerSession *session,
				    char *host,
				    char *port)
{
	VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(session);

	g_return_val_if_fail(self != NULL, FALSE);
	g_return_val_if_fail(self->priv->session != NULL, FALSE);

	g_object_set(self->priv->session,
		     "host", host,
		     "port", port,
		     NULL);

	return spice_session_connect(self->priv->session);
}

static gboolean
virt_viewer_session_spice_open_uri(VirtViewerSession *session,
				   char *uri)
{
	VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(session);

	g_return_val_if_fail(self != NULL, FALSE);
	g_return_val_if_fail(self->priv->session != NULL, FALSE);

	g_object_set(self->priv->session, "uri", uri, NULL);

	return spice_session_connect(self->priv->session);
}

static gboolean
virt_viewer_session_spice_open_fd(VirtViewerSession *session,
				  int fd)
{
	VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(session);

	g_return_val_if_fail(self != NULL, FALSE);

	return spice_session_open_fd(self->priv->session, fd);
}

static gboolean
virt_viewer_session_spice_channel_open_fd(VirtViewerSession *session,
					  VirtViewerSessionChannel *channel,
					  int fd)
{
	VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(session);

	g_return_val_if_fail(self != NULL, FALSE);

	return spice_channel_open_fd(SPICE_CHANNEL(channel), fd);
}

static void
virt_viewer_session_spice_channel_open_fd_request(SpiceChannel *channel,
						  gint tls G_GNUC_UNUSED,
						  VirtViewerSession *session)
{
	g_signal_emit_by_name(session, "session-channel-open", channel);
}

static void
virt_viewer_session_spice_main_channel_event(SpiceChannel *channel G_GNUC_UNUSED,
					     SpiceChannelEvent event,
					     VirtViewerSession *session)
{
	VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(session);
	char *password = NULL;

	g_return_if_fail(self != NULL);

	switch (event) {
	case SPICE_CHANNEL_OPENED:
		DEBUG_LOG("main channel: opened");
		break;
	case SPICE_CHANNEL_CLOSED:
		DEBUG_LOG("main channel: closed");
		g_signal_emit_by_name(session, "session-disconnected");
		break;
	case SPICE_CHANNEL_ERROR_CONNECT:
		DEBUG_LOG("main channel: failed to connect");
		g_signal_emit_by_name(session, "session-disconnected");
		break;
	case SPICE_CHANNEL_ERROR_AUTH:
		DEBUG_LOG("main channel: auth failure (wrong password?)");
		int ret = virt_viewer_auth_collect_credentials("SPICE",
							       NULL,
							       NULL, &password);
		if (ret < 0) {
			g_signal_emit_by_name(session, "session-cancelled");
		} else {
			g_object_set(self->priv->session, "password", password, NULL);
			spice_session_connect(self->priv->session);
		}
		break;
	default:
		g_message("unhandled spice main channel event: %d", event);
		g_signal_emit_by_name(session, "session-disconnected");
		break;
	}

	g_free(password);
}

static gboolean
virt_viewer_session_spice_has_usb(VirtViewerSession *session)
{
	VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(session);
	VirtViewerSessionSpicePrivate *priv = self->priv;

	return spice_usb_device_manager_get(priv->session, NULL) &&
	       spice_session_has_channel_type(priv->session,
					      SPICE_CHANNEL_USBREDIR);
}

static void
virt_viewer_session_spice_usb_device_selection(VirtViewerSession *session,
					       GtkWindow *parent)
{
	VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(session);
	VirtViewerSessionSpicePrivate *priv = self->priv;
	GtkWidget *dialog, *area, *usb_device_widget;

	/* Create the widgets */
	dialog = gtk_dialog_new_with_buttons(
		    _("Select USB devices for redirection"), parent,
		    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
		    GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
		    NULL);
	gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
	area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));

	usb_device_widget = spice_usb_device_widget_new(priv->session,
							"%s %s");
	g_signal_connect(usb_device_widget, "connect-failed",
			 G_CALLBACK(usb_connect_failed), self);
	gtk_box_pack_start(GTK_BOX(area), usb_device_widget, TRUE, TRUE, 5);

	/* show and run */
	gtk_widget_show_all(dialog);
	gtk_dialog_run(GTK_DIALOG(dialog));
	gtk_widget_destroy(dialog);
}

static void
virt_viewer_session_spice_channel_new(SpiceSession *s,
				      SpiceChannel *channel,
				      VirtViewerSession *session)
{
	VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(session);
	int id;

	g_return_if_fail(self != NULL);

	g_signal_connect(channel, "open-fd",
			 G_CALLBACK(virt_viewer_session_spice_channel_open_fd_request), self);

	g_object_get(channel, "channel-id", &id, NULL);

	if (SPICE_IS_MAIN_CHANNEL(channel)) {
		g_signal_connect(channel, "channel-event",
				 G_CALLBACK(virt_viewer_session_spice_main_channel_event), self);
	}

	if (SPICE_IS_DISPLAY_CHANNEL(channel)) {
		GtkWidget *display;

		g_signal_emit_by_name(session, "session-connected");

		DEBUG_LOG("new session channel (#%d)", id);
		display = virt_viewer_display_spice_new(channel,
							spice_display_new(s, id));

		virt_viewer_session_add_display(VIRT_VIEWER_SESSION(session),
						VIRT_VIEWER_DISPLAY(display));

		g_signal_emit_by_name(session, "session-initialized");
	}

	if (SPICE_IS_INPUTS_CHANNEL(channel)) {
		DEBUG_LOG("new inputs channel");
	}

	if (SPICE_IS_PLAYBACK_CHANNEL(channel)) {
		DEBUG_LOG("new audio channel");
		if (self->priv->audio != NULL)
			return;
		self->priv->audio = spice_audio_new(s, NULL, NULL);
	}
}

static void
virt_viewer_session_spice_channel_destroy(G_GNUC_UNUSED SpiceSession *s,
					  SpiceChannel *channel,
					  VirtViewerSession *session)
{
	VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(session);
	int id;

	g_return_if_fail(self != NULL);

	g_object_get(channel, "channel-id", &id, NULL);
	if (SPICE_IS_MAIN_CHANNEL(channel)) {
		DEBUG_LOG("zap main channel");
	}

	if (SPICE_IS_DISPLAY_CHANNEL(channel)) {
		DEBUG_LOG("zap session channel (#%d)", id);
	}

	if (SPICE_IS_PLAYBACK_CHANNEL(channel) && self->priv->audio) {
		DEBUG_LOG("zap audio channel");
		g_object_unref(self->priv->audio);
		self->priv->audio = NULL;
	}
}

VirtViewerSession *
virt_viewer_session_spice_new(void)
{
	VirtViewerSessionSpice *self;

	self = g_object_new(VIRT_VIEWER_TYPE_SESSION_SPICE, NULL);

	create_spice_session(self);

	return VIRT_VIEWER_SESSION(self);
}

/*
 * Local variables:
 *  c-indent-level: 8
 *  c-basic-offset: 8
 *  tab-width: 8
 *  indent-tabs-mode: t
 * End:
 */