gtk.c 57.1 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"
71 72 73 74
#ifndef _WIN32
#include <gdk/gdkx.h>
#include <X11/XKBlib.h>
#endif
A
Anthony Liguori 已提交
75

76
#define MAX_VCS 10
G
Gerd Hoffmann 已提交
77 78 79 80 81 82
#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
83

S
Stefan Weil 已提交
84 85 86
#if !defined(CONFIG_VTE)
# define VTE_CHECK_VERSION(a, b, c) 0
#endif
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101
#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

102 103 104 105 106 107 108 109 110
/* 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

111 112 113 114
#if !GTK_CHECK_VERSION(2, 20, 0)
#define gtk_widget_get_realized(widget) GTK_WIDGET_REALIZED(widget)
#endif

115 116 117 118 119 120
#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
121
#define GDK_KEY_q GDK_q
122 123 124
#define GDK_KEY_plus GDK_plus
#define GDK_KEY_minus GDK_minus
#endif
125

J
Jan Kiszka 已提交
126 127
#define HOTKEY_MODIFIERS        (GDK_CONTROL_MASK | GDK_MOD1_MASK)

128 129 130 131 132
static const int modifier_keycode[] = {
    /* shift, control, alt keys, meta keys, both left & right */
    0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
};

133 134 135 136 137 138 139 140 141 142 143 144
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 已提交
145
#if defined(CONFIG_VTE)
G
Gerd Hoffmann 已提交
146
typedef struct VirtualVteConsole {
147 148
    GtkWidget *box;
    GtkWidget *scrollbar;
149
    GtkWidget *terminal;
A
Anthony Liguori 已提交
150
    CharDriverState *chr;
G
Gerd Hoffmann 已提交
151 152 153
} VirtualVteConsole;
#endif

154 155 156 157 158
typedef enum VirtualConsoleType {
    GD_VC_GFX,
    GD_VC_VTE,
} VirtualConsoleType;

G
Gerd Hoffmann 已提交
159
typedef struct VirtualConsole {
160
    GtkDisplayState *s;
161 162
    char *label;
    GtkWidget *window;
G
Gerd Hoffmann 已提交
163 164
    GtkWidget *menu_item;
    GtkWidget *tab_item;
165
    VirtualConsoleType type;
G
Gerd Hoffmann 已提交
166
    union {
167
        VirtualGfxConsole gfx;
G
Gerd Hoffmann 已提交
168 169
#if defined(CONFIG_VTE)
        VirtualVteConsole vte;
S
Stefan Weil 已提交
170
#endif
G
Gerd Hoffmann 已提交
171
    };
A
Anthony Liguori 已提交
172 173
} VirtualConsole;

174
struct GtkDisplayState {
A
Anthony Liguori 已提交
175 176 177 178
    GtkWidget *window;

    GtkWidget *menu_bar;

179 180
    GtkAccelGroup *accel_group;

181 182 183 184 185
    GtkWidget *machine_menu_item;
    GtkWidget *machine_menu;
    GtkWidget *pause_item;
    GtkWidget *reset_item;
    GtkWidget *powerdown_item;
A
Anthony Liguori 已提交
186 187 188 189
    GtkWidget *quit_item;

    GtkWidget *view_menu_item;
    GtkWidget *view_menu;
190 191 192 193 194
    GtkWidget *full_screen_item;
    GtkWidget *zoom_in_item;
    GtkWidget *zoom_out_item;
    GtkWidget *zoom_fixed_item;
    GtkWidget *zoom_fit_item;
195 196
    GtkWidget *grab_item;
    GtkWidget *grab_on_hover_item;
A
Anthony Liguori 已提交
197

198 199 200
    int nb_vcs;
    VirtualConsole vc[MAX_VCS];

A
Anthony Liguori 已提交
201
    GtkWidget *show_tabs_item;
202
    GtkWidget *untabify_item;
A
Anthony Liguori 已提交
203 204 205 206

    GtkWidget *vbox;
    GtkWidget *notebook;
    int button_mask;
207
    gboolean last_set;
A
Anthony Liguori 已提交
208 209
    int last_x;
    int last_y;
210 211
    int grab_x_root;
    int grab_y_root;
G
Gerd Hoffmann 已提交
212 213
    VirtualConsole *kbd_owner;
    VirtualConsole *ptr_owner;
A
Anthony Liguori 已提交
214

215
    gboolean full_screen;
A
Anthony Liguori 已提交
216 217 218

    GdkCursor *null_cursor;
    Notifier mouse_mode_notifier;
219
    gboolean free_scale;
220 221

    bool external_pause_update;
222 223

    bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
224
    bool has_evdev;
225
};
A
Anthony Liguori 已提交
226

G
Gerd Hoffmann 已提交
227 228 229
static void gd_grab_pointer(VirtualConsole *vc);
static void gd_ungrab_pointer(GtkDisplayState *s);

A
Anthony Liguori 已提交
230 231
/** Utility Functions **/

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

262 263 264 265 266 267 268 269
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);
}

270 271 272 273 274 275 276 277 278 279
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));
}

280
static void gd_update_cursor(VirtualConsole *vc)
A
Anthony Liguori 已提交
281
{
282
    GtkDisplayState *s = vc->s;
A
Anthony Liguori 已提交
283 284
    GdkWindow *window;

285 286 287
    if (vc->type != GD_VC_GFX) {
        return;
    }
A
Anthony Liguori 已提交
288

289
    window = gtk_widget_get_window(GTK_WIDGET(vc->gfx.drawing_area));
G
Gerd Hoffmann 已提交
290
    if (s->full_screen || qemu_input_is_absolute() || s->ptr_owner == vc) {
A
Anthony Liguori 已提交
291 292 293 294 295 296 297 298 299
        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 = "";
G
Gerd Hoffmann 已提交
300
    gchar *prefix;
A
Anthony Liguori 已提交
301
    gchar *title;
302
    const char *grab = "";
303
    bool is_paused = !runstate_is_running();
G
Gerd Hoffmann 已提交
304 305 306 307 308 309 310
    int i;

    if (qemu_name) {
        prefix = g_strdup_printf("QEMU (%s)", qemu_name);
    } else {
        prefix = g_strdup_printf("QEMU");
    }
311

G
Gerd Hoffmann 已提交
312 313
    if (s->ptr_owner != NULL &&
        s->ptr_owner->window == NULL) {
314
        grab = _(" - Press Ctrl+Alt+G to release grab");
315
    }
A
Anthony Liguori 已提交
316

317
    if (is_paused) {
318
        status = _(" [Paused]");
A
Anthony Liguori 已提交
319
    }
320 321 322 323
    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 已提交
324

G
Gerd Hoffmann 已提交
325
    title = g_strdup_printf("%s%s%s", prefix, status, grab);
A
Anthony Liguori 已提交
326 327
    gtk_window_set_title(GTK_WINDOW(s->window), title);
    g_free(title);
G
Gerd Hoffmann 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

    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);
A
Anthony Liguori 已提交
343 344
}

