blob: 45f23b15e0fc14fd13225fc468d0a0b5e6bf0d9f [file] [log] [blame]
Dave Airlie47c03742013-12-10 14:05:51 +10001/*
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
26/* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
27#undef WIN32_LEAN_AND_MEAN
28
29#include <SDL.h>
Dave Airlie47c03742013-12-10 14:05:51 +100030#include <SDL_syswm.h>
31
32#include "qemu-common.h"
33#include "ui/console.h"
34#include "ui/input.h"
35#include "sysemu/sysemu.h"
Dave Airlie47c03742013-12-10 14:05:51 +100036
37#include "sdl2-keymap.h"
38
39static int sdl2_num_outputs;
40static struct sdl2_state {
41 DisplayChangeListener dcl;
42 DisplaySurface *surface;
43 SDL_Texture *texture;
44 SDL_Window *real_window;
45 SDL_Renderer *real_renderer;
46 int idx;
47 int last_vm_running; /* per console for caption reasons */
48 int x, y;
Gerd Hoffmann363f59d2014-05-27 09:44:39 +020049 int hidden;
Dave Airlie47c03742013-12-10 14:05:51 +100050} *sdl2_console;
51
52static SDL_Surface *guest_sprite_surface;
53static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
54
55static bool gui_saved_scaling;
56static int gui_saved_width;
57static int gui_saved_height;
58static int gui_saved_grab;
59static int gui_fullscreen;
60static int gui_noframe;
61static int gui_key_modifier_pressed;
62static int gui_keysym;
63static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
64static uint8_t modifiers_state[SDL_NUM_SCANCODES];
65static SDL_Cursor *sdl_cursor_normal;
66static SDL_Cursor *sdl_cursor_hidden;
67static int absolute_enabled;
68static int guest_cursor;
69static int guest_x, guest_y;
70static SDL_Cursor *guest_sprite;
71static int scaling_active;
72static Notifier mouse_mode_notifier;
73
74static void sdl_update_caption(struct sdl2_state *scon);
75
76static struct sdl2_state *get_scon_from_window(uint32_t window_id)
77{
78 int i;
79 for (i = 0; i < sdl2_num_outputs; i++) {
80 if (sdl2_console[i].real_window == SDL_GetWindowFromID(window_id)) {
81 return &sdl2_console[i];
82 }
83 }
84 return NULL;
85}
86
87static void sdl_update(DisplayChangeListener *dcl,
88 int x, int y, int w, int h)
89{
90 struct sdl2_state *scon = container_of(dcl, struct sdl2_state, dcl);
91 SDL_Rect rect;
92 DisplaySurface *surf = qemu_console_surface(dcl->con);
93
94 if (!surf) {
95 return;
96 }
97 if (!scon->texture) {
98 return;
99 }
100
101 rect.x = x;
102 rect.y = y;
103 rect.w = w;
104 rect.h = h;
105
106 SDL_UpdateTexture(scon->texture, NULL, surface_data(surf),
107 surface_stride(surf));
108 SDL_RenderCopy(scon->real_renderer, scon->texture, &rect, &rect);
109 SDL_RenderPresent(scon->real_renderer);
110}
111
112static void do_sdl_resize(struct sdl2_state *scon, int width, int height,
113 int bpp)
114{
115 int flags;
116
117 if (scon->real_window && scon->real_renderer) {
118 if (width && height) {
119 SDL_RenderSetLogicalSize(scon->real_renderer, width, height);
120 SDL_SetWindowSize(scon->real_window, width, height);
121 } else {
122 SDL_DestroyRenderer(scon->real_renderer);
123 SDL_DestroyWindow(scon->real_window);
124 scon->real_renderer = NULL;
125 scon->real_window = NULL;
126 }
127 } else {
128 if (!width || !height) {
129 return;
130 }
131 flags = 0;
132 if (gui_fullscreen) {
133 flags |= SDL_WINDOW_FULLSCREEN;
134 } else {
135 flags |= SDL_WINDOW_RESIZABLE;
136 }
Gerd Hoffmann363f59d2014-05-27 09:44:39 +0200137 if (scon->hidden) {
138 flags |= SDL_WINDOW_HIDDEN;
139 }
Dave Airlie47c03742013-12-10 14:05:51 +1000140
141 scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
142 SDL_WINDOWPOS_UNDEFINED,
143 width, height, flags);
144 scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
145 sdl_update_caption(scon);
146 }
147}
148
149static void sdl_switch(DisplayChangeListener *dcl,
150 DisplaySurface *new_surface)
151{
152 struct sdl2_state *scon = container_of(dcl, struct sdl2_state, dcl);
153 int format = 0;
154 int idx = scon->idx;
155 DisplaySurface *old_surface = scon->surface;
156
157 /* temporary hack: allows to call sdl_switch to handle scaling changes */
158 if (new_surface) {
159 scon->surface = new_surface;
160 }
161
162 if (!new_surface && idx > 0) {
163 scon->surface = NULL;
164 }
165
166 if (new_surface == NULL) {
167 do_sdl_resize(scon, 0, 0, 0);
168 } else {
169 do_sdl_resize(scon, surface_width(scon->surface),
170 surface_height(scon->surface), 0);
171 }
172
173 if (old_surface && scon->texture) {
174 SDL_DestroyTexture(scon->texture);
175 scon->texture = NULL;
176 }
177
178 if (new_surface) {
179 if (!scon->texture) {
180 if (surface_bits_per_pixel(scon->surface) == 16) {
181 format = SDL_PIXELFORMAT_RGB565;
182 } else if (surface_bits_per_pixel(scon->surface) == 32) {
183 format = SDL_PIXELFORMAT_ARGB8888;
184 }
185
186 scon->texture = SDL_CreateTexture(scon->real_renderer, format,
187 SDL_TEXTUREACCESS_STREAMING,
188 surface_width(new_surface),
189 surface_height(new_surface));
190 }
191 }
192}
193
Gerd Hoffmannee8c0b62014-05-20 08:11:25 +0200194static void reset_keys(struct sdl2_state *scon)
Dave Airlie47c03742013-12-10 14:05:51 +1000195{
Gerd Hoffmannee8c0b62014-05-20 08:11:25 +0200196 QemuConsole *con = scon ? scon->dcl.con : NULL;
Dave Airlie47c03742013-12-10 14:05:51 +1000197 int i;
198
199 for (i = 0; i < 256; i++) {
200 if (modifiers_state[i]) {
201 int qcode = sdl2_scancode_to_qcode[i];
Gerd Hoffmannee8c0b62014-05-20 08:11:25 +0200202 qemu_input_event_send_key_qcode(con, qcode, false);
Dave Airlie47c03742013-12-10 14:05:51 +1000203 modifiers_state[i] = 0;
204 }
205 }
206}
207
Gerd Hoffmannee8c0b62014-05-20 08:11:25 +0200208static void sdl_process_key(struct sdl2_state *scon,
209 SDL_KeyboardEvent *ev)
Dave Airlie47c03742013-12-10 14:05:51 +1000210{
211 int qcode = sdl2_scancode_to_qcode[ev->keysym.scancode];
Gerd Hoffmannee8c0b62014-05-20 08:11:25 +0200212 QemuConsole *con = scon ? scon->dcl.con : NULL;
Dave Airlie47c03742013-12-10 14:05:51 +1000213
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200214 if (!qemu_console_is_graphic(con)) {
215 if (ev->type == SDL_KEYDOWN) {
216 switch (ev->keysym.scancode) {
217 case SDL_SCANCODE_RETURN:
218 kbd_put_keysym_console(con, '\n');
219 break;
220 case SDL_SCANCODE_BACKSPACE:
221 kbd_put_keysym_console(con, QEMU_KEY_BACKSPACE);
222 break;
223 default:
224 kbd_put_qcode_console(con, qcode);
225 break;
226 }
227 }
228 return;
229 }
230
Dave Airlie47c03742013-12-10 14:05:51 +1000231 switch (ev->keysym.scancode) {
232#if 0
233 case SDL_SCANCODE_NUMLOCKCLEAR:
234 case SDL_SCANCODE_CAPSLOCK:
235 /* SDL does not send the key up event, so we generate it */
Gerd Hoffmannee8c0b62014-05-20 08:11:25 +0200236 qemu_input_event_send_key_qcode(con, qcode, true);
237 qemu_input_event_send_key_qcode(con, qcode, false);
Dave Airlie47c03742013-12-10 14:05:51 +1000238 return;
239#endif
240 case SDL_SCANCODE_LCTRL:
241 case SDL_SCANCODE_LSHIFT:
242 case SDL_SCANCODE_LALT:
243 case SDL_SCANCODE_LGUI:
244 case SDL_SCANCODE_RCTRL:
245 case SDL_SCANCODE_RSHIFT:
246 case SDL_SCANCODE_RALT:
247 case SDL_SCANCODE_RGUI:
248 if (ev->type == SDL_KEYUP) {
249 modifiers_state[ev->keysym.scancode] = 0;
250 } else {
251 modifiers_state[ev->keysym.scancode] = 1;
252 }
253 /* fall though */
254 default:
Gerd Hoffmannee8c0b62014-05-20 08:11:25 +0200255 qemu_input_event_send_key_qcode(con, qcode,
Dave Airlie47c03742013-12-10 14:05:51 +1000256 ev->type == SDL_KEYDOWN);
257 }
258}
259
260static void sdl_update_caption(struct sdl2_state *scon)
261{
262 char win_title[1024];
263 char icon_title[1024];
264 const char *status = "";
265
266 if (!runstate_is_running()) {
267 status = " [Stopped]";
268 } else if (gui_grab) {
269 if (alt_grab) {
270 status = " - Press Ctrl-Alt-Shift to exit mouse grab";
271 } else if (ctrl_grab) {
272 status = " - Press Right-Ctrl to exit mouse grab";
273 } else {
274 status = " - Press Ctrl-Alt to exit mouse grab";
275 }
276 }
277
278 if (qemu_name) {
279 snprintf(win_title, sizeof(win_title), "QEMU (%s-%d)%s", qemu_name,
280 scon->idx, status);
281 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
282 } else {
283 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
284 snprintf(icon_title, sizeof(icon_title), "QEMU");
285 }
286
287 if (scon->real_window) {
288 SDL_SetWindowTitle(scon->real_window, win_title);
289 }
290}
291
292static void sdl_hide_cursor(void)
293{
294 if (!cursor_hide) {
295 return;
296 }
297
298 if (qemu_input_is_absolute()) {
299 SDL_ShowCursor(1);
300 SDL_SetCursor(sdl_cursor_hidden);
301 } else {
Cole Robinson2d968ff2014-04-01 16:37:11 -0400302 SDL_SetRelativeMouseMode(SDL_TRUE);
Dave Airlie47c03742013-12-10 14:05:51 +1000303 }
304}
305
306static void sdl_show_cursor(void)
307{
308 if (!cursor_hide) {
309 return;
310 }
311
312 if (!qemu_input_is_absolute()) {
Cole Robinson2d968ff2014-04-01 16:37:11 -0400313 SDL_SetRelativeMouseMode(SDL_FALSE);
Dave Airlie47c03742013-12-10 14:05:51 +1000314 SDL_ShowCursor(1);
315 if (guest_cursor &&
316 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
317 SDL_SetCursor(guest_sprite);
318 } else {
319 SDL_SetCursor(sdl_cursor_normal);
320 }
321 }
322}
323
324static void sdl_grab_start(struct sdl2_state *scon)
325{
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200326 QemuConsole *con = scon ? scon->dcl.con : NULL;
327
328 if (!con || !qemu_console_is_graphic(con)) {
329 return;
330 }
Dave Airlie47c03742013-12-10 14:05:51 +1000331 /*
332 * If the application is not active, do not try to enter grab state. This
333 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
334 * application (SDL bug).
335 */
336 if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) {
337 return;
338 }
339 if (guest_cursor) {
340 SDL_SetCursor(guest_sprite);
341 if (!qemu_input_is_absolute() && !absolute_enabled) {
342 SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y);
343 }
344 } else {
345 sdl_hide_cursor();
346 }
347 SDL_SetWindowGrab(scon->real_window, SDL_TRUE);
348 gui_grab = 1;
349 sdl_update_caption(scon);
350}
351
352static void sdl_grab_end(struct sdl2_state *scon)
353{
354 SDL_SetWindowGrab(scon->real_window, SDL_FALSE);
355 gui_grab = 0;
356 sdl_show_cursor();
357 sdl_update_caption(scon);
358}
359
360static void absolute_mouse_grab(struct sdl2_state *scon)
361{
362 int mouse_x, mouse_y;
363 int scr_w, scr_h;
364 SDL_GetMouseState(&mouse_x, &mouse_y);
365 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
366 if (mouse_x > 0 && mouse_x < scr_w - 1 &&
367 mouse_y > 0 && mouse_y < scr_h - 1) {
368 sdl_grab_start(scon);
369 }
370}
371
372static void sdl_mouse_mode_change(Notifier *notify, void *data)
373{
374 if (qemu_input_is_absolute()) {
375 if (!absolute_enabled) {
376 absolute_enabled = 1;
377 absolute_mouse_grab(&sdl2_console[0]);
378 }
379 } else if (absolute_enabled) {
380 if (!gui_fullscreen) {
381 sdl_grab_end(&sdl2_console[0]);
382 }
383 absolute_enabled = 0;
384 }
385}
386
387static void sdl_send_mouse_event(struct sdl2_state *scon, int dx, int dy,
Cole Robinson3f2fde22014-04-21 18:58:50 -0400388 int x, int y, int state)
Dave Airlie47c03742013-12-10 14:05:51 +1000389{
390 static uint32_t bmap[INPUT_BUTTON_MAX] = {
391 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
392 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
393 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
Dave Airlie47c03742013-12-10 14:05:51 +1000394 };
395 static uint32_t prev_state;
396
397 if (prev_state != state) {
398 qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state);
399 prev_state = state;
400 }
401
402 if (qemu_input_is_absolute()) {
403 int scr_w, scr_h;
404 int max_w = 0, max_h = 0;
405 int off_x = 0, off_y = 0;
406 int cur_off_x = 0, cur_off_y = 0;
407 int i;
408
409 for (i = 0; i < sdl2_num_outputs; i++) {
410 struct sdl2_state *thiscon = &sdl2_console[i];
411 if (thiscon->real_window && thiscon->surface) {
412 SDL_GetWindowSize(thiscon->real_window, &scr_w, &scr_h);
413 cur_off_x = thiscon->x;
414 cur_off_y = thiscon->y;
415 if (scr_w + cur_off_x > max_w) {
416 max_w = scr_w + cur_off_x;
417 }
418 if (scr_h + cur_off_y > max_h) {
419 max_h = scr_h + cur_off_y;
420 }
421 if (i == scon->idx) {
422 off_x = cur_off_x;
423 off_y = cur_off_y;
424 }
425 }
426 }
427 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X, off_x + x, max_w);
428 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y, off_y + y, max_h);
Cole Robinsonafbc0dd2014-04-01 16:37:10 -0400429 } else {
430 if (guest_cursor) {
431 x -= guest_x;
432 y -= guest_y;
433 guest_x += x;
434 guest_y += y;
435 dx = x;
436 dy = y;
437 }
438 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
439 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
Dave Airlie47c03742013-12-10 14:05:51 +1000440 }
441 qemu_input_event_sync();
442}
443
444static void sdl_scale(struct sdl2_state *scon, int width, int height)
445{
446 int bpp = 0;
447 do_sdl_resize(scon, width, height, bpp);
448 scaling_active = 1;
449}
450
451static void toggle_full_screen(struct sdl2_state *scon)
452{
453 int width = surface_width(scon->surface);
454 int height = surface_height(scon->surface);
455 int bpp = surface_bits_per_pixel(scon->surface);
456
457 gui_fullscreen = !gui_fullscreen;
458 if (gui_fullscreen) {
459 SDL_GetWindowSize(scon->real_window,
460 &gui_saved_width, &gui_saved_height);
461 gui_saved_scaling = scaling_active;
462
463 do_sdl_resize(scon, width, height, bpp);
464 scaling_active = 0;
465
466 gui_saved_grab = gui_grab;
467 sdl_grab_start(scon);
468 } else {
469 if (gui_saved_scaling) {
470 sdl_scale(scon, gui_saved_width, gui_saved_height);
471 } else {
472 do_sdl_resize(scon, width, height, 0);
473 }
474 if (!gui_saved_grab) {
475 sdl_grab_end(scon);
476 }
477 }
478 graphic_hw_invalidate(scon->dcl.con);
479 graphic_hw_update(scon->dcl.con);
480}
481
482static void handle_keydown(SDL_Event *ev)
483{
Gerd Hoffmann363f59d2014-05-27 09:44:39 +0200484 int mod_state, win;
Dave Airlie47c03742013-12-10 14:05:51 +1000485 struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
486
487 if (alt_grab) {
488 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
489 (gui_grab_code | KMOD_LSHIFT);
490 } else if (ctrl_grab) {
491 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
492 } else {
493 mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
494 }
495 gui_key_modifier_pressed = mod_state;
496
497 if (gui_key_modifier_pressed) {
498 switch (ev->key.keysym.scancode) {
Gerd Hoffmann363f59d2014-05-27 09:44:39 +0200499 case SDL_SCANCODE_2:
500 case SDL_SCANCODE_3:
501 case SDL_SCANCODE_4:
502 case SDL_SCANCODE_5:
503 case SDL_SCANCODE_6:
504 case SDL_SCANCODE_7:
505 case SDL_SCANCODE_8:
506 case SDL_SCANCODE_9:
507 win = ev->key.keysym.scancode - SDL_SCANCODE_1;
508 if (win < sdl2_num_outputs) {
509 sdl2_console[win].hidden = !sdl2_console[win].hidden;
510 if (sdl2_console[win].real_window) {
511 if (sdl2_console[win].hidden) {
512 SDL_HideWindow(sdl2_console[win].real_window);
513 } else {
514 SDL_ShowWindow(sdl2_console[win].real_window);
515 }
516 }
517 gui_keysym = 1;
518 }
519 break;
Dave Airlie47c03742013-12-10 14:05:51 +1000520 case SDL_SCANCODE_F:
521 toggle_full_screen(scon);
522 gui_keysym = 1;
523 break;
524 case SDL_SCANCODE_U:
525 if (scaling_active) {
526 scaling_active = 0;
527 sdl_switch(&scon->dcl, NULL);
528 graphic_hw_invalidate(scon->dcl.con);
529 graphic_hw_update(scon->dcl.con);
530 }
531 gui_keysym = 1;
532 break;
533 case SDL_SCANCODE_KP_PLUS:
534 case SDL_SCANCODE_KP_MINUS:
535 if (!gui_fullscreen) {
536 int scr_w, scr_h;
537 int width, height;
538 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
539
540 width = MAX(scr_w + (ev->key.keysym.scancode ==
541 SDL_SCANCODE_KP_PLUS ? 50 : -50),
542 160);
543 height = (surface_height(scon->surface) * width) /
544 surface_width(scon->surface);
545
546 sdl_scale(scon, width, height);
547 graphic_hw_invalidate(NULL);
548 graphic_hw_update(NULL);
549 gui_keysym = 1;
550 }
551 default:
552 break;
553 }
554 }
555 if (!gui_keysym) {
Gerd Hoffmannee8c0b62014-05-20 08:11:25 +0200556 sdl_process_key(scon, &ev->key);
Dave Airlie47c03742013-12-10 14:05:51 +1000557 }
558}
559
560static void handle_keyup(SDL_Event *ev)
561{
562 int mod_state;
563 struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
564
565 if (!alt_grab) {
566 mod_state = (ev->key.keysym.mod & gui_grab_code);
567 } else {
568 mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
569 }
570 if (!mod_state && gui_key_modifier_pressed) {
571 gui_key_modifier_pressed = 0;
572 if (gui_keysym == 0) {
573 /* exit/enter grab if pressing Ctrl-Alt */
574 if (!gui_grab) {
575 sdl_grab_start(scon);
576 } else if (!gui_fullscreen) {
577 sdl_grab_end(scon);
578 }
579 /* SDL does not send back all the modifiers key, so we must
580 * correct it. */
Gerd Hoffmannee8c0b62014-05-20 08:11:25 +0200581 reset_keys(scon);
Dave Airlie47c03742013-12-10 14:05:51 +1000582 return;
583 }
584 gui_keysym = 0;
585 }
586 if (!gui_keysym) {
Gerd Hoffmannee8c0b62014-05-20 08:11:25 +0200587 sdl_process_key(scon, &ev->key);
Dave Airlie47c03742013-12-10 14:05:51 +1000588 }
589}
590
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200591static void handle_textinput(SDL_Event *ev)
592{
593 struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
594 QemuConsole *con = scon ? scon->dcl.con : NULL;
595
596 if (qemu_console_is_graphic(con)) {
597 return;
598 }
599 kbd_put_string_console(con, ev->text.text, strlen(ev->text.text));
600}
601
Dave Airlie47c03742013-12-10 14:05:51 +1000602static void handle_mousemotion(SDL_Event *ev)
603{
604 int max_x, max_y;
605 struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
606
607 if (qemu_input_is_absolute() || absolute_enabled) {
608 int scr_w, scr_h;
609 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
610 max_x = scr_w - 1;
611 max_y = scr_h - 1;
612 if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
613 ev->motion.x == max_x || ev->motion.y == max_y)) {
614 sdl_grab_end(scon);
615 }
616 if (!gui_grab &&
617 (ev->motion.x > 0 && ev->motion.x < max_x &&
618 ev->motion.y > 0 && ev->motion.y < max_y)) {
619 sdl_grab_start(scon);
620 }
621 }
622 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
Cole Robinson3f2fde22014-04-21 18:58:50 -0400623 sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel,
Dave Airlie47c03742013-12-10 14:05:51 +1000624 ev->motion.x, ev->motion.y, ev->motion.state);
625 }
626}
627
628static void handle_mousebutton(SDL_Event *ev)
629{
630 int buttonstate = SDL_GetMouseState(NULL, NULL);
631 SDL_MouseButtonEvent *bev;
632 struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
Dave Airlie47c03742013-12-10 14:05:51 +1000633
634 bev = &ev->button;
635 if (!gui_grab && !qemu_input_is_absolute()) {
636 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
637 /* start grabbing all events */
638 sdl_grab_start(scon);
639 }
640 } else {
Dave Airlie47c03742013-12-10 14:05:51 +1000641 if (ev->type == SDL_MOUSEBUTTONDOWN) {
642 buttonstate |= SDL_BUTTON(bev->button);
643 } else {
644 buttonstate &= ~SDL_BUTTON(bev->button);
645 }
Cole Robinson3f2fde22014-04-21 18:58:50 -0400646 sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate);
Dave Airlie47c03742013-12-10 14:05:51 +1000647 }
648}
649
Cole Robinson3f2fde22014-04-21 18:58:50 -0400650static void handle_mousewheel(SDL_Event *ev)
651{
652 struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
653 SDL_MouseWheelEvent *wev = &ev->wheel;
654 InputButton btn;
655
656 if (wev->y > 0) {
657 btn = INPUT_BUTTON_WHEEL_UP;
658 } else if (wev->y < 0) {
659 btn = INPUT_BUTTON_WHEEL_DOWN;
660 } else {
661 return;
662 }
663
664 qemu_input_queue_btn(scon->dcl.con, btn, true);
665 qemu_input_event_sync();
666 qemu_input_queue_btn(scon->dcl.con, btn, false);
667 qemu_input_event_sync();
668}
669
Dave Airlie47c03742013-12-10 14:05:51 +1000670static void handle_windowevent(DisplayChangeListener *dcl, SDL_Event *ev)
671{
672 int w, h;
673 struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
674
675 switch (ev->window.event) {
676 case SDL_WINDOWEVENT_RESIZED:
677 sdl_scale(scon, ev->window.data1, ev->window.data2);
Dave Airlie8b15d9f2014-03-25 16:50:36 +1000678 {
679 QemuUIInfo info;
680 memset(&info, 0, sizeof(info));
681 info.width = ev->window.data1;
682 info.height = ev->window.data2;
683 dpy_set_ui_info(scon->dcl.con, &info);
684 }
Dave Airlie47c03742013-12-10 14:05:51 +1000685 graphic_hw_invalidate(scon->dcl.con);
686 graphic_hw_update(scon->dcl.con);
687 break;
688 case SDL_WINDOWEVENT_EXPOSED:
689 SDL_GetWindowSize(SDL_GetWindowFromID(ev->window.windowID), &w, &h);
690 sdl_update(dcl, 0, 0, w, h);
691 break;
692 case SDL_WINDOWEVENT_FOCUS_GAINED:
693 case SDL_WINDOWEVENT_ENTER:
694 if (!gui_grab && (qemu_input_is_absolute() || absolute_enabled)) {
695 absolute_mouse_grab(scon);
696 }
697 break;
698 case SDL_WINDOWEVENT_FOCUS_LOST:
699 if (gui_grab && !gui_fullscreen) {
700 sdl_grab_end(scon);
701 }
702 break;
703 case SDL_WINDOWEVENT_RESTORED:
704 update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT);
705 break;
706 case SDL_WINDOWEVENT_MINIMIZED:
707 update_displaychangelistener(dcl, 500);
708 break;
709 case SDL_WINDOWEVENT_CLOSE:
710 if (!no_quit) {
711 no_shutdown = 0;
712 qemu_system_shutdown_request();
713 }
714 break;
715 }
716}
717
718static void sdl_refresh(DisplayChangeListener *dcl)
719{
720 struct sdl2_state *scon = container_of(dcl, struct sdl2_state, dcl);
721 SDL_Event ev1, *ev = &ev1;
722
723 if (scon->last_vm_running != runstate_is_running()) {
724 scon->last_vm_running = runstate_is_running();
725 sdl_update_caption(scon);
726 }
727
728 graphic_hw_update(dcl->con);
729
730 while (SDL_PollEvent(ev)) {
731 switch (ev->type) {
732 case SDL_KEYDOWN:
733 handle_keydown(ev);
734 break;
735 case SDL_KEYUP:
736 handle_keyup(ev);
737 break;
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200738 case SDL_TEXTINPUT:
739 handle_textinput(ev);
740 break;
Dave Airlie47c03742013-12-10 14:05:51 +1000741 case SDL_QUIT:
742 if (!no_quit) {
743 no_shutdown = 0;
744 qemu_system_shutdown_request();
745 }
746 break;
747 case SDL_MOUSEMOTION:
748 handle_mousemotion(ev);
749 break;
750 case SDL_MOUSEBUTTONDOWN:
751 case SDL_MOUSEBUTTONUP:
752 handle_mousebutton(ev);
753 break;
Cole Robinson3f2fde22014-04-21 18:58:50 -0400754 case SDL_MOUSEWHEEL:
755 handle_mousewheel(ev);
756 break;
Dave Airlie47c03742013-12-10 14:05:51 +1000757 case SDL_WINDOWEVENT:
758 handle_windowevent(dcl, ev);
759 break;
760 default:
761 break;
762 }
763 }
764}
765
766static void sdl_mouse_warp(DisplayChangeListener *dcl,
767 int x, int y, int on)
768{
769 struct sdl2_state *scon = container_of(dcl, struct sdl2_state, dcl);
770 if (on) {
771 if (!guest_cursor) {
772 sdl_show_cursor();
773 }
774 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
775 SDL_SetCursor(guest_sprite);
776 if (!qemu_input_is_absolute() && !absolute_enabled) {
777 SDL_WarpMouseInWindow(scon->real_window, x, y);
778 }
779 }
780 } else if (gui_grab) {
781 sdl_hide_cursor();
782 }
783 guest_cursor = on;
784 guest_x = x, guest_y = y;
785}
786
787static void sdl_mouse_define(DisplayChangeListener *dcl,
788 QEMUCursor *c)
789{
790
791 if (guest_sprite) {
792 SDL_FreeCursor(guest_sprite);
793 }
794
795 if (guest_sprite_surface) {
796 SDL_FreeSurface(guest_sprite_surface);
797 }
798
799 guest_sprite_surface =
800 SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4,
801 0xff0000, 0x00ff00, 0xff, 0xff000000);
802
803 if (!guest_sprite_surface) {
804 fprintf(stderr, "Failed to make rgb surface from %p\n", c);
805 return;
806 }
807 guest_sprite = SDL_CreateColorCursor(guest_sprite_surface,
808 c->hot_x, c->hot_y);
809 if (!guest_sprite) {
810 fprintf(stderr, "Failed to make color cursor from %p\n", c);
811 return;
812 }
813 if (guest_cursor &&
814 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
815 SDL_SetCursor(guest_sprite);
816 }
817}
818
819static void sdl_cleanup(void)
820{
821 if (guest_sprite) {
822 SDL_FreeCursor(guest_sprite);
823 }
824 SDL_QuitSubSystem(SDL_INIT_VIDEO);
825}
826
827static const DisplayChangeListenerOps dcl_ops = {
828 .dpy_name = "sdl",
829 .dpy_gfx_update = sdl_update,
830 .dpy_gfx_switch = sdl_switch,
831 .dpy_refresh = sdl_refresh,
832 .dpy_mouse_set = sdl_mouse_warp,
833 .dpy_cursor_define = sdl_mouse_define,
834};
835
836void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
837{
838 int flags;
839 uint8_t data = 0;
840 char *filename;
841 int i;
842
843 if (no_frame) {
844 gui_noframe = 1;
845 }
846
847#ifdef __linux__
848 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
849 * accessible $DISPLAY to open X11 window. This is often the case
850 * when qemu is run using sudo. But in this case, and when actually
851 * run in X11 environment, SDL fights with X11 for the video card,
852 * making current display unavailable, often until reboot.
853 * So make x11 the default SDL video driver if this variable is unset.
854 * This is a bit hackish but saves us from bigger problem.
855 * Maybe it's a good idea to fix this in SDL instead.
856 */
857 setenv("SDL_VIDEODRIVER", "x11", 0);
858#endif
859
860 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
861 if (SDL_Init(flags)) {
862 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
863 SDL_GetError());
864 exit(1);
865 }
866
867 for (i = 0;; i++) {
868 QemuConsole *con = qemu_console_lookup_by_index(i);
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200869 if (!con) {
Dave Airlie47c03742013-12-10 14:05:51 +1000870 break;
871 }
872 }
873 sdl2_num_outputs = i;
874 sdl2_console = g_new0(struct sdl2_state, sdl2_num_outputs);
875 for (i = 0; i < sdl2_num_outputs; i++) {
876 QemuConsole *con = qemu_console_lookup_by_index(i);
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200877 if (!qemu_console_is_graphic(con)) {
878 sdl2_console[i].hidden = true;
879 }
Dave Airlie47c03742013-12-10 14:05:51 +1000880 sdl2_console[i].dcl.ops = &dcl_ops;
881 sdl2_console[i].dcl.con = con;
882 register_displaychangelistener(&sdl2_console[i].dcl);
883 sdl2_console[i].idx = i;
884 }
885
886 /* Load a 32x32x4 image. White pixels are transparent. */
887 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
888 if (filename) {
889 SDL_Surface *image = SDL_LoadBMP(filename);
890 if (image) {
891 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
892 SDL_SetColorKey(image, SDL_TRUE, colorkey);
893 SDL_SetWindowIcon(sdl2_console[0].real_window, image);
894 }
895 g_free(filename);
896 }
897
898 if (full_screen) {
899 gui_fullscreen = 1;
900 sdl_grab_start(0);
901 }
902
903 mouse_mode_notifier.notify = sdl_mouse_mode_change;
904 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
905
906 gui_grab = 0;
907
908 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
909 sdl_cursor_normal = SDL_GetCursor();
910
911 atexit(sdl_cleanup);
912}