gtk.c 51.7 KB
Newer Older
A
Anthony Liguori 已提交
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
/*
 * GTK UI
 *
 * Copyright IBM, Corp. 2012
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *
 * 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 <anthony@codemonkey.ws>
 * Copyright (C) 2009-2010 Daniel P. Berrange <dan@berrange.com>
 *
 * 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
 */

34 35 36
#define GETTEXT_PACKAGE "qemu"
#define LOCALEDIR "po"

37 38 39 40
#ifdef _WIN32
# define _WIN32_WINNT 0x0601 /* needed to get definition of MAPVK_VK_TO_VSC */
#endif

K
Kevin Wolf 已提交
41 42 43 44
#include "qemu-common.h"

#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
/* Work around an -Wstrict-prototypes warning in GTK headers */
45
#pragma GCC diagnostic push
K
Kevin Wolf 已提交
46 47
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
#endif
A
Anthony Liguori 已提交
48
#include <gtk/gtk.h>
K
Kevin Wolf 已提交
49
#ifdef CONFIG_PRAGMA_DIAGNOSTIC_AVAILABLE
50
#pragma GCC diagnostic pop
K
Kevin Wolf 已提交
51 52 53
#endif


A
Anthony Liguori 已提交
54
#include <gdk/gdkkeysyms.h>
55
#include <glib/gi18n.h>
56
#include <locale.h>
S
Stefan Weil 已提交
57
#if defined(CONFIG_VTE)
A
Anthony Liguori 已提交
58
#include <vte/vte.h>
S
Stefan Weil 已提交
59
#endif
A
Anthony Liguori 已提交
60 61
#include <math.h>

62
#include "trace.h"
A
Anthony Liguori 已提交
63
#include "ui/console.h"
64
#include "ui/input.h"
A
Anthony Liguori 已提交
65 66 67 68
#include "sysemu/sysemu.h"
#include "qmp-commands.h"
#include "x_keymap.h"
#include "keymaps.h"
69
#include "sysemu/char.h"
G
Gerd Hoffmann 已提交
70
#include "qom/object.h"
A
Anthony Liguori 已提交
71

72 73
#define MAX_VCS 10

S
Stefan Weil 已提交
74 75 76
#if !defined(CONFIG_VTE)
# define VTE_CHECK_VERSION(a, b, c) 0
#endif
77 78 79 80 81 82 83 84 85 86

/* 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

87 88 89 90
#if !GTK_CHECK_VERSION(2, 20, 0)
#define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
#endif

91 92 93 94 95 96
#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
97
#define GDK_KEY_q GDK_q
98 99 100
#define GDK_KEY_plus GDK_plus
#define GDK_KEY_minus GDK_minus
#endif
101

J
Jan Kiszka 已提交
102 103
#define HOTKEY_MODIFIERS        (GDK_CONTROL_MASK | GDK_MOD1_MASK)

104 105 106 107 108
static const int modifier_keycode[] = {
    /* shift, control, alt keys, meta keys, both left & right */
    0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
};

109 110 111 112 113 114 115 116 117 118 119 120
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;

S
Stefan Weil 已提交
121
#if defined(CONFIG_VTE)
G
Gerd Hoffmann 已提交
122
typedef struct VirtualVteConsole {
123 124
    GtkWidget *box;
    GtkWidget *scrollbar;
125
    GtkWidget *terminal;
A
Anthony Liguori 已提交
126
    CharDriverState *chr;
G
Gerd Hoffmann 已提交
127 128 129
} VirtualVteConsole;
#endif

130 131 132 133 134
typedef enum VirtualConsoleType {
    GD_VC_GFX,
    GD_VC_VTE,
} VirtualConsoleType;

G
Gerd Hoffmann 已提交
135
typedef struct VirtualConsole {
136
    GtkDisplayState *s;
137 138
    char *label;
    GtkWidget *window;
G
Gerd Hoffmann 已提交
139 140
    GtkWidget *menu_item;
    GtkWidget *tab_item;
141
    VirtualConsoleType type;
G
Gerd Hoffmann 已提交
142
    union {
143
        VirtualGfxConsole gfx;
G
Gerd Hoffmann 已提交
144 145
#if defined(CONFIG_VTE)
        VirtualVteConsole vte;
S
Stefan Weil 已提交
146
#endif
G
Gerd Hoffmann 已提交
147
    };
A
Anthony Liguori 已提交
148 149
} VirtualConsole;

150
struct GtkDisplayState {
A
Anthony Liguori 已提交
151 152 153 154
    GtkWidget *window;

    GtkWidget *menu_bar;

155 156
    GtkAccelGroup *accel_group;

157 158 159 160 161
    GtkWidget *machine_menu_item;
    GtkWidget *machine_menu;
    GtkWidget *pause_item;
    GtkWidget *reset_item;
    GtkWidget *powerdown_item;
A
Anthony Liguori 已提交
162 163 164 165
    GtkWidget *quit_item;

    GtkWidget *view_menu_item;
    GtkWidget *view_menu;
166 167 168 169 170
    GtkWidget *full_screen_item;
    GtkWidget *zoom_in_item;
    GtkWidget *zoom_out_item;
    GtkWidget *zoom_fixed_item;
    GtkWidget *zoom_fit_item;
171 172
    GtkWidget *grab_item;
    GtkWidget *grab_on_hover_item;
A
Anthony Liguori 已提交
173

174 175 176
    int nb_vcs;
    VirtualConsole vc[MAX_VCS];

A
Anthony Liguori 已提交
177
    GtkWidget *show_tabs_item;
178
    GtkWidget *untabify_item;
A
Anthony Liguori 已提交
179 180 181 182

    GtkWidget *vbox;
    GtkWidget *notebook;
    int button_mask;
183
    gboolean last_set;
A
Anthony Liguori 已提交
184 185
    int last_x;
    int last_y;
186 187
    int grab_x_root;
    int grab_y_root;
A
Anthony Liguori 已提交
188

189
    gboolean full_screen;
A
Anthony Liguori 已提交
190 191 192

    GdkCursor *null_cursor;
    Notifier mouse_mode_notifier;
193
    gboolean free_scale;
194 195

    bool external_pause_update;
196 197

    bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
198
};
A
Anthony Liguori 已提交
199 200 201 202 203

static GtkDisplayState *global_state;

/** Utility Functions **/

G
Gerd Hoffmann 已提交
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
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;
}

234 235 236 237 238 239 240 241
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);
}

242 243 244 245 246 247 248 249 250 251
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));
}