G
Gerd Hoffmann 已提交
345
static void gd_update_geometry_hints(VirtualConsole *vc)
G
Gerd Hoffmann 已提交
346
{
347
    GtkDisplayState *s = vc->s;
G
Gerd Hoffmann 已提交
348 349 350 351
    GdkWindowHints mask = 0;
    GdkGeometry geo = {};
    GtkWidget *geo_widget = NULL;
    GtkWindow *geo_window;
352

G
Gerd Hoffmann 已提交
353 354 355 356 357 358 359 360 361 362 363 364
    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);
G
Gerd Hoffmann 已提交
365

G
Gerd Hoffmann 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
#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
G
Gerd Hoffmann 已提交
387
    }
G
Gerd Hoffmann 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401

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

405
static void gd_update_full_redraw(VirtualConsole *vc)
G
Gerd Hoffmann 已提交
406
{
407
    GtkWidget *area = vc->gfx.drawing_area;
G
Gerd Hoffmann 已提交
408
    int ww, wh;
409 410
    gdk_drawable_get_size(gtk_widget_get_window(area), &ww, &wh);
    gtk_widget_queue_draw_area(area, 0, 0, ww, wh);
G
Gerd Hoffmann 已提交
411 412
}

413 414
static void gtk_release_modifiers(GtkDisplayState *s)
{
415
    VirtualConsole *vc = gd_vc_find_current(s);
416 417
    int i, keycode;

418
    if (vc->type != GD_VC_GFX) {
419 420 421 422 423 424 425
        return;
    }
    for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
        keycode = modifier_keycode[i];
        if (!s->modifier_pressed[i]) {
            continue;
        }
426
        qemu_input_event_send_key_number(vc->gfx.dcl.con, keycode, false);
427 428 429 430
        s->modifier_pressed[i] = false;
    }
}

A
Anthony Liguori 已提交
431 432
/** DisplayState Callbacks **/

433
static void gd_update(DisplayChangeListener *dcl,
434
                      int x, int y, int w, int h)
A
Anthony Liguori 已提交
435
{
436
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
A
Anthony Liguori 已提交
437
    int x1, x2, y1, y2;
438 439 440
    int mx, my;
    int fbw, fbh;
    int ww, wh;
A
Anthony Liguori 已提交
441

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

444 445 446
    if (vc->gfx.convert) {
        pixman_image_composite(PIXMAN_OP_SRC, vc->gfx.ds->image,
                               NULL, vc->gfx.convert,
447 448 449
                               x, y, 0, 0, x, y, w, h);
    }

450 451
    x1 = floor(x * vc->gfx.scale_x);
    y1 = floor(y * vc->gfx.scale_y);
A
Anthony Liguori 已提交
452

453 454
    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 已提交
455

456 457
    fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
    fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
458

459 460
    gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
                          &ww, &wh);
461 462 463 464 465 466 467 468 469

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

470 471
    gtk_widget_queue_draw_area(vc->gfx.drawing_area,
                               mx + x1, my + y1, (x2 - x1), (y2 - y1));
A
Anthony Liguori 已提交
472 473
}

474
static void gd_refresh(DisplayChangeListener *dcl)
A
Anthony Liguori 已提交
475
{
476
    graphic_hw_update(dcl->con);
A
Anthony Liguori 已提交
477 478
}

479 480 481 482
#if GTK_CHECK_VERSION(3, 0, 0)
static void gd_mouse_set(DisplayChangeListener *dcl,
                         int x, int y, int visible)
{
483
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
484 485 486 487
    GdkDisplay *dpy;
    GdkDeviceManager *mgr;
    gint x_root, y_root;

C
Cole Robinson 已提交
488 489 490 491
    if (qemu_input_is_absolute()) {
        return;
    }

492
    dpy = gtk_widget_get_display(vc->gfx.drawing_area);
493
    mgr = gdk_display_get_device_manager(dpy);
494
    gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
495 496
                               x, y, &x_root, &y_root);
    gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
497
                    gtk_widget_get_screen(vc->gfx.drawing_area),
C
Cole Robinson 已提交
498
                    x_root, y_root);
499 500
}
#else
G
Gerd Hoffmann 已提交
501 502 503
static void gd_mouse_set(DisplayChangeListener *dcl,
                         int x, int y, int visible)
{
504
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
G
Gerd Hoffmann 已提交
505 506
    gint x_root, y_root;

C
Cole Robinson 已提交
507 508 509 510
    if (qemu_input_is_absolute()) {
        return;
    }

511
    gdk_window_get_root_coords(gtk_widget_get_window(vc->gfx.drawing_area),
G
Gerd Hoffmann 已提交
512
                               x, y, &x_root, &y_root);
513 514
    gdk_display_warp_pointer(gtk_widget_get_display(vc->gfx.drawing_area),
                             gtk_widget_get_screen(vc->gfx.drawing_area),
G
Gerd Hoffmann 已提交
515 516
                             x_root, y_root);
}
517
#endif
G
Gerd Hoffmann 已提交
518 519 520 521

static void gd_cursor_define(DisplayChangeListener *dcl,
                             QEMUCursor *c)
{
522
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
G
Gerd Hoffmann 已提交
523 524 525 526 527 528 529
    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);
530 531 532 533
    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 已提交
534
    g_object_unref(pixbuf);
535
#if !GTK_CHECK_VERSION(3, 0, 0)
536
    gdk_cursor_unref(cursor);
537 538 539
#else
    g_object_unref(cursor);
#endif
G
Gerd Hoffmann 已提交
540 541
}

542 543
static void gd_switch(DisplayChangeListener *dcl,
                      DisplaySurface *surface)
