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