252
static void gd_update_cursor(VirtualConsole *vc)
A
Anthony Liguori 已提交
253
{
254
    GtkDisplayState *s = vc->s;
A
Anthony Liguori 已提交
255 256
    GdkWindow *window;

257 258 259
    if (vc->type != GD_VC_GFX) {
        return;
    }
A
Anthony Liguori 已提交
260

261 262
    window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
    if (s->full_screen || qemu_input_is_absolute() || gd_is_grab_active(s)) {
A
Anthony Liguori 已提交
263 264 265 266 267 268 269 270 271 272
        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 *title;
273
    const char *grab = "";
274
    bool is_paused = !runstate_is_running();
275 276

    if (gd_is_grab_active(s)) {
277
        grab = _(" - Press Ctrl+Alt+G to release grab");
278
    }
A
Anthony Liguori 已提交
279

280
    if (is_paused) {
281
        status = _(" [Paused]");
A
Anthony Liguori 已提交
282
    }
283 284 285 286
    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;
A
Anthony Liguori 已提交
287 288

    if (qemu_name) {
289
        title = g_strdup_printf("QEMU (%s)%s%s", qemu_name, status, grab);
A
Anthony Liguori 已提交
290
    } else {
291
        title = g_strdup_printf("QEMU%s%s", status, grab);
A
Anthony Liguori 已提交
292 293 294 295 296 297 298
    }

    gtk_window_set_title(GTK_WINDOW(s->window), title);

    g_free(title);
}

299
static void gd_update_windowsize(VirtualConsole *vc)
G
Gerd Hoffmann 已提交
300
{
301
    GtkDisplayState *s = vc->s;
G
Gerd Hoffmann 已提交
302
    double sx, sy;
303

G
Gerd Hoffmann 已提交
304 305 306
    if (vc->type != GD_VC_GFX || s->full_screen) {
        return;
    }
G
Gerd Hoffmann 已提交
307

G
Gerd Hoffmann 已提交
308 309 310 311 312 313
    if (s->free_scale) {
        sx = 1.0;
        sy = 1.0;
    } else {
        sx = vc->gfx.scale_x;
        sy = vc->gfx.scale_y;
G
Gerd Hoffmann 已提交
314
    }
G
Gerd Hoffmann 已提交
315 316 317 318
    gtk_widget_set_size_request(vc->gfx.drawing_area,
                                surface_width(vc->gfx.ds) * sx,
                                surface_height(vc->gfx.ds) * sy);
    gtk_window_resize(GTK_WINDOW(s->window), 320, 240);
G
Gerd Hoffmann 已提交
319 320
}

321
static void gd_update_full_redraw(VirtualConsole *vc)
G
Gerd Hoffmann 已提交
322
{
323
    GtkWidget *area = vc->gfx.drawing_area;
G
Gerd Hoffmann 已提交
324
    int ww, wh;
325 326
    gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
    gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
G
Gerd Hoffmann 已提交
327 328
}

329 330
static void gtk_release_modifiers(GtkDisplayState *s)
{
331
    VirtualConsole *vc = gd_vc_find_current(s);
332 333
    int i, keycode;

334
    if (vc->type != GD_VC_GFX) {
335 336 337 338 339 340 341
        return;
    }
    for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
        keycode = modifier_keycode[i];
        if (!s->modifier_pressed[i]) {
            continue;
        }
342
        qemu_input_event_send_key_number(vc->gfx.dcl.con, keycode, false);
343 344 345 346
        s->modifier_pressed[i] = false;
    }
}

A
Anthony Liguori 已提交
347 348
/** DisplayState Callbacks **/

349
static void gd_update(DisplayChangeListener *dcl,
350
                      int x, int y, int w, int h)
A
Anthony Liguori 已提交
351
{
352
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
A
Anthony Liguori 已提交
353
    int x1, x2, y1, y2;
354 355 356
    int mx, my;
    int fbw, fbh;
    int ww, wh;
A
Anthony Liguori 已提交
357

G
Gerd Hoffmann 已提交
358
    trace_gd_update(vc->label, x, y, w, h);
A
Anthony Liguori 已提交
359

360 361 362
    if (vc->gfx.convert) {
        pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
                               NULL, vc->gfx.convert,
363 364 365
                               x, y, 0, 0, x, y, w, h);
    }

366 367
    x1 = floor(x * vc->gfx.scale_x);
    y1 = floor(y * vc->gfx.scale_y);
A
Anthony Liguori 已提交
368

369 370
    x2 = ceil(x * vc->gfx.scale_x + w * vc->gfx.scale_x);
    y2 = ceil(y * vc->gfx.scale_y + h * vc->gfx.scale_y);
A
Anthony Liguori 已提交
371

372 373
    fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
    fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
374

375 376
    gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
                          &ww, &wh);
377 378 379 380 381 382 383 384 385

    mx = my = 0;
    if (ww > fbw) {
        mx = (ww - fbw) / 2;
    }
    if (wh > fbh) {
        my = (wh - fbh) / 2;
    }

386 387
    gtk_widget_queue_draw_area(vc->gfx.drawing_area,
                               mx + x1, my + y1, (x2 - x1), (y2 - y1));
A
Anthony Liguori 已提交
388 389
}

390
static void gd_refresh(DisplayChangeListener *dcl)
A
Anthony Liguori 已提交
391
{
392
    graphic_hw_update(dcl->con);
A
Anthony Liguori 已提交
393 394
}

395 396 397 398
#if GTK_CHECK_VERSION(3, 0, 0)
static void gd_mouse_set(DisplayChangeListener *dcl,
                         int x, int y, int visible)
{
399
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
400 401 402 403
    GdkDisplay *dpy;
    GdkDeviceManager *mgr;
    gint x_root, y_root;

C
Cole Robinson 已提交
404 405 406 407
    if (qemu_input_is_absolute()) {
        return;
    }

408
    dpy = gtk_widget_get_display(vc->gfx.drawing_area);
409
    mgr = gdk_display_get_device_manager(dpy);
410
    gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
411 412
                               x, y, &x_root, &y_root);
    gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
413
                    gtk_widget_get_screen(vc->gfx.drawing_area),
C
Cole Robinson 已提交
414
                    x_root, y_root);
415 416
}
#else
G
Gerd Hoffmann 已提交
417 418 419
static void gd_mouse_set(DisplayChangeListener *dcl,
                         int x, int y, int visible)
{
420
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
G
Gerd Hoffmann 已提交
421 422
    gint x_root, y_root;

C
Cole Robinson 已提交
423 424 425 426
    if (qemu_input_is_absolute()) {
        return;
    }

427
    gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
G
Gerd Hoffmann 已提交
428
                               x, y, &x_root, &y_root);
429 430
    gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
                             gtk_widget_get_screen(vc->gfx.drawing_area),
G
Gerd Hoffmann 已提交
431 432
                             x_root, y_root);
}
433
#endif
G
Gerd Hoffmann 已提交
434 435 436 437

static void gd_cursor_define(DisplayChangeListener *dcl,
                             QEMUCursor *c)
{
438
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
G
Gerd Hoffmann 已提交
439 440 441 442 443 444 445
    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);
446 447 448 449
    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
Gerd Hoffmann 已提交
450
    g_object_unref(pixbuf);