A
Anthony Liguori 已提交
544
{
545
    VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
G
Gerd Hoffmann 已提交
546
    bool resized = true;
A
Anthony Liguori 已提交
547

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

550 551
    if (vc->gfx.surface) {
        cairo_surface_destroy(vc->gfx.surface);
A
Anthony Liguori 已提交
552 553
    }

554 555 556
    if (vc->gfx.ds &&
        surface_width(vc->gfx.ds) == surface_width(surface) &&
        surface_height(vc->gfx.ds) == surface_height(surface)) {
G
Gerd Hoffmann 已提交
557 558
        resized = false;
    }
559
    vc->gfx.ds = surface;
A
Anthony Liguori 已提交
560

561 562 563
    if (vc->gfx.convert) {
        pixman_image_unref(vc->gfx.convert);
        vc->gfx.convert = NULL;
564
    }
A
Anthony Liguori 已提交
565

566 567 568 569 570 571 572
    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.
         */
573
        vc->gfx.surface = cairo_image_surface_create_for_data
574 575 576 577 578 579 580
            (surface_data(surface),
             CAIRO_FORMAT_RGB24,
             surface_width(surface),
             surface_height(surface),
             surface_stride(surface));
    } else {
        /* Must convert surface, use pixman to do it. */
581 582 583 584 585 586
        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),
587
             CAIRO_FORMAT_RGB24,
588 589 590 591 592
             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,
593
                               0, 0, 0, 0, 0, 0,
594 595
                               pixman_image_get_width(vc->gfx.convert),
                               pixman_image_get_height(vc->gfx.convert));
596
    }
597

G
Gerd Hoffmann 已提交
598
    if (resized) {
599
        gd_update_windowsize(vc);
G
Gerd Hoffmann 已提交
600
    } else {
601
        gd_update_full_redraw(vc);
602
    }
A
Anthony Liguori 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615
}

/** 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)
{
616
    GtkDisplayState *s;
617
    int i;
618 619 620 621 622 623 624

    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);
    }
625 626 627 628
    for (i = 0; i < s->nb_vcs; i++) {
        VirtualConsole *vc = &s->vc[i];
        gd_update_cursor(vc);
    }
A
Anthony Liguori 已提交
629 630 631 632 633 634 635 636
}

/** GTK Events **/

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

    if (!no_quit) {
640 641 642 643 644 645
        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 已提交
646 647 648 649 650 651 652 653 654
        qmp_quit(NULL);
        return FALSE;
    }

    return TRUE;
}

static gboolean gd_draw_event(GtkWidget *widget, cairo_t *cr, void *opaque)
{
655 656
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
657
    int mx, my;
A
Anthony Liguori 已提交
658 659 660
    int ww, wh;
    int fbw, fbh;

661 662 663 664
    if (!gtk_widget_get_realized(widget)) {
        return FALSE;
    }

665 666
    fbw = surface_width(vc->gfx.ds);
    fbh = surface_height(vc->gfx.ds);
A
Anthony Liguori 已提交
667 668 669

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

670
    if (s->full_screen) {
671 672
        vc->gfx.scale_x = (double)ww / fbw;
        vc->gfx.scale_y = (double)wh / fbh;
673 674 675 676 677 678
    } else if (s->free_scale) {
        double sx, sy;

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

679
        vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
A
Anthony Liguori 已提交
680 681
    }

682 683
    fbw *= vc->gfx.scale_x;
    fbh *= vc->gfx.scale_y;
684

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
    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);

704 705 706
    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 已提交
707 708 709 710 711
    cairo_paint(cr);

    return TRUE;
}

712
#if !GTK_CHECK_VERSION(3, 0, 0)
A
Anthony Liguori 已提交
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
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;
}
733
#endif
A
Anthony Liguori 已提交
734 735 736 737

static gboolean gd_motion_event(GtkWidget *widget, GdkEventMotion *motion,
                                void *opaque)
{
738 739
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
A
Anthony Liguori 已提交
740
    int x, y;
741 742 743 744
    int mx, my;
    int fbh, fbw;
    int ww, wh;

745 746
    fbw = surface_width(vc->gfx.ds) * vc->gfx.scale_x;
    fbh = surface_height(vc->gfx.ds) * vc->gfx.scale_y;
A
Anthony Liguori 已提交
747

748 749
    gdk_drawable_get_size(gtk_widget_get_window(vc->gfx.drawing_area),
                          &ww, &wh);
750 751 752 753 754 755 756 757 758

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

759 760
    x = (motion->x - mx) / vc->gfx.scale_x;
    y = (motion->y - my) / vc->gfx.scale_y;
761

762
    if (qemu_input_is_absolute()) {
763
        if (x < 0 || y < 0 ||
764 765
            x >= surface_width(vc->gfx.ds) ||
            y >= surface_height(vc->gfx.ds)) {
766 767
            return TRUE;
        }
768 769 770 771
        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));
772
        qemu_input_event_sync();
G
Gerd Hoffmann 已提交
773
    } else if (s->last_set && s->ptr_owner == vc) {
774 775
        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);
776
        qemu_input_event_sync();
A
Anthony Liguori 已提交
777 778 779
    }
    s->last_x = x;
    s->last_y = y;
780
    s->last_set = TRUE;
A
Anthony Liguori 已提交
781

G
Gerd Hoffmann 已提交
782
    if (!qemu_input_is_absolute() && s->ptr_owner == vc) {
783
        GdkScreen *screen = gtk_widget_get_screen(vc->gfx.drawing_area);
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
        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) {
808 809 810 811 812
#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);
813
            gdk_display_warp_pointer(display, screen, x, y);
814
#endif
815
            s->last_set = FALSE;
816 817 818
            return FALSE;
        }
    }
A
Anthony Liguori 已提交
819 820 821 822 823 824
    return TRUE;
}

static gboolean gd_button_event(GtkWidget *widget, GdkEventButton *button,
                                void *opaque)
{
825 826
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
827
    InputButton btn;
A
Anthony Liguori 已提交
828

829 830
    /* implicitly grab the input at the first click in the relative mode */
    if (button->button == 1 && button->type == GDK_BUTTON_PRESS &&
G
Gerd Hoffmann 已提交
831 832 833 834 835 836 837 838 839
        !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);
        }
840 841 842
        return TRUE;
    }

