Codebase list virt-viewer / debian/0.4.1-1 src / virt-viewer-display.c
debian/0.4.1-1

Tree @debian/0.4.1-1 (Download .tar.gz)

virt-viewer-display.c @debian/0.4.1-1raw · history · blame

  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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
 * 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 <config.h>

#include <locale.h>

#include "virt-viewer-display.h"
#include "virt-viewer-util.h"

#define VIRT_VIEWER_DISPLAY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), VIRT_VIEWER_TYPE_DISPLAY, VirtViewerDisplayPrivate))

struct _VirtViewerDisplayPrivate
{
	gboolean dirty;
	guint desktopWidth;
	guint desktopHeight;
	guint zoom_level;
	gboolean zoom;
        gint nth_display;
        gint show_hint;
};

static void virt_viewer_display_size_request(GtkWidget *widget,
					     GtkRequisition *requisition);
#if GTK_CHECK_VERSION(3, 0, 0)
static void virt_viewer_display_get_preferred_width(GtkWidget *widget,
						    int *minwidth,
						    int *defwidth);
static void virt_viewer_display_get_preferred_height(GtkWidget *widget,
						     int *minheight,
						     int *defheight);
#endif
static void virt_viewer_display_size_allocate(GtkWidget *widget,
					      GtkAllocation *allocation);
static void virt_viewer_display_set_property(GObject *object,
					     guint prop_id,
					     const GValue *value,
					     GParamSpec *pspec);
static void virt_viewer_display_get_property(GObject *object,
					     guint prop_id,
					     GValue *value,
					     GParamSpec *pspec);

G_DEFINE_ABSTRACT_TYPE(VirtViewerDisplay, virt_viewer_display, GTK_TYPE_BIN)

enum {
	PROP_0,

	PROP_DESKTOP_WIDTH,
	PROP_DESKTOP_HEIGHT,
	PROP_NTH_DISPLAY,
	PROP_ZOOM,
	PROP_ZOOM_LEVEL,
	PROP_SHOW_HINT,
};

static void
virt_viewer_display_class_init(VirtViewerDisplayClass *class)
{
	GObjectClass *object_class = G_OBJECT_CLASS(class);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);

	object_class->set_property = virt_viewer_display_set_property;
	object_class->get_property = virt_viewer_display_get_property;

#if GTK_CHECK_VERSION(3, 0, 0)
	widget_class->get_preferred_width = virt_viewer_display_get_preferred_width;
	widget_class->get_preferred_height = virt_viewer_display_get_preferred_height;
#else
	widget_class->size_request = virt_viewer_display_size_request;
#endif
	widget_class->size_allocate = virt_viewer_display_size_allocate;

	g_object_class_install_property(object_class,
					PROP_DESKTOP_WIDTH,
					g_param_spec_int("desktop-width",
							 "Width",
							 "Desktop width",
							 100,
							 G_MAXINT32,
							 100,
							 G_PARAM_READWRITE));

	g_object_class_install_property(object_class,
					PROP_DESKTOP_HEIGHT,
					g_param_spec_int("desktop-height",
							 "Height",
							 "Desktop height",
							 100,
							 G_MAXINT32,
							 100,
							 G_PARAM_READWRITE));

	g_object_class_install_property(object_class,
					PROP_ZOOM,
					g_param_spec_boolean("zoom",
							     "Zoom",
							     "Zoom",
							     TRUE,
							     G_PARAM_READWRITE));

	g_object_class_install_property(object_class,
					PROP_ZOOM_LEVEL,
					g_param_spec_int("zoom-level",
							 "Zoom",
							 "Zoom level",
							 10,
							 400,
							 100,
							 G_PARAM_READWRITE));

	g_object_class_install_property(object_class,
					PROP_NTH_DISPLAY,
					g_param_spec_int("nth-display",
							 "Nth display",
							 "Nth display",
							 0,
							 G_MAXINT32,
							 0,
							 G_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT_ONLY));

	g_object_class_install_property(object_class,
					PROP_SHOW_HINT,
					g_param_spec_int("show-hint",
							 "Show hint",
							 "Show state hint",
							 0,
							 G_MAXINT32,
							 0,
							 G_PARAM_READABLE));


	g_signal_new("display-pointer-grab",
		     G_OBJECT_CLASS_TYPE(object_class),
		     G_SIGNAL_RUN_LAST | G_SIGNAL_NO_HOOKS,
		     G_STRUCT_OFFSET(VirtViewerDisplayClass, display_pointer_grab),
		     NULL,
		     NULL,
		     g_cclosure_marshal_VOID__VOID,
		     G_TYPE_NONE,
		     0);

	g_signal_new("display-pointer-ungrab",
		     G_OBJECT_CLASS_TYPE(object_class),
		     G_SIGNAL_RUN_LAST | G_SIGNAL_NO_HOOKS,
		     G_STRUCT_OFFSET(VirtViewerDisplayClass, display_pointer_ungrab),
		     NULL,
		     NULL,
		     g_cclosure_marshal_VOID__VOID,
		     G_TYPE_NONE,
		     0);

	g_signal_new("display-keyboard-grab",
		     G_OBJECT_CLASS_TYPE(object_class),
		     G_SIGNAL_RUN_LAST | G_SIGNAL_NO_HOOKS,
		     G_STRUCT_OFFSET(VirtViewerDisplayClass, display_keyboard_grab),
		     NULL,
		     NULL,
		     g_cclosure_marshal_VOID__VOID,
		     G_TYPE_NONE,
		     0);

	g_signal_new("display-keyboard-ungrab",
		     G_OBJECT_CLASS_TYPE(object_class),
		     G_SIGNAL_RUN_LAST | G_SIGNAL_NO_HOOKS,
		     G_STRUCT_OFFSET(VirtViewerDisplayClass, display_keyboard_ungrab),
		     NULL,
		     NULL,
		     g_cclosure_marshal_VOID__VOID,
		     G_TYPE_NONE,
		     0);

	g_signal_new("display-desktop-resize",
		     G_OBJECT_CLASS_TYPE(object_class),
		     G_SIGNAL_RUN_LAST | G_SIGNAL_NO_HOOKS,
		     G_STRUCT_OFFSET(VirtViewerDisplayClass, display_desktop_resize),
		     NULL,
		     NULL,
		     g_cclosure_marshal_VOID__VOID,
		     G_TYPE_NONE,
		     0);

	g_type_class_add_private(object_class, sizeof(VirtViewerDisplayPrivate));
}