451
#if !GTK_CHECK_VERSION(3, 0, 0)
452
    gdk_cursor_unref(cursor);
453 454 455
#else
    g_object_unref(cursor);
#endif
G
Gerd Hoffmann 已提交
456 457
}

458 459
static void gd_switch(DisplayChangeListener *dcl,
                      DisplaySurface *surface)
A
Anthony Liguori 已提交
460
{
461
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
G
Gerd Hoffmann 已提交
462
    bool resized = true;
A
Anthony Liguori 已提交
463

G
Gerd Hoffmann 已提交
464
    trace_gd_switch(vc->label, surface_width(surface), surface_height(surface));
A
Anthony Liguori 已提交
465

466 467
    if (vc->gfx.surface) {
        cairo_surface_destroy(vc->gfx.surface);
A
Anthony Liguori 已提交
468 469
    }

470 471 472
    if (vc->gfx.ds &&
        surface_width(vc->gfx.ds) == surface_width(surface) &&
        surface_height(vc->gfx.ds) == surface_height(surface)) {
G
Gerd Hoffmann 已提交
473 474
        resized = false;
    }
475
    vc->gfx.ds = surface;
A
Anthony Liguori 已提交
476

477 478 479
    if (vc->gfx.convert) {
        pixman_image_unref(vc->gfx.convert);
        vc->gfx.convert = NULL;
480
    }
A
Anthony Liguori 已提交
481

482 483 484 485 486 487 488
    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.
         */
489
        vc->gfx.surface = cairo_image_surface_create_for_data
490 491 492 493 494 495 496
            (surface_data(surface),
             CAIRO_FORMAT_RGB24,
             surface_width(surface),
             surface_height(surface),
             surface_stride(surface));
    } else {
        /* Must convert surface, use pixman to do it. */
497 498 499 500 501 502
        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),
503
             CAIRO_FORMAT_RGB24,
504 505 506 507 508
             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,
509
                               0, 0, 0, 0, 0, 0,
510 511
                               pixman_image_get_width(vc->gfx.convert),
                               pixman_image_get_height(vc->gfx.convert));
512
    }
513

G
Gerd Hoffmann 已提交
514
    if (resized) {
515
        gd_update_windowsize(vc);
G
Gerd Hoffmann 已提交
516
    } else {
517
        gd_update_full_redraw(vc);
518
    }
A
Anthony Liguori 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531
}

/** 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)
{
532 533 534 535 536 537 538 539
    GtkDisplayState *s;

    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);
    }
540
    gd_update_cursor(gd_vc_find_current(s));
A
Anthony Liguori 已提交
541 542 543 544 545 546 547 548
}

/** GTK Events **/

static gboolean gd_window_close(GtkWidget *widget, GdkEvent *event,
                                void *opaque)
{
    GtkDisplayState *s = opaque;
549
    int i;
A
Anthony Liguori 已提交
550 551

    if (!no_quit) {
552 553 554 555 556 557
        for (i = 0; i < s->nb_vcs; i++) {
            if (s->vc[i].type != GD_VC_GFX) {
                continue;
            }
            unregister_displaychangelistener(&s->vc[i].gfx.dcl);
        }
A
Anthony Liguori 已提交
558 559 560 561 562 563 564 565 566
        qmp_quit(NULL);
        return FALSE;
    }

    return TRUE;
}

static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
{
567 568
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
569
    int mx, my;
A
Anthony Liguori 已提交
570 571 572
    int ww, wh;
    int fbw, fbh;

573 574 575 576
    if (!gtk_widget_get_realized(widget)) {
        return FALSE;
    }

577 578
    fbw = surface_width(vc->gfx.ds);
    fbh = surface_height(vc->gfx.ds);
A
Anthony Liguori 已提交
579 580 581

    gdk_drawable_get_size(gtk_widget_get_window(widget), &ww, &wh);

582
    if (s->full_screen) {
583 584
        vc->gfx.scale_x = (double)ww / fbw;
        vc->gfx.scale_y = (double)wh / fbh;
585 586 587 588 589 590
    } else if (s->free_scale) {
        double sx, sy;

        sx = (double)ww / fbw;
        sy = (double)wh / fbh;

591
        vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
A
Anthony Liguori 已提交
592 593
    }

594 595
    fbw *= vc->gfx.scale_x;
    fbh *= vc->gfx.scale_y;
596

597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
    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);

616 617 618
    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);
A
Anthony Liguori 已提交
619 620 621 622 623
    cairo_paint(cr);

    return TRUE;
}

624
#if !GTK_CHECK_VERSION(3, 0, 0)
A
Anthony Liguori 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
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;
}
645
#endif
A
Anthony Liguori 已提交
646 647 648 649

static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
                                void *opaque)
{
650 651
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
A
Anthony Liguori 已提交
652
    int x, y;
653 654 655 656
    int mx, my;
    int fbh, fbw;
    int ww, wh;

657 658
    fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
    fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
A
Anthony Liguori 已提交
659

660 661
    gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
                          &ww, &wh);
662 663 664 665 666 667 668 669 670

    mx = my = 0;
    if (ww > fbw) {
        mx = (ww - fbw) / 2;
    }
    if (wh > fbh) {
        my = (wh - fbh) / 2;
    }

671 672
    x = (motion->x - mx) / vc->gfx.scale_x;
    y = (motion->y - my) / vc->gfx.scale_y;
673

674
    if (qemu_input_is_absolute()) {
675
        if (x < 0 || y < 0 ||
676 677
            x >= surface_width(vc->gfx.ds) ||
            y >= surface_height(vc->gfx.ds)) {
678 679
            return TRUE;
        }
680 681 682 683
        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));
684
        qemu_input_event_sync();
685
    } else if (s->last_set && gd_is_grab_active(s)) {
686 687
        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);
688
        qemu_input_event_sync();
A
Anthony Liguori 已提交
689 690 691
    }
    s->last_x = x;
    s->last_y = y;
692
    s->last_set = TRUE;
A
Anthony Liguori 已提交
693

694
    if (!qemu_input_is_absolute() && gd_is_grab_active(s)) {
695
        GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
        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) {
720 721 722 723 724
#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);
725
            gdk_display_warp_pointer(display, screen, x, y);
726
#endif
727
            s->last_set = FALSE;
728 729 730
            return FALSE;
        }
    }
A
Anthony Liguori 已提交
731 732 733 734 735 736
    return TRUE;
}

