Merge commit 'origin/master' into dev-mini2440-staging
[sniper_test.git] / sdl.c
blob843a6fc89a5999084b99b264098530bcd73f910b
1 /*
2 * QEMU SDL display driver
4 * Copyright (c) 2003 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include <SDL.h>
25 #include <SDL_syswm.h>
27 #ifndef _WIN32
28 #include <signal.h>
29 #endif
31 #include "qemu-common.h"
32 #include "console.h"
33 #include "sysemu.h"
34 #include "x_keymap.h"
36 static DisplayChangeListener *dcl;
37 static SDL_Surface *real_screen;
38 static SDL_Surface *guest_screen = NULL;
39 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
40 static int last_vm_running;
41 static int gui_saved_grab;
42 static int gui_fullscreen;
43 static int gui_noframe;
44 static int gui_key_modifier_pressed;
45 static int gui_keysym;
46 static int gui_fullscreen_initial_grab;
47 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
48 static uint8_t modifiers_state[256];
49 static int width, height;
50 static SDL_Cursor *sdl_cursor_normal;
51 static SDL_Cursor *sdl_cursor_hidden;
52 static int absolute_enabled = 0;
53 static int guest_cursor = 0;
54 static int guest_x, guest_y;
55 static SDL_Cursor *guest_sprite = 0;
56 static uint8_t allocator;
57 static uint8_t hostbpp;
59 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
61 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
62 if (guest_screen) {
63 SDL_Rect rec;
64 rec.x = x;
65 rec.y = y;
66 rec.w = w;
67 rec.h = h;
68 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
70 SDL_UpdateRect(real_screen, x, y, w, h);
73 static void sdl_setdata(DisplayState *ds)
75 SDL_Rect rec;
76 rec.x = 0;
77 rec.y = 0;
78 rec.w = real_screen->w;
79 rec.h = real_screen->h;
81 if (guest_screen != NULL) SDL_FreeSurface(guest_screen);
83 guest_screen = SDL_CreateRGBSurfaceFrom(ds_get_data(ds), ds_get_width(ds), ds_get_height(ds),
84 ds_get_bits_per_pixel(ds), ds_get_linesize(ds),
85 ds->surface->pf.rmask, ds->surface->pf.gmask,
86 ds->surface->pf.bmask, ds->surface->pf.amask);
89 static void do_sdl_resize(int width, int height, int bpp)
91 int flags;
93 // printf("resizing to %d %d\n", w, h);
95 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
96 if (gui_fullscreen)
97 flags |= SDL_FULLSCREEN;
98 if (gui_noframe)
99 flags |= SDL_NOFRAME;
101 real_screen = SDL_SetVideoMode(width, height, bpp, flags);
102 if (!real_screen) {
103 fprintf(stderr, "Could not open SDL display\n");
104 exit(1);
108 static void sdl_resize(DisplayState *ds)
110 if (!allocator) {
111 do_sdl_resize(ds_get_width(ds), ds_get_height(ds), 0);
112 sdl_setdata(ds);
113 } else {
114 if (guest_screen != NULL) {
115 SDL_FreeSurface(guest_screen);
116 guest_screen = NULL;
121 static PixelFormat sdl_to_qemu_pixelformat(SDL_PixelFormat *sdl_pf)
123 PixelFormat qemu_pf;
125 memset(&qemu_pf, 0x00, sizeof(PixelFormat));
127 qemu_pf.bits_per_pixel = sdl_pf->BitsPerPixel;
128 qemu_pf.bytes_per_pixel = sdl_pf->BytesPerPixel;
129 qemu_pf.depth = (qemu_pf.bits_per_pixel) == 32 ? 24 : (qemu_pf.bits_per_pixel);
131 qemu_pf.rmask = sdl_pf->Rmask;
132 qemu_pf.gmask = sdl_pf->Gmask;
133 qemu_pf.bmask = sdl_pf->Bmask;
134 qemu_pf.amask = sdl_pf->Amask;
136 qemu_pf.rshift = sdl_pf->Rshift;
137 qemu_pf.gshift = sdl_pf->Gshift;
138 qemu_pf.bshift = sdl_pf->Bshift;
139 qemu_pf.ashift = sdl_pf->Ashift;
141 qemu_pf.rbits = 8 - sdl_pf->Rloss;
142 qemu_pf.gbits = 8 - sdl_pf->Gloss;
143 qemu_pf.bbits = 8 - sdl_pf->Bloss;
144 qemu_pf.abits = 8 - sdl_pf->Aloss;
146 qemu_pf.rmax = ((1 << qemu_pf.rbits) - 1);
147 qemu_pf.gmax = ((1 << qemu_pf.gbits) - 1);
148 qemu_pf.bmax = ((1 << qemu_pf.bbits) - 1);
149 qemu_pf.amax = ((1 << qemu_pf.abits) - 1);
151 return qemu_pf;
154 static DisplaySurface* sdl_create_displaysurface(int width, int height)
156 DisplaySurface *surface = (DisplaySurface*) qemu_mallocz(sizeof(DisplaySurface));
157 if (surface == NULL) {
158 fprintf(stderr, "sdl_create_displaysurface: malloc failed\n");
159 exit(1);
162 surface->width = width;
163 surface->height = height;
165 if (hostbpp == 16)
166 do_sdl_resize(width, height, 16);
167 else
168 do_sdl_resize(width, height, 32);
170 surface->pf = sdl_to_qemu_pixelformat(real_screen->format);
171 surface->linesize = real_screen->pitch;
172 surface->data = real_screen->pixels;
174 #ifdef WORDS_BIGENDIAN
175 surface->flags = QEMU_ALLOCATED_FLAG | QEMU_BIG_ENDIAN_FLAG;
176 #else
177 surface->flags = QEMU_ALLOCATED_FLAG;
178 #endif
179 allocator = 1;
181 return surface;
184 static void sdl_free_displaysurface(DisplaySurface *surface)
186 allocator = 0;
187 if (surface == NULL)
188 return;
189 qemu_free(surface);
192 static DisplaySurface* sdl_resize_displaysurface(DisplaySurface *surface, int width, int height)
194 sdl_free_displaysurface(surface);
195 return sdl_create_displaysurface(width, height);
198 /* generic keyboard conversion */
200 #include "sdl_keysym.h"
202 static kbd_layout_t *kbd_layout = NULL;
204 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
206 int keysym;
207 /* workaround for X11+SDL bug with AltGR */
208 keysym = ev->keysym.sym;
209 if (keysym == 0 && ev->keysym.scancode == 113)
210 keysym = SDLK_MODE;
211 /* For Japanese key '\' and '|' */
212 if (keysym == 92 && ev->keysym.scancode == 133) {
213 keysym = 0xa5;
215 return keysym2scancode(kbd_layout, keysym);
218 /* specific keyboard conversions from scan codes */
220 #if defined(_WIN32)
222 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
224 return ev->keysym.scancode;
227 #else
229 #if defined(SDL_VIDEO_DRIVER_X11)
230 #include <X11/XKBlib.h>
232 static int check_for_evdev(void)
234 SDL_SysWMinfo info;
235 XkbDescPtr desc;
236 int has_evdev = 0;
237 const char *keycodes;
239 SDL_VERSION(&info.version);
240 if (!SDL_GetWMInfo(&info))
241 return 0;
243 desc = XkbGetKeyboard(info.info.x11.display,
244 XkbGBN_AllComponentsMask,
245 XkbUseCoreKbd);
246 if (desc == NULL || desc->names == NULL)
247 return 0;
249 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
250 if (keycodes == NULL)
251 fprintf(stderr, "could not lookup keycode name\n");
252 else if (strstart(keycodes, "evdev_", NULL))
253 has_evdev = 1;
254 else if (!strstart(keycodes, "xfree86_", NULL))
255 fprintf(stderr,
256 "unknown keycodes `%s', please report to qemu-devel@nongnu.org\n",
257 keycodes);
259 XkbFreeClientMap(desc, XkbGBN_AllComponentsMask, True);
261 return has_evdev;
263 #else
264 static int check_for_evdev(void)
266 return 0;
268 #endif
270 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
272 int keycode;
273 static int has_evdev = -1;
275 if (has_evdev == -1)
276 has_evdev = check_for_evdev();
278 keycode = ev->keysym.scancode;
280 if (keycode < 9) {
281 keycode = 0;
282 } else if (keycode < 97) {
283 keycode -= 8; /* just an offset */
284 } else if (keycode < 158) {
285 /* use conversion table */
286 if (has_evdev)
287 keycode = translate_evdev_keycode(keycode - 97);
288 else
289 keycode = translate_xfree86_keycode(keycode - 97);
290 } else if (keycode == 208) { /* Hiragana_Katakana */
291 keycode = 0x70;
292 } else if (keycode == 211) { /* backslash */
293 keycode = 0x73;
294 } else {
295 keycode = 0;
297 return keycode;
300 #endif
302 static void reset_keys(void)
304 int i;
305 for(i = 0; i < 256; i++) {
306 if (modifiers_state[i]) {
307 if (i & 0x80)
308 kbd_put_keycode(0xe0);
309 kbd_put_keycode(i | 0x80);
310 modifiers_state[i] = 0;
315 static void sdl_process_key(SDL_KeyboardEvent *ev)
317 int keycode, v;
319 if (ev->keysym.sym == SDLK_PAUSE) {
320 /* specific case */
321 v = 0;
322 if (ev->type == SDL_KEYUP)
323 v |= 0x80;
324 kbd_put_keycode(0xe1);
325 kbd_put_keycode(0x1d | v);
326 kbd_put_keycode(0x45 | v);
327 return;
330 if (kbd_layout) {
331 keycode = sdl_keyevent_to_keycode_generic(ev);
332 } else {
333 keycode = sdl_keyevent_to_keycode(ev);
336 switch(keycode) {
337 case 0x00:
338 /* sent when leaving window: reset the modifiers state */
339 reset_keys();
340 return;
341 case 0x2a: /* Left Shift */
342 case 0x36: /* Right Shift */
343 case 0x1d: /* Left CTRL */
344 case 0x9d: /* Right CTRL */
345 case 0x38: /* Left ALT */
346 case 0xb8: /* Right ALT */
347 if (ev->type == SDL_KEYUP)
348 modifiers_state[keycode] = 0;
349 else
350 modifiers_state[keycode] = 1;
351 break;
352 case 0x45: /* num lock */
353 case 0x3a: /* caps lock */
354 /* SDL does not send the key up event, so we generate it */
355 kbd_put_keycode(keycode);
356 kbd_put_keycode(keycode | 0x80);
357 return;
360 /* now send the key code */
361 if (keycode & 0x80)
362 kbd_put_keycode(0xe0);
363 if (ev->type == SDL_KEYUP)
364 kbd_put_keycode(keycode | 0x80);
365 else
366 kbd_put_keycode(keycode & 0x7f);
369 static void sdl_update_caption(void)
371 char buf[1024];
372 const char *status = "";
374 if (!vm_running)
375 status = " [Stopped]";
376 else if (gui_grab) {
377 if (!alt_grab)
378 status = " - Press Ctrl-Alt to exit grab";
379 else
380 status = " - Press Ctrl-Alt-Shift to exit grab";
383 if (qemu_name)
384 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
385 else
386 snprintf(buf, sizeof(buf), "QEMU%s", status);
388 SDL_WM_SetCaption(buf, "QEMU");
391 static void sdl_hide_cursor(void)
393 if (!cursor_hide)
394 return;
396 if (kbd_mouse_is_absolute()) {
397 SDL_ShowCursor(1);
398 SDL_SetCursor(sdl_cursor_hidden);
399 } else {
400 SDL_ShowCursor(0);
404 static void sdl_show_cursor(void)
406 if (!cursor_hide)
407 return;
409 if (!kbd_mouse_is_absolute()) {
410 SDL_ShowCursor(1);
411 if (guest_cursor &&
412 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
413 SDL_SetCursor(guest_sprite);
414 else
415 SDL_SetCursor(sdl_cursor_normal);
419 static void sdl_grab_start(void)
421 if (guest_cursor) {
422 SDL_SetCursor(guest_sprite);
423 if (!kbd_mouse_is_absolute() && !absolute_enabled)
424 SDL_WarpMouse(guest_x, guest_y);
425 } else
426 sdl_hide_cursor();
428 if (SDL_WM_GrabInput(SDL_GRAB_ON) == SDL_GRAB_ON) {
429 gui_grab = 1;
430 sdl_update_caption();
431 } else
432 sdl_show_cursor();
435 static void sdl_grab_end(void)
437 SDL_WM_GrabInput(SDL_GRAB_OFF);
438 gui_grab = 0;
439 sdl_show_cursor();
440 sdl_update_caption();
443 static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
445 int buttons;
446 buttons = 0;
447 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
448 buttons |= MOUSE_EVENT_LBUTTON;
449 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
450 buttons |= MOUSE_EVENT_RBUTTON;
451 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
452 buttons |= MOUSE_EVENT_MBUTTON;
454 if (kbd_mouse_is_absolute()) {
455 if (!absolute_enabled) {
456 sdl_hide_cursor();
457 if (gui_grab) {
458 sdl_grab_end();
460 absolute_enabled = 1;
463 dx = x * 0x7FFF / (width - 1);
464 dy = y * 0x7FFF / (height - 1);
465 } else if (absolute_enabled) {
466 sdl_show_cursor();
467 absolute_enabled = 0;
468 } else if (guest_cursor) {
469 x -= guest_x;
470 y -= guest_y;
471 guest_x += x;
472 guest_y += y;
473 dx = x;
474 dy = y;
477 kbd_mouse_event(dx, dy, dz, buttons);
480 static void toggle_full_screen(DisplayState *ds)
482 gui_fullscreen = !gui_fullscreen;
483 do_sdl_resize(real_screen->w, real_screen->h, real_screen->format->BitsPerPixel);
484 if (gui_fullscreen) {
485 gui_saved_grab = gui_grab;
486 sdl_grab_start();
487 } else {
488 if (!gui_saved_grab)
489 sdl_grab_end();
491 vga_hw_invalidate();
492 vga_hw_update();
495 static void sdl_refresh(DisplayState *ds)
497 SDL_Event ev1, *ev = &ev1;
498 int mod_state;
499 int buttonstate = SDL_GetMouseState(NULL, NULL);
501 if (last_vm_running != vm_running) {
502 last_vm_running = vm_running;
503 sdl_update_caption();
506 vga_hw_update();
507 SDL_EnableUNICODE(!is_graphic_console());
509 while (SDL_PollEvent(ev)) {
510 switch (ev->type) {
511 case SDL_VIDEOEXPOSE:
512 sdl_update(ds, 0, 0, real_screen->w, real_screen->h);
513 break;
514 case SDL_KEYDOWN:
515 case SDL_KEYUP:
516 if (ev->type == SDL_KEYDOWN) {
517 if (!alt_grab) {
518 mod_state = (SDL_GetModState() & gui_grab_code) ==
519 gui_grab_code;
520 } else {
521 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
522 (gui_grab_code | KMOD_LSHIFT);
524 gui_key_modifier_pressed = mod_state;
525 if (gui_key_modifier_pressed) {
526 int keycode;
527 keycode = sdl_keyevent_to_keycode(&ev->key);
528 switch(keycode) {
529 case 0x21: /* 'f' key on US keyboard */
530 toggle_full_screen(ds);
531 gui_keysym = 1;
532 break;
533 case 0x02 ... 0x0a: /* '1' to '9' keys */
534 /* Reset the modifiers sent to the current console */
535 reset_keys();
536 console_select(keycode - 0x02);
537 if (!is_graphic_console()) {
538 /* display grab if going to a text console */
539 if (gui_grab)
540 sdl_grab_end();
542 gui_keysym = 1;
543 break;
544 default:
545 break;
547 } else if (!is_graphic_console()) {
548 int keysym;
549 keysym = 0;
550 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
551 switch(ev->key.keysym.sym) {
552 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
553 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
554 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
555 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
556 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
557 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
558 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
559 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
560 default: break;
562 } else {
563 switch(ev->key.keysym.sym) {
564 case SDLK_UP: keysym = QEMU_KEY_UP; break;
565 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
566 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
567 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
568 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
569 case SDLK_END: keysym = QEMU_KEY_END; break;
570 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
571 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
572 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
573 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
574 default: break;
577 if (keysym) {
578 kbd_put_keysym(keysym);
579 } else if (ev->key.keysym.unicode != 0) {
580 kbd_put_keysym(ev->key.keysym.unicode);
583 } else if (ev->type == SDL_KEYUP) {
584 if (!alt_grab) {
585 mod_state = (ev->key.keysym.mod & gui_grab_code);
586 } else {
587 mod_state = (ev->key.keysym.mod &
588 (gui_grab_code | KMOD_LSHIFT));
590 if (!mod_state) {
591 if (gui_key_modifier_pressed) {
592 gui_key_modifier_pressed = 0;
593 if (gui_keysym == 0) {
594 /* exit/enter grab if pressing Ctrl-Alt */
595 if (!gui_grab) {
596 /* if the application is not active,
597 do not try to enter grab state. It
598 prevents
599 'SDL_WM_GrabInput(SDL_GRAB_ON)'
600 from blocking all the application
601 (SDL bug). */
602 if (SDL_GetAppState() & SDL_APPACTIVE)
603 sdl_grab_start();
604 } else {
605 sdl_grab_end();
607 /* SDL does not send back all the
608 modifiers key, so we must correct it */
609 reset_keys();
610 break;
612 gui_keysym = 0;
616 if (is_graphic_console() && !gui_keysym)
617 sdl_process_key(&ev->key);
618 break;
619 case SDL_QUIT:
620 if (!no_quit)
621 qemu_system_shutdown_request();
622 break;
623 case SDL_MOUSEMOTION:
624 if (gui_grab || kbd_mouse_is_absolute() ||
625 absolute_enabled) {
626 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
627 ev->motion.x, ev->motion.y, ev->motion.state);
629 break;
630 case SDL_MOUSEBUTTONDOWN:
631 case SDL_MOUSEBUTTONUP:
633 SDL_MouseButtonEvent *bev = &ev->button;
634 if (!gui_grab && !kbd_mouse_is_absolute()) {
635 if (ev->type == SDL_MOUSEBUTTONDOWN &&
636 (bev->button == SDL_BUTTON_LEFT)) {
637 /* start grabbing all events */
638 sdl_grab_start();
640 } else {
641 int dz;
642 dz = 0;
643 if (ev->type == SDL_MOUSEBUTTONDOWN) {
644 buttonstate |= SDL_BUTTON(bev->button);
645 } else {
646 buttonstate &= ~SDL_BUTTON(bev->button);
648 #ifdef SDL_BUTTON_WHEELUP
649 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
650 dz = -1;
651 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
652 dz = 1;
654 #endif
655 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
658 break;
659 case SDL_ACTIVEEVENT:
660 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
661 !ev->active.gain && !gui_fullscreen_initial_grab) {
662 sdl_grab_end();
664 if (ev->active.state & SDL_APPACTIVE) {
665 if (ev->active.gain) {
666 /* Back to default interval */
667 dcl->gui_timer_interval = 0;
668 dcl->idle = 0;
669 } else {
670 /* Sleeping interval */
671 dcl->gui_timer_interval = 500;
672 dcl->idle = 1;
675 break;
676 default:
677 break;
682 static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
684 SDL_Rect dst = { x, y, w, h };
685 SDL_FillRect(real_screen, &dst, c);
688 static void sdl_mouse_warp(int x, int y, int on)
690 if (on) {
691 if (!guest_cursor)
692 sdl_show_cursor();
693 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
694 SDL_SetCursor(guest_sprite);
695 if (!kbd_mouse_is_absolute() && !absolute_enabled)
696 SDL_WarpMouse(x, y);
698 } else if (gui_grab)
699 sdl_hide_cursor();
700 guest_cursor = on;
701 guest_x = x, guest_y = y;
704 static void sdl_mouse_define(int width, int height, int bpp,
705 int hot_x, int hot_y,
706 uint8_t *image, uint8_t *mask)
708 uint8_t sprite[256], *line;
709 int x, y, dst, bypl, src = 0;
710 if (guest_sprite)
711 SDL_FreeCursor(guest_sprite);
713 memset(sprite, 0, 256);
714 bypl = ((width * bpp + 31) >> 5) << 2;
715 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
716 line = image;
717 for (x = 0; x < width; x ++, dst ++) {
718 switch (bpp) {
719 case 24:
720 src = *(line ++); src |= *(line ++); src |= *(line ++);
721 break;
722 case 16:
723 case 15:
724 src = *(line ++); src |= *(line ++);
725 break;
726 case 8:
727 src = *(line ++);
728 break;
729 case 4:
730 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
731 break;
732 case 2:
733 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
734 break;
735 case 1:
736 src = 1 & (line[x >> 3] >> (x & 7));
737 break;
739 if (!src)
740 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
743 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
745 if (guest_cursor &&
746 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
747 SDL_SetCursor(guest_sprite);
750 static void sdl_cleanup(void)
752 if (guest_sprite)
753 SDL_FreeCursor(guest_sprite);
754 SDL_Quit();
757 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
759 int flags;
760 uint8_t data = 0;
761 DisplayAllocator *da;
762 const SDL_VideoInfo *vi;
764 #if defined(__APPLE__)
765 /* always use generic keymaps */
766 if (!keyboard_layout)
767 keyboard_layout = "en-us";
768 #endif
769 if(keyboard_layout) {
770 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
771 if (!kbd_layout)
772 exit(1);
775 if (no_frame)
776 gui_noframe = 1;
778 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
779 if (SDL_Init (flags)) {
780 fprintf(stderr, "Could not initialize SDL - exiting\n");
781 exit(1);
783 vi = SDL_GetVideoInfo();
784 hostbpp = vi->vfmt->BitsPerPixel;
786 dcl = qemu_mallocz(sizeof(DisplayChangeListener));
787 dcl->dpy_update = sdl_update;
788 dcl->dpy_resize = sdl_resize;
789 dcl->dpy_refresh = sdl_refresh;
790 dcl->dpy_setdata = sdl_setdata;
791 dcl->dpy_fill = sdl_fill;
792 ds->mouse_set = sdl_mouse_warp;
793 ds->cursor_define = sdl_mouse_define;
794 register_displaychangelistener(ds, dcl);
796 da = qemu_mallocz(sizeof(DisplayAllocator));
797 da->create_displaysurface = sdl_create_displaysurface;
798 da->resize_displaysurface = sdl_resize_displaysurface;
799 da->free_displaysurface = sdl_free_displaysurface;
800 if (register_displayallocator(ds, da) == da) {
801 DisplaySurface *surf;
802 surf = sdl_create_displaysurface(ds_get_width(ds), ds_get_height(ds));
803 defaultallocator_free_displaysurface(ds->surface);
804 ds->surface = surf;
805 dpy_resize(ds);
808 sdl_update_caption();
809 SDL_EnableKeyRepeat(250, 50);
810 gui_grab = 0;
812 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
813 sdl_cursor_normal = SDL_GetCursor();
815 atexit(sdl_cleanup);
816 if (full_screen) {
817 gui_fullscreen = 1;
818 gui_fullscreen_initial_grab = 1;
819 sdl_grab_start();