static void
virt_viewer_display_init(VirtViewerDisplay *display)
{
	gtk_widget_set_has_window(GTK_WIDGET(display), FALSE);
	gtk_widget_set_redraw_on_allocate(GTK_WIDGET(display), FALSE);

	display->priv = VIRT_VIEWER_DISPLAY_GET_PRIVATE(display);

	display->priv->desktopWidth = 100;
	display->priv->desktopHeight = 100;
	display->priv->zoom_level = 100;
	display->priv->zoom = TRUE;
}

GtkWidget*
virt_viewer_display_new(void)
{
	return g_object_new(VIRT_VIEWER_TYPE_DISPLAY, NULL);
}

static void
virt_viewer_display_set_property(GObject *object,
				 guint prop_id,
				 const GValue *value,
				 GParamSpec *pspec)
{
	VirtViewerDisplay *display = VIRT_VIEWER_DISPLAY(object);
	VirtViewerDisplayPrivate *priv = display->priv;

	switch (prop_id) {
	case PROP_DESKTOP_WIDTH:
		virt_viewer_display_set_desktop_size(display,
						     g_value_get_int(value),
						     priv->desktopHeight);
		break;
	case PROP_DESKTOP_HEIGHT:
		virt_viewer_display_set_desktop_size(display,
						     priv->desktopWidth,
						     g_value_get_int(value));
		break;
        case PROP_NTH_DISPLAY:
                priv->nth_display = g_value_get_int(value);
                break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
virt_viewer_display_get_property(GObject *object,
				 guint prop_id,
				 GValue *value,
				 GParamSpec *pspec)
{
	VirtViewerDisplay *display = VIRT_VIEWER_DISPLAY(object);
	VirtViewerDisplayPrivate *priv = display->priv;

	switch (prop_id) {
	case PROP_DESKTOP_WIDTH:
		g_value_set_int(value, priv->desktopWidth);
		break;
	case PROP_DESKTOP_HEIGHT:
		g_value_set_int(value, priv->desktopHeight);
		break;
	case PROP_NTH_DISPLAY:
		g_value_set_int(value, priv->nth_display);
		break;
	case PROP_SHOW_HINT:
		g_value_set_int(value, priv->show_hint);
		break;

	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}


static gboolean
virt_viewer_display_idle(gpointer opaque)
{
	VirtViewerDisplay *display = opaque;
	VirtViewerDisplayPrivate *priv = display->priv;
	if (!priv->dirty)
		gtk_widget_queue_resize_no_redraw(GTK_WIDGET(display));
	return FALSE;
}


static void
virt_viewer_display_size_request(GtkWidget *widget,
				 GtkRequisition *requisition)
{
	VirtViewerDisplay *display = VIRT_VIEWER_DISPLAY(widget);
	VirtViewerDisplayPrivate *priv = display->priv;
	int border_width = gtk_container_get_border_width(GTK_CONTAINER(widget));

	requisition->width = border_width * 2;
	requisition->height = border_width * 2;

	if (priv->dirty) {
		if (priv->zoom)
			requisition->width += priv->desktopWidth * priv->zoom_level / 100;
		else
			requisition->width += priv->desktopWidth;
	} else {
		requisition->width += 50;
	}
	if (priv->dirty) {
		if (priv->zoom)
			requisition->height += priv->desktopHeight * priv->zoom_level / 100;
		else
			requisition->height += priv->desktopHeight;
	} else {
		requisition->height += 50;
	}

	DEBUG_LOG("Display size request %dx%d (desktop %dx%d)",
		  requisition->width, requisition->height,
		  priv->desktopWidth, priv->desktopHeight);
}


#if GTK_CHECK_VERSION(3, 0, 0)
static void virt_viewer_display_get_preferred_width(GtkWidget *widget,
						    int *minwidth,
						    int *defwidth)
{
	GtkRequisition req;

	virt_viewer_display_size_request(widget, &req);

	*minwidth = *defwidth = req.width;
}


static void virt_viewer_display_get_preferred_height(GtkWidget *widget,
						     int *minheight,
						     int *defheight)
{
	GtkRequisition req;

	virt_viewer_display_size_request(widget, &req);

	*minheight = *defheight = req.height;
}
#endif


static void
virt_viewer_display_size_allocate(GtkWidget *widget,
				  GtkAllocation *allocation)
{
	GtkBin *bin = GTK_BIN(widget);
	VirtViewerDisplay *display = VIRT_VIEWER_DISPLAY(widget);
	VirtViewerDisplayPrivate *priv = display->priv;
	GtkAllocation child_allocation;
	gint width, height;
	gint border_width;
	double desktopAspect;
	double actualAspect;
	GtkWidget *child = gtk_bin_get_child(bin);

	DEBUG_LOG("Allocated %dx%d", allocation->width, allocation->height);
	gtk_widget_set_allocation(widget, allocation);

	desktopAspect = (double)priv->desktopWidth / (double)priv->desktopHeight;

	if (child && gtk_widget_get_visible(child)) {
		border_width = gtk_container_get_border_width(GTK_CONTAINER(display));

		width  = MAX(1, allocation->width - 2 * border_width);
		height = MAX(1, allocation->height - 2 * border_width);
		actualAspect = (double)width / (double)height;

		if (actualAspect > desktopAspect) {
			child_allocation.width = height * desktopAspect;
			child_allocation.height = height;
		} else {
			child_allocation.width = width;
			child_allocation.height = width / desktopAspect;
		}

		child_allocation.x = 0.5 * (width - child_allocation.width) + allocation->x + border_width;
		child_allocation.y = 0.5 * (height - child_allocation.height) + allocation->y + border_width;

		DEBUG_LOG("Child allocate %dx%d", child_allocation.width, child_allocation.height);
		gtk_widget_size_allocate(child, &child_allocation);
	}


	/* This unsets the size request, so that the user can
	 * manually resize the window smaller again
	 */
	if (priv->dirty) {
		g_idle_add(virt_viewer_display_idle, widget);
		priv->dirty = FALSE;
	}
}


void virt_viewer_display_set_desktop_size(VirtViewerDisplay *display,
					  guint width,
					  guint height)
{
	VirtViewerDisplayPrivate *priv = display->priv;

	priv->desktopWidth = width;
	priv->desktopHeight = height;
	priv->dirty = TRUE;

	gtk_widget_queue_resize(GTK_WIDGET(display));
}


void virt_viewer_display_get_desktop_size(VirtViewerDisplay *display,
					  guint *width,
					  guint *height)
{
	VirtViewerDisplayPrivate *priv = display->priv;

	*width = priv->desktopWidth;
	*height = priv->desktopHeight;
}


void virt_viewer_display_set_zoom_level(VirtViewerDisplay *display,
					guint zoom)
{
	VirtViewerDisplayPrivate *priv = display->priv;
	GtkWidget *child = gtk_bin_get_child(GTK_BIN(display));

	if (zoom < 10)
		zoom = 10;
	if (zoom > 400)
		zoom = 400;
	priv->zoom_level = zoom;

	if (child && gtk_widget_get_visible(child)) {
		priv->dirty = TRUE;
		gtk_widget_queue_resize(GTK_WIDGET(display));
	}
}


void virt_viewer_display_set_zoom(VirtViewerDisplay *display,
				  gboolean zoom)
{
	VirtViewerDisplayPrivate *priv = display->priv;
	GtkWidget *child = gtk_bin_get_child(GTK_BIN(display));

	priv->zoom = zoom;
	if (child && gtk_widget_get_visible(child)) {
		priv->dirty = TRUE;
		gtk_widget_queue_resize(GTK_WIDGET(display));
	}
}

void virt_viewer_display_send_keys(VirtViewerDisplay *display,
				   const guint *keyvals, int nkeyvals)
{
	g_return_if_fail(VIRT_VIEWER_IS_DISPLAY(display));

	VIRT_VIEWER_DISPLAY_GET_CLASS(display)->send_keys(display, keyvals, nkeyvals);
}

GdkPixbuf* virt_viewer_display_get_pixbuf(VirtViewerDisplay *display)
{
	g_return_val_if_fail(VIRT_VIEWER_IS_DISPLAY(display), NULL);

	return VIRT_VIEWER_DISPLAY_GET_CLASS(display)->get_pixbuf(display);
}

void virt_viewer_display_set_show_hint(VirtViewerDisplay *self, gint hint)
{
	VirtViewerDisplayPrivate *priv;
	g_return_if_fail(VIRT_VIEWER_IS_DISPLAY(self));

        priv = self->priv;
        if (priv->show_hint == hint)
                return;

        priv->show_hint = hint;
        g_object_notify(G_OBJECT(self), "show-hint");
}

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