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