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