Statistics
| Branch: | Revision:

root / sdl.c @ fb6cf1d0

History | View | Annotate | Download (9.7 kB)

1 0f0b7264 bellard
/*
2 0f0b7264 bellard
 * QEMU SDL display driver
3 0f0b7264 bellard
 * 
4 0f0b7264 bellard
 * Copyright (c) 2003 Fabrice Bellard
5 0f0b7264 bellard
 * 
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 67b915a5 bellard
#include "vl.h"
25 0f0b7264 bellard
26 0f0b7264 bellard
#include <SDL.h>
27 0f0b7264 bellard
28 67b915a5 bellard
#ifndef _WIN32
29 67b915a5 bellard
#include <signal.h>
30 67b915a5 bellard
#endif
31 0f0b7264 bellard
32 0f0b7264 bellard
static SDL_Surface *screen;
33 0f0b7264 bellard
static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
34 8a7ddc38 bellard
static int last_vm_running;
35 8e9c4afe bellard
static int gui_saved_grab;
36 8e9c4afe bellard
static int gui_fullscreen;
37 8e9c4afe bellard
static int gui_key_modifier_pressed;
38 8e9c4afe bellard
static int gui_keysym;
39 0f0b7264 bellard
40 0f0b7264 bellard
static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
41 0f0b7264 bellard
{
42 898712a8 bellard
    //    printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
43 0f0b7264 bellard
    SDL_UpdateRect(screen, x, y, w, h);
44 0f0b7264 bellard
}
45 0f0b7264 bellard
46 0f0b7264 bellard
static void sdl_resize(DisplayState *ds, int w, int h)
47 0f0b7264 bellard
{
48 0f0b7264 bellard
    int flags;
49 0f0b7264 bellard
50 0f0b7264 bellard
    //    printf("resizing to %d %d\n", w, h);
51 0f0b7264 bellard
52 0f0b7264 bellard
    flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
53 0f0b7264 bellard
    flags |= SDL_RESIZABLE;
54 8e9c4afe bellard
    if (gui_fullscreen)
55 8e9c4afe bellard
        flags |= SDL_FULLSCREEN;
56 0f0b7264 bellard
    screen = SDL_SetVideoMode(w, h, 0, flags);
57 0f0b7264 bellard
    if (!screen) {
58 0f0b7264 bellard
        fprintf(stderr, "Could not open SDL display\n");
59 0f0b7264 bellard
        exit(1);
60 0f0b7264 bellard
    }
61 0f0b7264 bellard
    ds->data = screen->pixels;
62 0f0b7264 bellard
    ds->linesize = screen->pitch;
63 0f0b7264 bellard
    ds->depth = screen->format->BitsPerPixel;
64 0f0b7264 bellard
}
65 0f0b7264 bellard
66 0f0b7264 bellard
static const uint32_t x_keycode_to_pc_keycode[61] = {
67 0f0b7264 bellard
   0x47e0,      /*  97  Home   */
68 0f0b7264 bellard
   0x48e0,      /*  98  Up     */
69 0f0b7264 bellard
   0x49e0,      /*  99  PgUp   */
70 0f0b7264 bellard
   0x4be0,      /* 100  Left   */
71 0f0b7264 bellard
   0x4c,        /* 101  KP-5   */
72 0f0b7264 bellard
   0x4de0,      /* 102  Right  */
73 0f0b7264 bellard
   0x4fe0,      /* 103  End    */
74 0f0b7264 bellard
   0x50e0,      /* 104  Down   */
75 0f0b7264 bellard
   0x51e0,      /* 105  PgDn   */
76 0f0b7264 bellard
   0x52e0,      /* 106  Ins    */
77 0f0b7264 bellard
   0x53e0,      /* 107  Del    */
78 0f0b7264 bellard
   0x1ce0,      /* 108  Enter  */
79 0f0b7264 bellard
   0x1de0,      /* 109  Ctrl-R */
80 0f0b7264 bellard
   0x451de1,    /* 110  Pause  */
81 0f0b7264 bellard
   0x37e0,      /* 111  Print  */
82 0f0b7264 bellard
   0x35e0,      /* 112  Divide */
