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