Statistics
| Branch: | Revision:

root / sdl.c @ c227f099

History | View | Annotate | Download (27.3 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 660f11be Blue Swirl
static SDL_Cursor *guest_sprite = NULL;
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 c227f099 Anthony Liguori
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 b4ed5d18 Dominic Evans
    char win_title[1024];
411 b4ed5d18 Dominic Evans
    char icon_title[1024];
412 c35734b2 ths
    const char *status = "";
413 c35734b2 ths
414 c35734b2 ths
    if (!vm_running)
415 c35734b2 ths
        status = " [Stopped]";
416 3780e197 ths
    else if (gui_grab) {
417 3780e197 ths
        if (!alt_grab)
418 3780e197 ths
            status = " - Press Ctrl-Alt to exit grab";
419 3780e197 ths
        else
420 3780e197 ths
            status = " - Press Ctrl-Alt-Shift to exit grab";
421 3780e197 ths
    }
422 c35734b2 ths
423 b4ed5d18 Dominic Evans
    if (qemu_name) {
424 b4ed5d18 Dominic Evans
        snprintf(win_title, sizeof(win_title), "QEMU (%s)%s", qemu_name, status);
425 b4ed5d18 Dominic Evans
        snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
426 b4ed5d18 Dominic Evans
    } else {
427 b4ed5d18 Dominic Evans
        snprintf(win_title, sizeof(win_title), "QEMU%s", status);
428 b4ed5d18 Dominic Evans
        snprintf(icon_title, sizeof(icon_title), "QEMU");
429 b4ed5d18 Dominic Evans
    }
430 c35734b2 ths
431 b4ed5d18 Dominic Evans
    SDL_WM_SetCaption(win_title, icon_title);
432 8a7ddc38 bellard
}
433 8a7ddc38 bellard
434 09b26c5e bellard
static void sdl_hide_cursor(void)
435 09b26c5e bellard
{
436 9467cd46 balrog
    if (!cursor_hide)
437 9467cd46 balrog
        return;
438 9467cd46 balrog
439 8785a8dd bellard
    if (kbd_mouse_is_absolute()) {
440 8785a8dd bellard
        SDL_ShowCursor(1);
441 8785a8dd bellard
        SDL_SetCursor(sdl_cursor_hidden);
442 8785a8dd bellard
    } else {
443 8785a8dd bellard
        SDL_ShowCursor(0);
444 8785a8dd bellard
    }
445 09b26c5e bellard
}
446 09b26c5e bellard
447 09b26c5e bellard
static void sdl_show_cursor(void)
448 09b26c5e bellard
{
449 9467cd46 balrog
    if (!cursor_hide)
450 9467cd46 balrog
        return;
451 9467cd46 balrog
452 09b26c5e bellard
    if (!kbd_mouse_is_absolute()) {
453 8785a8dd bellard
        SDL_ShowCursor(1);
454 d34cab9f ths
        if (guest_cursor &&
455 d34cab9f ths
                (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
456 d34cab9f ths
            SDL_SetCursor(guest_sprite);
457 d34cab9f ths
        else
458 d34cab9f ths
            SDL_SetCursor(sdl_cursor_normal);
459 09b26c5e bellard
    }
460 09b26c5e bellard
}
461 09b26c5e bellard
462 0f0b7264 bellard
static void sdl_grab_start(void)
463 0f0b7264 bellard
{
464 d34cab9f ths
    if (guest_cursor) {
465 d34cab9f ths
        SDL_SetCursor(guest_sprite);
466 08a2d4c4 balrog
        if (!kbd_mouse_is_absolute() && !absolute_enabled)
467 08a2d4c4 balrog
            SDL_WarpMouse(guest_x, guest_y);
468 d34cab9f ths
    } else
469 d34cab9f ths
        sdl_hide_cursor();
470 6bb81603 aliguori
471 6bb81603 aliguori
    if (SDL_WM_GrabInput(SDL_GRAB_ON) == SDL_GRAB_ON) {
472 6bb81603 aliguori
        gui_grab = 1;
473 6bb81603 aliguori
        sdl_update_caption();
474 6bb81603 aliguori
    } else
475 6bb81603 aliguori
        sdl_show_cursor();
476 0f0b7264 bellard
}
477 0f0b7264 bellard
478 0f0b7264 bellard
static void sdl_grab_end(void)
479 0f0b7264 bellard
{
480 0f0b7264 bellard
    SDL_WM_GrabInput(SDL_GRAB_OFF);
481 0f0b7264 bellard
    gui_grab = 0;
482 d34cab9f ths
    sdl_show_cursor();
483 8a7ddc38 bellard
    sdl_update_caption();
484 0f0b7264 bellard
}
485 0f0b7264 bellard
486 4c44bdcb aurel32
static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
487 0f0b7264 bellard
{
488 4c44bdcb aurel32
    int buttons;
489 0f0b7264 bellard
    buttons = 0;
490 0f0b7264 bellard
    if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
491 0f0b7264 bellard
        buttons |= MOUSE_EVENT_LBUTTON;
492 0f0b7264 bellard
    if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
493 0f0b7264 bellard
        buttons |= MOUSE_EVENT_RBUTTON;
494 0f0b7264 bellard
    if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
495 0f0b7264 bellard
        buttons |= MOUSE_EVENT_MBUTTON;
496 09b26c5e bellard
497 09b26c5e bellard
    if (kbd_mouse_is_absolute()) {
498 09b26c5e bellard
        if (!absolute_enabled) {
499 09b26c5e bellard
            sdl_hide_cursor();
500 09b26c5e bellard
            if (gui_grab) {
501 09b26c5e bellard
                sdl_grab_end();
502 09b26c5e bellard
            }
503 09b26c5e bellard
            absolute_enabled = 1;
504 09b26c5e bellard
        }
505 09b26c5e bellard
506 4c44bdcb aurel32
       dx = x * 0x7FFF / (width - 1);
507 4c44bdcb aurel32
       dy = y * 0x7FFF / (height - 1);
508 455204eb ths
    } else if (absolute_enabled) {
509 455204eb ths
        sdl_show_cursor();
510 455204eb ths
        absolute_enabled = 0;
511 d34cab9f ths
    } else if (guest_cursor) {
512 4c44bdcb aurel32
        x -= guest_x;
513 4c44bdcb aurel32
        y -= guest_y;
514 4c44bdcb aurel32
        guest_x += x;
515 4c44bdcb aurel32
        guest_y += y;
516 4c44bdcb aurel32
        dx = x;
517 4c44bdcb aurel32
        dy = y;
518 09b26c5e bellard
    }
519 09b26c5e bellard
520 0f0b7264 bellard
    kbd_mouse_event(dx, dy, dz, buttons);
521 0f0b7264 bellard
}
522 0f0b7264 bellard
523 8e9c4afe bellard
static void toggle_full_screen(DisplayState *ds)
524 8e9c4afe bellard
{
525 8e9c4afe bellard
    gui_fullscreen = !gui_fullscreen;
526 8e9c4afe bellard
    if (gui_fullscreen) {
527 c18a2c36 Stefano Stabellini
        scaling_active = 0;
528 8e9c4afe bellard
        gui_saved_grab = gui_grab;
529 8e9c4afe bellard
        sdl_grab_start();
530 8e9c4afe bellard
    } else {
531 8e9c4afe bellard
        if (!gui_saved_grab)
532 8e9c4afe bellard
            sdl_grab_end();
533 8e9c4afe bellard
    }
534 95219897 pbrook
    vga_hw_invalidate();
535 95219897 pbrook
    vga_hw_update();
536 8e9c4afe bellard
}
537 8e9c4afe bellard
538 0f0b7264 bellard
static void sdl_refresh(DisplayState *ds)
539 0f0b7264 bellard
{
540 0f0b7264 bellard
    SDL_Event ev1, *ev = &ev1;
541 8e9c4afe bellard
    int mod_state;
542 4c44bdcb aurel32
    int buttonstate = SDL_GetMouseState(NULL, NULL);
543 3b46e624 ths
544 8a7ddc38 bellard
    if (last_vm_running != vm_running) {
545 8a7ddc38 bellard
        last_vm_running = vm_running;
546 8a7ddc38 bellard
        sdl_update_caption();
547 8a7ddc38 bellard
    }
548 8a7ddc38 bellard
549 95219897 pbrook
    vga_hw_update();
550 3bee8bd0 aurel32
    SDL_EnableUNICODE(!is_graphic_console());
551 457831f4 bellard
552 0f0b7264 bellard
    while (SDL_PollEvent(ev)) {
553 0f0b7264 bellard
        switch (ev->type) {
554 0f0b7264 bellard
        case SDL_VIDEOEXPOSE:
555 7d957bd8 aliguori
            sdl_update(ds, 0, 0, real_screen->w, real_screen->h);
556 0f0b7264 bellard
            break;
557 0f0b7264 bellard
        case SDL_KEYDOWN:
558 0f0b7264 bellard
        case SDL_KEYUP:
559 0f0b7264 bellard
            if (ev->type == SDL_KEYDOWN) {
560 3780e197 ths
                if (!alt_grab) {
561 3780e197 ths
                    mod_state = (SDL_GetModState() & gui_grab_code) ==
562 3780e197 ths
                                gui_grab_code;
563 3780e197 ths
                } else {
564 3780e197 ths
                    mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
565 3780e197 ths
                                (gui_grab_code | KMOD_LSHIFT);
566 3780e197 ths
                }
567 8e9c4afe bellard
                gui_key_modifier_pressed = mod_state;
568 457831f4 bellard
                if (gui_key_modifier_pressed) {
569 32ff25bf bellard
                    int keycode;
570 32ff25bf bellard
                    keycode = sdl_keyevent_to_keycode(&ev->key);
571 32ff25bf bellard
                    switch(keycode) {
572 32ff25bf bellard
                    case 0x21: /* 'f' key on US keyboard */
573 457831f4 bellard
                        toggle_full_screen(ds);
574 457831f4 bellard
                        gui_keysym = 1;
575 457831f4 bellard
                        break;
576 c4a735f9 malc
                    case 0x16: /* 'u' key on US keyboard */
577 c4a735f9 malc
                        scaling_active = 0;
578 c4a735f9 malc
                        sdl_resize(ds);
579 c4a735f9 malc
                        vga_hw_invalidate();
580 c4a735f9 malc
                        vga_hw_update();
581 c4a735f9 malc
                        break;
582 5fafdf24 ths
                    case 0x02 ... 0x0a: /* '1' to '9' keys */
583 dfd92d3a bellard
                        /* Reset the modifiers sent to the current console */
584 dfd92d3a bellard
                        reset_keys();
585 32ff25bf bellard
                        console_select(keycode - 0x02);
586 95219897 pbrook
                        if (!is_graphic_console()) {
587 457831f4 bellard
                            /* display grab if going to a text console */
588 457831f4 bellard
                            if (gui_grab)
589 457831f4 bellard
                                sdl_grab_end();
590 457831f4 bellard
                        }
591 457831f4 bellard
                        gui_keysym = 1;
592 457831f4 bellard
                        break;
593 457831f4 bellard
                    default:
594 457831f4 bellard
                        break;
595 457831f4 bellard
                    }
596 95219897 pbrook
                } else if (!is_graphic_console()) {
597 457831f4 bellard
                    int keysym;
598 457831f4 bellard
                    keysym = 0;
599 457831f4 bellard
                    if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
600 457831f4 bellard
                        switch(ev->key.keysym.sym) {
601 457831f4 bellard
                        case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
602 457831f4 bellard
                        case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
603 457831f4 bellard
                        case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
604 457831f4 bellard
                        case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
605 457831f4 bellard
                        case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
606 457831f4 bellard
                        case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
607 457831f4 bellard
                        case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
608 457831f4 bellard
                        case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
609 457831f4 bellard
                        default: break;
610 457831f4 bellard
                        }
611 457831f4 bellard
                    } else {
612 457831f4 bellard
                        switch(ev->key.keysym.sym) {
613 457831f4 bellard
                        case SDLK_UP: keysym = QEMU_KEY_UP; break;
614 457831f4 bellard
                        case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
615 457831f4 bellard
                        case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
616 457831f4 bellard
                        case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
617 457831f4 bellard
                        case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
618 457831f4 bellard
                        case SDLK_END: keysym = QEMU_KEY_END; break;
619 457831f4 bellard
                        case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
620 457831f4 bellard
                        case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
621 e91c8a77 ths
                        case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
622 e91c8a77 ths
                        case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
623 457831f4 bellard
                        default: break;
624 457831f4 bellard
                        }
625 457831f4 bellard
                    }
626 457831f4 bellard
                    if (keysym) {
627 457831f4 bellard
                        kbd_put_keysym(keysym);
628 457831f4 bellard
                    } else if (ev->key.keysym.unicode != 0) {
629 457831f4 bellard
                        kbd_put_keysym(ev->key.keysym.unicode);
630 457831f4 bellard
                    }
631 8e9c4afe bellard
                }
632 8e9c4afe bellard
            } else if (ev->type == SDL_KEYUP) {
633 3780e197 ths
                if (!alt_grab) {
634 3780e197 ths
                    mod_state = (ev->key.keysym.mod & gui_grab_code);
635 3780e197 ths
                } else {
636 3780e197 ths
                    mod_state = (ev->key.keysym.mod &
637 3780e197 ths
                                 (gui_grab_code | KMOD_LSHIFT));
638 3780e197 ths
                }
639 8e9c4afe bellard
                if (!mod_state) {
640 8e9c4afe bellard
                    if (gui_key_modifier_pressed) {
641 5b311878 pbrook
                        gui_key_modifier_pressed = 0;
642 457831f4 bellard
                        if (gui_keysym == 0) {
643 32ff25bf bellard
                            /* exit/enter grab if pressing Ctrl-Alt */
644 c66b0d4c bellard
                            if (!gui_grab) {
645 c66b0d4c bellard
                                /* if the application is not active,
646 c66b0d4c bellard
                                   do not try to enter grab state. It
647 c66b0d4c bellard
                                   prevents
648 c66b0d4c bellard
                                   'SDL_WM_GrabInput(SDL_GRAB_ON)'
649 c66b0d4c bellard
                                   from blocking all the application
650 c66b0d4c bellard
                                   (SDL bug). */
651 c66b0d4c bellard
                                if (SDL_GetAppState() & SDL_APPACTIVE)
652 c66b0d4c bellard
                                    sdl_grab_start();
653 c66b0d4c bellard
                            } else {
654 8e9c4afe bellard
                                sdl_grab_end();
655 c66b0d4c bellard
                            }
656 32ff25bf bellard
                            /* SDL does not send back all the
657 32ff25bf bellard
                               modifiers key, so we must correct it */
658 32ff25bf bellard
                            reset_keys();
659 8e9c4afe bellard
                            break;
660 8e9c4afe bellard
                        }
661 8e9c4afe bellard
                        gui_keysym = 0;
662 8e9c4afe bellard
                    }
663 0f0b7264 bellard
                }
664 0f0b7264 bellard
            }
665 5fafdf24 ths
            if (is_graphic_console() && !gui_keysym)
666 457831f4 bellard
                sdl_process_key(&ev->key);
667 0f0b7264 bellard
            break;
668 0f0b7264 bellard
        case SDL_QUIT:
669 5b08fc10 aliguori
            if (!no_quit)
670 731345e1 balrog
                qemu_system_shutdown_request();
671 0f0b7264 bellard
            break;
672 0f0b7264 bellard
        case SDL_MOUSEMOTION:
673 455204eb ths
            if (gui_grab || kbd_mouse_is_absolute() ||
674 455204eb ths
                absolute_enabled) {
675 4c44bdcb aurel32
                sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
676 4c44bdcb aurel32
                       ev->motion.x, ev->motion.y, ev->motion.state);
677 0f0b7264 bellard
            }
678 0f0b7264 bellard
            break;
679 0f0b7264 bellard
        case SDL_MOUSEBUTTONDOWN:
680 0f0b7264 bellard
        case SDL_MOUSEBUTTONUP:
681 0f0b7264 bellard
            {
682 0f0b7264 bellard
                SDL_MouseButtonEvent *bev = &ev->button;
683 09b26c5e bellard
                if (!gui_grab && !kbd_mouse_is_absolute()) {
684 0f0b7264 bellard
                    if (ev->type == SDL_MOUSEBUTTONDOWN &&
685 4c44bdcb aurel32
                        (bev->button == SDL_BUTTON_LEFT)) {
686 0f0b7264 bellard
                        /* start grabbing all events */
687 0f0b7264 bellard
                        sdl_grab_start();
688 0f0b7264 bellard
                    }
689 0f0b7264 bellard
                } else {
690 18a6d284 bellard
                    int dz;
691 18a6d284 bellard
                    dz = 0;
692 4c44bdcb aurel32
                    if (ev->type == SDL_MOUSEBUTTONDOWN) {
693 4c44bdcb aurel32
                        buttonstate |= SDL_BUTTON(bev->button);
694 4c44bdcb aurel32
                    } else {
695 4c44bdcb aurel32
                        buttonstate &= ~SDL_BUTTON(bev->button);
696 4c44bdcb aurel32
                    }
697 18a6d284 bellard
#ifdef SDL_BUTTON_WHEELUP
698 09b26c5e bellard
                    if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
699 18a6d284 bellard
                        dz = -1;
700 09b26c5e bellard
                    } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
701 18a6d284 bellard
                        dz = 1;
702 18a6d284 bellard
                    }
703 3b46e624 ths
#endif
704 4c44bdcb aurel32
                    sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
705 0f0b7264 bellard
                }
706 0f0b7264 bellard
            }
707 0f0b7264 bellard
            break;
708 0294ffb9 bellard
        case SDL_ACTIVEEVENT:
709 5b311878 pbrook
            if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
710 5b311878 pbrook
                !ev->active.gain && !gui_fullscreen_initial_grab) {
711 0294ffb9 bellard
                sdl_grab_end();
712 0294ffb9 bellard
            }
