-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtk.c
1900 lines (1594 loc) · 56.6 KB
/
gtk.c
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GTK UI
*
* Copyright IBM, Corp. 2012
*
* Authors:
* Anthony Liguori <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*
* Portions from gtk-vnc:
*
* GTK VNC Widget
*
* Copyright (C) 2006 Anthony Liguori <[email protected]>
* Copyright (C) 2009-2010 Daniel P. Berrange <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.0 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define GETTEXT_PACKAGE "qemu"
#define LOCALEDIR "po"
#ifdef _WIN32
# define _WIN32_WINNT 0x0601 /* needed to get definition of MAPVK_VK_TO_VSC */
#endif
#include "qemu-common.h"
#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
/* Work around an -Wstrict-prototypes warning in GTK headers */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
#endif
#include <gtk/gtk.h>
#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
#pragma GCC diagnostic pop
#endif
#include <gdk/gdkkeysyms.h>
#include <glib/gi18n.h>
#include <locale.h>
#if defined(CONFIG_VTE)
#include <vte/vte.h>
#endif
#include <math.h>
#include "trace.h"
#include "ui/console.h"
#include "ui/input.h"
#include "sysemu/sysemu.h"
#include "qmp-commands.h"
#include "x_keymap.h"
#include "keymaps.h"
#include "sysemu/char.h"
#include "qom/object.h"
#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#include <X11/XKBlib.h>
#endif
#define MAX_VCS 10
#define VC_WINDOW_X_MIN 320
#define VC_WINDOW_Y_MIN 240
#define VC_TERM_X_MIN 80
#define VC_TERM_Y_MIN 25
#define VC_SCALE_MIN 0.25
#define VC_SCALE_STEP 0.25
#if !defined(CONFIG_VTE)
# define VTE_CHECK_VERSION(a, b, c) 0
#endif
#if defined(CONFIG_VTE) && !GTK_CHECK_VERSION(3, 0, 0)
/*
* The gtk2 vte terminal widget seriously messes up the window resize
* for some reason. You basically can't make the qemu window smaller
* any more because the toplevel window geoemtry hints are overridden.
*
* Workaround that by hiding all vte widgets, except the one in the
* current tab.
*
* Luckily everything works smooth in gtk3.
*/
# define VTE_RESIZE_HACK 1
#endif
/* Compatibility define to let us build on both Gtk2 and Gtk3 */
#if GTK_CHECK_VERSION(3, 0, 0)
static inline void gdk_drawable_get_size(GdkWindow *w, gint *ww, gint *wh)
{
*ww = gdk_window_get_width(w);
*wh = gdk_window_get_height(w);
}
#endif
#if !GTK_CHECK_VERSION(2, 20, 0)
#define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
#endif
#ifndef GDK_IS_X11_DISPLAY
#define GDK_IS_X11_DISPLAY(dpy) (dpy == dpy)
#endif
#ifndef GDK_IS_WIN32_DISPLAY
#define GDK_IS_WIN32_DISPLAY(dpy) (dpy == dpy)
#endif
#ifndef GDK_KEY_0
#define GDK_KEY_0 GDK_0
#define GDK_KEY_1 GDK_1
#define GDK_KEY_2 GDK_2
#define GDK_KEY_f GDK_f
#define GDK_KEY_g GDK_g
#define GDK_KEY_q GDK_q
#define GDK_KEY_plus GDK_plus
#define GDK_KEY_minus GDK_minus
#endif
#define HOTKEY_MODIFIERS (GDK_CONTROL_MASK | GDK_MOD1_MASK)
static const int modifier_keycode[] = {
/* shift, control, alt keys, meta keys, both left & right */
0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
};
typedef struct GtkDisplayState GtkDisplayState;
typedef struct VirtualGfxConsole {
GtkWidget *drawing_area;
DisplayChangeListener dcl;
DisplaySurface *ds;
pixman_image_t *convert;
cairo_surface_t *surface;
double scale_x;
double scale_y;
} VirtualGfxConsole;
#if defined(CONFIG_VTE)
typedef struct VirtualVteConsole {
GtkWidget *box;
GtkWidget *scrollbar;
GtkWidget *terminal;
CharDriverState *chr;
} VirtualVteConsole;
#endif
typedef enum VirtualConsoleType {
GD_VC_GFX,
GD_VC_VTE,
} VirtualConsoleType;
typedef struct VirtualConsole {
GtkDisplayState *s;
char *label;
GtkWidget *window;
GtkWidget *menu_item;
GtkWidget *tab_item;
VirtualConsoleType type;
union {
VirtualGfxConsole gfx;
#if defined(CONFIG_VTE)
VirtualVteConsole vte;
#endif
};
} VirtualConsole;
struct GtkDisplayState {
GtkWidget *window;
GtkWidget *menu_bar;
GtkAccelGroup *accel_group;
GtkWidget *machine_menu_item;
GtkWidget *machine_menu;
GtkWidget *pause_item;
GtkWidget *reset_item;
GtkWidget *powerdown_item;
GtkWidget *quit_item;
GtkWidget *view_menu_item;
GtkWidget *view_menu;
GtkWidget *full_screen_item;
GtkWidget *zoom_in_item;
GtkWidget *zoom_out_item;
GtkWidget *zoom_fixed_item;
GtkWidget *zoom_fit_item;
GtkWidget *grab_item;
GtkWidget *grab_on_hover_item;
int nb_vcs;
VirtualConsole vc[MAX_VCS];
GtkWidget *show_tabs_item;
GtkWidget *untabify_item;
GtkWidget *vbox;
GtkWidget *notebook;
int button_mask;
gboolean last_set;
int last_x;
int last_y;
int grab_x_root;
int grab_y_root;
VirtualConsole *kbd_owner;
VirtualConsole *ptr_owner;
gboolean full_screen;
GdkCursor *null_cursor;
Notifier mouse_mode_notifier;
gboolean free_scale;
bool external_pause_update;
bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
bool has_evdev;
};
static void gd_grab_pointer(VirtualConsole *vc);
static void gd_ungrab_pointer(GtkDisplayState *s);
/** Utility Functions **/
static VirtualConsole *gd_vc_find_by_menu(GtkDisplayState *s)
{
VirtualConsole *vc;
gint i;
for (i = 0; i < s->nb_vcs; i++) {
vc = &s->vc[i];
if (gtk_check_menu_item_get_active
(GTK_CHECK_MENU_ITEM(vc->menu_item))) {
return vc;
}
}
return NULL;
}
static VirtualConsole *gd_vc_find_by_page(GtkDisplayState *s, gint page)
{
VirtualConsole *vc;
gint i, p;
for (i = 0; i < s->nb_vcs; i++) {
vc = &s->vc[i];
p = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook), vc->tab_item);
if (p == page) {
return vc;
}
}
return NULL;
}
static VirtualConsole *gd_vc_find_current(GtkDisplayState *s)
{
gint page;
page = gtk_notebook_get_current_page(GTK_NOTEBOOK(s->notebook));
return gd_vc_find_by_page(s, page);
}
static bool gd_is_grab_active(GtkDisplayState *s)
{
return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_item));
}
static bool gd_grab_on_hover(GtkDisplayState *s)
{
return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->grab_on_hover_item));
}
static void gd_update_cursor(VirtualConsole *vc)
{
GtkDisplayState *s = vc->s;
GdkWindow *window;
if (vc->type != GD_VC_GFX) {
return;
}
window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
gdk_window_set_cursor(window, s->null_cursor);
} else {
gdk_window_set_cursor(window, NULL);
}
}
static void gd_update_caption(GtkDisplayState *s)
{
const char *status = "";
gchar *prefix;
gchar *title;
const char *grab = "";
bool is_paused = !runstate_is_running();
int i;
if (qemu_name) {
prefix = g_strdup_printf("QEMU (%s)", qemu_name);
} else {
prefix = g_strdup_printf("QEMU");
}
if (s->ptr_owner != NULL &&
s->ptr_owner->window == NULL) {
grab = _(" - Press Ctrl+Alt+G to release grab");
}
if (is_paused) {
status = _(" [Paused]");
}
s->external_pause_update = true;
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->pause_item),
is_paused);
s->external_pause_update = false;
title = g_strdup_printf("%s%s%s", prefix, status, grab);
gtk_window_set_title(GTK_WINDOW(s->window), title);
g_free(title);
for (i = 0; i < s->nb_vcs; i++) {
VirtualConsole *vc = &s->vc[i];
if (!vc->window) {
continue;
}
title = g_strdup_printf("%s: %s%s%s", prefix, vc->label,
vc == s->kbd_owner ? " +kbd" : "",
vc == s->ptr_owner ? " +ptr" : "");
gtk_window_set_title(GTK_WINDOW(vc->window), title);
g_free(title);
}
g_free(prefix);
}
static void gd_update_geometry_hints(VirtualConsole *vc)
{
GtkDisplayState *s = vc->s;
GdkWindowHints mask = 0;
GdkGeometry geo = {};
GtkWidget *geo_widget = NULL;
GtkWindow *geo_window;
if (vc->type == GD_VC_GFX) {
if (s->free_scale) {
geo.min_width = surface_width(vc->gfx.ds) * VC_SCALE_MIN;
geo.min_height = surface_height(vc->gfx.ds) * VC_SCALE_MIN;
mask |= GDK_HINT_MIN_SIZE;
} else {
geo.min_width = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
geo.min_height = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
mask |= GDK_HINT_MIN_SIZE;
}
geo_widget = vc->gfx.drawing_area;
gtk_widget_set_size_request(geo_widget, geo.min_width, geo.min_height);
#if defined(CONFIG_VTE)
} else if (vc->type == GD_VC_VTE) {
VteTerminal *term = VTE_TERMINAL(vc->vte.terminal);
GtkBorder *ib;
geo.width_inc = vte_terminal_get_char_width(term);
geo.height_inc = vte_terminal_get_char_height(term);
mask |= GDK_HINT_RESIZE_INC;
geo.base_width = geo.width_inc;
geo.base_height = geo.height_inc;
mask |= GDK_HINT_BASE_SIZE;
geo.min_width = geo.width_inc * VC_TERM_X_MIN;
geo.min_height = geo.height_inc * VC_TERM_Y_MIN;
mask |= GDK_HINT_MIN_SIZE;
gtk_widget_style_get(vc->vte.terminal, "inner-border", &ib, NULL);
geo.base_width += ib->left + ib->right;
geo.base_height += ib->top + ib->bottom;
geo.min_width += ib->left + ib->right;
geo.min_height += ib->top + ib->bottom;
geo_widget = vc->vte.terminal;
#endif
}
geo_window = GTK_WINDOW(vc->window ? vc->window : s->window);
gtk_window_set_geometry_hints(geo_window, geo_widget, &geo, mask);
}
static void gd_update_windowsize(VirtualConsole *vc)
{
GtkDisplayState *s = vc->s;
gd_update_geometry_hints(vc);
if (vc->type == GD_VC_GFX && !s->full_screen && !s->free_scale) {
gtk_window_resize(GTK_WINDOW(vc->window ? vc->window : s->window),
VC_WINDOW_X_MIN, VC_WINDOW_Y_MIN);
}
}
static void gd_update_full_redraw(VirtualConsole *vc)
{
GtkWidget *area = vc->gfx.drawing_area;
int ww, wh;
gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
}
static void gtk_release_modifiers(GtkDisplayState *s)
{
VirtualConsole *vc = gd_vc_find_current(s);
int i, keycode;
if (vc->type != GD_VC_GFX) {
return;
}
for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
keycode = modifier_keycode[i];
if (!s->modifier_pressed[i]) {
continue;
}
qemu_input_event_send_key_number(vc->gfx.dcl.con, keycode, false);
s->modifier_pressed[i] = false;
}
}
/** DisplayState Callbacks **/
static void gd_update(DisplayChangeListener *dcl,
int x, int y, int w, int h)
{
VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
int x1, x2, y1, y2;
int mx, my;
int fbw, fbh;
int ww, wh;
trace_gd_update(vc->label, x, y, w, h);
if (vc->gfx.convert) {
pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
NULL, vc->gfx.convert,
x, y, 0, 0, x, y, w, h);
}
x1 = floor(x * vc->gfx.scale_x);
y1 = floor(y * vc->gfx.scale_y);
x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
&ww, &wh);
mx = my = 0;
if (ww > fbw) {
mx = (ww - fbw) / 2;
}
if (wh > fbh) {
my = (wh - fbh) / 2;
}
gtk_widget_queue_draw_area(vc->gfx.drawing_area,
mx + x1, my + y1, (x2 - x1), (y2 - y1));
}
static void gd_refresh(DisplayChangeListener *dcl)
{
graphic_hw_update(dcl->con);
}
#if GTK_CHECK_VERSION(3, 0, 0)
static void gd_mouse_set(DisplayChangeListener *dcl,
int x, int y, int visible)
{
VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
GdkDisplay *dpy;
GdkDeviceManager *mgr;
gint x_root, y_root;
if (qemu_input_is_absolute()) {
return;
}
dpy = gtk_widget_get_display(vc->gfx.drawing_area);
mgr = gdk_display_get_device_manager(dpy);
gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
x, y, &x_root, &y_root);
gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
gtk_widget_get_screen(vc->gfx.drawing_area),
x_root, y_root);
}
#else
static void gd_mouse_set(DisplayChangeListener *dcl,
int x, int y, int visible)
{
VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
gint x_root, y_root;
if (qemu_input_is_absolute()) {
return;
}
gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
x, y, &x_root, &y_root);
gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
gtk_widget_get_screen(vc->gfx.drawing_area),
x_root, y_root);
}
#endif
static void gd_cursor_define(DisplayChangeListener *dcl,
QEMUCursor *c)
{
VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
GdkPixbuf *pixbuf;
GdkCursor *cursor;
pixbuf = gdk_pixbuf_new_from_data((guchar *)(c->data),
GDK_COLORSPACE_RGB, true, 8,
c->width, c->height, c->width * 4,
NULL, NULL);
cursor = gdk_cursor_new_from_pixbuf
(gtk_widget_get_display(vc->gfx.drawing_area),
pixbuf, c->hot_x, c->hot_y);
gdk_window_set_cursor(gtk_widget_get_window(vc->gfx.drawing_area), cursor);
g_object_unref(pixbuf);
#if !GTK_CHECK_VERSION(3, 0, 0)
gdk_cursor_unref(cursor);
#else
g_object_unref(cursor);
#endif
}
static void gd_switch(DisplayChangeListener *dcl,
DisplaySurface *surface)
{
VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
bool resized = true;
trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
if (vc->gfx.surface) {
cairo_surface_destroy(vc->gfx.surface);
}
if (vc->gfx.ds &&
surface_width(vc->gfx.ds) == surface_width(surface) &&
surface_height(vc->gfx.ds) == surface_height(surface)) {
resized = false;
}
vc->gfx.ds = surface;
if (vc->gfx.convert) {
pixman_image_unref(vc->gfx.convert);
vc->gfx.convert = NULL;
}
if (surface->format == PIXMAN_x8r8g8b8) {
/*
* PIXMAN_x8r8g8b8 == CAIRO_FORMAT_RGB24
*
* No need to convert, use surface directly. Should be the
* common case as this is qemu_default_pixelformat(32) too.
*/
vc->gfx.surface = cairo_image_surface_create_for_data
(surface_data(surface),
CAIRO_FORMAT_RGB24,
surface_width(surface),
surface_height(surface),
surface_stride(surface));
} else {
/* Must convert surface, use pixman to do it. */
vc->gfx.convert = pixman_image_create_bits(PIXMAN_x8r8g8b8,
surface_width(surface),
surface_height(surface),
NULL, 0);
vc->gfx.surface = cairo_image_surface_create_for_data
((void *)pixman_image_get_data(vc->gfx.convert),
CAIRO_FORMAT_RGB24,
pixman_image_get_width(vc->gfx.convert),
pixman_image_get_height(vc->gfx.convert),
pixman_image_get_stride(vc->gfx.convert));
pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
NULL, vc->gfx.convert,
0, 0, 0, 0, 0, 0,
pixman_image_get_width(vc->gfx.convert),
pixman_image_get_height(vc->gfx.convert));
}
if (resized) {
gd_update_windowsize(vc);
} else {
gd_update_full_redraw(vc);
}
}
/** QEMU Events **/
static void gd_change_runstate(void *opaque, int running, RunState state)
{
GtkDisplayState *s = opaque;
gd_update_caption(s);
}
static void gd_mouse_mode_change(Notifier *notify, void *data)
{
GtkDisplayState *s;
int i;
s = container_of(notify, GtkDisplayState, mouse_mode_notifier);
/* release the grab at switching to absolute mode */
if (qemu_input_is_absolute() && gd_is_grab_active(s)) {
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
FALSE);
}
for (i = 0; i < s->nb_vcs; i++) {
VirtualConsole *vc = &s->vc[i];
gd_update_cursor(vc);
}
}
/** GTK Events **/
static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
void *opaque)
{
GtkDisplayState *s = opaque;
int i;
if (!no_quit) {
for (i = 0; i < s->nb_vcs; i++) {
if (s->vc[i].type != GD_VC_GFX) {
continue;
}
unregister_displaychangelistener(&s->vc[i].gfx.dcl);
}
qmp_quit(NULL);
return FALSE;
}
return TRUE;
}
static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
{
VirtualConsole *vc = opaque;
GtkDisplayState *s = vc->s;
int mx, my;
int ww, wh;
int fbw, fbh;
if (!gtk_widget_get_realized(widget)) {
return FALSE;
}
fbw = surface_width(vc->gfx.ds);
fbh = surface_height(vc->gfx.ds);
gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);
if (s->full_screen) {
vc->gfx.scale_x = (double)ww / fbw;
vc->gfx.scale_y = (double)wh / fbh;
} else if (s->free_scale) {
double sx, sy;
sx = (double)ww / fbw;
sy = (double)wh / fbh;
vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
}
fbw *= vc->gfx.scale_x;
fbh *= vc->gfx.scale_y;
mx = my = 0;
if (ww > fbw) {
mx = (ww - fbw) / 2;
}
if (wh > fbh) {
my = (wh - fbh) / 2;
}
cairo_rectangle(cr, 0, 0, ww, wh);
/* Optionally cut out the inner area where the pixmap
will be drawn. This avoids 'flashing' since we're
not double-buffering. Note we're using the undocumented
behaviour of drawing the rectangle from right to left
to cut out the whole */
cairo_rectangle(cr, mx + fbw, my,
-1 * fbw, fbh);
cairo_fill(cr);
cairo_scale(cr, vc->gfx.scale_x, vc->gfx.scale_y);
cairo_set_source_surface(cr, vc->gfx.surface,
mx / vc->gfx.scale_x, my / vc->gfx.scale_y);
cairo_paint(cr);
return TRUE;
}
#if !GTK_CHECK_VERSION(3, 0, 0)
static gboolean gd_expose_event(GtkWidget *widget, GdkEventExpose *expose,
void *opaque)
{
cairo_t *cr;
gboolean ret;
cr = gdk_cairo_create(gtk_widget_get_window(widget));
cairo_rectangle(cr,
expose->area.x,
expose->area.y,
expose->area.width,
expose->area.height);
cairo_clip(cr);
ret = gd_draw_event(widget, cr, opaque);
cairo_destroy(cr);
return ret;
}
#endif
static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
void *opaque)
{
VirtualConsole *vc = opaque;
GtkDisplayState *s = vc->s;
int x, y;
int mx, my;
int fbh, fbw;
int ww, wh;
fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
&ww, &wh);
mx = my = 0;
if (ww > fbw) {
mx = (ww - fbw) / 2;
}
if (wh > fbh) {
my = (wh - fbh) / 2;
}
x = (motion->x - mx) / vc->gfx.scale_x;
y = (motion->y - my) / vc->gfx.scale_y;
if (qemu_input_is_absolute()) {
if (x < 0 || y < 0 ||
x >= surface_width(vc->gfx.ds) ||
y >= surface_height(vc->gfx.ds)) {
return TRUE;
}
qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_X, x,
surface_width(vc->gfx.ds));
qemu_input_queue_abs(vc->gfx.dcl.con, INPUT_AXIS_Y, y,
surface_height(vc->gfx.ds));
qemu_input_event_sync();
} else if (s->last_set && s->ptr_owner == vc) {
qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_X, x - s->last_x);
qemu_input_queue_rel(vc->gfx.dcl.con, INPUT_AXIS_Y, y - s->last_y);
qemu_input_event_sync();
}
s->last_x = x;
s->last_y = y;
s->last_set = TRUE;
if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
int x = (int)motion->x_root;
int y = (int)motion->y_root;
/* In relative mode check to see if client pointer hit
* one of the screen edges, and if so move it back by
* 200 pixels. This is important because the pointer
* in the server doesn't correspond 1-for-1, and so
* may still be only half way across the screen. Without
* this warp, the server pointer would thus appear to hit
* an invisible wall */
if (x == 0) {
x += 200;
}
if (y == 0) {
y += 200;
}
if (x == (gdk_screen_get_width(screen) - 1)) {
x -= 200;
}
if (y == (gdk_screen_get_height(screen) - 1)) {
y -= 200;
}
if (x != (int)motion->x_root || y != (int)motion->y_root) {
#if GTK_CHECK_VERSION(3, 0, 0)
GdkDevice *dev = gdk_event_get_device((GdkEvent *)motion);
gdk_device_warp(dev, screen, x, y);
#else
GdkDisplay *display = gtk_widget_get_display(widget);
gdk_display_warp_pointer(display, screen, x, y);
#endif
s->last_set = FALSE;
return FALSE;
}
}
return TRUE;
}
static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
void *opaque)
{
VirtualConsole *vc = opaque;
GtkDisplayState *s = vc->s;
InputButton btn;
/* implicitly grab the input at the first click in the relative mode */
if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
!qemu_input_is_absolute() && s->ptr_owner != vc) {
gd_ungrab_pointer(s);
if (!vc->window) {
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
TRUE);
} else {
gd_grab_pointer(vc);
gd_update_caption(s);
}
return TRUE;
}
if (button->button == 1) {
btn = INPUT_BUTTON_LEFT;
} else if (button->button == 2) {
btn = INPUT_BUTTON_MIDDLE;
} else if (button->button == 3) {
btn = INPUT_BUTTON_RIGHT;
} else {
return TRUE;
}
qemu_input_queue_btn(vc->gfx.dcl.con, btn,
button->type == GDK_BUTTON_PRESS);
qemu_input_event_sync();
return TRUE;
}
static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
void *opaque)
{
VirtualConsole *vc = opaque;
InputButton btn;
if (scroll->direction == GDK_SCROLL_UP) {
btn = INPUT_BUTTON_WHEEL_UP;
} else if (scroll->direction == GDK_SCROLL_DOWN) {
btn = INPUT_BUTTON_WHEEL_DOWN;
} else {
return TRUE;
}
qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
qemu_input_event_sync();
qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
qemu_input_event_sync();
return TRUE;
}
static int gd_map_keycode(GtkDisplayState *s, GdkDisplay *dpy, int gdk_keycode)
{
int qemu_keycode;
#ifdef GDK_WINDOWING_WIN32
if (GDK_IS_WIN32_DISPLAY(dpy)) {
qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
switch (qemu_keycode) {
case 103: /* alt gr */
qemu_keycode = 56 | SCANCODE_GREY;
break;
}
return qemu_keycode;
}
#endif
if (gdk_keycode < 9) {
qemu_keycode = 0;
} else if (gdk_keycode < 97) {
qemu_keycode = gdk_keycode - 8;
#ifdef GDK_WINDOWING_X11
} else if (GDK_IS_X11_DISPLAY(dpy) && gdk_keycode < 158) {
if (s->has_evdev) {
qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
} else {
qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97);
}
#endif
} else if (gdk_keycode == 208) { /* Hiragana_Katakana */
qemu_keycode = 0x70;
} else if (gdk_keycode == 211) { /* backslash */
qemu_keycode = 0x73;
} else {
qemu_keycode = 0;
}
return qemu_keycode;
}
static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
{
VirtualConsole *vc = opaque;
GtkDisplayState *s = vc->s;
int gdk_keycode = key->hardware_keycode;
int qemu_keycode;
int i;
qemu_keycode = gd_map_keycode(s, gtk_widget_get_display(widget),
gdk_keycode);
trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode,
(key->type == GDK_KEY_PRESS) ? "down" : "up");
for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
if (qemu_keycode == modifier_keycode[i]) {
s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
}
}
qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
key->type == GDK_KEY_PRESS);
return TRUE;
}
static gboolean gd_event(GtkWidget *widget, GdkEvent *event, void *opaque)
{
if (event->type == GDK_MOTION_NOTIFY) {
return gd_motion_event(widget, &event->motion, opaque);
}
return FALSE;
}
/** Window Menu Actions **/
static void gd_menu_pause(GtkMenuItem *item, void *opaque)
{
GtkDisplayState *s = opaque;
if (s->external_pause_update) {
return;
}
if (runstate_is_running()) {
qmp_stop(NULL);
} else {
qmp_cont(NULL);
}
}
static void gd_menu_reset(GtkMenuItem *item, void *opaque)
{
qmp_system_reset(NULL);
}
static void gd_menu_powerdown(GtkMenuItem *item, void *opaque)
{
qmp_system_powerdown(NULL);
}
static void gd_menu_quit(GtkMenuItem *item, void *opaque)
{
qmp_quit(NULL);
}
static void gd_menu_switch_vc(GtkMenuItem *item, void *opaque)
{
GtkDisplayState *s = opaque;
VirtualConsole *vc = gd_vc_find_by_menu(s);
gint page;
gtk_release_modifiers(s);
if (vc) {
page = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook),
vc->tab_item);