static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
                                void *opaque)
{
737 738
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
739
    InputButton btn;
A
Anthony Liguori 已提交
740

741 742 743 744 745 746 747 748
    /* 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() && !gd_is_grab_active(s)) {
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                       TRUE);
        return TRUE;
    }

A
Anthony Liguori 已提交
749
    if (button->button == 1) {
750
        btn = INPUT_BUTTON_LEFT;
A
Anthony Liguori 已提交
751
    } else if (button->button == 2) {
752
        btn = INPUT_BUTTON_MIDDLE;
A
Anthony Liguori 已提交
753
    } else if (button->button == 3) {
754
        btn = INPUT_BUTTON_RIGHT;
A
Anthony Liguori 已提交
755
    } else {
756
        return TRUE;
A
Anthony Liguori 已提交
757 758
    }

759 760
    qemu_input_queue_btn(vc->gfx.dcl.con, btn,
                         button->type == GDK_BUTTON_PRESS);
761
    qemu_input_event_sync();
A
Anthony Liguori 已提交
762 763 764
    return TRUE;
}

J
Jan Kiszka 已提交
765 766 767
static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
                                void *opaque)
{
768
    VirtualConsole *vc = opaque;
J
Jan Kiszka 已提交
769 770 771 772 773 774 775 776 777 778
    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;
    }

779
    qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
J
Jan Kiszka 已提交
780
    qemu_input_event_sync();
781
    qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
J
Jan Kiszka 已提交
782 783 784 785
    qemu_input_event_sync();
    return TRUE;
}

A
Anthony Liguori 已提交
786 787
static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
{
788 789
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
790
    int gdk_keycode = key->hardware_keycode;
791
    int i;
A
Anthony Liguori 已提交
792

793 794 795 796 797 798 799 800 801
#ifdef _WIN32
    UINT qemu_keycode = MapVirtualKey(gdk_keycode, MAPVK_VK_TO_VSC);
    switch (qemu_keycode) {
    case 103:   /* alt gr */
        qemu_keycode = 56 | SCANCODE_GREY;
        break;
    }
#else
    int qemu_keycode;
A
Anthony Liguori 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815

    if (gdk_keycode < 9) {
        qemu_keycode = 0;
    } else if (gdk_keycode < 97) {
        qemu_keycode = gdk_keycode - 8;
    } else if (gdk_keycode < 158) {
        qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
    } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
        qemu_keycode = 0x70;
    } else if (gdk_keycode == 211) { /* backslash */
        qemu_keycode = 0x73;
    } else {
        qemu_keycode = 0;
    }
816
#endif
A
Anthony Liguori 已提交
817

G
Gerd Hoffmann 已提交
818
    trace_gd_key_event(vc->label, gdk_keycode, qemu_keycode,
819
                       (key->type == GDK_KEY_PRESS) ? "down" : "up");
A
Anthony Liguori 已提交
820

821 822 823 824 825 826
    for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
        if (qemu_keycode == modifier_keycode[i]) {
            s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
        }
    }

827
    qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
828
                                     key->type == GDK_KEY_PRESS);
A
Anthony Liguori 已提交
829 830 831 832

    return TRUE;
}

833 834 835 836 837 838 839 840
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;
}

A
Anthony Liguori 已提交
841 842
/** Window Menu Actions **/

843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
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);
}

A
Anthony Liguori 已提交
867 868 869 870 871 872 873 874
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;
875
    VirtualConsole *vc = gd_vc_find_by_menu(s);
876
    gint page;
A
Anthony Liguori 已提交
877

878 879
    gtk_release_modifiers(s);
    if (vc) {
880
        page = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook),
881
                                     vc->tab_item);
882
        gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), page);
A
Anthony Liguori 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895 896
    }
}

static void gd_menu_show_tabs(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;

    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->show_tabs_item))) {
        gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), TRUE);
    } else {
        gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
    }
}

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
static gboolean gd_tab_window_close(GtkWidget *widget, GdkEvent *event,
                                    void *opaque)
{
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;

    gtk_widget_set_sensitive(vc->menu_item, true);
    gtk_widget_reparent(vc->tab_item, s->notebook);
    gtk_notebook_set_tab_label_text(GTK_NOTEBOOK(s->notebook),
                                    vc->tab_item, vc->label);
    gtk_widget_destroy(vc->window);
    vc->window = NULL;
    return TRUE;
}

static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
    VirtualConsole *vc = gd_vc_find_current(s);
    char *title;

    if (vc->type == GD_VC_GFX) {
        /* temporary: needs more work to get grabs etc correct */
        return;
    }
    if (!vc->window) {
        gtk_widget_set_sensitive(vc->menu_item, false);
        vc->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_widget_reparent(vc->tab_item, vc->window);

        if (qemu_name) {
            title = g_strdup_printf("QEMU (%s): %s", qemu_name, vc->label);
        } else {
            title = g_strdup_printf("QEMU: %s", vc->label);
        }
        gtk_window_set_title(GTK_WINDOW(vc->window), title);
        g_free(title);

        g_signal_connect(vc->window, "delete-event",
                         G_CALLBACK(gd_tab_window_close), vc);
        gtk_widget_show_all(vc->window);
    }
}

941 942 943
static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
944
    VirtualConsole *vc = gd_vc_find_current(s);
945

946
    if (!s->full_screen) {
947 948
        gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
        gtk_widget_set_size_request(s->menu_bar, 0, 0);
949 950 951 952
        if (vc->type == GD_VC_GFX) {
            gtk_widget_set_size_request(vc->gfx.drawing_area, -1, -1);
            gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                           TRUE);
953
        }
954
        gtk_window_fullscreen(GTK_WINDOW(s->window));
955 956 957 958 959 960
        s->full_screen = TRUE;
    } else {
        gtk_window_unfullscreen(GTK_WINDOW(s->window));
        gd_menu_show_tabs(GTK_MENU_ITEM(s->show_tabs_item), s);
        gtk_widget_set_size_request(s->menu_bar, -1, -1);
        s->full_screen = FALSE;
961 962 963 964 965 966 967 968 969
        if (vc->type == GD_VC_GFX) {
            vc->gfx.scale_x = 1.0;
            vc->gfx.scale_y = 1.0;
            gtk_widget_set_size_request(vc->gfx.drawing_area,
                                        surface_width(vc->gfx.ds),
                                        surface_height(vc->gfx.ds));
            gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                           FALSE);
        }
970 971
    }

972
    gd_update_cursor(vc);
973 974 975 976 977
}

static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
978
    VirtualConsole *vc = gd_vc_find_current(s);
979 980 981 982

    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
                                   FALSE);

983 984
    vc->gfx.scale_x += .25;
    vc->gfx.scale_y += .25;
985

986
    gd_update_windowsize(vc);
987 988 989 990 991
}

static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
992
    VirtualConsole *vc = gd_vc_find_current(s);
993 994 995 996

    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item),
                                   FALSE);

997 998
    vc->gfx.scale_x -= .25;
    vc->gfx.scale_y -= .25;
999

1000 1001
    vc->gfx.scale_x = MAX(vc->gfx.scale_x, .25);
    vc->gfx.scale_y = MAX(vc->gfx.scale_y, .25);
1002

1003
    gd_update_windowsize(vc);
1004 1005 1006 1007 1008
}