A
Anthony Liguori 已提交
843
    if (button->button == 1) {
844
        btn = INPUT_BUTTON_LEFT;
A
Anthony Liguori 已提交
845
    } else if (button->button == 2) {
846
        btn = INPUT_BUTTON_MIDDLE;
A
Anthony Liguori 已提交
847
    } else if (button->button == 3) {
848
        btn = INPUT_BUTTON_RIGHT;
A
Anthony Liguori 已提交
849
    } else {
850
        return TRUE;
A
Anthony Liguori 已提交
851 852
    }

853 854
    qemu_input_queue_btn(vc->gfx.dcl.con, btn,
                         button->type == GDK_BUTTON_PRESS);
855
    qemu_input_event_sync();
A
Anthony Liguori 已提交
856 857 858
    return TRUE;
}

J
Jan Kiszka 已提交
859 860 861
static gboolean gd_scroll_event(GtkWidget *widget, GdkEventScroll *scroll,
                                void *opaque)
{
862
    VirtualConsole *vc = opaque;
J
Jan Kiszka 已提交
863 864 865 866 867 868 869 870 871 872
    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;
    }

873
    qemu_input_queue_btn(vc->gfx.dcl.con, btn, true);
J
Jan Kiszka 已提交
874
    qemu_input_event_sync();
875
    qemu_input_queue_btn(vc->gfx.dcl.con, btn, false);
J
Jan Kiszka 已提交
876 877 878 879
    qemu_input_event_sync();
    return TRUE;
}

A
Anthony Liguori 已提交
880 881
static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void *opaque)
{
882 883
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
884
    int gdk_keycode = key->hardware_keycode;
885
    int i;
A
Anthony Liguori 已提交
886

887 888 889 890 891 892 893 894 895
#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 已提交
896 897 898 899 900 901

    if (gdk_keycode < 9) {
        qemu_keycode = 0;
    } else if (gdk_keycode < 97) {
        qemu_keycode = gdk_keycode - 8;
    } else if (gdk_keycode < 158) {
902 903 904 905 906
        if (s->has_evdev) {
            qemu_keycode = translate_evdev_keycode(gdk_keycode - 97);
        } else {
            qemu_keycode = translate_xfree86_keycode(gdk_keycode - 97);
        }
A
Anthony Liguori 已提交
907 908 909 910 911 912 913
    } else if (gdk_keycode == 208) { /* Hiragana_Katakana */
        qemu_keycode = 0x70;
    } else if (gdk_keycode == 211) { /* backslash */
        qemu_keycode = 0x73;
    } else {
        qemu_keycode = 0;
    }
914
#endif
A
Anthony Liguori 已提交
915

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

919 920 921 922 923 924
    for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
        if (qemu_keycode == modifier_keycode[i]) {
            s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
        }
    }

925
    qemu_input_event_send_key_number(vc->gfx.dcl.con, qemu_keycode,
926
                                     key->type == GDK_KEY_PRESS);
A
Anthony Liguori 已提交
927 928 929 930

    return TRUE;
}

931 932 933 934 935 936 937 938
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 已提交
939 940
/** Window Menu Actions **/

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
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 已提交
965 966 967 968 969 970 971 972
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;
973
    VirtualConsole *vc = gd_vc_find_by_menu(s);
974
    gint page;
A
Anthony Liguori 已提交
975

976 977
    gtk_release_modifiers(s);
    if (vc) {
978
        page = gtk_notebook_page_num(GTK_NOTEBOOK(s->notebook),
979
                                     vc->tab_item);
980
        gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), page);
A
Anthony Liguori 已提交
981 982 983 984 985 986 987 988 989 990 991 992 993 994
    }
}

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);
    }
}

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
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;
}

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
static gboolean gd_win_grab(void *opaque)
{
    VirtualConsole *vc = opaque;

    fprintf(stderr, "%s: %s\n", __func__, vc->label);
    if (vc->s->ptr_owner) {
        gd_ungrab_pointer(vc->s);
    } else {
        gd_grab_pointer(vc);
    }
    gd_update_caption(vc->s);
    return TRUE;
}

1024 1025 1026 1027 1028 1029
static void gd_menu_untabify(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
    VirtualConsole *vc = gd_vc_find_current(s);

    if (vc->type == GD_VC_GFX) {
G
Gerd Hoffmann 已提交
1030 1031
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                       FALSE);
1032 1033 1034 1035 1036 1037 1038 1039 1040
    }
    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);

        g_signal_connect(vc->window, "delete-event",
                         G_CALLBACK(gd_tab_window_close), vc);
        gtk_widget_show_all(vc->window);
G
Gerd Hoffmann 已提交
1041

1042 1043 1044 1045 1046 1047
        GtkAccelGroup *ag = gtk_accel_group_new();
        gtk_window_add_accel_group(GTK_WINDOW(vc->window), ag);

        GClosure *cb = g_cclosure_new_swap(G_CALLBACK(gd_win_grab), vc, NULL);
        gtk_accel_group_connect(ag, GDK_KEY_g, HOTKEY_MODIFIERS, 0, cb);

G
Gerd Hoffmann 已提交
1048
        gd_update_geometry_hints(vc);
G
Gerd Hoffmann 已提交
1049
        gd_update_caption(s);
1050 1051 1052
    }
}

1053 1054 1055
static void gd_menu_full_screen(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1056
    VirtualConsole *vc = gd_vc_find_current(s);
1057

1058
    if (!s->full_screen) {
1059 1060
        gtk_notebook_set_show_tabs(GTK_NOTEBOOK(s->notebook), FALSE);
        gtk_widget_set_size_request(s->menu_bar, 0, 0);
1061 1062 1063 1064
        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);
1065
        }
1066
        gtk_window_fullscreen(GTK_WINDOW(s->window));
1067 1068 1069 1070 1071 1072
        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;
1073 1074 1075
        if (vc->type == GD_VC_GFX) {
            vc->gfx.scale_x = 1.0;
            vc->gfx.scale_y = 1.0;
G
Gerd Hoffmann 已提交
1076
            gd_update_windowsize(vc);
1077 1078 1079
            gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                           FALSE);
        }
1080 1081
    }

1082
    gd_update_cursor(vc);
1083 1084 1085 1086 1087
}

static void gd_menu_zoom_in(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1088
    VirtualConsole *vc = gd_vc_find_current(s);
1089 1090 1091 1092

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

G
Gerd Hoffmann 已提交
1093 1094
    vc->gfx.scale_x += VC_SCALE_STEP;
    vc->gfx.scale_y += VC_SCALE_STEP;
1095

1096
    gd_update_windowsize(vc);
1097 1098 1099 1100 1101
}

