]> git.proxmox.com Git - mirror_qemu.git/blob - ui/sdl.c
ui/sdl2 : initial port to SDL 2.0 (v2.0)
[mirror_qemu.git] / ui / sdl.c
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
25 /* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
26 #undef WIN32_LEAN_AND_MEAN
27
28 #include <SDL.h>
29
30 #if SDL_MAJOR_VERSION == 1
31 #include <SDL_syswm.h>
32
33 #include "qemu-common.h"
34 #include "ui/console.h"
35 #include "ui/input.h"
36 #include "sysemu/sysemu.h"
37 #include "x_keymap.h"
38 #include "sdl_zoom.h"
39
40 static DisplayChangeListener *dcl;
41 static DisplaySurface *surface;
42 static SDL_Surface *real_screen;
43 static SDL_Surface *guest_screen = NULL;
44 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
45 static int last_vm_running;
46 static bool gui_saved_scaling;
47 static int gui_saved_width;
48 static int gui_saved_height;
49 static int gui_saved_grab;
50 static int gui_fullscreen;
51 static int gui_noframe;
52 static int gui_key_modifier_pressed;
53 static int gui_keysym;
54 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
55 static uint8_t modifiers_state[256];
56 static SDL_Cursor *sdl_cursor_normal;
57 static SDL_Cursor *sdl_cursor_hidden;
58 static int absolute_enabled = 0;
59 static int guest_cursor = 0;
60 static int guest_x, guest_y;
61 static SDL_Cursor *guest_sprite = NULL;
62 static SDL_PixelFormat host_format;
63 static int scaling_active = 0;
64 static Notifier mouse_mode_notifier;
65
66 static void sdl_update(DisplayChangeListener *dcl,
67 int x, int y, int w, int h)
68 {
69 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
70 SDL_Rect rec;
71 rec.x = x;
72 rec.y = y;
73 rec.w = w;
74 rec.h = h;
75
76 if (guest_screen) {
77 if (!scaling_active) {
78 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
79 } else {
80 if (sdl_zoom_blit(guest_screen, real_screen, SMOOTHING_ON, &rec) < 0) {
81 fprintf(stderr, "Zoom blit failed\n");
82 exit(1);
83 }
84 }
85 }
86 SDL_UpdateRect(real_screen, rec.x, rec.y, rec.w, rec.h);
87 }
88
89 static void do_sdl_resize(int width, int height, int bpp)
90 {
91 int flags;
92 SDL_Surface *tmp_screen;
93
94 // printf("resizing to %d %d\n", w, h);
95
96 flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
97 if (gui_fullscreen) {
98 flags |= SDL_FULLSCREEN;
99 } else {
100 flags |= SDL_RESIZABLE;
101 }
102 if (gui_noframe)
103 flags |= SDL_NOFRAME;
104
105 tmp_screen = SDL_SetVideoMode(width, height, bpp, flags);
106 if (!real_screen) {
107 if (!tmp_screen) {
108 fprintf(stderr, "Could not open SDL display (%dx%dx%d): %s\n",
109 width, height, bpp, SDL_GetError());
110 exit(1);
111 }
112 } else {
113 /*
114 * Revert to the previous video mode if the change of resizing or
115 * resolution failed.
116 */
117 if (!tmp_screen) {
118 fprintf(stderr, "Failed to set SDL display (%dx%dx%d): %s\n",
119 width, height, bpp, SDL_GetError());
120 return;
121 }
122 }
123
124 real_screen = tmp_screen;
125 }
126
127 static void sdl_switch(DisplayChangeListener *dcl,
128 DisplaySurface *new_surface)
129 {
130
131 /* temporary hack: allows to call sdl_switch to handle scaling changes */
132 if (new_surface) {
133 surface = new_surface;
134 }
135
136 if (!scaling_active) {
137 do_sdl_resize(surface_width(surface), surface_height(surface), 0);
138 } else if (real_screen->format->BitsPerPixel !=
139 surface_bits_per_pixel(surface)) {
140 do_sdl_resize(real_screen->w, real_screen->h,
141 surface_bits_per_pixel(surface));
142 }
143
144 if (guest_screen != NULL) {
145 SDL_FreeSurface(guest_screen);
146 }
147 guest_screen = SDL_CreateRGBSurfaceFrom
148 (surface_data(surface),
149 surface_width(surface), surface_height(surface),
150 surface_bits_per_pixel(surface), surface_stride(surface),
151 surface->pf.rmask, surface->pf.gmask,
152 surface->pf.bmask, surface->pf.amask);
153 }
154
155 /* generic keyboard conversion */
156
157 #include "sdl_keysym.h"
158
159 static kbd_layout_t *kbd_layout = NULL;
160
161 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
162 {
163 int keysym;
164 /* workaround for X11+SDL bug with AltGR */
165 keysym = ev->keysym.sym;
166 if (keysym == 0 && ev->keysym.scancode == 113)
167 keysym = SDLK_MODE;
168 /* For Japanese key '\' and '|' */
169 if (keysym == 92 && ev->keysym.scancode == 133) {
170 keysym = 0xa5;
171 }
172 return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK;
173 }
174
175 /* specific keyboard conversions from scan codes */
176
177 #if defined(_WIN32)
178
179 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
180 {
181 return ev->keysym.scancode;
182 }
183
184 #else
185
186 #if defined(SDL_VIDEO_DRIVER_X11)
187 #include <X11/XKBlib.h>
188
189 static int check_for_evdev(void)
190 {
191 SDL_SysWMinfo info;
192 XkbDescPtr desc = NULL;
193 int has_evdev = 0;
194 char *keycodes = NULL;
195
196 SDL_VERSION(&info.version);
197 if (!SDL_GetWMInfo(&info)) {
198 return 0;
199 }
200 desc = XkbGetKeyboard(info.info.x11.display,
201 XkbGBN_AllComponentsMask,
202 XkbUseCoreKbd);
203 if (desc && desc->names) {
204 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
205 if (keycodes == NULL) {
206 fprintf(stderr, "could not lookup keycode name\n");
207 } else if (strstart(keycodes, "evdev", NULL)) {
208 has_evdev = 1;
209 } else if (!strstart(keycodes, "xfree86", NULL)) {
210 fprintf(stderr, "unknown keycodes `%s', please report to "
211 "qemu-devel@nongnu.org\n", keycodes);
212 }
213 }
214
215 if (desc) {
216 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
217 }
218 if (keycodes) {
219 XFree(keycodes);
220 }
221 return has_evdev;
222 }
223 #else
224 static int check_for_evdev(void)
225 {
226 return 0;
227 }
228 #endif
229
230 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
231 {
232 int keycode;
233 static int has_evdev = -1;
234
235 if (has_evdev == -1)
236 has_evdev = check_for_evdev();
237
238 keycode = ev->keysym.scancode;
239
240 if (keycode < 9) {
241 keycode = 0;
242 } else if (keycode < 97) {
243 keycode -= 8; /* just an offset */
244 } else if (keycode < 158) {
245 /* use conversion table */
246 if (has_evdev)
247 keycode = translate_evdev_keycode(keycode - 97);
248 else
249 keycode = translate_xfree86_keycode(keycode - 97);
250 } else if (keycode == 208) { /* Hiragana_Katakana */
251 keycode = 0x70;
252 } else if (keycode == 211) { /* backslash */
253 keycode = 0x73;
254 } else {
255 keycode = 0;
256 }
257 return keycode;
258 }
259
260 #endif
261
262 static void reset_keys(void)
263 {
264 int i;
265 for(i = 0; i < 256; i++) {
266 if (modifiers_state[i]) {
267 qemu_input_event_send_key_number(dcl->con, i, false);
268 modifiers_state[i] = 0;
269 }
270 }
271 }
272
273 static void sdl_process_key(SDL_KeyboardEvent *ev)
274 {
275 int keycode;
276
277 if (ev->keysym.sym == SDLK_PAUSE) {
278 /* specific case */
279 qemu_input_event_send_key_qcode(dcl->con, Q_KEY_CODE_PAUSE,
280 ev->type == SDL_KEYDOWN);
281 return;
282 }
283
284 if (kbd_layout) {
285 keycode = sdl_keyevent_to_keycode_generic(ev);
286 } else {
287 keycode = sdl_keyevent_to_keycode(ev);
288 }
289
290 switch(keycode) {
291 case 0x00:
292 /* sent when leaving window: reset the modifiers state */
293 reset_keys();
294 return;
295 case 0x2a: /* Left Shift */
296 case 0x36: /* Right Shift */
297 case 0x1d: /* Left CTRL */
298 case 0x9d: /* Right CTRL */
299 case 0x38: /* Left ALT */
300 case 0xb8: /* Right ALT */
301 if (ev->type == SDL_KEYUP)
302 modifiers_state[keycode] = 0;
303 else
304 modifiers_state[keycode] = 1;
305 break;
306 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
307 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
308 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
309 case 0x45: /* num lock */
310 case 0x3a: /* caps lock */
311 /* SDL does not send the key up event, so we generate it */
312 qemu_input_event_send_key_number(dcl->con, keycode, true);
313 qemu_input_event_send_key_number(dcl->con, keycode, false);
314 return;
315 #endif
316 }
317
318 /* now send the key code */
319 qemu_input_event_send_key_number(dcl->con, keycode,
320 ev->type == SDL_KEYDOWN);
321 }
322
323 static void sdl_update_caption(void)
324 {
325 char win_title[1024];
326 char icon_title[1024];
327 const char *status = "";
328
329 if (!runstate_is_running())
330 status = " [Stopped]";
331 else if (gui_grab) {
332 if (alt_grab)
333 status = " - Press Ctrl-Alt-Shift to exit mouse grab";
334 else if (ctrl_grab)
335 status = " - Press Right-Ctrl to exit mouse grab";
336 else
337 status = " - Press Ctrl-Alt to exit mouse grab";
338 }
339
340 if (qemu_name) {
341 snprintf(win_title, sizeof(win_title), "QEMU (%s)%s", qemu_name, status);
342 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
343 } else {
344 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
345 snprintf(icon_title, sizeof(icon_title), "QEMU");
346 }
347
348 SDL_WM_SetCaption(win_title, icon_title);
349 }
350
351 static void sdl_hide_cursor(void)
352 {
353 if (!cursor_hide)
354 return;
355
356 if (qemu_input_is_absolute()) {
357 SDL_ShowCursor(1);
358 SDL_SetCursor(sdl_cursor_hidden);
359 } else {
360 SDL_ShowCursor(0);
361 }
362 }
363
364 static void sdl_show_cursor(void)
365 {
366 if (!cursor_hide)
367 return;
368
369 if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL)) {
370 SDL_ShowCursor(1);
371 if (guest_cursor &&
372 (gui_grab || qemu_input_is_absolute() || absolute_enabled))
373 SDL_SetCursor(guest_sprite);
374 else
375 SDL_SetCursor(sdl_cursor_normal);
376 }
377 }
378
379 static void sdl_grab_start(void)
380 {
381 /*
382 * If the application is not active, do not try to enter grab state. This
383 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
384 * application (SDL bug).
385 */
386 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS)) {
387 return;
388 }
389 if (guest_cursor) {
390 SDL_SetCursor(guest_sprite);
391 if (!qemu_input_is_absolute() && !absolute_enabled) {
392 SDL_WarpMouse(guest_x, guest_y);
393 }
394 } else
395 sdl_hide_cursor();
396 SDL_WM_GrabInput(SDL_GRAB_ON);
397 gui_grab = 1;
398 sdl_update_caption();
399 }
400
401 static void sdl_grab_end(void)
402 {
403 SDL_WM_GrabInput(SDL_GRAB_OFF);
404 gui_grab = 0;
405 sdl_show_cursor();
406 sdl_update_caption();
407 }
408
409 static void absolute_mouse_grab(void)
410 {
411 int mouse_x, mouse_y;
412
413 SDL_GetMouseState(&mouse_x, &mouse_y);
414 if (mouse_x > 0 && mouse_x < real_screen->w - 1 &&
415 mouse_y > 0 && mouse_y < real_screen->h - 1) {
416 sdl_grab_start();
417 }
418 }
419
420 static void sdl_mouse_mode_change(Notifier *notify, void *data)
421 {
422 if (qemu_input_is_absolute()) {
423 if (!absolute_enabled) {
424 absolute_enabled = 1;
425 if (qemu_console_is_graphic(NULL)) {
426 absolute_mouse_grab();
427 }
428 }
429 } else if (absolute_enabled) {
430 if (!gui_fullscreen) {
431 sdl_grab_end();
432 }
433 absolute_enabled = 0;
434 }
435 }
436
437 static void sdl_send_mouse_event(int dx, int dy, int x, int y, int state)
438 {
439 static uint32_t bmap[INPUT_BUTTON_MAX] = {
440 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
441 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
442 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
443 [INPUT_BUTTON_WHEEL_UP] = SDL_BUTTON(SDL_BUTTON_WHEELUP),
444 [INPUT_BUTTON_WHEEL_DOWN] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN),
445 };
446 static uint32_t prev_state;
447
448 if (prev_state != state) {
449 qemu_input_update_buttons(dcl->con, bmap, prev_state, state);
450 prev_state = state;
451 }
452
453 if (qemu_input_is_absolute()) {
454 qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, x,
455 real_screen->w);
456 qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, y,
457 real_screen->h);
458 } else if (guest_cursor) {
459 x -= guest_x;
460 y -= guest_y;
461 guest_x += x;
462 guest_y += y;
463 qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, x);
464 qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, y);
465 }
466 qemu_input_event_sync();
467 }
468
469 static void sdl_scale(int width, int height)
470 {
471 int bpp = real_screen->format->BitsPerPixel;
472
473 if (bpp != 16 && bpp != 32) {
474 bpp = 32;
475 }
476 do_sdl_resize(width, height, bpp);
477 scaling_active = 1;
478 }
479
480 static void toggle_full_screen(void)
481 {
482 int width = surface_width(surface);
483 int height = surface_height(surface);
484 int bpp = surface_bits_per_pixel(surface);
485
486 gui_fullscreen = !gui_fullscreen;
487 if (gui_fullscreen) {
488 gui_saved_width = real_screen->w;
489 gui_saved_height = real_screen->h;
490 gui_saved_scaling = scaling_active;
491
492 do_sdl_resize(width, height, bpp);
493 scaling_active = 0;
494
495 gui_saved_grab = gui_grab;
496 sdl_grab_start();
497 } else {
498 if (gui_saved_scaling) {
499 sdl_scale(gui_saved_width, gui_saved_height);
500 } else {
501 do_sdl_resize(width, height, 0);
502 }
503 if (!gui_saved_grab || !qemu_console_is_graphic(NULL)) {
504 sdl_grab_end();
505 }
506 }
507 graphic_hw_invalidate(NULL);
508 graphic_hw_update(NULL);
509 }
510
511 static void handle_keydown(SDL_Event *ev)
512 {
513 int mod_state;
514 int keycode;
515
516 if (alt_grab) {
517 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
518 (gui_grab_code | KMOD_LSHIFT);
519 } else if (ctrl_grab) {
520 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
521 } else {
522 mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
523 }
524 gui_key_modifier_pressed = mod_state;
525
526 if (gui_key_modifier_pressed) {
527 keycode = sdl_keyevent_to_keycode(&ev->key);
528 switch (keycode) {
529 case 0x21: /* 'f' key on US keyboard */
530 toggle_full_screen();
531 gui_keysym = 1;
532 break;
533 case 0x16: /* 'u' key on US keyboard */
534 if (scaling_active) {
535 scaling_active = 0;
536 sdl_switch(dcl, NULL);
537 graphic_hw_invalidate(NULL);
538 graphic_hw_update(NULL);
539 }
540 gui_keysym = 1;
541 break;
542 case 0x02 ... 0x0a: /* '1' to '9' keys */
543 /* Reset the modifiers sent to the current console */
544 reset_keys();
545 console_select(keycode - 0x02);
546 gui_keysym = 1;
547 if (gui_fullscreen) {
548 break;
549 }
550 if (!qemu_console_is_graphic(NULL)) {
551 /* release grab if going to a text console */
552 if (gui_grab) {
553 sdl_grab_end();
554 } else if (absolute_enabled) {
555 sdl_show_cursor();
556 }
557 } else if (absolute_enabled) {
558 sdl_hide_cursor();
559 absolute_mouse_grab();
560 }
561 break;
562 case 0x1b: /* '+' */
563 case 0x35: /* '-' */
564 if (!gui_fullscreen) {
565 int width = MAX(real_screen->w + (keycode == 0x1b ? 50 : -50),
566 160);
567 int height = (surface_height(surface) * width) /
568 surface_width(surface);
569
570 sdl_scale(width, height);
571 graphic_hw_invalidate(NULL);
572 graphic_hw_update(NULL);
573 gui_keysym = 1;
574 }
575 default:
576 break;
577 }
578 } else if (!qemu_console_is_graphic(NULL)) {
579 int keysym = 0;
580
581 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
582 switch (ev->key.keysym.sym) {
583 case SDLK_UP:
584 keysym = QEMU_KEY_CTRL_UP;
585 break;
586 case SDLK_DOWN:
587 keysym = QEMU_KEY_CTRL_DOWN;
588 break;
589 case SDLK_LEFT:
590 keysym = QEMU_KEY_CTRL_LEFT;
591 break;
592 case SDLK_RIGHT:
593 keysym = QEMU_KEY_CTRL_RIGHT;
594 break;
595 case SDLK_HOME:
596 keysym = QEMU_KEY_CTRL_HOME;
597 break;
598 case SDLK_END:
599 keysym = QEMU_KEY_CTRL_END;
600 break;
601 case SDLK_PAGEUP:
602 keysym = QEMU_KEY_CTRL_PAGEUP;
603 break;
604 case SDLK_PAGEDOWN:
605 keysym = QEMU_KEY_CTRL_PAGEDOWN;
606 break;
607 default:
608 break;
609 }
610 } else {
611 switch (ev->key.keysym.sym) {
612 case SDLK_UP:
613 keysym = QEMU_KEY_UP;
614 break;
615 case SDLK_DOWN:
616 keysym = QEMU_KEY_DOWN;
617 break;
618 case SDLK_LEFT:
619 keysym = QEMU_KEY_LEFT;
620 break;
621 case SDLK_RIGHT:
622 keysym = QEMU_KEY_RIGHT;
623 break;
624 case SDLK_HOME:
625 keysym = QEMU_KEY_HOME;
626 break;
627 case SDLK_END:
628 keysym = QEMU_KEY_END;
629 break;
630 case SDLK_PAGEUP:
631 keysym = QEMU_KEY_PAGEUP;
632 break;
633 case SDLK_PAGEDOWN:
634 keysym = QEMU_KEY_PAGEDOWN;
635 break;
636 case SDLK_BACKSPACE:
637 keysym = QEMU_KEY_BACKSPACE;
638 break;
639 case SDLK_DELETE:
640 keysym = QEMU_KEY_DELETE;
641 break;
642 default:
643 break;
644 }
645 }
646 if (keysym) {
647 kbd_put_keysym(keysym);
648 } else if (ev->key.keysym.unicode != 0) {
649 kbd_put_keysym(ev->key.keysym.unicode);
650 }
651 }
652 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
653 sdl_process_key(&ev->key);
654 }
655 }
656
657 static void handle_keyup(SDL_Event *ev)
658 {
659 int mod_state;
660
661 if (!alt_grab) {
662 mod_state = (ev->key.keysym.mod & gui_grab_code);
663 } else {
664 mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
665 }
666 if (!mod_state && gui_key_modifier_pressed) {
667 gui_key_modifier_pressed = 0;
668 if (gui_keysym == 0) {
669 /* exit/enter grab if pressing Ctrl-Alt */
670 if (!gui_grab) {
671 if (qemu_console_is_graphic(NULL)) {
672 sdl_grab_start();
673 }
674 } else if (!gui_fullscreen) {
675 sdl_grab_end();
676 }
677 /* SDL does not send back all the modifiers key, so we must
678 * correct it. */
679 reset_keys();
680 return;
681 }
682 gui_keysym = 0;
683 }
684 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
685 sdl_process_key(&ev->key);
686 }
687 }
688
689 static void handle_mousemotion(SDL_Event *ev)
690 {
691 int max_x, max_y;
692
693 if (qemu_console_is_graphic(NULL) &&
694 (qemu_input_is_absolute() || absolute_enabled)) {
695 max_x = real_screen->w - 1;
696 max_y = real_screen->h - 1;
697 if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
698 ev->motion.x == max_x || ev->motion.y == max_y)) {
699 sdl_grab_end();
700 }
701 if (!gui_grab &&
702 (ev->motion.x > 0 && ev->motion.x < max_x &&
703 ev->motion.y > 0 && ev->motion.y < max_y)) {
704 sdl_grab_start();
705 }
706 }
707 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
708 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel,
709 ev->motion.x, ev->motion.y, ev->motion.state);
710 }
711 }
712
713 static void handle_mousebutton(SDL_Event *ev)
714 {
715 int buttonstate = SDL_GetMouseState(NULL, NULL);
716 SDL_MouseButtonEvent *bev;
717
718 if (!qemu_console_is_graphic(NULL)) {
719 return;
720 }
721
722 bev = &ev->button;
723 if (!gui_grab && !qemu_input_is_absolute()) {
724 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
725 /* start grabbing all events */
726 sdl_grab_start();
727 }
728 } else {
729 if (ev->type == SDL_MOUSEBUTTONDOWN) {
730 buttonstate |= SDL_BUTTON(bev->button);
731 } else {
732 buttonstate &= ~SDL_BUTTON(bev->button);
733 }
734 sdl_send_mouse_event(0, 0, bev->x, bev->y, buttonstate);
735 }
736 }
737
738 static void handle_activation(SDL_Event *ev)
739 {
740 #ifdef _WIN32
741 /* Disable grab if the window no longer has the focus
742 * (Windows-only workaround) */
743 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
744 !ev->active.gain && !gui_fullscreen) {
745 sdl_grab_end();
746 }
747 #endif
748 if (!gui_grab && ev->active.gain && qemu_console_is_graphic(NULL) &&
749 (qemu_input_is_absolute() || absolute_enabled)) {
750 absolute_mouse_grab();
751 }
752 if (ev->active.state & SDL_APPACTIVE) {
753 if (ev->active.gain) {
754 /* Back to default interval */
755 update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT);
756 } else {
757 /* Sleeping interval. Not using the long default here as
758 * sdl_refresh does not only update the guest screen, but
759 * also checks for gui events. */
760 update_displaychangelistener(dcl, 500);
761 }
762 }
763 }
764
765 static void sdl_refresh(DisplayChangeListener *dcl)
766 {
767 SDL_Event ev1, *ev = &ev1;
768
769 if (last_vm_running != runstate_is_running()) {
770 last_vm_running = runstate_is_running();
771 sdl_update_caption();
772 }
773
774 graphic_hw_update(NULL);
775 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL));
776
777 while (SDL_PollEvent(ev)) {
778 switch (ev->type) {
779 case SDL_VIDEOEXPOSE:
780 sdl_update(dcl, 0, 0, real_screen->w, real_screen->h);
781 break;
782 case SDL_KEYDOWN:
783 handle_keydown(ev);
784 break;
785 case SDL_KEYUP:
786 handle_keyup(ev);
787 break;
788 case SDL_QUIT:
789 if (!no_quit) {
790 no_shutdown = 0;
791 qemu_system_shutdown_request();
792 }
793 break;
794 case SDL_MOUSEMOTION:
795 handle_mousemotion(ev);
796 break;
797 case SDL_MOUSEBUTTONDOWN:
798 case SDL_MOUSEBUTTONUP:
799 handle_mousebutton(ev);
800 break;
801 case SDL_ACTIVEEVENT:
802 handle_activation(ev);
803 break;
804 case SDL_VIDEORESIZE:
805 sdl_scale(ev->resize.w, ev->resize.h);
806 graphic_hw_invalidate(NULL);
807 graphic_hw_update(NULL);
808 break;
809 default:
810 break;
811 }
812 }
813 }
814
815 static void sdl_mouse_warp(DisplayChangeListener *dcl,
816 int x, int y, int on)
817 {
818 if (on) {
819 if (!guest_cursor)
820 sdl_show_cursor();
821 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
822 SDL_SetCursor(guest_sprite);
823 if (!qemu_input_is_absolute() && !absolute_enabled) {
824 SDL_WarpMouse(x, y);
825 }
826 }
827 } else if (gui_grab)
828 sdl_hide_cursor();
829 guest_cursor = on;
830 guest_x = x, guest_y = y;
831 }
832
833 static void sdl_mouse_define(DisplayChangeListener *dcl,
834 QEMUCursor *c)
835 {
836 uint8_t *image, *mask;
837 int bpl;
838
839 if (guest_sprite)
840 SDL_FreeCursor(guest_sprite);
841
842 bpl = cursor_get_mono_bpl(c);
843 image = g_malloc0(bpl * c->height);
844 mask = g_malloc0(bpl * c->height);
845 cursor_get_mono_image(c, 0x000000, image);
846 cursor_get_mono_mask(c, 0, mask);
847 guest_sprite = SDL_CreateCursor(image, mask, c->width, c->height,
848 c->hot_x, c->hot_y);
849 g_free(image);
850 g_free(mask);
851
852 if (guest_cursor &&
853 (gui_grab || qemu_input_is_absolute() || absolute_enabled))
854 SDL_SetCursor(guest_sprite);
855 }
856
857 static void sdl_cleanup(void)
858 {
859 if (guest_sprite)
860 SDL_FreeCursor(guest_sprite);
861 SDL_QuitSubSystem(SDL_INIT_VIDEO);
862 }
863
864 static const DisplayChangeListenerOps dcl_ops = {
865 .dpy_name = "sdl",
866 .dpy_gfx_update = sdl_update,
867 .dpy_gfx_switch = sdl_switch,
868 .dpy_refresh = sdl_refresh,
869 .dpy_mouse_set = sdl_mouse_warp,
870 .dpy_cursor_define = sdl_mouse_define,
871 };
872
873 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
874 {
875 int flags;
876 uint8_t data = 0;
877 const SDL_VideoInfo *vi;
878 char *filename;
879
880 #if defined(__APPLE__)
881 /* always use generic keymaps */
882 if (!keyboard_layout)
883 keyboard_layout = "en-us";
884 #endif
885 if(keyboard_layout) {
886 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
887 if (!kbd_layout)
888 exit(1);
889 }
890
891 if (no_frame)
892 gui_noframe = 1;
893
894 if (!full_screen) {
895 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
896 }
897 #ifdef __linux__
898 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
899 * accessible $DISPLAY to open X11 window. This is often the case
900 * when qemu is run using sudo. But in this case, and when actually
901 * run in X11 environment, SDL fights with X11 for the video card,
902 * making current display unavailable, often until reboot.
903 * So make x11 the default SDL video driver if this variable is unset.
904 * This is a bit hackish but saves us from bigger problem.
905 * Maybe it's a good idea to fix this in SDL instead.
906 */
907 setenv("SDL_VIDEODRIVER", "x11", 0);
908 #endif
909
910 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
911 * This requires SDL >= 1.2.14. */
912 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
913
914 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
915 if (SDL_Init (flags)) {
916 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
917 SDL_GetError());
918 exit(1);
919 }
920 vi = SDL_GetVideoInfo();
921 host_format = *(vi->vfmt);
922
923 /* Load a 32x32x4 image. White pixels are transparent. */
924 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
925 if (filename) {
926 SDL_Surface *image = SDL_LoadBMP(filename);
927 if (image) {
928 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
929 SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
930 SDL_WM_SetIcon(image, NULL);
931 }
932 g_free(filename);
933 }
934
935 if (full_screen) {
936 gui_fullscreen = 1;
937 sdl_grab_start();
938 }
939
940 dcl = g_malloc0(sizeof(DisplayChangeListener));
941 dcl->ops = &dcl_ops;
942 register_displaychangelistener(dcl);
943
944 mouse_mode_notifier.notify = sdl_mouse_mode_change;
945 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
946
947 sdl_update_caption();
948 SDL_EnableKeyRepeat(250, 50);
949 gui_grab = 0;
950
951 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
952 sdl_cursor_normal = SDL_GetCursor();
953
954 atexit(sdl_cleanup);
955 }
956 #endif