713 f442e08b aurel32
            if (ev->active.state & SDL_APPACTIVE) {
714 f442e08b aurel32
                if (ev->active.gain) {
715 f442e08b aurel32
                    /* Back to default interval */
716 7d957bd8 aliguori
                    dcl->gui_timer_interval = 0;
717 7d957bd8 aliguori
                    dcl->idle = 0;
718 f442e08b aurel32
                } else {
719 f442e08b aurel32
                    /* Sleeping interval */
720 7d957bd8 aliguori
                    dcl->gui_timer_interval = 500;
721 7d957bd8 aliguori
                    dcl->idle = 1;
722 f442e08b aurel32
                }
723 f442e08b aurel32
            }
724 0294ffb9 bellard
            break;
725 c18a2c36 Stefano Stabellini
        case SDL_VIDEORESIZE:
726 c18a2c36 Stefano Stabellini
        {
727 c18a2c36 Stefano Stabellini
            SDL_ResizeEvent *rev = &ev->resize;
728 c18a2c36 Stefano Stabellini
            int bpp = real_screen->format->BitsPerPixel;
729 c18a2c36 Stefano Stabellini
            if (bpp != 16 && bpp != 32)
730 c18a2c36 Stefano Stabellini
                bpp = 32;
731 c18a2c36 Stefano Stabellini
            do_sdl_resize(rev->w, rev->h, bpp);
732 c18a2c36 Stefano Stabellini
            scaling_active = 1;
733 ae288347 Stefano Stabellini
            if (!is_buffer_shared(ds->surface)) {
734 ae288347 Stefano Stabellini
                ds->surface = qemu_resize_displaysurface(ds, ds_get_width(ds), ds_get_height(ds));
735 ae288347 Stefano Stabellini
                dpy_resize(ds);
736 ae288347 Stefano Stabellini
            }
737 c18a2c36 Stefano Stabellini
            vga_hw_invalidate();
738 c18a2c36 Stefano Stabellini
            vga_hw_update();
739 c18a2c36 Stefano Stabellini
            break;
740 c18a2c36 Stefano Stabellini
        }
