1 /*
2 * Virt Viewer: A virtual machine console viewer
3 *
4 * Copyright (C) 2007-2015 Red Hat, Inc.
5 * Copyright (C) 2009-2012 Daniel P. Berrange
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Daniel P. Berrange <berrange@redhat.com>
22 */
23
24 #include <config.h>
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <locale.h>
29
30 #ifdef G_OS_WIN32
31 #include <windows.h>
32 #include <io.h>
33 #endif
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <libxml/xpath.h>
40 #include <libxml/uri.h>
41
42 #include "virt-viewer-util.h"
43
44 GQuark
virt_viewer_error_quark(void)45 virt_viewer_error_quark(void)
46 {
47 return g_quark_from_static_string ("virt-viewer-error-quark");
48 }
49
virt_viewer_util_load_ui(const char * name)50 GtkBuilder *virt_viewer_util_load_ui(const char *name)
51 {
52 GtkBuilder *builder;
53 gchar *resource = g_strdup_printf("%s/ui/%s",
54 VIRT_VIEWER_RESOURCE_PREFIX,
55 name);
56
57 builder = gtk_builder_new_from_resource(resource);
58
59 g_free(resource);
60 return builder;
61 }
62
63 int
virt_viewer_util_extract_host(const char * uristr,char ** scheme,char ** host,char ** transport,char ** user,int * port)64 virt_viewer_util_extract_host(const char *uristr,
65 char **scheme,
66 char **host,
67 char **transport,
68 char **user,
69 int *port)
70 {
71 xmlURIPtr uri;
72 char *offset = NULL;
73
74 if (uristr == NULL ||
75 !g_ascii_strcasecmp(uristr, "xen"))
76 uristr = "xen:///";
77
78 uri = xmlParseURI(uristr);
79 g_return_val_if_fail(uri != NULL, 1);
80
81 if (host) {
82 if (!uri->server) {
83 *host = g_strdup("localhost");
84 } else {
85 if (uri->server[0] == '[') {
86 gchar *tmp;
87 *host = g_strdup(uri->server + 1);
88 if ((tmp = strchr(*host, ']')))
89 *tmp = '\0';
90 } else {
91 *host = g_strdup(uri->server);
92 }
93 }
94 }
95
96 if (user) {
97 if (uri->user)
98 *user = g_strdup(uri->user);
99 else
100 *user = NULL;
101 }
102
103 if (port)
104 *port = uri->port;
105
106 if (uri->scheme)
107 offset = strchr(uri->scheme, '+');
108
109 if (transport) {
110 if (offset)
111 *transport = g_strdup(offset + 1);
112 else
113 *transport = NULL;
114 }
115
116 if (scheme && uri->scheme) {
117 if (offset)
118 *scheme = g_strndup(uri->scheme, offset - uri->scheme);
119 else
120 *scheme = g_strdup(uri->scheme);
121 }
122
123 xmlFreeURI(uri);
124 return 0;
125 }
126
127 typedef struct {
128 GObject *instance;
129 GObject *observer;
130 GClosure *closure;
131 gulong handler_id;
132 } WeakHandlerCtx;
133
134 static WeakHandlerCtx *
whc_new(GObject * instance,GObject * observer)135 whc_new(GObject *instance,
136 GObject *observer)
137 {
138 WeakHandlerCtx *ctx = g_new0(WeakHandlerCtx, 1);
139
140 ctx->instance = instance;
141 ctx->observer = observer;
142
143 return ctx;
144 }
145
146 static void
whc_free(WeakHandlerCtx * ctx)147 whc_free(WeakHandlerCtx *ctx)
148 {
149 g_free(ctx);
150 }
151
152 static void observer_destroyed_cb(gpointer, GObject *);
153 static void closure_invalidated_cb(gpointer, GClosure *);
154
155 /*
156 * If signal handlers are removed before the object is destroyed, this
157 * callback will never get triggered.
158 */
159 static void
instance_destroyed_cb(gpointer ctx_,GObject * where_the_instance_was G_GNUC_UNUSED)160 instance_destroyed_cb(gpointer ctx_,
161 GObject *where_the_instance_was G_GNUC_UNUSED)
162 {
163 WeakHandlerCtx *ctx = ctx_;
164
165 /* No need to disconnect the signal here, the instance has gone away. */
166 g_object_weak_unref(ctx->observer, observer_destroyed_cb, ctx);
167 g_closure_remove_invalidate_notifier(ctx->closure, ctx,
168 closure_invalidated_cb);
169 whc_free(ctx);
170 }
171
172 /* Triggered when the observer is destroyed. */
173 static void
observer_destroyed_cb(gpointer ctx_,GObject * where_the_observer_was G_GNUC_UNUSED)174 observer_destroyed_cb(gpointer ctx_,
175 GObject *where_the_observer_was G_GNUC_UNUSED)
176 {
177 WeakHandlerCtx *ctx = ctx_;
178
179 g_closure_remove_invalidate_notifier(ctx->closure, ctx,
180 closure_invalidated_cb);
181 g_signal_handler_disconnect(ctx->instance, ctx->handler_id);
182 g_object_weak_unref(ctx->instance, instance_destroyed_cb, ctx);
183 whc_free(ctx);
184 }
185
186 /* Triggered when either object is destroyed or the handler is disconnected. */
187 static void
closure_invalidated_cb(gpointer ctx_,GClosure * where_the_closure_was G_GNUC_UNUSED)188 closure_invalidated_cb(gpointer ctx_,
189 GClosure *where_the_closure_was G_GNUC_UNUSED)
190 {
191 WeakHandlerCtx *ctx = ctx_;
192
193 g_object_weak_unref(ctx->instance, instance_destroyed_cb, ctx);
194 g_object_weak_unref(ctx->observer, observer_destroyed_cb, ctx);
195 whc_free(ctx);
196 }
197
198 /* Copied from tp_g_signal_connect_object. */
199 /**
200 * virt_viewer_signal_connect_object: (skip)
201 * @instance: the instance to connect to.
202 * @detailed_signal: a string of the form "signal-name::detail".
203 * @c_handler: the #GCallback to connect.
204 * @gobject: the object to pass as data to @c_handler.
205 * @connect_flags: a combination of #GConnectFlags.
206 *
207 * Similar to g_signal_connect_object() but will delete connection
208 * when any of the objects is destroyed.
209 *
210 * Returns: the handler id.
211 */
virt_viewer_signal_connect_object(gpointer instance,const gchar * detailed_signal,GCallback c_handler,gpointer gobject,GConnectFlags connect_flags)212 gulong virt_viewer_signal_connect_object(gpointer instance,
213 const gchar *detailed_signal,
214 GCallback c_handler,
215 gpointer gobject,
216 GConnectFlags connect_flags)
217 {
218 GObject *instance_obj = G_OBJECT(instance);
219 WeakHandlerCtx *ctx = whc_new(instance_obj, gobject);
220
221 g_return_val_if_fail(G_TYPE_CHECK_INSTANCE (instance), 0);
222 g_return_val_if_fail(detailed_signal != NULL, 0);
223 g_return_val_if_fail(c_handler != NULL, 0);
224 g_return_val_if_fail(G_IS_OBJECT (gobject), 0);
225 g_return_val_if_fail((connect_flags & ~(G_CONNECT_AFTER|G_CONNECT_SWAPPED)) == 0, 0);
226
227 if (connect_flags & G_CONNECT_SWAPPED)
228 ctx->closure = g_cclosure_new_object_swap(c_handler, gobject);
229 else
230 ctx->closure = g_cclosure_new_object(c_handler, gobject);
231
232 ctx->handler_id = g_signal_connect_closure(instance, detailed_signal,
233 ctx->closure, (connect_flags & G_CONNECT_AFTER) ? TRUE : FALSE);
234
235 g_object_weak_ref(instance_obj, instance_destroyed_cb, ctx);
236 g_object_weak_ref(gobject, observer_destroyed_cb, ctx);
237 g_closure_add_invalidate_notifier(ctx->closure, ctx,
238 closure_invalidated_cb);
239
240 return ctx->handler_id;
241 }
242
log_handler(const gchar * log_domain,GLogLevelFlags log_level,const gchar * message,gpointer unused_data)243 static void log_handler(const gchar *log_domain,
244 GLogLevelFlags log_level,
245 const gchar *message,
246 gpointer unused_data)
247 {
248 if (glib_check_version(2, 32, 0) != NULL)
249 if (log_level >= G_LOG_LEVEL_DEBUG && !doDebug)
250 return;
251
252 g_log_default_handler(log_domain, log_level, message, unused_data);
253 }
254
255 #ifdef G_OS_WIN32
is_handle_valid(HANDLE h)256 static BOOL is_handle_valid(HANDLE h)
257 {
258 if (h == INVALID_HANDLE_VALUE || h == NULL)
259 return FALSE;
260
261 DWORD flags;
262 return GetHandleInformation(h, &flags);
263 }
264 #endif
265
virt_viewer_util_init(const char * appname)266 void virt_viewer_util_init(const char *appname)
267 {
268 #ifdef G_OS_WIN32
269 /*
270 * This named mutex will be kept around by Windows until the
271 * process terminates. This allows other instances to check if it
272 * already exists, indicating already running instances. It is
273 * used to warn the user that installer can't proceed in this
274 * case.
275 */
276 CreateMutexA(0, 0, "VirtViewerMutex");
277
278 /* Get redirection from parent */
279 BOOL out_valid = is_handle_valid(GetStdHandle(STD_OUTPUT_HANDLE));
280 BOOL err_valid = is_handle_valid(GetStdHandle(STD_ERROR_HANDLE));
281
282 /*
283 * If not all output are redirected try to redirect to parent console.
284 * If parent has no console (for instance as launched from GUI) just
285 * rely on default (no output).
286 */
287 if ((!out_valid || !err_valid) && AttachConsole(ATTACH_PARENT_PROCESS)) {
288 if (!out_valid) {
289 freopen("CONOUT$", "w", stdout);
290 dup2(fileno(stdout), STDOUT_FILENO);
291 }
292 if (!err_valid) {
293 freopen("CONOUT$", "w", stderr);
294 dup2(fileno(stderr), STDERR_FILENO);
295 }
296 }
297 #endif
298
299 setlocale(LC_ALL, "");
300
301 #ifdef G_OS_WIN32
302 gchar *base_path = g_win32_get_package_installation_directory_of_module(NULL);
303 gchar *utf8_locale_dir = g_build_filename(base_path, "share", "locale", NULL);
304 /* bindtextdomain's 2nd argument is not UTF-8 aware */
305 gchar *locale_dir = g_win32_locale_filename_from_utf8 (utf8_locale_dir);
306
307 g_warn_if_fail(locale_dir != NULL);
308 bindtextdomain(GETTEXT_PACKAGE, locale_dir);
309
310 g_free(base_path);
311 g_free(utf8_locale_dir);
312 g_free(locale_dir);
313 #else
314 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
315 #endif
316 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
317 textdomain(GETTEXT_PACKAGE);
318
319 g_set_application_name(appname);
320
321 g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_MASK, log_handler, NULL);
322 }
323
324 static gchar *
ctrl_key_to_gtk_key(const gchar * key)325 ctrl_key_to_gtk_key(const gchar *key)
326 {
327 guint i;
328
329 static const struct {
330 const char *ctrl;
331 const char *gtk;
332 } keys[] = {
333 /* FIXME: right alt, right ctrl, right shift, cmds */
334 { "alt", "<Alt>" },
335 { "ralt", "<Alt>" },
336 { "rightalt", "<Alt>" },
337 { "right-alt", "<Alt>" },
338 { "lalt", "<Alt>" },
339 { "leftalt", "<Alt>" },
340 { "left-alt", "<Alt>" },
341
342 { "ctrl", "<Ctrl>" },
343 { "rctrl", "<Ctrl>" },
344 { "rightctrl", "<Ctrl>" },
345 { "right-ctrl", "<Ctrl>" },
346 { "lctrl", "<Ctrl>" },
347 { "leftctrl", "<Ctrl>" },
348 { "left-ctrl", "<Ctrl>" },
349
350 { "shift", "<Shift>" },
351 { "rshift", "<Shift>" },
352 { "rightshift", "<Shift>" },
353 { "right-shift", "<Shift>" },
354 { "lshift", "<Shift>" },
355 { "leftshift", "<Shift>" },
356 { "left-shift", "<Shift>" },
357
358 { "cmd", "<Ctrl>" },
359 { "rcmd", "<Ctrl>" },
360 { "rightcmd", "<Ctrl>" },
361 { "right-cmd", "<Ctrl>" },
362 { "lcmd", "<Ctrl>" },
363 { "leftcmd", "<Ctrl>" },
364 { "left-cmd", "<Ctrl>" },
365
366 { "win", "<Super>" },
367 { "rwin", "<Super>" },
368 { "rightwin", "<Super>" },
369 { "right-win", "<Super>" },
370 { "lwin", "<Super>" },
371 { "leftwin", "<Super>" },
372 { "left-win", "<Super>" },
373
374 { "esc", "Escape" },
375 /* { "escape", "Escape" }, */
376
377 { "ins", "Insert" },
378 /* { "insert", "Insert" }, */
379
380 { "del", "Delete" },
381 /* { "delete", "Delete" }, */
382
383 { "pgup", "Page_Up" },
384 { "pageup", "Page_Up" },
385 { "pgdn", "Page_Down" },
386 { "pagedown", "Page_Down" },
387
388 /* { "home", "home" }, */
389 { "end", "End" },
390 /* { "space", "space" }, */
391
392 { "enter", "Return" },
393
394 /* { "tab", "tab" }, */
395 /* { "f1", "F1" }, */
396 /* { "f2", "F2" }, */
397 /* { "f3", "F3" }, */
398 /* { "f4", "F4" }, */
399 /* { "f5", "F5" }, */
400 /* { "f6", "F6" }, */
401 /* { "f7", "F7" }, */
402 /* { "f8", "F8" }, */
403 /* { "f9", "F9" }, */
404 /* { "f10", "F10" }, */
405 /* { "f11", "F11" }, */
406 /* { "f12", "F12" } */
407 };
408
409 for (i = 0; i < G_N_ELEMENTS(keys); ++i) {
410 if (g_ascii_strcasecmp(keys[i].ctrl, key) == 0)
411 return g_strdup(keys[i].gtk);
412 }
413
414 return g_ascii_strup(key, -1);
415 }
416
417 gchar*
spice_hotkey_to_gtk_accelerator(const gchar * key)418 spice_hotkey_to_gtk_accelerator(const gchar *key)
419 {
420 gchar *accel, **k, **keyv;
421
422 keyv = g_strsplit(key, "+", -1);
423 g_return_val_if_fail(keyv != NULL, NULL);
424
425 for (k = keyv; *k != NULL; k++) {
426 gchar *tmp = *k;
427 *k = ctrl_key_to_gtk_key(tmp);
428 g_free(tmp);
429 }
430
431 accel = g_strjoinv(NULL, keyv);
432 g_strfreev(keyv);
433
434 return accel;
435 }
436
437 static gchar *
spice_key_to_gdk_key(const gchar * spice_key)438 spice_key_to_gdk_key(const gchar *spice_key)
439 {
440 guint i;
441 gchar *key = g_strdup(spice_key);
442
443 static const struct {
444 const char *spice;
445 const char *gdk;
446 } keys[] = {
447
448 { "alt", "Alt_L" },
449 { "lalt", "Alt_L" },
450 { "leftalt", "Alt_L" },
451 { "left-alt", "Alt_L" },
452 { "ralt", "Alt_R" },
453 { "rightalt", "Alt_R" },
454 { "right-alt", "Alt_R" },
455
456 { "ctrl", "Control_L" },
457 { "ctl", "Control_L" },
458 { "control", "Control_L" },
459 { "lctrl", "Control_L" },
460 { "leftctrl", "Control_L" },
461 { "left-ctrl", "Control_L" },
462 { "rctrl", "Control_R" },
463 { "rightctrl", "Control_R" },
464 { "right-ctrl", "Control_R" },
465
466 { "cmd", "Control_L" },
467 { "lcmd", "Control_L" },
468 { "leftcmd", "Control_L" },
469 { "left-cmd", "Control_L" },
470 { "rcmd", "Control_R" },
471 { "rightcmd", "Control_R" },
472 { "right-cmd", "Control_R" },
473
474 { "shift", "Shift_L" },
475 { "shft", "Shift_L" },
476 { "lshift", "Shift_L" },
477 { "leftshift", "Shift_L" },
478 { "left-shift", "Shift_L" },
479 { "rshift", "Shift_R" },
480 { "rightshift", "Shift_R" },
481 { "right-shift", "Shift_R" },
482
483 { "win", "Super_L" },
484 { "lwin", "Super_L" },
485 { "leftwin", "Super_L" },
486 { "left-win", "Super_L" },
487 { "rwin", "Super_R" },
488 { "rightwin", "Super_R" },
489 { "right-win", "Super_R" },
490
491 { "super", "Super_L" },
492 { "hyper", "Hyper_L" },
493 { "meta", "Meta_L" },
494
495 { "esc", "Escape" },
496 { "escape", "Escape" },
497 { "ins", "Insert" },
498 { "insert", "Insert" },
499 { "del", "Delete" },
500 { "delete", "Delete" },
501
502 { "pgup", "Page_Up" },
503 { "pageup", "Page_Up" },
504 { "pgdn", "Page_Down" },
505 { "pagedown", "Page_Down" },
506
507 { "home", "Home" },
508 { "end", "End" },
509 { "space", "space" },
510
511 { "enter", "Return" },
512
513 { "tab", "Tab" },
514 { "f1", "F1" },
515 { "f2", "F2" },
516 { "f3", "F3" },
517 { "f4", "F4" },
518 { "f5", "F5" },
519 { "f6", "F6" },
520 { "f7", "F7" },
521 { "f8", "F8" },
522 { "f9", "F9" },
523 { "f10", "F10" },
524 { "f11", "F11" },
525 { "f12", "F12" },
526 };
527
528 if (key[0] == '<' && key[strlen(key)-1] == '>') {
529 gchar *tmp = key;
530 key = g_strndup(key+1, strlen(key)-2);
531 g_free(tmp);
532 }
533
534 for (i = 0; i < G_N_ELEMENTS(keys); ++i) {
535 if (g_ascii_strcasecmp(keys[i].spice, key) == 0) {
536 g_free(key);
537 return g_strdup(keys[i].gdk);
538 }
539 }
540
541 return key;
542 }
543
544 gchar*
spice_hotkey_to_display_hotkey(const gchar * key)545 spice_hotkey_to_display_hotkey(const gchar *key)
546 {
547 gchar *new_key, **k, **keyv;
548
549 keyv = g_strsplit(key, "+", -1);
550 g_return_val_if_fail(keyv != NULL, NULL);
551
552 for (k = keyv; *k != NULL; k++) {
553 gchar *tmp = *k;
554 *k = spice_key_to_gdk_key(tmp);
555 g_free(tmp);
556 }
557
558 new_key = g_strjoinv("+", keyv);
559 g_strfreev(keyv);
560
561 return new_key;
562 }
563
str_is_empty(const gchar * str)564 static gboolean str_is_empty(const gchar *str)
565 {
566 return ((str == NULL) || (str[0] == '\0'));
567 }
568
569 static gint
virt_viewer_compare_version(const gchar * s1,const gchar * s2)570 virt_viewer_compare_version(const gchar *s1, const gchar *s2)
571 {
572 gint i, retval = 0;
573 gchar **v1, **v2;
574
575 if (str_is_empty(s1) && str_is_empty(s2)) {
576 return 0;
577 } else if (str_is_empty(s1)) {
578 return -1;
579 } else if (str_is_empty(s2)) {
580 return 1;
581 }
582
583 v1 = g_strsplit(s1, ".", -1);
584 v2 = g_strsplit(s2, ".", -1);
585
586 for (i = 0; v1[i] && v2[i]; ++i) {
587 gchar *e1 = NULL, *e2 = NULL;
588 guint64 m1 = g_ascii_strtoull(v1[i], &e1, 10);
589 guint64 m2 = g_ascii_strtoull(v2[i], &e2, 10);
590
591 retval = m1 - m2;
592 if (retval != 0)
593 goto end;
594
595 g_return_val_if_fail(e1 && e2, 0);
596 if (*e1 || *e2) {
597 g_warning("the version string contains a suffix");
598 goto end;
599 }
600 }
601
602 if (v1[i])
603 retval = 1;
604 else if (v2[i])
605 retval = -1;
606
607 end:
608 g_strfreev(v1);
609 g_strfreev(v2);
610 return retval;
611 }
612
613 /**
614 * virt_viewer_compare_buildid:
615 * @s1: a version-like string
616 * @s2: a version-like string
617 *
618 * Compare two buildid strings: 1.1-1 > 1.0-1, 1.0-2 > 1.0-1, 1.10 > 1.7...
619 *
620 * String suffix (1.0rc1 etc) are not accepted, and will return 0.
621 *
622 * Returns: negative value if s1 < s2; zero if s1 = s2; positive value if s1 > s2.
623 **/
624 gint
virt_viewer_compare_buildid(const gchar * s1,const gchar * s2)625 virt_viewer_compare_buildid(const gchar *s1, const gchar *s2)
626 {
627 int ret = 0;
628 GStrv split1 = NULL;
629 GStrv split2 = NULL;
630
631 split1 = g_strsplit(s1, "-", 2);
632 split2 = g_strsplit(s2, "-", 2);
633 if ((split1 == NULL) || (split2 == NULL)) {
634 goto end;
635 }
636 /* Compare versions */
637 ret = virt_viewer_compare_version(split1[0], split2[0]);
638 if (ret != 0) {
639 goto end;
640 }
641 if ((split1[0] == NULL) || (split2[0] == NULL)) {
642 goto end;
643 }
644
645 /* Compare -release */
646 ret = virt_viewer_compare_version(split1[1], split2[1]);
647
648 end:
649 g_strfreev(split1);
650 g_strfreev(split2);
651
652 return ret;
653 }
654
655 /* simple sorting of monitors. Primary sort left-to-right, secondary sort from
656 * top-to-bottom, finally by monitor id */
657 static int
displays_cmp(const void * p1,const void * p2,gpointer user_data)658 displays_cmp(const void *p1, const void *p2, gpointer user_data)
659 {
660 guint diff;
661 GHashTable *displays = user_data;
662 guint i = *(guint*)p1;
663 guint j = *(guint*)p2;
664 GdkRectangle *m1 = g_hash_table_lookup(displays, GINT_TO_POINTER(i));
665 GdkRectangle *m2 = g_hash_table_lookup(displays, GINT_TO_POINTER(j));
666 g_return_val_if_fail(m1 != NULL && m2 != NULL, 0);
667 diff = m1->x - m2->x;
668 if (diff == 0)
669 diff = m1->y - m2->y;
670 if (diff == 0)
671 diff = i - j;
672
673 return diff;
674 }
675
find_max_id(gpointer key,gpointer value G_GNUC_UNUSED,gpointer user_data)676 static void find_max_id(gpointer key,
677 gpointer value G_GNUC_UNUSED,
678 gpointer user_data)
679 {
680 guint *max_id = user_data;
681 guint id = GPOINTER_TO_INT(key);
682 *max_id = MAX(*max_id, id);
683 }
684
685 void
virt_viewer_align_monitors_linear(GHashTable * displays)686 virt_viewer_align_monitors_linear(GHashTable *displays)
687 {
688 guint i;
689 gint x = 0;
690 guint *sorted_displays;
691 guint max_id = 0;
692 guint ndisplays = 0;
693 GHashTableIter iter;
694 gpointer key;
695
696 g_return_if_fail(displays != NULL);
697
698 if (g_hash_table_size(displays) == 0)
699 return;
700
701 g_hash_table_foreach(displays, find_max_id, &max_id);
702 ndisplays = max_id + 1;
703
704 sorted_displays = g_new0(guint, ndisplays);
705
706 g_hash_table_iter_init(&iter, displays);
707 while (g_hash_table_iter_next(&iter, &key, NULL))
708 sorted_displays[GPOINTER_TO_INT(key)] = GPOINTER_TO_INT(key);
709
710 g_qsort_with_data(sorted_displays, ndisplays, sizeof(guint), displays_cmp, displays);
711
712 /* adjust monitor positions so that there's no gaps or overlap between
713 * monitors */
714 for (i = 0; i < ndisplays; i++) {
715 guint nth = sorted_displays[i];
716 g_assert(nth < ndisplays);
717 GdkRectangle *rect = g_hash_table_lookup(displays, GINT_TO_POINTER(nth));
718 g_return_if_fail(rect != NULL);
719 rect->x = x;
720 rect->y = 0;
721 x += rect->width;
722 }
723 g_free(sorted_displays);
724 }
725
726 /* Shift all displays so that the monitor origin is at (0,0). This reduces the
727 * size of the screen that will be required on the guest when all client
728 * monitors are fullscreen but do not begin at the origin. For example, instead
729 * of sending down the following configuration:
730 * 1280x1024+4240+0
731 * (which implies that the guest screen must be at least 5520x1024), we'd send
732 * 1280x1024+0+0
733 * (which implies the guest screen only needs to be 1280x1024). The first
734 * version might fail if the guest video memory is not large enough to handle a
735 * screen of that size.
736 */
737 void
virt_viewer_shift_monitors_to_origin(GHashTable * displays)738 virt_viewer_shift_monitors_to_origin(GHashTable *displays)
739 {
740 gint xmin = G_MAXINT;
741 gint ymin = G_MAXINT;
742 GHashTableIter iter;
743 gpointer value;
744
745 if (g_hash_table_size(displays) == 0)
746 return;
747
748 g_hash_table_iter_init(&iter, displays);
749 while (g_hash_table_iter_next(&iter, NULL, &value)) {
750 GdkRectangle *display = value;
751 g_return_if_fail(display != NULL);
752 if (display->width > 0 && display->height > 0) {
753 xmin = MIN(xmin, display->x);
754 ymin = MIN(ymin, display->y);
755 }
756 }
757 g_return_if_fail(xmin < G_MAXINT && ymin < G_MAXINT);
758
759 if (xmin > 0 || ymin > 0) {
760 g_debug("%s: Shifting all monitors by (%i, %i)", G_STRFUNC, xmin, ymin);
761 g_hash_table_iter_init(&iter, displays);
762 while (g_hash_table_iter_next(&iter, NULL, &value)) {
763 GdkRectangle *display = value;
764 if (display->width > 0 && display->height > 0) {
765 display->x -= xmin;
766 display->y -= ymin;
767 }
768 }
769 }
770 }
771
772 /**
773 * virt_viewer_parse_monitor_mappings:
774 * @mappings: (array zero-terminated=1) values for the "monitor-mapping" key
775 * @nmappings: the size of @mappings
776 * @nmonitors: the count of client's monitors
777 *
778 * Parses and validates monitor mappings values to return a hash table
779 * containing the mapping from guest display ids to client monitors ids.
780 *
781 * Returns: (transfer full) a #GHashTable containing mapping from guest display
782 * ids to client monitor ids or %NULL if the mapping is invalid.
783 */
784 GHashTable*
virt_viewer_parse_monitor_mappings(gchar ** mappings,const gsize nmappings,const gint nmonitors)785 virt_viewer_parse_monitor_mappings(gchar **mappings, const gsize nmappings, const gint nmonitors)
786 {
787 GHashTable *displaymap;
788 GHashTable *monitormap;
789 guint i, max_display_id = 0;
790 gchar **tokens = NULL;
791
792 g_return_val_if_fail(nmonitors != 0, NULL);
793 displaymap = g_hash_table_new(g_direct_hash, g_direct_equal);
794 monitormap = g_hash_table_new(g_direct_hash, g_direct_equal);
795 if (nmappings == 0) {
796 g_warning("Empty monitor-mapping configuration");
797 goto configerror;
798 }
799
800 for (i = 0; i < nmappings; i++) {
801 gchar *endptr = NULL;
802 gint display = 0, monitor = 0;
803
804 tokens = g_strsplit(mappings[i], ":", 2);
805 if (g_strv_length(tokens) != 2) {
806 g_warning("Invalid monitor-mapping configuration: '%s'. "
807 "Expected format is '<DISPLAY-ID>:<MONITOR-ID>'",
808 mappings[i]);
809 g_strfreev(tokens);
810 goto configerror;
811 }
812
813 display = strtol(tokens[0], &endptr, 10);
814 if ((endptr && *endptr != '\0') || display < 1) {
815 g_warning("Invalid monitor-mapping configuration: display id is invalid: %s %p='%s'", tokens[0], endptr, endptr);
816 g_strfreev(tokens);
817 goto configerror;
818 }
819 monitor = strtol(tokens[1], &endptr, 10);
820 if ((endptr && *endptr != '\0') || monitor < 1) {
821 g_warning("Invalid monitor-mapping configuration: monitor id '%s' is invalid", tokens[1]);
822 g_strfreev(tokens);
823 goto configerror;
824 }
825 g_strfreev(tokens);
826
827 if (monitor > nmonitors) {
828 g_warning("Invalid monitor-mapping configuration: monitor #%i for display #%i does not exist", monitor, display);
829 goto configerror;
830 }
831
832 /* config file format is 1-based, not 0-based */
833 display--;
834 monitor--;
835
836 if (g_hash_table_lookup_extended(displaymap, GINT_TO_POINTER(display), NULL, NULL) ||
837 g_hash_table_lookup_extended(monitormap, GINT_TO_POINTER(monitor), NULL, NULL)) {
838 g_warning("Invalid monitor-mapping configuration: a display or monitor id was specified twice");
839 goto configerror;
840 }
841 g_debug("Fullscreen config: mapping guest display %i to monitor %i", display, monitor);
842 g_hash_table_insert(displaymap, GINT_TO_POINTER(display), GINT_TO_POINTER(monitor));
843 g_hash_table_insert(monitormap, GINT_TO_POINTER(monitor), GINT_TO_POINTER(display));
844 max_display_id = MAX((guint) display, max_display_id);
845 }
846
847 for (i = 0; i < max_display_id; i++) {
848 if (!g_hash_table_lookup_extended(displaymap, GINT_TO_POINTER(i), NULL, NULL)) {
849 g_warning("Invalid monitor-mapping configuration: display #%d was not specified", i+1);
850 goto configerror;
851 }
852 }
853
854 g_hash_table_unref(monitormap);
855 return displaymap;
856
857 configerror:
858 g_hash_table_unref(monitormap);
859 g_hash_table_unref(displaymap);
860 return NULL;
861 }
862
863 int
virt_viewer_enum_from_string(GType enum_type,gchar * name)864 virt_viewer_enum_from_string(GType enum_type, gchar *name)
865 {
866 GEnumClass *enum_class;
867 GEnumValue *enum_value;
868
869 g_return_val_if_fail(G_TYPE_IS_ENUM(enum_type), -1);
870
871 enum_class = g_type_class_ref(enum_type);
872 enum_value = g_enum_get_value_by_nick(enum_class, name);
873 g_type_class_unref(enum_class);
874
875 if (enum_value == NULL)
876 return -1;
877
878 return enum_value->value;
879 }
880