static void gd_menu_zoom_out(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1102
    VirtualConsole *vc = gd_vc_find_current(s);
1103 1104 1105 1106

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

G
Gerd Hoffmann 已提交
1107 1108
    vc->gfx.scale_x -= VC_SCALE_STEP;
    vc->gfx.scale_y -= VC_SCALE_STEP;
1109

G
Gerd Hoffmann 已提交
1110 1111
    vc->gfx.scale_x = MAX(vc->gfx.scale_x, VC_SCALE_MIN);
    vc->gfx.scale_y = MAX(vc->gfx.scale_y, VC_SCALE_MIN);
1112

1113
    gd_update_windowsize(vc);
1114 1115 1116 1117 1118
}

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

1121 1122
    vc->gfx.scale_x = 1.0;
    vc->gfx.scale_y = 1.0;
1123

1124
    gd_update_windowsize(vc);
1125 1126 1127 1128 1129
}

static void gd_menu_zoom_fit(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1130
    VirtualConsole *vc = gd_vc_find_current(s);
1131 1132 1133 1134 1135

    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->zoom_fit_item))) {
        s->free_scale = TRUE;
    } else {
        s->free_scale = FALSE;
1136 1137
        vc->gfx.scale_x = 1.0;
        vc->gfx.scale_y = 1.0;
1138 1139
    }

G
Gerd Hoffmann 已提交
1140
    gd_update_windowsize(vc);
1141
    gd_update_full_redraw(vc);
1142 1143
}

1144
static void gd_grab_keyboard(VirtualConsole *vc)
1145
{
1146
#if GTK_CHECK_VERSION(3, 0, 0)
1147
    GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1148 1149 1150 1151 1152 1153 1154 1155
    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,
1156
                            gtk_widget_get_window(vc->gfx.drawing_area),
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
                            GDK_OWNERSHIP_NONE,
                            FALSE,
                            GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
                            NULL,
                            GDK_CURRENT_TIME);
        }
        tmp = tmp->next;
    }
    g_list_free(devices);
#else
1167
    gdk_keyboard_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1168 1169
                      FALSE,
                      GDK_CURRENT_TIME);
1170
#endif
G
Gerd Hoffmann 已提交
1171
    vc->s->kbd_owner = vc;
G
Gerd Hoffmann 已提交
1172
    trace_gd_grab(vc->label, "kbd", true);
1173 1174
}

G
Gerd Hoffmann 已提交
1175
static void gd_ungrab_keyboard(GtkDisplayState *s)
1176
{
G
Gerd Hoffmann 已提交
1177 1178 1179 1180 1181 1182 1183
    VirtualConsole *vc = s->kbd_owner;

    if (vc == NULL) {
        return;
    }
    s->kbd_owner = NULL;

1184
#if GTK_CHECK_VERSION(3, 0, 0)
1185
    GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
    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
1200
    gdk_keyboard_ungrab(GDK_CURRENT_TIME);
1201
#endif
G
Gerd Hoffmann 已提交
1202
    trace_gd_grab(vc->label, "kbd", false);
1203 1204
}

1205
static void gd_grab_pointer(VirtualConsole *vc)
1206
{
1207
    GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1208
#if GTK_CHECK_VERSION(3, 0, 0)
1209 1210 1211 1212 1213 1214 1215 1216
    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,
1217
                            gtk_widget_get_window(vc->gfx.drawing_area),
1218 1219 1220 1221 1222 1223 1224 1225
                            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,
1226
                            vc->s->null_cursor,
1227 1228 1229 1230 1231
                            GDK_CURRENT_TIME);
        }
        tmp = tmp->next;
    }
    g_list_free(devices);
1232
    gdk_device_get_position(gdk_device_manager_get_client_pointer(mgr),
1233
                            NULL, &vc->s->grab_x_root, &vc->s->grab_y_root);
1234
#else
1235
    gdk_pointer_grab(gtk_widget_get_window(vc->gfx.drawing_area),
1236 1237 1238 1239 1240 1241 1242
                     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 */
1243
                     vc->s->null_cursor,
1244
                     GDK_CURRENT_TIME);
1245
    gdk_display_get_pointer(display, NULL,
1246
                            &vc->s->grab_x_root, &vc->s->grab_y_root, NULL);
1247
#endif
G
Gerd Hoffmann 已提交
1248
    vc->s->ptr_owner = vc;
G
Gerd Hoffmann 已提交
1249
    trace_gd_grab(vc->label, "ptr", true);
1250 1251
}

G
Gerd Hoffmann 已提交
1252
static void gd_ungrab_pointer(GtkDisplayState *s)
1253
{
G
Gerd Hoffmann 已提交
1254 1255 1256 1257 1258 1259 1260
    VirtualConsole *vc = s->ptr_owner;

    if (vc == NULL) {
        return;
    }
    s->ptr_owner = NULL;

1261
    GdkDisplay *display = gtk_widget_get_display(vc->gfx.drawing_area);
1262
#if GTK_CHECK_VERSION(3, 0, 0)
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
    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);
1276
    gdk_device_warp(gdk_device_manager_get_client_pointer(mgr),
1277 1278
                    gtk_widget_get_screen(vc->gfx.drawing_area),
                    vc->s->grab_x_root, vc->s->grab_y_root);
1279 1280
#else
    gdk_pointer_ungrab(GDK_CURRENT_TIME);
1281
    gdk_display_warp_pointer(display,
1282 1283
                             gtk_widget_get_screen(vc->gfx.drawing_area),
                             vc->s->grab_x_root, vc->s->grab_y_root);
1284
#endif
G
Gerd Hoffmann 已提交
1285
    trace_gd_grab(vc->label, "ptr", false);
1286 1287
}

1288 1289 1290
static void gd_menu_grab_input(GtkMenuItem *item, void *opaque)
{
    GtkDisplayState *s = opaque;
1291
    VirtualConsole *vc = gd_vc_find_current(s);
1292 1293

    if (gd_is_grab_active(s)) {
1294 1295 1296
        if (!gd_grab_on_hover(s)) {
            gd_grab_keyboard(vc);
        }
1297
        gd_grab_pointer(vc);
1298
    } else {
G
Gerd Hoffmann 已提交
1299 1300
        gd_ungrab_keyboard(s);
        gd_ungrab_pointer(s);
1301 1302 1303
    }

    gd_update_caption(s);
1304
    gd_update_cursor(vc);
1305 1306
}