static void gd_menu_zoom_fixed(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1009
    VirtualConsole *vc = gd_vc_find_current(s);
1010

1011 1012
    vc->gfx.scale_x = 1.0;
    vc->gfx.scale_y = 1.0;
1013

1014
    gd_update_windowsize(vc);
1015 1016 1017 1018 1019
}

static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1020
    VirtualConsole *vc = gd_vc_find_current(s);
1021 1022 1023 1024 1025

    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
        s->free_scale = TRUE;
    } else {
        s->free_scale = FALSE;
1026 1027 1028
        vc->gfx.scale_x = 1.0;
        vc->gfx.scale_y = 1.0;
        gd_update_windowsize(vc);
1029 1030
    }

1031
    gd_update_full_redraw(vc);
1032 1033
}

1034
static void gd_grab_keyboard(VirtualConsole *vc)
1035
{
1036
#if GTK_CHECK_VERSION(3, 0, 0)
1037
    GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1038 1039 1040 1041 1042 1043 1044 1045
    GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
    GList *devices = gdk_device_manager_list_devices(mgr,
                                                     GDK_DEVICE_TYPE_MASTER);
    GList *tmp = devices;
    while (tmp) {
        GdkDevice *dev = tmp->data;
        if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
            gdk_device_grab(dev,
1046
                            gtk_widget_get_window(vc->gfx.drawing_area),
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
                            GDK_OWNERSHIP_NONE,
                            FALSE,
                            GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
                            NULL,
                            GDK_CURRENT_TIME);
        }
        tmp = tmp->next;
    }
    g_list_free(devices);
#else
1057
    gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1058 1059
                      FALSE,
                      GDK_CURRENT_TIME);
1060
#endif
G
Gerd Hoffmann 已提交
1061
    trace_gd_grab(vc->label, "kbd", true);
1062 1063
}

1064
static void gd_ungrab_keyboard(VirtualConsole *vc)
1065
{
1066
#if GTK_CHECK_VERSION(3, 0, 0)
1067
    GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
    GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
    GList *devices = gdk_device_manager_list_devices(mgr,
                                                     GDK_DEVICE_TYPE_MASTER);
    GList *tmp = devices;
    while (tmp) {
        GdkDevice *dev = tmp->data;
        if (gdk_device_get_source(dev) == GDK_SOURCE_KEYBOARD) {
            gdk_device_ungrab(dev,
                              GDK_CURRENT_TIME);
        }
        tmp = tmp->next;
    }
    g_list_free(devices);
#else
1082
    gdk_keyboard_ungrab(GDK_CURRENT_TIME);
1083
#endif
G
Gerd Hoffmann 已提交
1084
    trace_gd_grab(vc->label, "kbd", false);
1085 1086
}

1087
static void gd_grab_pointer(VirtualConsole *vc)
1088
{
1089
    GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1090
#if GTK_CHECK_VERSION(3, 0, 0)
1091 1092 1093 1094 1095 1096 1097 1098
    GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
    GList *devices = gdk_device_manager_list_devices(mgr,
                                                     GDK_DEVICE_TYPE_MASTER);
    GList *tmp = devices;
    while (tmp) {
        GdkDevice *dev = tmp->data;
        if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
            gdk_device_grab(dev,
1099
                            gtk_widget_get_window(vc->gfx.drawing_area),
1100 1101 1102 1103 1104 1105 1106 1107
                            GDK_OWNERSHIP_NONE,
                            FALSE, /* All events to come to our
                                      window directly */
                            GDK_POINTER_MOTION_MASK |
                            GDK_BUTTON_PRESS_MASK |
                            GDK_BUTTON_RELEASE_MASK |
                            GDK_BUTTON_MOTION_MASK |
                            GDK_SCROLL_MASK,
1108
                            vc->s->null_cursor,
1109 1110 1111 1112 1113
                            GDK_CURRENT_TIME);
        }
        tmp = tmp->next;
    }
    g_list_free(devices);
1114
    gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr),
1115
                            NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1116
#else
1117
    gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1118 1119 1120 1121 1122 1123 1124
                     FALSE, /* All events to come to our window directly */
                     GDK_POINTER_MOTION_MASK |
                     GDK_BUTTON_PRESS_MASK |
                     GDK_BUTTON_RELEASE_MASK |
                     GDK_BUTTON_MOTION_MASK |
                     GDK_SCROLL_MASK,
                     NULL, /* Allow cursor to move over entire desktop */
1125
                     vc->s->null_cursor,
1126
                     GDK_CURRENT_TIME);
1127
    gdk_display_get_pointer(display, NULL,
1128
                            &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
1129
#endif
G
Gerd Hoffmann 已提交
1130
    trace_gd_grab(vc->label, "ptr", true);
1131 1132
}

1133
static void gd_ungrab_pointer(VirtualConsole *vc)
1134
{
1135
    GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1136
#if GTK_CHECK_VERSION(3, 0, 0)
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
    GdkDeviceManager *mgr = gdk_display_get_device_manager(display);
    GList *devices = gdk_device_manager_list_devices(mgr,
                                                     GDK_DEVICE_TYPE_MASTER);
    GList *tmp = devices;
    while (tmp) {
        GdkDevice *dev = tmp->data;
        if (gdk_device_get_source(dev) == GDK_SOURCE_MOUSE) {
            gdk_device_ungrab(dev,
                              GDK_CURRENT_TIME);
        }
        tmp = tmp->next;
    }
    g_list_free(devices);
1150
    gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
1151 1152
                    gtk_widget_get_screen(vc->gfx.drawing_area),
                    vc->s->grab_x_root, vc->s->grab_y_root);
1153 1154
#else
    gdk_pointer_ungrab(GDK_CURRENT_TIME);
1155
    gdk_display_warp_pointer(display,
1156 1157
                             gtk_widget_get_screen(vc->gfx.drawing_area),
                             vc->s->grab_x_root, vc->s->grab_y_root);
1158
#endif
G
Gerd Hoffmann 已提交
1159
    trace_gd_grab(vc->label, "ptr", false);
1160 1161
}

1162 1163 1164
static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1165
    VirtualConsole *vc = gd_vc_find_current(s);
1166 1167

    if (gd_is_grab_active(s)) {
1168 1169
        gd_grab_keyboard(vc);
        gd_grab_pointer(vc);
1170
    } else {
1171 1172
        gd_ungrab_keyboard(vc);
        gd_ungrab_pointer(vc);
1173 1174 1175
    }

    gd_update_caption(s);
1176
    gd_update_cursor(vc);
1177 1178
}

A
Anthony Liguori 已提交
1179 1180 1181 1182
static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
                           gpointer data)
{
    GtkDisplayState *s = data;
1183
    VirtualConsole *vc;
1184
    gboolean on_vga;
A
Anthony Liguori 已提交
1185 1186 1187 1188 1189

    if (!gtk_widget_get_realized(s->notebook)) {
        return;
    }

1190 1191 1192 1193 1194 1195
    vc = gd_vc_find_by_page(s, arg2);
    if (!vc) {
        return;
    }
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
                                   TRUE);
