Statistics
| Branch: | Revision:

root / sdl.c @ e2542fe2

History | View | Annotate | Download (26.5 kB)

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