741 0f0b7264 bellard
        default:
742 0f0b7264 bellard
            break;
743 0f0b7264 bellard
        }
744 0f0b7264 bellard
    }
745 0f0b7264 bellard
}
746 0f0b7264 bellard
747 d34cab9f ths
static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
748 d34cab9f ths
{
749 d34cab9f ths
    SDL_Rect dst = { x, y, w, h };
750 7d957bd8 aliguori
    SDL_FillRect(real_screen, &dst, c);
751 d34cab9f ths
}
752 d34cab9f ths
753 d34cab9f ths
static void sdl_mouse_warp(int x, int y, int on)
754 d34cab9f ths
{
755 d34cab9f ths
    if (on) {
756 d34cab9f ths
        if (!guest_cursor)
757 d34cab9f ths
            sdl_show_cursor();
758 d34cab9f ths
        if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
759 d34cab9f ths
            SDL_SetCursor(guest_sprite);
760 08a2d4c4 balrog
            if (!kbd_mouse_is_absolute() && !absolute_enabled)
761 08a2d4c4 balrog
                SDL_WarpMouse(x, y);
762 d34cab9f ths
        }
763 d34cab9f ths
    } else if (gui_grab)
764 d34cab9f ths
        sdl_hide_cursor();
765 d34cab9f ths
    guest_cursor = on;
