Statistics
| Branch: | Revision:

root / sdl.c @ f58ae59c

History | View | Annotate | Download (27 kB)

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