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