766 d34cab9f ths
    guest_x = x, guest_y = y;
767 d34cab9f ths
}
768 d34cab9f ths
769 d34cab9f ths
static void sdl_mouse_define(int width, int height, int bpp,
770 d34cab9f ths
                             int hot_x, int hot_y,
771 d34cab9f ths
                             uint8_t *image, uint8_t *mask)
772 d34cab9f ths
{
773 d34cab9f ths
    uint8_t sprite[256], *line;
774 d34cab9f ths
    int x, y, dst, bypl, src = 0;
775 d34cab9f ths
    if (guest_sprite)
776 d34cab9f ths
        SDL_FreeCursor(guest_sprite);
777 d34cab9f ths
778 d34cab9f ths
    memset(sprite, 0, 256);
779 d34cab9f ths
    bypl = ((width * bpp + 31) >> 5) << 2;
780 d34cab9f ths
    for (y = 0, dst = 0; y < height; y ++, image += bypl) {
781 d34cab9f ths
        line = image;
782 d34cab9f ths
        for (x = 0; x < width; x ++, dst ++) {
783 d34cab9f ths
            switch (bpp) {
784 699960b2 Reimar Döffinger
            case 32:
785 699960b2 Reimar Döffinger
                src = *(line ++); src |= *(line ++); src |= *(line ++); line++;
786 699960b2 Reimar Döffinger
                break;
787 d34cab9f ths
            case 24:
788 d34cab9f ths
                src = *(line ++); src |= *(line ++); src |= *(line ++);
789 d34cab9f ths
                break;
790 d34cab9f ths
            case 16:
791 d34cab9f ths
            case 15:
792 d34cab9f ths
                src = *(line ++); src |= *(line ++);
793 d34cab9f ths
                break;
794 d34cab9f ths
            case 8:
795 d34cab9f ths
                src = *(line ++);
796 d34cab9f ths
                break;
797 d34cab9f ths
            case 4:
798 d34cab9f ths
                src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
799 d34cab9f ths
                break;
800 d34cab9f ths
            case 2:
801 d34cab9f ths
                src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
802 d34cab9f ths
                break;
803 d34cab9f ths
            case 1:
804 d34cab9f ths
                src = 1 & (line[x >> 3] >> (x & 7));
805 d34cab9f ths
                break;
806 d34cab9f ths
            }
807 d34cab9f ths
            if (!src)
808 d34cab9f ths
                sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
809 d34cab9f ths
        }
810 d34cab9f ths
    }
811 d34cab9f ths
    guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
812 d34cab9f ths
813 d34cab9f ths
    if (guest_cursor &&
814 d34cab9f ths
            (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
815 d34cab9f ths
        SDL_SetCursor(guest_sprite);
816 d34cab9f ths
}
817 d34cab9f ths
818 5fafdf24 ths
static void sdl_cleanup(void)
819 898712a8 bellard
{
820 d34cab9f ths
    if (guest_sprite)
821 d34cab9f ths
        SDL_FreeCursor(guest_sprite);
822 d8ee7665 malc
    SDL_QuitSubSystem(SDL_INIT_VIDEO);
823 898712a8 bellard
}
824 898712a8 bellard
825 43523e93 ths
void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
826 0f0b7264 bellard
{
827 0f0b7264 bellard
    int flags;
828 09b26c5e bellard
    uint8_t data = 0;
829 7b5d76da aliguori
    DisplayAllocator *da;
830 7b5d76da aliguori
    const SDL_VideoInfo *vi;
831 0f0b7264 bellard
832 3d11d0eb bellard
#if defined(__APPLE__)
833 3d11d0eb bellard
    /* always use generic keymaps */
834 3d11d0eb bellard
    if (!keyboard_layout)
835 3d11d0eb bellard
        keyboard_layout = "en-us";
836 3d11d0eb bellard
#endif
837 3d11d0eb bellard
    if(keyboard_layout) {
838 0483755a aliguori
        kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
839 3d11d0eb bellard
        if (!kbd_layout)
840 3d11d0eb bellard
            exit(1);
841 3d11d0eb bellard
    }
842 3d11d0eb bellard
843 43523e93 ths
    if (no_frame)
844 43523e93 ths
        gui_noframe = 1;
845 43523e93 ths
846 0f0b7264 bellard
    flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
847 0f0b7264 bellard
    if (SDL_Init (flags)) {
848 0f0b7264 bellard
        fprintf(stderr, "Could not initialize SDL - exiting\n");
849 0f0b7264 bellard
        exit(1);
850 0f0b7264 bellard
    }
851 7b5d76da aliguori
    vi = SDL_GetVideoInfo();
852 c18a2c36 Stefano Stabellini
    host_format = *(vi->vfmt);
853 0ae04d73 bellard
854 7d957bd8 aliguori
    dcl = qemu_mallocz(sizeof(DisplayChangeListener));
855 7d957bd8 aliguori
    dcl->dpy_update = sdl_update;
856 7d957bd8 aliguori
    dcl->dpy_resize = sdl_resize;
857 7d957bd8 aliguori
    dcl->dpy_refresh = sdl_refresh;
858 7d957bd8 aliguori
    dcl->dpy_setdata = sdl_setdata;
859 7d957bd8 aliguori
    dcl->dpy_fill = sdl_fill;
860 d34cab9f ths
    ds->mouse_set = sdl_mouse_warp;
861 d34cab9f ths
    ds->cursor_define = sdl_mouse_define;
862 7d957bd8 aliguori
    register_displaychangelistener(ds, dcl);
863 0f0b7264 bellard
864 7b5d76da aliguori
    da = qemu_mallocz(sizeof(DisplayAllocator));
865 7b5d76da aliguori
    da->create_displaysurface = sdl_create_displaysurface;
866 7b5d76da aliguori
    da->resize_displaysurface = sdl_resize_displaysurface;
867 7b5d76da aliguori
    da->free_displaysurface = sdl_free_displaysurface;
868 7b5d76da aliguori
    if (register_displayallocator(ds, da) == da) {
869 7b5d76da aliguori
        DisplaySurface *surf;
870 7b5d76da aliguori
        surf = sdl_create_displaysurface(ds_get_width(ds), ds_get_height(ds));
871 7b5d76da aliguori
        defaultallocator_free_displaysurface(ds->surface);
872 7b5d76da aliguori
        ds->surface = surf;
873 7b5d76da aliguori
        dpy_resize(ds);
874 7b5d76da aliguori
    }
875 7b5d76da aliguori
876 8a7ddc38 bellard
    sdl_update_caption();
877 0f0b7264 bellard
    SDL_EnableKeyRepeat(250, 50);
878 0f0b7264 bellard
    gui_grab = 0;
879 898712a8 bellard
880 09b26c5e bellard
    sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
881 09b26c5e bellard
    sdl_cursor_normal = SDL_GetCursor();
882 09b26c5e bellard
883 898712a8 bellard
    atexit(sdl_cleanup);
884 d63d307f bellard
    if (full_screen) {
885 d63d307f bellard
        gui_fullscreen = 1;
886 d63d307f bellard
        gui_fullscreen_initial_grab = 1;
887 d63d307f bellard
        sdl_grab_start();
888 d63d307f bellard
    }
889 0f0b7264 bellard
}