A
Anthony Liguori 已提交
1307 1308 1309 1310
static void gd_change_page(GtkNotebook *nb, gpointer arg1, guint arg2,
                           gpointer data)
{
    GtkDisplayState *s = data;
1311
    VirtualConsole *vc;
1312
    gboolean on_vga;
A
Anthony Liguori 已提交
1313 1314 1315 1316 1317

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

1318 1319 1320 1321 1322 1323
#ifdef VTE_RESIZE_HACK
    vc = gd_vc_find_current(s);
    if (vc && vc->type == GD_VC_VTE) {
        gtk_widget_hide(vc->vte.terminal);
    }
#endif
1324 1325 1326 1327
    vc = gd_vc_find_by_page(s, arg2);
    if (!vc) {
        return;
    }
1328 1329 1330 1331 1332
#ifdef VTE_RESIZE_HACK
    if (vc->type == GD_VC_VTE) {
        gtk_widget_show(vc->vte.terminal);
    }
#endif
1333 1334 1335
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(vc->menu_item),
                                   TRUE);
    on_vga = (vc->type == GD_VC_GFX);
1336 1337 1338
    if (!on_vga) {
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                       FALSE);
1339 1340 1341
    } else if (s->full_screen) {
        gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(s->grab_item),
                                       TRUE);
1342 1343 1344
    }
    gtk_widget_set_sensitive(s->grab_item, on_vga);

G
Gerd Hoffmann 已提交
1345
    gd_update_windowsize(vc);
1346
    gd_update_cursor(vc);
A
Anthony Liguori 已提交
1347 1348
}

1349 1350
static gboolean gd_enter_event(GtkWidget *widget, GdkEventCrossing *crossing,
                               gpointer opaque)
1351
{
1352 1353
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
1354

G
Gerd Hoffmann 已提交
1355 1356
    if (gd_grab_on_hover(s)) {
        gd_ungrab_keyboard(s);
1357
        gd_grab_keyboard(vc);
G
Gerd Hoffmann 已提交
1358
        gd_update_caption(s);
1359 1360 1361 1362
    }
    return TRUE;
}

1363 1364
static gboolean gd_leave_event(GtkWidget *widget, GdkEventCrossing *crossing,
                               gpointer opaque)
1365
{
1366 1367
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
1368

G
Gerd Hoffmann 已提交
1369
    if (gd_grab_on_hover(s)) {
G
Gerd Hoffmann 已提交
1370
        gd_ungrab_keyboard(s);
G
Gerd Hoffmann 已提交
1371
        gd_update_caption(s);
1372 1373 1374 1375
    }
    return TRUE;
}

1376
static gboolean gd_focus_out_event(GtkWidget *widget,
1377
                                   GdkEventCrossing *crossing, gpointer opaque)
1378
{
1379 1380
    VirtualConsole *vc = opaque;
    GtkDisplayState *s = vc->s;
1381 1382 1383 1384 1385

    gtk_release_modifiers(s);
    return TRUE;
}

1386 1387
/** Virtual Console Callbacks **/

1388
static GSList *gd_vc_menu_init(GtkDisplayState *s, VirtualConsole *vc,
1389
                               int idx, GSList *group, GtkWidget *view_menu)
1390 1391 1392 1393 1394
{
    char path[32];

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

1395
    vc->menu_item = gtk_radio_menu_item_new_with_mnemonic(group, vc->label);
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
    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;
}

1407
#if defined(CONFIG_VTE)
1408 1409 1410 1411 1412 1413
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 已提交
1414
        gtk_widget_show(vc->vte.scrollbar);
1415
    } else {
G
Gerd Hoffmann 已提交
1416
        gtk_widget_hide(vc->vte.scrollbar);
1417 1418 1419
    }
}

1420 1421 1422 1423
static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
{
    VirtualConsole *vc = chr->opaque;

G
Gerd Hoffmann 已提交
1424
    vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char *)buf, len);
C
Cole Robinson 已提交
1425
    return len;
1426 1427 1428 1429 1430
}

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

G
Gerd Hoffmann 已提交
1431
static CharDriverState *gd_vc_handler(ChardevVC *unused)
1432 1433 1434 1435 1436
{
    CharDriverState *chr;

    chr = g_malloc0(sizeof(*chr));
    chr->chr_write = gd_vc_chr_write;
1437 1438
    /* defer OPENED events until our vc is fully initialized */
    chr->explicit_be_open = true;
1439 1440 1441 1442 1443 1444

    vcs[nb_vcs++] = chr;

    return chr;
}

C
Cole Robinson 已提交
1445 1446
static gboolean gd_vc_in(VteTerminal *terminal, gchar *text, guint size,
                         gpointer user_data)
1447
{
C
Cole Robinson 已提交
1448
    VirtualConsole *vc = user_data;
1449

G
Gerd Hoffmann 已提交
1450
    qemu_chr_be_write(vc->vte.chr, (uint8_t  *)text, (unsigned int)size);
1451 1452 1453
    return TRUE;
}

1454 1455
static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,
                              CharDriverState *chr, int idx,
1456
                              GSList *group, GtkWidget *view_menu)
1457 1458
{
    char buffer[32];
1459 1460 1461
    GtkWidget *box;
    GtkWidget *scrollbar;
    GtkAdjustment *vadjustment;
1462

1463
    vc->s = s;
1464
    vc->vte.chr = chr;
1465

1466
    snprintf(buffer, sizeof(buffer), "vc%d", idx);
1467 1468 1469
    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);
1470

G
Gerd Hoffmann 已提交
1471 1472
    vc->vte.terminal = vte_terminal_new();
    g_signal_connect(vc->vte.terminal, "commit", G_CALLBACK(gd_vc_in), vc);
1473

G
Gerd Hoffmann 已提交
1474
    vte_terminal_set_scrollback_lines(VTE_TERMINAL(vc->vte.terminal), -1);
G
Gerd Hoffmann 已提交
1475 1476
    vte_terminal_set_size(VTE_TERMINAL(vc->vte.terminal),
                          VC_TERM_X_MIN, VC_TERM_Y_MIN);
1477

1478
#if VTE_CHECK_VERSION(0, 28, 0) && GTK_CHECK_VERSION(3, 0, 0)
G
Gerd Hoffmann 已提交
1479 1480
    vadjustment = gtk_scrollable_get_vadjustment
        (GTK_SCROLLABLE(vc->vte.terminal));