83 0f0b7264 bellard
   0x38e0,      /* 113  Alt-R  */
84 0f0b7264 bellard
   0x46e0,      /* 114  Break  */   
85 0f0b7264 bellard
   0x0,         /* 115 */
86 0f0b7264 bellard
   0x0,         /* 116 */
87 0f0b7264 bellard
   0x0,         /* 117 */
88 0f0b7264 bellard
   0x0,         /* 118 */
89 0f0b7264 bellard
   0x0,         /* 119 */
90 0f0b7264 bellard
   0x0,         /* 120 */
91 0f0b7264 bellard
   0x0,         /* 121 */
92 0f0b7264 bellard
   0x0,         /* 122 */
93 0f0b7264 bellard
   0x0,         /* 123 */
94 0f0b7264 bellard
   0x0,         /* 124 */
95 0f0b7264 bellard
   0x0,         /* 125 */
96 0f0b7264 bellard
   0x0,         /* 126 */
97 0f0b7264 bellard
   0x0,         /* 127 */
98 0f0b7264 bellard
   0x0,         /* 128 */
99 0f0b7264 bellard
   0x0,         /* 129 */
100 0f0b7264 bellard
   0x0,         /* 130 */
101 0f0b7264 bellard
   0x0,         /* 131 */
102 0f0b7264 bellard
   0x0,         /* 132 */
103 0f0b7264 bellard
   0x0,         /* 133 */
104 0f0b7264 bellard
   0x0,         /* 134 */
105 0f0b7264 bellard
   0x0,         /* 135 */
106 0f0b7264 bellard
   0x47,         /* 136 KP_7 */
107 0f0b7264 bellard
   0x48,         /* 137 KP_8 */
108 0f0b7264 bellard
   0x49,         /* 138 KP_9 */
109 0f0b7264 bellard
   0x4b,         /* 139 KP_4 */
110 0f0b7264 bellard
   0x4c,         /* 140 KP_5 */
111 0f0b7264 bellard
   0x4d,         /* 141 KP_6 */
112 0f0b7264 bellard
   0x4f,         /* 142 KP_1 */
113 0f0b7264 bellard
   0x50,         /* 143 KP_2 */
114 0f0b7264 bellard
   0x51,         /* 144 KP_3 */
115 0f0b7264 bellard
   0x52,         /* 145 KP_0 */
116 0f0b7264 bellard
   0x53,         /* 146 KP_. */
117 0f0b7264 bellard
   0x47,         /* 147 KP_HOME */
118 0f0b7264 bellard
   0x48,         /* 148 KP_UP */
119 0f0b7264 bellard
   0x49,         /* 149 KP_PgUp */
120 0f0b7264 bellard
   0x4b,         /* 150 KP_Left */
121 0f0b7264 bellard
   0x4c,         /* 151 KP_ */
122 0f0b7264 bellard
   0x4d,         /* 152 KP_Right */
123 0f0b7264 bellard
   0x4f,         /* 153 KP_End */
124 0f0b7264 bellard
   0x50,         /* 154 KP_Down */
125 0f0b7264 bellard
   0x51,         /* 155 KP_PgDn */
126 0f0b7264 bellard
   0x52,         /* 156 KP_Ins */
127 0f0b7264 bellard
   0x53,         /* 157 KP_Del */
