]> git.proxmox.com Git - mirror_qemu.git/blame - ui/sdl2.c
migration: Move migrate_postcopy() to options.c
[mirror_qemu.git] / ui / sdl2.c
CommitLineData
47c03744
DA
1/*
2 * QEMU SDL display driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24/* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
25
e16f4c87 26#include "qemu/osdep.h"
0b8fa32f 27#include "qemu/module.h"
77d910fb 28#include "qemu/cutils.h"
47c03744
DA
29#include "ui/console.h"
30#include "ui/input.h"
5d0fe650 31#include "ui/sdl2.h"
54d31236 32#include "sysemu/runstate.h"
e6dba048 33#include "sysemu/runstate-action.h"
47c03744 34#include "sysemu/sysemu.h"
83047345 35#include "ui/win32-kbd-hook.h"
ed80f503 36#include "qemu/log.h"
47c03744 37
47c03744 38static int sdl2_num_outputs;
5d0fe650 39static struct sdl2_console *sdl2_console;
47c03744
DA
40
41static SDL_Surface *guest_sprite_surface;
42static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
9eafdeea
TH
43static bool alt_grab;
44static bool ctrl_grab;
47c03744 45
47c03744
DA
46static int gui_saved_grab;
47static int gui_fullscreen;
47c03744 48static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
47c03744
DA
49static SDL_Cursor *sdl_cursor_normal;
50static SDL_Cursor *sdl_cursor_hidden;
51static int absolute_enabled;
52static int guest_cursor;
53static int guest_x, guest_y;
54static SDL_Cursor *guest_sprite;
47c03744
DA
55static Notifier mouse_mode_notifier;
56
56bdd4b6
JM
57#define SDL2_REFRESH_INTERVAL_BUSY 10
58#define SDL2_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
59 / SDL2_REFRESH_INTERVAL_BUSY + 1)
60
da3f7a3a
MAL
61/* introduced in SDL 2.0.10 */
62#ifndef SDL_HINT_RENDER_BATCHING
63#define SDL_HINT_RENDER_BATCHING "SDL_RENDER_BATCHING"
64#endif
65
5d0fe650 66static void sdl_update_caption(struct sdl2_console *scon);
47c03744 67
5d0fe650 68static struct sdl2_console *get_scon_from_window(uint32_t window_id)
47c03744
DA
69{
70 int i;
71 for (i = 0; i < sdl2_num_outputs; i++) {
72 if (sdl2_console[i].real_window == SDL_GetWindowFromID(window_id)) {
73 return &sdl2_console[i];
74 }
75 }
76 return NULL;
77}
78
2c3056f1 79void sdl2_window_create(struct sdl2_console *scon)
47c03744 80{
46522a82 81 int flags = 0;
47c03744 82
46522a82
GH
83 if (!scon->surface) {
84 return;
85 }
86 assert(!scon->real_window);
87
88 if (gui_fullscreen) {
89 flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
47c03744 90 } else {
46522a82
GH
91 flags |= SDL_WINDOW_RESIZABLE;
92 }
93 if (scon->hidden) {
94 flags |= SDL_WINDOW_HIDDEN;
95 }
67c6f1db
JHW
96#ifdef CONFIG_OPENGL
97 if (scon->opengl) {
98 flags |= SDL_WINDOW_OPENGL;
99 }
100#endif
46522a82
GH
101
102 scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
103 SDL_WINDOWPOS_UNDEFINED,
104 surface_width(scon->surface),
105 surface_height(scon->surface),
106 flags);
da3f7a3a
MAL
107 if (scon->opengl) {
108 const char *driver = "opengl";
109
110 if (scon->opts->gl == DISPLAYGL_MODE_ES) {
111 driver = "opengles2";
112 }
113
114 SDL_SetHint(SDL_HINT_RENDER_DRIVER, driver);
115 SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
116 }
46522a82 117 scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
da3f7a3a 118
0b71a5d5 119 if (scon->opengl) {
6f2688f7 120 scon->winctx = SDL_GL_CreateContext(scon->real_window);
0b71a5d5 121 }
46522a82
GH
122 sdl_update_caption(scon);
123}
47c03744 124
2c3056f1 125void sdl2_window_destroy(struct sdl2_console *scon)
46522a82
GH
126{
127 if (!scon->real_window) {
128 return;
129 }
130
6f2688f7
MAL
131 SDL_GL_DeleteContext(scon->winctx);
132 scon->winctx = NULL;
46522a82
GH
133 SDL_DestroyRenderer(scon->real_renderer);
134 scon->real_renderer = NULL;
135 SDL_DestroyWindow(scon->real_window);
136 scon->real_window = NULL;
137}
138
2c3056f1 139void sdl2_window_resize(struct sdl2_console *scon)
46522a82
GH
140{
141 if (!scon->real_window) {
142 return;
47c03744 143 }
46522a82
GH
144
145 SDL_SetWindowSize(scon->real_window,
146 surface_width(scon->surface),
147 surface_height(scon->surface));
47c03744
DA
148}
149
0b71a5d5
GH
150static void sdl2_redraw(struct sdl2_console *scon)
151{
152 if (scon->opengl) {
153#ifdef CONFIG_OPENGL
154 sdl2_gl_redraw(scon);
155#endif
156 } else {
157 sdl2_2d_redraw(scon);
158 }
159}
160
5d0fe650 161static void sdl_update_caption(struct sdl2_console *scon)
47c03744
DA
162{
163 char win_title[1024];
164 char icon_title[1024];
165 const char *status = "";
166
167 if (!runstate_is_running()) {
168 status = " [Stopped]";
169 } else if (gui_grab) {
170 if (alt_grab) {
f8d2c936 171 status = " - Press Ctrl-Alt-Shift-G to exit grab";
47c03744 172 } else if (ctrl_grab) {
f8d2c936 173 status = " - Press Right-Ctrl-G to exit grab";
47c03744 174 } else {
f8d2c936 175 status = " - Press Ctrl-Alt-G to exit grab";
47c03744
DA
176 }
177 }
178
179 if (qemu_name) {
180 snprintf(win_title, sizeof(win_title), "QEMU (%s-%d)%s", qemu_name,
181 scon->idx, status);
182 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
183 } else {
184 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
185 snprintf(icon_title, sizeof(icon_title), "QEMU");
186 }
187
188 if (scon->real_window) {
189 SDL_SetWindowTitle(scon->real_window, win_title);
190 }
191}
192
86a088e6 193static void sdl_hide_cursor(struct sdl2_console *scon)
47c03744 194{
86a088e6 195 if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
47c03744
DA
196 return;
197 }
198
253347e1
JM
199 SDL_ShowCursor(SDL_DISABLE);
200 SDL_SetCursor(sdl_cursor_hidden);
201
202 if (!qemu_input_is_absolute()) {
2d968ffb 203 SDL_SetRelativeMouseMode(SDL_TRUE);
47c03744
DA
204 }
205}
206
86a088e6 207static void sdl_show_cursor(struct sdl2_console *scon)
47c03744 208{
86a088e6 209 if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
47c03744
DA
210 return;
211 }
212
213 if (!qemu_input_is_absolute()) {
2d968ffb 214 SDL_SetRelativeMouseMode(SDL_FALSE);
47c03744 215 }
253347e1
JM
216
217 if (guest_cursor &&
218 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
219 SDL_SetCursor(guest_sprite);
220 } else {
221 SDL_SetCursor(sdl_cursor_normal);
222 }
223
224 SDL_ShowCursor(SDL_ENABLE);
47c03744
DA
225}
226
5d0fe650 227static void sdl_grab_start(struct sdl2_console *scon)
47c03744 228{
f2335791
GH
229 QemuConsole *con = scon ? scon->dcl.con : NULL;
230
231 if (!con || !qemu_console_is_graphic(con)) {
232 return;
233 }
47c03744
DA
234 /*
235 * If the application is not active, do not try to enter grab state. This
236 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
237 * application (SDL bug).
238 */
239 if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) {
240 return;
241 }
242 if (guest_cursor) {
243 SDL_SetCursor(guest_sprite);
244 if (!qemu_input_is_absolute() && !absolute_enabled) {
245 SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y);
246 }
247 } else {
86a088e6 248 sdl_hide_cursor(scon);
47c03744
DA
249 }
250 SDL_SetWindowGrab(scon->real_window, SDL_TRUE);
251 gui_grab = 1;
83047345 252 win32_kbd_set_grab(true);
47c03744
DA
253 sdl_update_caption(scon);
254}
255
5d0fe650 256static void sdl_grab_end(struct sdl2_console *scon)
47c03744
DA
257{
258 SDL_SetWindowGrab(scon->real_window, SDL_FALSE);
259 gui_grab = 0;
83047345 260 win32_kbd_set_grab(false);
86a088e6 261 sdl_show_cursor(scon);
47c03744
DA
262 sdl_update_caption(scon);
263}
264
5d0fe650 265static void absolute_mouse_grab(struct sdl2_console *scon)
47c03744
DA
266{
267 int mouse_x, mouse_y;
268 int scr_w, scr_h;
269 SDL_GetMouseState(&mouse_x, &mouse_y);
270 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
271 if (mouse_x > 0 && mouse_x < scr_w - 1 &&
272 mouse_y > 0 && mouse_y < scr_h - 1) {
273 sdl_grab_start(scon);
274 }
275}
276
277static void sdl_mouse_mode_change(Notifier *notify, void *data)
278{
279 if (qemu_input_is_absolute()) {
280 if (!absolute_enabled) {
281 absolute_enabled = 1;
8dfa3061 282 SDL_SetRelativeMouseMode(SDL_FALSE);
47c03744
DA
283 absolute_mouse_grab(&sdl2_console[0]);
284 }
285 } else if (absolute_enabled) {
286 if (!gui_fullscreen) {
287 sdl_grab_end(&sdl2_console[0]);
288 }
289 absolute_enabled = 0;
290 }
291}
292
5d0fe650 293static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
3f2fde2a 294 int x, int y, int state)
47c03744 295{
7fb1cf16 296 static uint32_t bmap[INPUT_BUTTON__MAX] = {
47c03744
DA
297 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
298 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
299 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
29511061
DW
300 [INPUT_BUTTON_SIDE] = SDL_BUTTON(SDL_BUTTON_X1),
301 [INPUT_BUTTON_EXTRA] = SDL_BUTTON(SDL_BUTTON_X2)
47c03744
DA
302 };
303 static uint32_t prev_state;
304
305 if (prev_state != state) {
306 qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state);
307 prev_state = state;
308 }
309
310 if (qemu_input_is_absolute()) {
d9f06262
JM
311 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X,
312 x, 0, surface_width(scon->surface));
313 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y,
314 y, 0, surface_height(scon->surface));
afbc0dd6
CR
315 } else {
316 if (guest_cursor) {
317 x -= guest_x;
318 y -= guest_y;
319 guest_x += x;
320 guest_y += y;
321 dx = x;
322 dy = y;
323 }
324 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
325 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
47c03744
DA
326 }
327 qemu_input_event_sync();
328}
329
5d0fe650 330static void toggle_full_screen(struct sdl2_console *scon)
47c03744 331{
47c03744
DA
332 gui_fullscreen = !gui_fullscreen;
333 if (gui_fullscreen) {
46522a82
GH
334 SDL_SetWindowFullscreen(scon->real_window,
335 SDL_WINDOW_FULLSCREEN_DESKTOP);
47c03744
DA
336 gui_saved_grab = gui_grab;
337 sdl_grab_start(scon);
338 } else {
47c03744
DA
339 if (!gui_saved_grab) {
340 sdl_grab_end(scon);
341 }
46522a82 342 SDL_SetWindowFullscreen(scon->real_window, 0);
47c03744 343 }
0b71a5d5 344 sdl2_redraw(scon);
47c03744
DA
345}
346
849bbe60 347static int get_mod_state(void)
47c03744 348{
849bbe60 349 SDL_Keymod mod = SDL_GetModState();
47c03744
DA
350
351 if (alt_grab) {
849bbe60 352 return (mod & (gui_grab_code | KMOD_LSHIFT)) ==
47c03744
DA
353 (gui_grab_code | KMOD_LSHIFT);
354 } else if (ctrl_grab) {
849bbe60 355 return (mod & KMOD_RCTRL) == KMOD_RCTRL;
47c03744 356 } else {
849bbe60 357 return (mod & gui_grab_code) == gui_grab_code;
47c03744 358 }
849bbe60 359}
47c03744 360
83047345
VR
361static void *sdl2_win32_get_hwnd(struct sdl2_console *scon)
362{
363#ifdef CONFIG_WIN32
364 SDL_SysWMinfo info;
365
366 SDL_VERSION(&info.version);
367 if (SDL_GetWindowWMInfo(scon->real_window, &info)) {
368 return info.info.win.window;
369 }
370#endif
371 return NULL;
372}
373
849bbe60
JM
374static void handle_keydown(SDL_Event *ev)
375{
376 int win;
377 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
afb92eb9 378 int gui_key_modifier_pressed = get_mod_state();
07333e1c 379 int gui_keysym = 0;
849bbe60 380
32ec9839
CD
381 if (!scon) {
382 return;
383 }
384
849bbe60 385 if (!scon->ignore_hotkeys && gui_key_modifier_pressed && !ev->key.repeat) {
47c03744 386 switch (ev->key.keysym.scancode) {
363f59d9
GH
387 case SDL_SCANCODE_2:
388 case SDL_SCANCODE_3:
389 case SDL_SCANCODE_4:
390 case SDL_SCANCODE_5:
391 case SDL_SCANCODE_6:
392 case SDL_SCANCODE_7:
393 case SDL_SCANCODE_8:
394 case SDL_SCANCODE_9:
56f289f3
CR
395 if (gui_grab) {
396 sdl_grab_end(scon);
397 }
398
363f59d9
GH
399 win = ev->key.keysym.scancode - SDL_SCANCODE_1;
400 if (win < sdl2_num_outputs) {
401 sdl2_console[win].hidden = !sdl2_console[win].hidden;
402 if (sdl2_console[win].real_window) {
403 if (sdl2_console[win].hidden) {
404 SDL_HideWindow(sdl2_console[win].real_window);
405 } else {
406 SDL_ShowWindow(sdl2_console[win].real_window);
407 }
408 }
409 gui_keysym = 1;
410 }
411 break;
47c03744
DA
412 case SDL_SCANCODE_F:
413 toggle_full_screen(scon);
414 gui_keysym = 1;
415 break;
f8d2c936
GH
416 case SDL_SCANCODE_G:
417 gui_keysym = 1;
418 if (!gui_grab) {
419 sdl_grab_start(scon);
420 } else if (!gui_fullscreen) {
421 sdl_grab_end(scon);
422 }
423 break;
47c03744 424 case SDL_SCANCODE_U:
64bf97e5 425 sdl2_window_resize(scon);
0b71a5d5
GH
426 if (!scon->opengl) {
427 /* re-create scon->texture */
428 sdl2_2d_switch(&scon->dcl, scon->surface);
429 }
47c03744
DA
430 gui_keysym = 1;
431 break;
46522a82 432#if 0
47c03744
DA
433 case SDL_SCANCODE_KP_PLUS:
434 case SDL_SCANCODE_KP_MINUS:
435 if (!gui_fullscreen) {
436 int scr_w, scr_h;
437 int width, height;
438 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
439
440 width = MAX(scr_w + (ev->key.keysym.scancode ==
441 SDL_SCANCODE_KP_PLUS ? 50 : -50),
442 160);
443 height = (surface_height(scon->surface) * width) /
444 surface_width(scon->surface);
46522a82
GH
445 fprintf(stderr, "%s: scale to %dx%d\n",
446 __func__, width, height);
47c03744 447 sdl_scale(scon, width, height);
0b71a5d5 448 sdl2_redraw(scon);
47c03744
DA
449 gui_keysym = 1;
450 }
46522a82 451#endif
47c03744
DA
452 default:
453 break;
454 }
455 }
456 if (!gui_keysym) {
8fc1a3f5 457 sdl2_process_key(scon, &ev->key);
47c03744
DA
458 }
459}
460
461static void handle_keyup(SDL_Event *ev)
462{
5d0fe650 463 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
47c03744 464
32ec9839
CD
465 if (!scon) {
466 return;
467 }
468
849bbe60 469 scon->ignore_hotkeys = false;
07333e1c 470 sdl2_process_key(scon, &ev->key);
47c03744
DA
471}
472
f2335791
GH
473static void handle_textinput(SDL_Event *ev)
474{
48db08cf 475 struct sdl2_console *scon = get_scon_from_window(ev->text.windowID);
f2335791
GH
476 QemuConsole *con = scon ? scon->dcl.con : NULL;
477
32ec9839
CD
478 if (!con) {
479 return;
480 }
481
f2335791
GH
482 if (qemu_console_is_graphic(con)) {
483 return;
484 }
485 kbd_put_string_console(con, ev->text.text, strlen(ev->text.text));
486}
487
47c03744
DA
488static void handle_mousemotion(SDL_Event *ev)
489{
490 int max_x, max_y;
48db08cf 491 struct sdl2_console *scon = get_scon_from_window(ev->motion.windowID);
47c03744 492
49213b72 493 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
28216716
JM
494 return;
495 }
496
47c03744
DA
497 if (qemu_input_is_absolute() || absolute_enabled) {
498 int scr_w, scr_h;
499 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
500 max_x = scr_w - 1;
501 max_y = scr_h - 1;
24952847
JM
502 if (gui_grab && !gui_fullscreen
503 && (ev->motion.x == 0 || ev->motion.y == 0 ||
504 ev->motion.x == max_x || ev->motion.y == max_y)) {
47c03744
DA
505 sdl_grab_end(scon);
506 }
507 if (!gui_grab &&
508 (ev->motion.x > 0 && ev->motion.x < max_x &&
509 ev->motion.y > 0 && ev->motion.y < max_y)) {
510 sdl_grab_start(scon);
511 }
512 }
513 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
3f2fde2a 514 sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel,
47c03744
DA
515 ev->motion.x, ev->motion.y, ev->motion.state);
516 }
517}
518
519static void handle_mousebutton(SDL_Event *ev)
520{
521 int buttonstate = SDL_GetMouseState(NULL, NULL);
522 SDL_MouseButtonEvent *bev;
48db08cf 523 struct sdl2_console *scon = get_scon_from_window(ev->button.windowID);
47c03744 524
49213b72 525 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
28216716
JM
526 return;
527 }
528
47c03744
DA
529 bev = &ev->button;
530 if (!gui_grab && !qemu_input_is_absolute()) {
531 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
532 /* start grabbing all events */
533 sdl_grab_start(scon);
534 }
535 } else {
47c03744
DA
536 if (ev->type == SDL_MOUSEBUTTONDOWN) {
537 buttonstate |= SDL_BUTTON(bev->button);
538 } else {
539 buttonstate &= ~SDL_BUTTON(bev->button);
540 }
3f2fde2a
CR
541 sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate);
542 }
543}
544
545static void handle_mousewheel(SDL_Event *ev)
546{
48db08cf 547 struct sdl2_console *scon = get_scon_from_window(ev->wheel.windowID);
3f2fde2a
CR
548 SDL_MouseWheelEvent *wev = &ev->wheel;
549 InputButton btn;
550
49213b72 551 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
28216716
JM
552 return;
553 }
554
3f2fde2a 555 if (wev->y > 0) {
f22d0af0 556 btn = INPUT_BUTTON_WHEEL_UP;
3f2fde2a 557 } else if (wev->y < 0) {
f22d0af0 558 btn = INPUT_BUTTON_WHEEL_DOWN;
ed80f503
DP
559 } else if (wev->x < 0) {
560 btn = INPUT_BUTTON_WHEEL_RIGHT;
561 } else if (wev->x > 0) {
562 btn = INPUT_BUTTON_WHEEL_LEFT;
3f2fde2a
CR
563 } else {
564 return;
47c03744 565 }
3f2fde2a
CR
566
567 qemu_input_queue_btn(scon->dcl.con, btn, true);
568 qemu_input_event_sync();
569 qemu_input_queue_btn(scon->dcl.con, btn, false);
570 qemu_input_event_sync();
47c03744
DA
571}
572
1dfc5c88 573static void handle_windowevent(SDL_Event *ev)
47c03744 574{
1dfc5c88 575 struct sdl2_console *scon = get_scon_from_window(ev->window.windowID);
fe91f36a 576 bool allow_close = true;
1dfc5c88 577
08d49df0
AG
578 if (!scon) {
579 return;
580 }
581
47c03744
DA
582 switch (ev->window.event) {
583 case SDL_WINDOWEVENT_RESIZED:
8b15d9f1
DA
584 {
585 QemuUIInfo info;
586 memset(&info, 0, sizeof(info));
587 info.width = ev->window.data1;
588 info.height = ev->window.data2;
ca19ef52 589 dpy_set_ui_info(scon->dcl.con, &info, true);
8b15d9f1 590 }
0b71a5d5 591 sdl2_redraw(scon);
47c03744
DA
592 break;
593 case SDL_WINDOWEVENT_EXPOSED:
0b71a5d5 594 sdl2_redraw(scon);
47c03744
DA
595 break;
596 case SDL_WINDOWEVENT_FOCUS_GAINED:
83047345
VR
597 win32_kbd_set_grab(gui_grab);
598 if (qemu_console_is_graphic(scon->dcl.con)) {
599 win32_kbd_set_window(sdl2_win32_get_hwnd(scon));
600 }
601 /* fall through */
47c03744
DA
602 case SDL_WINDOWEVENT_ENTER:
603 if (!gui_grab && (qemu_input_is_absolute() || absolute_enabled)) {
604 absolute_mouse_grab(scon);
605 }
849bbe60
JM
606 /* If a new console window opened using a hotkey receives the
607 * focus, SDL sends another KEYDOWN event to the new window,
608 * closing the console window immediately after.
609 *
610 * Work around this by ignoring further hotkey events until a
611 * key is released.
612 */
613 scon->ignore_hotkeys = get_mod_state();
47c03744
DA
614 break;
615 case SDL_WINDOWEVENT_FOCUS_LOST:
83047345
VR
616 if (qemu_console_is_graphic(scon->dcl.con)) {
617 win32_kbd_set_window(NULL);
618 }
47c03744
DA
619 if (gui_grab && !gui_fullscreen) {
620 sdl_grab_end(scon);
621 }
622 break;
623 case SDL_WINDOWEVENT_RESTORED:
63ed4907 624 update_displaychangelistener(&scon->dcl, GUI_REFRESH_INTERVAL_DEFAULT);
47c03744
DA
625 break;
626 case SDL_WINDOWEVENT_MINIMIZED:
63ed4907 627 update_displaychangelistener(&scon->dcl, 500);
47c03744
DA
628 break;
629 case SDL_WINDOWEVENT_CLOSE:
fc49e727 630 if (qemu_console_is_graphic(scon->dcl.con)) {
844fd50d 631 if (scon->opts->has_window_close && !scon->opts->window_close) {
fe91f36a
GH
632 allow_close = false;
633 }
634 if (allow_close) {
e6dba048 635 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
fc49e727
JM
636 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
637 }
638 } else {
639 SDL_HideWindow(scon->real_window);
640 scon->hidden = true;
47c03744
DA
641 }
642 break;
d3f3a0f4 643 case SDL_WINDOWEVENT_SHOWN:
bcf43cdc 644 scon->hidden = false;
d3f3a0f4
HR
645 break;
646 case SDL_WINDOWEVENT_HIDDEN:
bcf43cdc 647 scon->hidden = true;
d3f3a0f4 648 break;
47c03744
DA
649 }
650}
651
63ed4907 652void sdl2_poll_events(struct sdl2_console *scon)
47c03744 653{
47c03744 654 SDL_Event ev1, *ev = &ev1;
fe91f36a 655 bool allow_close = true;
56bdd4b6 656 int idle = 1;
47c03744
DA
657
658 if (scon->last_vm_running != runstate_is_running()) {
659 scon->last_vm_running = runstate_is_running();
660 sdl_update_caption(scon);
661 }
662
47c03744
DA
663 while (SDL_PollEvent(ev)) {
664 switch (ev->type) {
665 case SDL_KEYDOWN:
56bdd4b6 666 idle = 0;
47c03744
DA
667 handle_keydown(ev);
668 break;
669 case SDL_KEYUP:
56bdd4b6 670 idle = 0;
47c03744
DA
671 handle_keyup(ev);
672 break;
f2335791 673 case SDL_TEXTINPUT:
56bdd4b6 674 idle = 0;
f2335791
GH
675 handle_textinput(ev);
676 break;
47c03744 677 case SDL_QUIT:
844fd50d 678 if (scon->opts->has_window_close && !scon->opts->window_close) {
fe91f36a
GH
679 allow_close = false;
680 }
681 if (allow_close) {
e6dba048 682 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
cf83f140 683 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
47c03744
DA
684 }
685 break;
686 case SDL_MOUSEMOTION:
56bdd4b6 687 idle = 0;
47c03744
DA
688 handle_mousemotion(ev);
689 break;
690 case SDL_MOUSEBUTTONDOWN:
691 case SDL_MOUSEBUTTONUP:
56bdd4b6 692 idle = 0;
47c03744
DA
693 handle_mousebutton(ev);
694 break;
3f2fde2a 695 case SDL_MOUSEWHEEL:
56bdd4b6 696 idle = 0;
3f2fde2a
CR
697 handle_mousewheel(ev);
698 break;
47c03744 699 case SDL_WINDOWEVENT:
1dfc5c88 700 handle_windowevent(ev);
47c03744
DA
701 break;
702 default:
703 break;
704 }
705 }
56bdd4b6
JM
706
707 if (idle) {
708 if (scon->idle_counter < SDL2_MAX_IDLE_COUNT) {
709 scon->idle_counter++;
710 if (scon->idle_counter >= SDL2_MAX_IDLE_COUNT) {
711 scon->dcl.update_interval = GUI_REFRESH_INTERVAL_DEFAULT;
712 }
713 }
714 } else {
715 scon->idle_counter = 0;
716 scon->dcl.update_interval = SDL2_REFRESH_INTERVAL_BUSY;
717 }
47c03744
DA
718}
719
720static void sdl_mouse_warp(DisplayChangeListener *dcl,
721 int x, int y, int on)
722{
5d0fe650 723 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
28216716
JM
724
725 if (!qemu_console_is_graphic(scon->dcl.con)) {
726 return;
727 }
728
47c03744
DA
729 if (on) {
730 if (!guest_cursor) {
86a088e6 731 sdl_show_cursor(scon);
47c03744
DA
732 }
733 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
734 SDL_SetCursor(guest_sprite);
735 if (!qemu_input_is_absolute() && !absolute_enabled) {
736 SDL_WarpMouseInWindow(scon->real_window, x, y);
737 }
738 }
739 } else if (gui_grab) {
86a088e6 740 sdl_hide_cursor(scon);
47c03744
DA
741 }
742 guest_cursor = on;
743 guest_x = x, guest_y = y;
744}
745
746static void sdl_mouse_define(DisplayChangeListener *dcl,
747 QEMUCursor *c)
748{
749
750 if (guest_sprite) {
751 SDL_FreeCursor(guest_sprite);
752 }
753
754 if (guest_sprite_surface) {
755 SDL_FreeSurface(guest_sprite_surface);
756 }
757
758 guest_sprite_surface =
759 SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4,
760 0xff0000, 0x00ff00, 0xff, 0xff000000);
761
762 if (!guest_sprite_surface) {
763 fprintf(stderr, "Failed to make rgb surface from %p\n", c);
764 return;
765 }
766 guest_sprite = SDL_CreateColorCursor(guest_sprite_surface,
767 c->hot_x, c->hot_y);
768 if (!guest_sprite) {
769 fprintf(stderr, "Failed to make color cursor from %p\n", c);
770 return;
771 }
772 if (guest_cursor &&
773 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
774 SDL_SetCursor(guest_sprite);
775 }
776}
777
778static void sdl_cleanup(void)
779{
780 if (guest_sprite) {
781 SDL_FreeCursor(guest_sprite);
782 }
783 SDL_QuitSubSystem(SDL_INIT_VIDEO);
784}
785
f1ddebd8 786static const DisplayChangeListenerOps dcl_2d_ops = {
877417d9
GH
787 .dpy_name = "sdl2-2d",
788 .dpy_gfx_update = sdl2_2d_update,
789 .dpy_gfx_switch = sdl2_2d_switch,
790 .dpy_gfx_check_format = sdl2_2d_check_format,
791 .dpy_refresh = sdl2_2d_refresh,
792 .dpy_mouse_set = sdl_mouse_warp,
793 .dpy_cursor_define = sdl_mouse_define,
47c03744
DA
794};
795
0b71a5d5
GH
796#ifdef CONFIG_OPENGL
797static const DisplayChangeListenerOps dcl_gl_ops = {
798 .dpy_name = "sdl2-gl",
799 .dpy_gfx_update = sdl2_gl_update,
800 .dpy_gfx_switch = sdl2_gl_switch,
801 .dpy_gfx_check_format = console_gl_check_format,
802 .dpy_refresh = sdl2_gl_refresh,
803 .dpy_mouse_set = sdl_mouse_warp,
804 .dpy_cursor_define = sdl_mouse_define,
cb47dc9a 805
db6cdfbe 806 .dpy_gl_scanout_disable = sdl2_gl_scanout_disable,
f4c36bda 807 .dpy_gl_scanout_texture = sdl2_gl_scanout_texture,
cb47dc9a 808 .dpy_gl_update = sdl2_gl_scanout_flush,
0b71a5d5 809};
5e79d516 810
a62c4a17
MAL
811static bool
812sdl2_gl_is_compatible_dcl(DisplayGLCtx *dgc,
813 DisplayChangeListener *dcl)
814{
815 return dcl->ops == &dcl_gl_ops;
816}
817
5e79d516 818static const DisplayGLCtxOps gl_ctx_ops = {
a62c4a17 819 .dpy_gl_ctx_is_compatible_dcl = sdl2_gl_is_compatible_dcl,
5e79d516
MAL
820 .dpy_gl_ctx_create = sdl2_gl_create_context,
821 .dpy_gl_ctx_destroy = sdl2_gl_destroy_context,
822 .dpy_gl_ctx_make_current = sdl2_gl_make_context_current,
823};
0b71a5d5
GH
824#endif
825
5ee1718f 826static void sdl2_display_early_init(DisplayOptions *o)
0b71a5d5 827{
fe91f36a
GH
828 assert(o->type == DISPLAY_TYPE_SDL);
829 if (o->has_gl && o->gl) {
0b71a5d5
GH
830#ifdef CONFIG_OPENGL
831 display_opengl = 1;
832#endif
0b71a5d5
GH
833 }
834}
835
5ee1718f 836static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
47c03744 837{
47c03744 838 uint8_t data = 0;
47c03744 839 int i;
d8253671 840 SDL_SysWMinfo info;
a442fe2f 841 SDL_Surface *icon = NULL;
77d910fb 842 char *dir;
47c03744 843
fe91f36a 844 assert(o->type == DISPLAY_TYPE_SDL);
fe91f36a 845
82f483dd
MAL
846 if (SDL_GetHintBoolean("QEMU_ENABLE_SDL_LOGGING", SDL_FALSE)) {
847 SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
848 }
849
2313e482 850 if (SDL_Init(SDL_INIT_VIDEO)) {
47c03744
DA
851 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
852 SDL_GetError());
853 exit(1);
854 }
8c2b816f
SK
855#ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* only available since SDL 2.0.8 */
856 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
857#endif
44f017d0 858 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
d8253671
ST
859 memset(&info, 0, sizeof(info));
860 SDL_VERSION(&info.version);
47c03744 861
6fb34ffc
TH
862 gui_fullscreen = o->has_full_screen && o->full_screen;
863
9eafdeea
TH
864 if (o->u.sdl.has_grab_mod) {
865 if (o->u.sdl.grab_mod == HOT_KEY_MOD_LSHIFT_LCTRL_LALT) {
866 alt_grab = true;
867 } else if (o->u.sdl.grab_mod == HOT_KEY_MOD_RCTRL) {
868 ctrl_grab = true;
869 }
870 }
871
47c03744
DA
872 for (i = 0;; i++) {
873 QemuConsole *con = qemu_console_lookup_by_index(i);
f2335791 874 if (!con) {
47c03744
DA
875 break;
876 }
877 }
878 sdl2_num_outputs = i;
8efa5f29
GH
879 if (sdl2_num_outputs == 0) {
880 return;
881 }
5d0fe650 882 sdl2_console = g_new0(struct sdl2_console, sdl2_num_outputs);
47c03744
DA
883 for (i = 0; i < sdl2_num_outputs; i++) {
884 QemuConsole *con = qemu_console_lookup_by_index(i);
85970a62 885 assert(con != NULL);
6624c38d
GH
886 if (!qemu_console_is_graphic(con) &&
887 qemu_console_get_index(con) != 0) {
f2335791
GH
888 sdl2_console[i].hidden = true;
889 }
0b71a5d5 890 sdl2_console[i].idx = i;
f88e5c57 891 sdl2_console[i].opts = o;
0b71a5d5
GH
892#ifdef CONFIG_OPENGL
893 sdl2_console[i].opengl = display_opengl;
894 sdl2_console[i].dcl.ops = display_opengl ? &dcl_gl_ops : &dcl_2d_ops;
5e79d516 895 sdl2_console[i].dgc.ops = display_opengl ? &gl_ctx_ops : NULL;
0b71a5d5
GH
896#else
897 sdl2_console[i].opengl = 0;
f1ddebd8 898 sdl2_console[i].dcl.ops = &dcl_2d_ops;
0b71a5d5 899#endif
47c03744 900 sdl2_console[i].dcl.con = con;
07333e1c 901 sdl2_console[i].kbd = qkbd_state_init(con);
ac32b2ff 902 if (display_opengl) {
5e79d516 903 qemu_console_set_display_gl_ctx(con, &sdl2_console[i].dgc);
ac32b2ff 904 }
47c03744 905 register_displaychangelistener(&sdl2_console[i].dcl);
d8253671 906
8417cd00 907#if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
d8253671 908 if (SDL_GetWindowWMInfo(sdl2_console[i].real_window, &info)) {
8417cd00
GH
909#if defined(SDL_VIDEO_DRIVER_WINDOWS)
910 qemu_console_set_window_id(con, (uintptr_t)info.info.win.window);
911#elif defined(SDL_VIDEO_DRIVER_X11)
d8253671 912 qemu_console_set_window_id(con, info.info.x11.window);
8417cd00 913#endif
d8253671 914 }
8417cd00 915#endif
47c03744
DA
916 }
917
a442fe2f 918#ifdef CONFIG_SDL_IMAGE
77d910fb
PB
919 dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/128x128/apps/qemu.png");
920 icon = IMG_Load(dir);
a442fe2f 921#else
47c03744 922 /* Load a 32x32x4 image. White pixels are transparent. */
77d910fb
PB
923 dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/32x32/apps/qemu.bmp");
924 icon = SDL_LoadBMP(dir);
a442fe2f
DB
925 if (icon) {
926 uint32_t colorkey = SDL_MapRGB(icon->format, 255, 255, 255);
927 SDL_SetColorKey(icon, SDL_TRUE, colorkey);
928 }
929#endif
77d910fb 930 g_free(dir);
a442fe2f
DB
931 if (icon) {
932 SDL_SetWindowIcon(sdl2_console[0].real_window, icon);
47c03744
DA
933 }
934
47c03744
DA
935 mouse_mode_notifier.notify = sdl_mouse_mode_change;
936 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
937
47c03744
DA
938 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
939 sdl_cursor_normal = SDL_GetCursor();
940
7dafc679
VR
941 if (gui_fullscreen) {
942 sdl_grab_start(&sdl2_console[0]);
943 }
944
47c03744
DA
945 atexit(sdl_cleanup);
946}
5ee1718f
GH
947
948static QemuDisplay qemu_display_sdl2 = {
949 .type = DISPLAY_TYPE_SDL,
950 .early_init = sdl2_display_early_init,
951 .init = sdl2_display_init,
952};
953
954static void register_sdl1(void)
955{
956 qemu_display_register(&qemu_display_sdl2);
957}
958
959type_init(register_sdl1);
b36ae1c1
GH
960
961#ifdef CONFIG_OPENGL
962module_dep("ui-opengl");
963#endif