Statistics
| Branch: | Revision:

root / ui / sdl.c @ 9a85d394

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