Statistics
| Branch: | Revision:

root / sdl.c @ 3f7638ec

History | View | Annotate | Download (27.5 kB)

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