1196

1197
    on_vga = (vc->type == GD_VC_GFX);
1198 1199 1200
    if (!on_vga) {
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                       FALSE);
1201 1202 1203
    } else if (s->full_screen) {
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                       TRUE);
1204 1205 1206
    }
    gtk_widget_set_sensitive(s->grab_item, on_vga);

1207
    gd_update_cursor(vc);
A
Anthony Liguori 已提交
1208 1209
}

1210 1211
static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
                               gpointer opaque)
1212
{
1213 1214
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
1215 1216

    if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
1217
        gd_grab_keyboard(vc);
1218 1219 1220 1221 1222
    }

    return TRUE;
}

1223 1224
static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
                               gpointer opaque)
1225
{
1226 1227
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
1228 1229

    if (!gd_is_grab_active(s) && gd_grab_on_hover(s)) {
1230
        gd_ungrab_keyboard(vc);
1231 1232 1233 1234 1235
    }

    return TRUE;
}

1236
static gboolean gd_focus_out_event(GtkWidget *widget,
1237
                                   GdkEventCrossing *crossing, gpointer opaque)
1238
{
1239 1240
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
1241 1242 1243 1244 1245 1246

    gtk_release_modifiers(s);

    return TRUE;
}

1247 1248
/** Virtual Console Callbacks **/

1249
static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1250
                               int idx, GSList *group, GtkWidget *view_menu)
1251 1252 1253 1254 1255
{
    char path[32];

    snprintf(path, sizeof(path), "<QEMU>/View/VC%d", idx);

1256
    vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
    group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(vc->menu_item));
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(vc->menu_item), path);
    gtk_accel_map_add_entry(path, GDK_KEY_1 + idx, HOTKEY_MODIFIERS);

    g_signal_connect(vc->menu_item, "activate",
                     G_CALLBACK(gd_menu_switch_vc), s);
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), vc->menu_item);

    return group;
}

1268
#if defined(CONFIG_VTE)
1269 1270 1271 1272 1273 1274
static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)
{
    VirtualConsole *vc = opaque;

    if (gtk_adjustment_get_upper(adjustment) >
        gtk_adjustment_get_page_size(adjustment)) {
G
Gerd Hoffmann 已提交
1275
        gtk_widget_show(vc->vte.scrollbar);
1276
    } else {
G
Gerd Hoffmann 已提交
1277
        gtk_widget_hide(vc->vte.scrollbar);
1278 1279 1280
    }
}

1281 1282 1283 1284
static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
{
    VirtualConsole *vc = chr->opaque;

G
Gerd Hoffmann 已提交
1285
    vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
C
Cole Robinson 已提交
1286
    return len;
1287 1288 1289 1290 1291
}

static int nb_vcs;
static CharDriverState *vcs[MAX_VCS];

G
Gerd Hoffmann 已提交
1292
static CharDriverState *gd_vc_handler(ChardevVC *unused)
1293 1294 1295 1296 1297
{
    CharDriverState *chr;

    chr = g_malloc0(sizeof(*chr));
    chr->chr_write = gd_vc_chr_write;
1298 1299
    /* defer OPENED events until our vc is fully initialized */
    chr->explicit_be_open = true;
1300 1301 1302 1303 1304 1305

    vcs[nb_vcs++] = chr;

    return chr;
}

C
Cole Robinson 已提交
1306 1307
static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
                         gpointer user_data)
1308
{
C
Cole Robinson 已提交
1309
    VirtualConsole *vc = user_data;
1310

G
Gerd Hoffmann 已提交
1311
    qemu_chr_be_write(vc->vte.chr, (uint8_t  *)text, (unsigned int)size);
1312 1313 1314
    return TRUE;
}

1315 1316
static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
                              CharDriverState *chr, int idx,
1317
                              GSList *group, GtkWidget *view_menu)
1318 1319
{
    char buffer[32];
1320 1321 1322
    GtkWidget *box;
    GtkWidget *scrollbar;
    GtkAdjustment *vadjustment;
1323

1324
    vc->s = s;
1325
    vc->vte.chr = chr;
1326

1327
    snprintf(buffer, sizeof(buffer), "vc%d", idx);
1328 1329 1330
    vc->label = g_strdup_printf("%s", vc->vte.chr->label
                                ? vc->vte.chr->label : buffer);
    group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1331

G
Gerd Hoffmann 已提交
1332 1333
    vc->vte.terminal = vte_terminal_new();
    g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1334

G
Gerd Hoffmann 已提交
1335 1336
    vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
    vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal), 80, 25);
1337

1338
#if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
G
Gerd Hoffmann 已提交
1339 1340
    vadjustment = gtk_scrollable_get_vadjustment
        (GTK_SCROLLABLE(vc->vte.terminal));
1341
#else
G
Gerd Hoffmann 已提交
1342
    vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
#endif

#if GTK_CHECK_VERSION(3, 0, 0)
    box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
    scrollbar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, vadjustment);
#else
    box = gtk_hbox_new(false, 2);
    scrollbar = gtk_vscrollbar_new(vadjustment);
#endif

G
Gerd Hoffmann 已提交
1353
    gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1354 1355
    gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);

G
Gerd Hoffmann 已提交
1356 1357 1358
    vc->vte.chr->opaque = vc;
    vc->vte.box = box;
    vc->vte.scrollbar = scrollbar;
1359 1360 1361

    g_signal_connect(vadjustment, "changed",
                     G_CALLBACK(gd_vc_adjustment_changed), vc);
1362

1363
    vc->type = GD_VC_VTE;
G
Gerd Hoffmann 已提交
1364 1365
    vc->tab_item = box;
    gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1366
                             gtk_label_new(vc->label));
1367

G
Gerd Hoffmann 已提交
1368 1369 1370
    qemu_chr_be_generic_open(vc->vte.chr);
    if (vc->vte.chr->init) {
        vc->vte.chr->init(vc->vte.chr);
1371 1372 1373
    }

    return group;
A
Anthony Liguori 已提交
1374 1375
}

1376
static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1377 1378 1379 1380 1381
                        GtkWidget *view_menu)
{
    int i;

    for (i = 0; i < nb_vcs; i++) {
1382 1383
        VirtualConsole *vc = &s->vc[s->nb_vcs];
        group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1384 1385 1386 1387 1388
        s->nb_vcs++;
    }
}
#endif /* CONFIG_VTE */

A
Anthony Liguori 已提交
1389 1390
/** Window Creation **/