128 0f0b7264 bellard
};
129 0f0b7264 bellard
130 0f0b7264 bellard
static void sdl_process_key(SDL_KeyboardEvent *ev)
131 0f0b7264 bellard
{
132 0f0b7264 bellard
    int keycode, v;
133 0f0b7264 bellard
    
134 0f0b7264 bellard
    /* XXX: not portable, but avoids complicated mappings */
135 0f0b7264 bellard
    keycode = ev->keysym.scancode;
136 aaba6c15 bellard
#ifdef _WIN32
137 aaba6c15 bellard
    if (keycode < 97) {
138 aaba6c15 bellard
        /* nothing to do */
139 aaba6c15 bellard
    } else 
140 aaba6c15 bellard
#else
141 0f0b7264 bellard
    if (keycode < 9) {
142 0f0b7264 bellard
        keycode = 0;
143 0f0b7264 bellard
    } else if (keycode < 97) {
144 0f0b7264 bellard
        keycode -= 8; /* just an offset */
145 aaba6c15 bellard
    } else 
146 aaba6c15 bellard
#endif
147 aaba6c15 bellard
    if (keycode < 158) {
148 0f0b7264 bellard
        /* use conversion table */
149 0f0b7264 bellard
        keycode = x_keycode_to_pc_keycode[keycode - 97];
150 0f0b7264 bellard
    } else {
151 0f0b7264 bellard
        keycode = 0;
152 0f0b7264 bellard
    }
153 0f0b7264 bellard
    
154 0f0b7264 bellard
    /* now send the key code */
155 0f0b7264 bellard
    while (keycode != 0) {
156 0f0b7264 bellard
        v = keycode & 0xff;
157 0f0b7264 bellard
        if (ev->type == SDL_KEYUP)
158 0f0b7264 bellard
            v |= 0x80;
159 0f0b7264 bellard
        kbd_put_keycode(v);
160 0f0b7264 bellard
        keycode >>= 8;
161 0f0b7264 bellard
    }
162 0f0b7264 bellard
}
163 0f0b7264 bellard
164 8a7ddc38 bellard
static void sdl_update_caption(void)
165 8a7ddc38 bellard
{
166 8a7ddc38 bellard
    char buf[1024];
167 8a7ddc38 bellard
    strcpy(buf, "QEMU");
168 8a7ddc38 bellard
    if (!vm_running) {
169 8a7ddc38 bellard
        strcat(buf, " [Stopped]");
170 8a7ddc38 bellard
    }
171 8a7ddc38 bellard
    if (gui_grab) {
172 8a7ddc38 bellard
        strcat(buf, " - Press Ctrl-Shift to exit grab");
173 8a7ddc38 bellard
    }
174 8a7ddc38 bellard
    SDL_WM_SetCaption(buf, "QEMU");
175 8a7ddc38 bellard
}
176 8a7ddc38 bellard
177 0f0b7264 bellard
static void sdl_grab_start(void)
178 0f0b7264 bellard
{
179 0f0b7264 bellard
    SDL_ShowCursor(0);
180 0f0b7264 bellard
    SDL_WM_GrabInput(SDL_GRAB_ON);
181 0f0b7264 bellard
    /* dummy read to avoid moving the mouse */
182 0f0b7264 bellard
    SDL_GetRelativeMouseState(NULL, NULL);
183 0f0b7264 bellard
    gui_grab = 1;
184 8a7ddc38 bellard
    sdl_update_caption();
185 0f0b7264 bellard
}
186 0f0b7264 bellard
187 0f0b7264 bellard
static void sdl_grab_end(void)
188 0f0b7264 bellard
{
189 0f0b7264 bellard
    SDL_WM_GrabInput(SDL_GRAB_OFF);
190 0f0b7264 bellard
    SDL_ShowCursor(1);
191 0f0b7264 bellard
    gui_grab = 0;
192 8a7ddc38 bellard
    sdl_update_caption();
193 0f0b7264 bellard
}
194 0f0b7264 bellard
195 0f0b7264 bellard
static void sdl_send_mouse_event(void)
196 0f0b7264 bellard
{
197 0f0b7264 bellard
    int dx, dy, dz, state, buttons;
198 0f0b7264 bellard
    state = SDL_GetRelativeMouseState(&dx, &dy);
199 0f0b7264 bellard
    buttons = 0;
200 0f0b7264 bellard
    if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
201 0f0b7264 bellard
        buttons |= MOUSE_EVENT_LBUTTON;
202 0f0b7264 bellard
    if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
203 0f0b7264 bellard
        buttons |= MOUSE_EVENT_RBUTTON;
204 0f0b7264 bellard
    if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
205 0f0b7264 bellard
        buttons |= MOUSE_EVENT_MBUTTON;
206 0f0b7264 bellard
    /* XXX: test wheel */
207 0f0b7264 bellard
    dz = 0;
208 8351d2d4 bellard
#ifdef SDL_BUTTON_WHEELUP
209 0f0b7264 bellard
    if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP))
