Statistics
| Branch: | Revision:

root / ui / sdl.c @ d4970b07

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