1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
static void gd_connect_vc_gfx_signals(VirtualConsole *vc)
{
#if GTK_CHECK_VERSION(3, 0, 0)
    g_signal_connect(vc->gfx.drawing_area, "draw",
                     G_CALLBACK(gd_draw_event), vc);
#else
    g_signal_connect(vc->gfx.drawing_area, "expose-event",
                     G_CALLBACK(gd_expose_event), vc);
#endif
    g_signal_connect(vc->gfx.drawing_area, "event",
                     G_CALLBACK(gd_event), vc);
    g_signal_connect(vc->gfx.drawing_area, "button-press-event",
                     G_CALLBACK(gd_button_event), vc);
    g_signal_connect(vc->gfx.drawing_area, "button-release-event",
                     G_CALLBACK(gd_button_event), vc);
    g_signal_connect(vc->gfx.drawing_area, "scroll-event",
                     G_CALLBACK(gd_scroll_event), vc);
    g_signal_connect(vc->gfx.drawing_area, "key-press-event",
                     G_CALLBACK(gd_key_event), vc);
    g_signal_connect(vc->gfx.drawing_area, "key-release-event",
                     G_CALLBACK(gd_key_event), vc);

    g_signal_connect(vc->gfx.drawing_area, "enter-notify-event",
                     G_CALLBACK(gd_enter_event), vc);
    g_signal_connect(vc->gfx.drawing_area, "leave-notify-event",
                     G_CALLBACK(gd_leave_event), vc);
    g_signal_connect(vc->gfx.drawing_area, "focus-out-event",
                     G_CALLBACK(gd_focus_out_event), vc);
}

A
Anthony Liguori 已提交
1421 1422 1423 1424
static void gd_connect_signals(GtkDisplayState *s)
{
    g_signal_connect(s->show_tabs_item, "activate",
                     G_CALLBACK(gd_menu_show_tabs), s);
1425 1426
    g_signal_connect(s->untabify_item, "activate",
                     G_CALLBACK(gd_menu_untabify), s);
A
Anthony Liguori 已提交
1427 1428 1429 1430

    g_signal_connect(s->window, "delete-event",
                     G_CALLBACK(gd_window_close), s);

1431 1432 1433 1434 1435 1436
    g_signal_connect(s->pause_item, "activate",
                     G_CALLBACK(gd_menu_pause), s);
    g_signal_connect(s->reset_item, "activate",
                     G_CALLBACK(gd_menu_reset), s);
    g_signal_connect(s->powerdown_item, "activate",
                     G_CALLBACK(gd_menu_powerdown), s);
A
Anthony Liguori 已提交
1437 1438
    g_signal_connect(s->quit_item, "activate",
                     G_CALLBACK(gd_menu_quit), s);
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
    g_signal_connect(s->full_screen_item, "activate",
                     G_CALLBACK(gd_menu_full_screen), s);
    g_signal_connect(s->zoom_in_item, "activate",
                     G_CALLBACK(gd_menu_zoom_in), s);
    g_signal_connect(s->zoom_out_item, "activate",
                     G_CALLBACK(gd_menu_zoom_out), s);
    g_signal_connect(s->zoom_fixed_item, "activate",
                     G_CALLBACK(gd_menu_zoom_fixed), s);
    g_signal_connect(s->zoom_fit_item, "activate",
                     G_CALLBACK(gd_menu_zoom_fit), s);
1449 1450
    g_signal_connect(s->grab_item, "activate",
                     G_CALLBACK(gd_menu_grab_input), s);
A
Anthony Liguori 已提交
1451 1452 1453 1454
    g_signal_connect(s->notebook, "switch-page",
                     G_CALLBACK(gd_change_page), s);
}

A
Anthony Liguori 已提交
1455
static GtkWidget *gd_create_menu_machine(GtkDisplayState *s, GtkAccelGroup *accel_group)
A
Anthony Liguori 已提交
1456
{
A
Anthony Liguori 已提交
1457
    GtkWidget *machine_menu;
A
Anthony Liguori 已提交
1458 1459
    GtkWidget *separator;

A
Anthony Liguori 已提交
1460 1461
    machine_menu = gtk_menu_new();
    gtk_menu_set_accel_group(GTK_MENU(machine_menu), accel_group);
1462 1463

    s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
A
Anthony Liguori 已提交
1464
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1465 1466

    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1467
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
1468

1469
    s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
A
Anthony Liguori 已提交
1470
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1471

1472
    s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
A
Anthony Liguori 已提交
1473
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1474 1475

    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1476
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), separator);
A
Anthony Liguori 已提交
1477

1478
    s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
A
Anthony Liguori 已提交
1479
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1480
                                 "<QEMU>/Machine/Quit");
1481
    gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1482
                            GDK_KEY_q, HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1483
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
A
Anthony Liguori 已提交
1484

A
Anthony Liguori 已提交
1485 1486 1487
    return machine_menu;
}

1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
static const DisplayChangeListenerOps dcl_ops = {
    .dpy_name          = "gtk",
    .dpy_gfx_update    = gd_update,
    .dpy_gfx_switch    = gd_switch,
    .dpy_refresh       = gd_refresh,
    .dpy_mouse_set     = gd_mouse_set,
    .dpy_cursor_define = gd_cursor_define,
};

static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
1498
                              QemuConsole *con, int idx,
1499 1500
                              GSList *group, GtkWidget *view_menu)
{
G
Gerd Hoffmann 已提交
1501 1502 1503 1504 1505
    Error *local_err = NULL;
    Object *obj;

    obj = object_property_get_link(OBJECT(con), "device", &local_err);
    if (obj) {
1506 1507 1508
        vc->label = g_strdup_printf("%s", object_get_typename(obj));
    } else {
        vc->label = g_strdup_printf("VGA");
G
Gerd Hoffmann 已提交
1509 1510
    }

1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
    vc->s = s;
    vc->gfx.scale_x = 1.0;
    vc->gfx.scale_y = 1.0;

    vc->gfx.drawing_area = gtk_drawing_area_new();
    gtk_widget_add_events(vc->gfx.drawing_area,
                          GDK_POINTER_MOTION_MASK |
                          GDK_BUTTON_PRESS_MASK |
                          GDK_BUTTON_RELEASE_MASK |
                          GDK_BUTTON_MOTION_MASK |
                          GDK_ENTER_NOTIFY_MASK |
                          GDK_LEAVE_NOTIFY_MASK |
                          GDK_SCROLL_MASK |
                          GDK_KEY_PRESS_MASK);
    gtk_widget_set_double_buffered(vc->gfx.drawing_area, FALSE);
    gtk_widget_set_can_focus(vc->gfx.drawing_area, TRUE);

    vc->type = GD_VC_GFX;
    vc->tab_item = vc->gfx.drawing_area;
    gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook),
1531
                             vc->tab_item, gtk_label_new(vc->label));
1532 1533
    gd_connect_vc_gfx_signals(vc);

1534
    group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1535 1536 1537 1538 1539 1540 1541 1542

    vc->gfx.dcl.ops = &dcl_ops;
    vc->gfx.dcl.con = con;
    register_displaychangelistener(&vc->gfx.dcl);

    return group;
}