210 0f0b7264 bellard
        dz--;
211 0f0b7264 bellard
    if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN))
212 0f0b7264 bellard
        dz++;
213 8351d2d4 bellard
#endif
214 0f0b7264 bellard
    kbd_mouse_event(dx, dy, dz, buttons);
215 0f0b7264 bellard
}
216 0f0b7264 bellard
217 8e9c4afe bellard
static void toggle_full_screen(DisplayState *ds)
218 8e9c4afe bellard
{
219 8e9c4afe bellard
    gui_fullscreen = !gui_fullscreen;
220 8e9c4afe bellard
    sdl_resize(ds, screen->w, screen->h);
221 8e9c4afe bellard
    if (gui_fullscreen) {
222 8e9c4afe bellard
        gui_saved_grab = gui_grab;
223 8e9c4afe bellard
        sdl_grab_start();
224 8e9c4afe bellard
    } else {
225 8e9c4afe bellard
        if (!gui_saved_grab)
226 8e9c4afe bellard
            sdl_grab_end();
227 8e9c4afe bellard
    }
228 8e9c4afe bellard
    vga_update_display();
229 8e9c4afe bellard
    sdl_update(ds, 0, 0, screen->w, screen->h);
230 8e9c4afe bellard
}
231 8e9c4afe bellard
232 0f0b7264 bellard
static void sdl_refresh(DisplayState *ds)
233 0f0b7264 bellard
{
234 0f0b7264 bellard
    SDL_Event ev1, *ev = &ev1;
235 8e9c4afe bellard
    int mod_state;
236 8e9c4afe bellard
                     
237 8a7ddc38 bellard
    if (last_vm_running != vm_running) {
238 8a7ddc38 bellard
        last_vm_running = vm_running;
239 8a7ddc38 bellard
        sdl_update_caption();
240 8a7ddc38 bellard
    }
241 8a7ddc38 bellard
242 0f0b7264 bellard
    vga_update_display();
243 0f0b7264 bellard
    while (SDL_PollEvent(ev)) {
244 0f0b7264 bellard
        switch (ev->type) {
245 0f0b7264 bellard
        case SDL_VIDEOEXPOSE:
246 0f0b7264 bellard
            sdl_update(ds, 0, 0, screen->w, screen->h);
247 0f0b7264 bellard
            break;
248 0f0b7264 bellard
        case SDL_KEYDOWN:
249 0f0b7264 bellard
        case SDL_KEYUP:
250 0f0b7264 bellard
            if (ev->type == SDL_KEYDOWN) {
251 8e9c4afe bellard
                mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) ==
252 8e9c4afe bellard
                    (KMOD_LSHIFT | KMOD_LCTRL);
253 8e9c4afe bellard
                gui_key_modifier_pressed = mod_state;
254 8e9c4afe bellard
                if (gui_key_modifier_pressed && 
255 8e9c4afe bellard
                    ev->key.keysym.sym == SDLK_f) {
256 8e9c4afe bellard
                    gui_keysym = ev->key.keysym.sym;
257 8e9c4afe bellard
                }
258 8e9c4afe bellard
            } else if (ev->type == SDL_KEYUP) {
259 8e9c4afe bellard
                mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL));
260 8e9c4afe bellard
                if (!mod_state) {
261 8e9c4afe bellard
                    if (gui_key_modifier_pressed) {
262 8e9c4afe bellard
                        switch(gui_keysym) {
263 8e9c4afe bellard
                        case SDLK_f:
264 8e9c4afe bellard
                            toggle_full_screen(ds);
265 8e9c4afe bellard
                            break;
266 8e9c4afe bellard
                        case 0:
267 8e9c4afe bellard
                            /* exit/enter grab if pressing Ctrl-Shift */
268 8e9c4afe bellard
                            if (!gui_grab)
269 8e9c4afe bellard
                                sdl_grab_start();
270 8e9c4afe bellard
                            else
271 8e9c4afe bellard
                                sdl_grab_end();
272 8e9c4afe bellard
                            break;
273 8e9c4afe bellard
                        }
274 8e9c4afe bellard
                        gui_key_modifier_pressed = 0;
275 8e9c4afe bellard
                        gui_keysym = 0;
276 8e9c4afe bellard
                    }
277 0f0b7264 bellard
                }
278 0f0b7264 bellard
            }