1481
#else
G
Gerd Hoffmann 已提交
1482
    vadjustment = vte_terminal_get_adjustment(VTE_TERMINAL(vc->vte.terminal));
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
#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 已提交
1493
    gtk_box_pack_start(GTK_BOX(box), vc->vte.terminal, TRUE, TRUE, 0);
1494 1495
    gtk_box_pack_start(GTK_BOX(box), scrollbar, FALSE, FALSE, 0);

G
Gerd Hoffmann 已提交
1496 1497 1498
    vc->vte.chr->opaque = vc;
    vc->vte.box = box;
    vc->vte.scrollbar = scrollbar;
1499 1500 1501

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

1503
    vc->type = GD_VC_VTE;
G
Gerd Hoffmann 已提交
1504 1505
    vc->tab_item = box;
    gtk_notebook_append_page(GTK_NOTEBOOK(s->notebook), vc->tab_item,
1506
                             gtk_label_new(vc->label));
1507

G
Gerd Hoffmann 已提交
1508 1509 1510
    qemu_chr_be_generic_open(vc->vte.chr);
    if (vc->vte.chr->init) {
        vc->vte.chr->init(vc->vte.chr);
1511 1512 1513
    }

    return group;
A
Anthony Liguori 已提交
1514 1515
}

1516
static void gd_vcs_init(GtkDisplayState *s, GSList *group,
1517 1518 1519 1520 1521
                        GtkWidget *view_menu)
{
    int i;

    for (i = 0; i < nb_vcs; i++) {
1522 1523
        VirtualConsole *vc = &s->vc[s->nb_vcs];
        group = gd_vc_vte_init(s, vc, vcs[i], s->nb_vcs, group, view_menu);
1524 1525 1526 1527 1528
        s->nb_vcs++;
    }
}
#endif /* CONFIG_VTE */

A
Anthony Liguori 已提交
1529 1530
/** Window Creation **/

1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
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 已提交
1561 1562 1563 1564
static void gd_connect_signals(GtkDisplayState *s)
{
    g_signal_connect(s->show_tabs_item, "activate",
                     G_CALLBACK(gd_menu_show_tabs), s);
1565 1566
    g_signal_connect(s->untabify_item, "activate",
                     G_CALLBACK(gd_menu_untabify), s);
A
Anthony Liguori 已提交
1567 1568 1569 1570

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

1571 1572 1573 1574 1575 1576
    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 已提交
1577 1578
    g_signal_connect(s->quit_item, "activate",
                     G_CALLBACK(gd_menu_quit), s);
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
    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);
1589 1590
    g_signal_connect(s->grab_item, "activate",
                     G_CALLBACK(gd_menu_grab_input), s);
A
Anthony Liguori 已提交
1591 1592 1593 1594
    g_signal_connect(s->notebook, "switch-page",
                     G_CALLBACK(gd_change_page), s);
}

A
Anthony Liguori 已提交
1595
static GtkWidget *gd_create_menu_machine(GtkDisplayState *s, GtkAccelGroup *accel_group)
A
Anthony Liguori 已提交
1596
{
A
Anthony Liguori 已提交
1597
    GtkWidget *machine_menu;
A
Anthony Liguori 已提交
1598 1599
    GtkWidget *separator;

A
Anthony Liguori 已提交
1600 1601
    machine_menu = gtk_menu_new();
    gtk_menu_set_accel_group(GTK_MENU(machine_menu), accel_group);
1602 1603

    s->pause_item = gtk_check_menu_item_new_with_mnemonic(_("_Pause"));
A
Anthony Liguori 已提交
1604
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->pause_item);
1605 1606

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

1609
    s->reset_item = gtk_menu_item_new_with_mnemonic(_("_Reset"));
A
Anthony Liguori 已提交
1610
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->reset_item);
1611

1612
    s->powerdown_item = gtk_menu_item_new_with_mnemonic(_("Power _Down"));
A
Anthony Liguori 已提交
1613
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->powerdown_item);
1614 1615

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

1618
    s->quit_item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
A
Anthony Liguori 已提交
1619
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->quit_item),
1620
                                 "<QEMU>/Machine/Quit");
1621
    gtk_accel_map_add_entry("<QEMU>/Machine/Quit",
1622
                            GDK_KEY_q, HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1623
    gtk_menu_shell_append(GTK_MENU_SHELL(machine_menu), s->quit_item);
A
Anthony Liguori 已提交
1624

A
Anthony Liguori 已提交
1625 1626 1627
    return machine_menu;
}

1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
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,
1638
                              QemuConsole *con, int idx,
1639 1640
                              GSList *group, GtkWidget *view_menu)
{
G
Gerd Hoffmann 已提交
1641 1642 1643 1644 1645
    Error *local_err = NULL;
    Object *obj;

    obj = object_property_get_link(OBJECT(con), "device", &local_err);
    if (obj) {
1646 1647 1648
        vc->label = g_strdup_printf("%s", object_get_typename(obj));
    } else {
        vc->label = g_strdup_printf("VGA");
G
Gerd Hoffmann 已提交
1649 1650
    }

1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
    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),
1671
                             vc->tab_item, gtk_label_new(vc->label));
1672 1673
    gd_connect_vc_gfx_signals(vc);

1674
    group = gd_vc_menu_init(s, vc, idx, group, view_menu);
1675 1676 1677 1678 1679 1680 1681 1682

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

    return group;
}

A
Anthony Liguori 已提交
1683 1684 1685 1686 1687
static GtkWidget *gd_create_menu_view(GtkDisplayState *s, GtkAccelGroup *accel_group)
{
    GSList *group = NULL;
    GtkWidget *view_menu;
    GtkWidget *separator;
1688 1689
    QemuConsole *con;
    int vc;
A
Anthony Liguori 已提交
1690 1691 1692

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

1694
    s->full_screen_item = gtk_menu_item_new_with_mnemonic(_("_Fullscreen"));
1695 1696
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->full_screen_item),
                                 "<QEMU>/View/Full Screen");
J
Jan Kiszka 已提交
1697 1698
    gtk_accel_map_add_entry("<QEMU>/View/Full Screen", GDK_KEY_f,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1699
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->full_screen_item);
1700 1701

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

1704
    s->zoom_in_item = gtk_menu_item_new_with_mnemonic(_("Zoom _In"));
1705 1706
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_in_item),
                                 "<QEMU>/View/Zoom In");