A
Anthony Liguori 已提交
1543 1544 1545 1546 1547
static GtkWidget *gd_create_menu_view(GtkDisplayState *s, GtkAccelGroup *accel_group)
{
    GSList *group = NULL;
    GtkWidget *view_menu;
    GtkWidget *separator;
1548 1549
    QemuConsole *con;
    int vc;
A
Anthony Liguori 已提交
1550 1551 1552

    view_menu = gtk_menu_new();
    gtk_menu_set_accel_group(GTK_MENU(view_menu), accel_group);
A
Anthony Liguori 已提交
1553

1554
    s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
1555 1556
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->full_screen_item),
                                 "<QEMU>/View/Full Screen");
J
Jan Kiszka 已提交
1557 1558
    gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1559
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
1560 1561

    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1562
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1563

1564
    s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
1565 1566
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
                                 "<QEMU>/View/Zoom In");
J
Jan Kiszka 已提交
1567 1568
    gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1569
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
1570

1571
    s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
1572 1573
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
                                 "<QEMU>/View/Zoom Out");
J
Jan Kiszka 已提交
1574 1575
    gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1576
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
1577

1578
    s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
1579 1580
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
                                 "<QEMU>/View/Zoom Fixed");
J
Jan Kiszka 已提交
1581 1582
    gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1583
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
1584

1585
    s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
A
Anthony Liguori 已提交
1586
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
1587 1588

    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1589
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
1590

1591
    s->grab_on_hover_item = gtk_check_menu_item_new_with_mnemonic(_("Grab On _Hover"));
A
Anthony Liguori 已提交
1592
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_on_hover_item);
1593

1594
    s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1595 1596
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
                                 "<QEMU>/View/Grab Input");
J
Jan Kiszka 已提交
1597 1598
    gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1599
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
1600

A
Anthony Liguori 已提交
1601
    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1602
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
A
Anthony Liguori 已提交
1603

1604
    /* gfx */
1605 1606 1607 1608 1609 1610 1611 1612 1613
    for (vc = 0;; vc++) {
        con = qemu_console_lookup_by_index(vc);
        if (!con || !qemu_console_is_graphic(con)) {
            break;
        }
        group = gd_vc_gfx_init(s, &s->vc[vc], con,
                               vc, group, view_menu);
        s->nb_vcs++;
    }
A
Anthony Liguori 已提交
1614

1615
#if defined(CONFIG_VTE)
1616
    /* vte */
1617
    gd_vcs_init(s, group, view_menu);
1618
#endif
1619

A
Anthony Liguori 已提交
1620
    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1621
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
A
Anthony Liguori 已提交
1622

1623
    s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
A
Anthony Liguori 已提交
1624
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
A
Anthony Liguori 已提交
1625

1626 1627 1628
    s->untabify_item = gtk_menu_item_new_with_mnemonic(_("Detach Tab"));
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->untabify_item);

A
Anthony Liguori 已提交
1629 1630 1631 1632 1633 1634
    return view_menu;
}

static void gd_create_menus(GtkDisplayState *s)
{
    GtkAccelGroup *accel_group;
A
Anthony Liguori 已提交
1635

A
Anthony Liguori 已提交
1636 1637 1638 1639 1640
    accel_group = gtk_accel_group_new();
    s->machine_menu = gd_create_menu_machine(s, accel_group);
    s->view_menu = gd_create_menu_view(s, accel_group);

    s->machine_menu_item = gtk_menu_item_new_with_mnemonic(_("_Machine"));
1641 1642 1643
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->machine_menu_item),
                              s->machine_menu);
    gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->machine_menu_item);
A
Anthony Liguori 已提交
1644

A
Anthony Liguori 已提交
1645
    s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
A
Anthony Liguori 已提交
1646 1647
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(s->view_menu_item), s->view_menu);
    gtk_menu_shell_append(GTK_MENU_SHELL(s->menu_bar), s->view_menu_item);
A
Anthony Liguori 已提交
1648 1649 1650 1651

    g_object_set_data(G_OBJECT(s->window), "accel_group", accel_group);
    gtk_window_add_accel_group(GTK_WINDOW(s->window), accel_group);
    s->accel_group = accel_group;
A
Anthony Liguori 已提交
1652 1653
}

1654
void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
A
Anthony Liguori 已提交
1655 1656
{
    GtkDisplayState *s = g_malloc0(sizeof(*s));
S
Stefan Weil 已提交
1657
    char *filename;
A
Anthony Liguori 已提交
1658 1659 1660 1661

    gtk_init(NULL, NULL);

    s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1662 1663 1664
#if GTK_CHECK_VERSION(3, 2, 0)
    s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
#else
A
Anthony Liguori 已提交
1665
    s->vbox = gtk_vbox_new(FALSE, 0);
1666
#endif
A
Anthony Liguori 已提交
1667 1668 1669
    s->notebook = gtk_notebook_new();
    s->menu_bar = gtk_menu_bar_new();

1670
    s->free_scale = FALSE;
A
Anthony Liguori 已提交
1671

1672 1673 1674 1675
    setlocale(LC_ALL, "");
    bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
    textdomain("qemu");

A
Anthony Liguori 已提交
1676 1677 1678 1679 1680 1681
    s->null_cursor = gdk_cursor_new(GDK_BLANK_CURSOR);

    s->mouse_mode_notifier.notify = gd_mouse_mode_change;
    qemu_add_mouse_mode_change_notifier(&s->mouse_mode_notifier);
    qemu_add_vm_change_state_handler(gd_change_runstate, s);

A
Anthony Liguori 已提交
1682
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
S
Stefan Weil 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
    if (filename) {
        GError *error = NULL;
        GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
        if (pixbuf) {
            gtk_window_set_icon(GTK_WINDOW(s->window), pixbuf);
        } else {
            g_error_free(error);
        }
        g_free(filename);
    }

A
Anthony Liguori 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
    gd_create_menus(s);

    gd_connect_signals(s);

    gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
    gtk_notebook_set_show_border(GTK_NOTEBOOK(s->notebook), FALSE);

    gd_update_caption(s);

    gtk_box_pack_start(GTK_BOX(s->vbox), s->menu_bar, FALSE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(s->vbox), s->notebook, TRUE, TRUE, 0);

    gtk_container_add(GTK_CONTAINER(s->window), s->vbox);

    gtk_widget_show_all(s->window);

P
Peter Wu 已提交
1710 1711 1712
    if (full_screen) {
        gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
    }
1713 1714 1715
    if (grab_on_hover) {
        gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
    }
P
Peter Wu 已提交
1716

A
Anthony Liguori 已提交
1717 1718
    global_state = s;
}
1719 1720 1721 1722 1723 1724 1725

void early_gtk_display_init(void)
{
#if defined(CONFIG_VTE)
    register_vc_handler(gd_vc_handler);
#endif
}