279 0f0b7264 bellard
            sdl_process_key(&ev->key);
280 0f0b7264 bellard
            break;
281 0f0b7264 bellard
        case SDL_QUIT:
282 0f0b7264 bellard
            reset_requested = 1;
283 0f0b7264 bellard
            break;
284 0f0b7264 bellard
        case SDL_MOUSEMOTION:
285 0f0b7264 bellard
            if (gui_grab) {
286 0f0b7264 bellard
                sdl_send_mouse_event();
287 0f0b7264 bellard
            }
288 0f0b7264 bellard
            break;
289 0f0b7264 bellard
        case SDL_MOUSEBUTTONDOWN:
290 0f0b7264 bellard
        case SDL_MOUSEBUTTONUP:
291 0f0b7264 bellard
            {
292 0f0b7264 bellard
                SDL_MouseButtonEvent *bev = &ev->button;
293 0f0b7264 bellard
                if (!gui_grab) {
294 0f0b7264 bellard
                    if (ev->type == SDL_MOUSEBUTTONDOWN &&
295 0f0b7264 bellard
                        (bev->state & SDL_BUTTON_LMASK)) {
296 0f0b7264 bellard
                        /* start grabbing all events */
297 0f0b7264 bellard
                        sdl_grab_start();
298 0f0b7264 bellard
                    }
299 0f0b7264 bellard
                } else {
300 0f0b7264 bellard
                    sdl_send_mouse_event();
301 0f0b7264 bellard
                }
302 0f0b7264 bellard
            }
303 0f0b7264 bellard
            break;
304 0294ffb9 bellard
        case SDL_ACTIVEEVENT:
305 0294ffb9 bellard
            if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0) {
306 0294ffb9 bellard
                sdl_grab_end();
307 0294ffb9 bellard
            }
308 0294ffb9 bellard
            break;
309 0f0b7264 bellard
        default:
310 0f0b7264 bellard
            break;
311 0f0b7264 bellard
        }
312 0f0b7264 bellard
    }
313 0f0b7264 bellard
}
314 0f0b7264 bellard
315 898712a8 bellard
static void sdl_cleanup(void) 
316 898712a8 bellard
{
317 898712a8 bellard
    SDL_Quit();
318 898712a8 bellard
}
319 898712a8 bellard
320 0f0b7264 bellard
void sdl_display_init(DisplayState *ds)
321 0f0b7264 bellard
{
322 0f0b7264 bellard
    int flags;
323 0f0b7264 bellard
324 0f0b7264 bellard
    flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
325 0f0b7264 bellard
    if (SDL_Init (flags)) {
326 0f0b7264 bellard
        fprintf(stderr, "Could not initialize SDL - exiting\n");
327 0f0b7264 bellard
        exit(1);
328 0f0b7264 bellard
    }
329 67b915a5 bellard
330 67b915a5 bellard
#ifndef _WIN32
331 0ae04d73 bellard
    /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
332 0ae04d73 bellard
    signal(SIGINT, SIG_DFL);
333 0ae04d73 bellard
    signal(SIGQUIT, SIG_DFL);
334 67b915a5 bellard
#endif
335 0ae04d73 bellard
336 0f0b7264 bellard
    ds->dpy_update = sdl_update;
337 0f0b7264 bellard
    ds->dpy_resize = sdl_resize;
338 0f0b7264 bellard
    ds->dpy_refresh = sdl_refresh;
339 0f0b7264 bellard
340 0f0b7264 bellard
    sdl_resize(ds, 640, 400);
341 8a7ddc38 bellard
    sdl_update_caption();
342 0f0b7264 bellard
    SDL_EnableKeyRepeat(250, 50);
343 0f0b7264 bellard
    gui_grab = 0;
344 898712a8 bellard
345 898712a8 bellard
    atexit(sdl_cleanup);
346 0f0b7264 bellard
}