J
Jan Kiszka 已提交
1707 1708
    gtk_accel_map_add_entry("<QEMU>/View/Zoom In", GDK_KEY_plus,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1709
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_in_item);
1710

1711
    s->zoom_out_item = gtk_menu_item_new_with_mnemonic(_("Zoom _Out"));
1712 1713
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_out_item),
                                 "<QEMU>/View/Zoom Out");
J
Jan Kiszka 已提交
1714 1715
    gtk_accel_map_add_entry("<QEMU>/View/Zoom Out", GDK_KEY_minus,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1716
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_out_item);
1717

1718
    s->zoom_fixed_item = gtk_menu_item_new_with_mnemonic(_("Best _Fit"));
1719 1720
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->zoom_fixed_item),
                                 "<QEMU>/View/Zoom Fixed");
J
Jan Kiszka 已提交
1721 1722
    gtk_accel_map_add_entry("<QEMU>/View/Zoom Fixed", GDK_KEY_0,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1723
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fixed_item);
1724

1725
    s->zoom_fit_item = gtk_check_menu_item_new_with_mnemonic(_("Zoom To _Fit"));
A
Anthony Liguori 已提交
1726
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->zoom_fit_item);
1727 1728

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

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

1734
    s->grab_item = gtk_check_menu_item_new_with_mnemonic(_("_Grab Input"));
1735 1736
    gtk_menu_item_set_accel_path(GTK_MENU_ITEM(s->grab_item),
                                 "<QEMU>/View/Grab Input");
J
Jan Kiszka 已提交
1737 1738
    gtk_accel_map_add_entry("<QEMU>/View/Grab Input", GDK_KEY_g,
                            HOTKEY_MODIFIERS);
A
Anthony Liguori 已提交
1739
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->grab_item);
1740

A
Anthony Liguori 已提交
1741
    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1742
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
A
Anthony Liguori 已提交
1743

1744
    /* gfx */
1745 1746 1747 1748 1749 1750 1751 1752 1753
    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 已提交
1754

1755
#if defined(CONFIG_VTE)
1756
    /* vte */
1757
    gd_vcs_init(s, group, view_menu);
1758
#endif
1759

A
Anthony Liguori 已提交
1760
    separator = gtk_separator_menu_item_new();
A
Anthony Liguori 已提交
1761
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), separator);
A
Anthony Liguori 已提交
1762

1763
    s->show_tabs_item = gtk_check_menu_item_new_with_mnemonic(_("Show _Tabs"));
A
Anthony Liguori 已提交
1764
    gtk_menu_shell_append(GTK_MENU_SHELL(view_menu), s->show_tabs_item);
A
Anthony Liguori 已提交
1765

1766 1767 1768
    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 已提交
1769 1770 1771 1772 1773 1774
    return view_menu;
}

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

A
Anthony Liguori 已提交
1776 1777 1778 1779 1780
    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"));
1781 1782 1783
    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 已提交
1784

A
Anthony Liguori 已提交
1785
    s->view_menu_item = gtk_menu_item_new_with_mnemonic(_("_View"));
A
Anthony Liguori 已提交
1786 1787
    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 已提交
1788 1789 1790 1791

    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 已提交
1792 1793
}

1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
static void gd_set_keycode_type(GtkDisplayState *s)
{
#ifndef _WIN32
    char *keycodes = NULL;
    GdkDisplay *display = gtk_widget_get_display(s->window);
    Display *x11_display = gdk_x11_display_get_xdisplay(display);
    XkbDescPtr desc = XkbGetKeyboard(x11_display, XkbGBN_AllComponentsMask,
                                     XkbUseCoreKbd);

    if (desc && desc->names) {
        keycodes = XGetAtomName(x11_display, desc->names->keycodes);
    }
    if (keycodes == NULL) {
        fprintf(stderr, "could not lookup keycode name\n");
    } else if (strstart(keycodes, "evdev", NULL)) {
        s->has_evdev = true;
    } else if (!strstart(keycodes, "xfree86", NULL)) {
        fprintf(stderr, "unknown keycodes `%s', please report to "
                "qemu-devel@nongnu.org\n", keycodes);
    }
#endif
}

1817
void gtk_display_init(DisplayState *ds, bool full_screen, bool grab_on_hover)
A
Anthony Liguori 已提交
1818 1819
{
    GtkDisplayState *s = g_malloc0(sizeof(*s));
S
Stefan Weil 已提交
1820
    char *filename;
A
Anthony Liguori 已提交
1821 1822 1823 1824

    gtk_init(NULL, NULL);

    s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1825 1826 1827
#if GTK_CHECK_VERSION(3, 2, 0)
    s->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
#else
A
Anthony Liguori 已提交
1828
    s->vbox = gtk_vbox_new(FALSE, 0);
1829
#endif
A
Anthony Liguori 已提交
1830 1831 1832
    s->notebook = gtk_notebook_new();
    s->menu_bar = gtk_menu_bar_new();

1833
    s->free_scale = FALSE;
A
Anthony Liguori 已提交
1834

1835 1836 1837 1838
    setlocale(LC_ALL, "");
    bindtextdomain("qemu", CONFIG_QEMU_LOCALEDIR);
    textdomain("qemu");

A
Anthony Liguori 已提交
1839 1840 1841 1842 1843 1844
    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 已提交
1845
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu_logo_no_text.svg");
S
Stefan Weil 已提交
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
    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 已提交
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
    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);

1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
#ifdef VTE_RESIZE_HACK
    {
        VirtualConsole *cur = gd_vc_find_current(s);
        int i;

        for (i = 0; i < s->nb_vcs; i++) {
            VirtualConsole *vc = &s->vc[i];
            if (vc && vc->type == GD_VC_VTE && vc != cur) {
                gtk_widget_hide(vc->vte.terminal);
            }
        }
        gd_update_windowsize(cur);
    }
#endif

P
Peter Wu 已提交
1888 1889 1890
    if (full_screen) {
        gtk_menu_item_activate(GTK_MENU_ITEM(s->full_screen_item));
    }
1891 1892 1893
    if (grab_on_hover) {
        gtk_menu_item_activate(GTK_MENU_ITEM(s->grab_on_hover_item));
    }
P
Peter Wu 已提交
1894

1895
    gd_set_keycode_type(s);
A
Anthony Liguori 已提交
1896
}
1897 1898 1899 1900 1901 1902 1903

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