Statistics
| Branch: | Revision:

root / sdl.c @ b67d5959

History | View | Annotate | Download (7.8 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 0f0b7264 bellard
#include <stdlib.h>
25 0f0b7264 bellard
#include <stdio.h>
26 0f0b7264 bellard
#include <stdarg.h>
27 0f0b7264 bellard
#include <string.h>
28 0f0b7264 bellard
#include <getopt.h>
29 0f0b7264 bellard
#include <inttypes.h>
30 0f0b7264 bellard
#include <unistd.h>
31 0f0b7264 bellard
#include <sys/mman.h>
32 0f0b7264 bellard
#include <fcntl.h>
33 0f0b7264 bellard
#include <signal.h>
34 0f0b7264 bellard
#include <time.h>
35 0f0b7264 bellard
#include <sys/time.h>
36 0f0b7264 bellard
#include <malloc.h>
37 0f0b7264 bellard
#include <termios.h>
38 0f0b7264 bellard
#include <sys/poll.h>
39 0f0b7264 bellard
#include <errno.h>
40 0f0b7264 bellard
#include <sys/wait.h>
41 0f0b7264 bellard
#include <netinet/in.h>
42 0f0b7264 bellard
43 0f0b7264 bellard
#include <SDL.h>
44 0f0b7264 bellard
45 0f0b7264 bellard
#include "cpu-i386.h"
46 0f0b7264 bellard
#include "exec.h"
47 0f0b7264 bellard
48 0f0b7264 bellard
#include "vl.h"
49 0f0b7264 bellard
50 0f0b7264 bellard
static SDL_Surface *screen;
51 0f0b7264 bellard
static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
52 0f0b7264 bellard
53 0f0b7264 bellard
static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
54 0f0b7264 bellard
{
55 0f0b7264 bellard
    SDL_UpdateRect(screen, x, y, w, h);
56 0f0b7264 bellard
}
57 0f0b7264 bellard
58 0f0b7264 bellard
static void sdl_resize(DisplayState *ds, int w, int h)
59 0f0b7264 bellard
{
60 0f0b7264 bellard
    int flags;
61 0f0b7264 bellard
62 0f0b7264 bellard
    //    printf("resizing to %d %d\n", w, h);
63 0f0b7264 bellard
64 0f0b7264 bellard
    flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
65 0f0b7264 bellard
    flags |= SDL_RESIZABLE;
66 0f0b7264 bellard
    screen = SDL_SetVideoMode(w, h, 0, flags);
67 0f0b7264 bellard
    if (!screen) {
68 0f0b7264 bellard
        fprintf(stderr, "Could not open SDL display\n");
69 0f0b7264 bellard
        exit(1);
70 0f0b7264 bellard
    }
71 0f0b7264 bellard
    ds->data = screen->pixels;
72 0f0b7264 bellard
    ds->linesize = screen->pitch;
73 0f0b7264 bellard
    ds->depth = screen->format->BitsPerPixel;
74 0f0b7264 bellard
}
75 0f0b7264 bellard
76 0f0b7264 bellard
static const uint32_t x_keycode_to_pc_keycode[61] = {
77 0f0b7264 bellard
   0x47e0,      /*  97  Home   */
78 0f0b7264 bellard
   0x48e0,      /*  98  Up     */
79 0f0b7264 bellard
   0x49e0,      /*  99  PgUp   */
80 0f0b7264 bellard
   0x4be0,      /* 100  Left   */
81 0f0b7264 bellard
   0x4c,        /* 101  KP-5   */
82 0f0b7264 bellard
   0x4de0,      /* 102  Right  */
83 0f0b7264 bellard
   0x4fe0,      /* 103  End    */
84 0f0b7264 bellard
   0x50e0,      /* 104  Down   */
85 0f0b7264 bellard
   0x51e0,      /* 105  PgDn   */
86 0f0b7264 bellard
   0x52e0,      /* 106  Ins    */
87 0f0b7264 bellard
   0x53e0,      /* 107  Del    */
88 0f0b7264 bellard
   0x1ce0,      /* 108  Enter  */
89 0f0b7264 bellard
   0x1de0,      /* 109  Ctrl-R */
90 0f0b7264 bellard
   0x451de1,    /* 110  Pause  */
91 0f0b7264 bellard
   0x37e0,      /* 111  Print  */
92 0f0b7264 bellard
   0x35e0,      /* 112  Divide */
93 0f0b7264 bellard
   0x38e0,      /* 113  Alt-R  */
94 0f0b7264 bellard
   0x46e0,      /* 114  Break  */   
95 0f0b7264 bellard
   0x0,         /* 115 */
96 0f0b7264 bellard
   0x0,         /* 116 */
97 0f0b7264 bellard
   0x0,         /* 117 */
98 0f0b7264 bellard
   0x0,         /* 118 */
99 0f0b7264 bellard
   0x0,         /* 119 */
100 0f0b7264 bellard
   0x0,         /* 120 */
101 0f0b7264 bellard
   0x0,         /* 121 */
102 0f0b7264 bellard
   0x0,         /* 122 */
103 0f0b7264 bellard
   0x0,         /* 123 */
104 0f0b7264 bellard
   0x0,         /* 124 */
105 0f0b7264 bellard
   0x0,         /* 125 */
106 0f0b7264 bellard
   0x0,         /* 126 */
107 0f0b7264 bellard
   0x0,         /* 127 */
108 0f0b7264 bellard
   0x0,         /* 128 */
109 0f0b7264 bellard
   0x0,         /* 129 */
110 0f0b7264 bellard
   0x0,         /* 130 */
111 0f0b7264 bellard
   0x0,         /* 131 */
112 0f0b7264 bellard
   0x0,         /* 132 */
113 0f0b7264 bellard
   0x0,         /* 133 */
114 0f0b7264 bellard
   0x0,         /* 134 */
115 0f0b7264 bellard
   0x0,         /* 135 */
116 0f0b7264 bellard
   0x47,         /* 136 KP_7 */
117 0f0b7264 bellard
   0x48,         /* 137 KP_8 */
118 0f0b7264 bellard
   0x49,         /* 138 KP_9 */
119 0f0b7264 bellard
   0x4b,         /* 139 KP_4 */
120 0f0b7264 bellard
   0x4c,         /* 140 KP_5 */
121 0f0b7264 bellard
   0x4d,         /* 141 KP_6 */
122 0f0b7264 bellard
   0x4f,         /* 142 KP_1 */
123 0f0b7264 bellard
   0x50,         /* 143 KP_2 */
124 0f0b7264 bellard
   0x51,         /* 144 KP_3 */
125 0f0b7264 bellard
   0x52,         /* 145 KP_0 */
126 0f0b7264 bellard
   0x53,         /* 146 KP_. */
127 0f0b7264 bellard
   0x47,         /* 147 KP_HOME */
128 0f0b7264 bellard
   0x48,         /* 148 KP_UP */
129 0f0b7264 bellard
   0x49,         /* 149 KP_PgUp */
130 0f0b7264 bellard
   0x4b,         /* 150 KP_Left */
131 0f0b7264 bellard
   0x4c,         /* 151 KP_ */
132 0f0b7264 bellard
   0x4d,         /* 152 KP_Right */
133 0f0b7264 bellard
   0x4f,         /* 153 KP_End */
134 0f0b7264 bellard
   0x50,         /* 154 KP_Down */
135 0f0b7264 bellard
   0x51,         /* 155 KP_PgDn */
136 0f0b7264 bellard
   0x52,         /* 156 KP_Ins */
137 0f0b7264 bellard
   0x53,         /* 157 KP_Del */
138 0f0b7264 bellard
};
139 0f0b7264 bellard
140 0f0b7264 bellard
static void sdl_process_key(SDL_KeyboardEvent *ev)
141 0f0b7264 bellard
{
142 0f0b7264 bellard
    int keycode, v;
143 0f0b7264 bellard
    
144 0f0b7264 bellard
    /* XXX: not portable, but avoids complicated mappings */
145 0f0b7264 bellard
    keycode = ev->keysym.scancode;
146 0f0b7264 bellard
    if (keycode < 9) {
147 0f0b7264 bellard
        keycode = 0;
148 0f0b7264 bellard
    } else if (keycode < 97) {
149 0f0b7264 bellard
        keycode -= 8; /* just an offset */
150 0f0b7264 bellard
    } else if (keycode < 158) {
151 0f0b7264 bellard
        /* use conversion table */
152 0f0b7264 bellard
        keycode = x_keycode_to_pc_keycode[keycode - 97];
153 0f0b7264 bellard
    } else {
154 0f0b7264 bellard
        keycode = 0;
155 0f0b7264 bellard
    }
156 0f0b7264 bellard
    
157 0f0b7264 bellard
    /* now send the key code */
158 0f0b7264 bellard
    while (keycode != 0) {
159 0f0b7264 bellard
        v = keycode & 0xff;
160 0f0b7264 bellard
        if (ev->type == SDL_KEYUP)
161 0f0b7264 bellard
            v |= 0x80;
162 0f0b7264 bellard
        kbd_put_keycode(v);
163 0f0b7264 bellard
        keycode >>= 8;
164 0f0b7264 bellard
    }
165 0f0b7264 bellard
}
166 0f0b7264 bellard
167 0f0b7264 bellard
static void sdl_grab_start(void)
168 0f0b7264 bellard
{
169 0f0b7264 bellard
    SDL_WM_SetCaption("QEMU - Press Ctrl-Shift to exit grab", "QEMU");
170 0f0b7264 bellard
    SDL_ShowCursor(0);
171 0f0b7264 bellard
    SDL_WM_GrabInput(SDL_GRAB_ON);
172 0f0b7264 bellard
    /* dummy read to avoid moving the mouse */
173 0f0b7264 bellard
    SDL_GetRelativeMouseState(NULL, NULL);
174 0f0b7264 bellard
    gui_grab = 1;
175 0f0b7264 bellard
}
176 0f0b7264 bellard
177 0f0b7264 bellard
static void sdl_grab_end(void)
178 0f0b7264 bellard
{
179 0f0b7264 bellard
    SDL_WM_SetCaption("QEMU", "QEMU");
180 0f0b7264 bellard
    SDL_WM_GrabInput(SDL_GRAB_OFF);
181 0f0b7264 bellard
    SDL_ShowCursor(1);
182 0f0b7264 bellard
    gui_grab = 0;
183 0f0b7264 bellard
}
184 0f0b7264 bellard
185 0f0b7264 bellard
static void sdl_send_mouse_event(void)
186 0f0b7264 bellard
{
187 0f0b7264 bellard
    int dx, dy, dz, state, buttons;
188 0f0b7264 bellard
    state = SDL_GetRelativeMouseState(&dx, &dy);
189 0f0b7264 bellard
    buttons = 0;
190 0f0b7264 bellard
    if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
191 0f0b7264 bellard
        buttons |= MOUSE_EVENT_LBUTTON;
192 0f0b7264 bellard
    if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
193 0f0b7264 bellard
        buttons |= MOUSE_EVENT_RBUTTON;
194 0f0b7264 bellard
    if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
195 0f0b7264 bellard
        buttons |= MOUSE_EVENT_MBUTTON;
196 0f0b7264 bellard
    /* XXX: test wheel */
197 0f0b7264 bellard
    dz = 0;
198 0f0b7264 bellard
    if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP))
199 0f0b7264 bellard
        dz--;
200 0f0b7264 bellard
    if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN))
201 0f0b7264 bellard
        dz++;
202 0f0b7264 bellard
    kbd_mouse_event(dx, dy, dz, buttons);
203 0f0b7264 bellard
}
204 0f0b7264 bellard
205 0f0b7264 bellard
static void sdl_refresh(DisplayState *ds)
206 0f0b7264 bellard
{
207 0f0b7264 bellard
    SDL_Event ev1, *ev = &ev1;
208 0f0b7264 bellard
209 0f0b7264 bellard
    vga_update_display();
210 0f0b7264 bellard
    while (SDL_PollEvent(ev)) {
211 0f0b7264 bellard
        switch (ev->type) {
212 0f0b7264 bellard
        case SDL_VIDEOEXPOSE:
213 0f0b7264 bellard
            sdl_update(ds, 0, 0, screen->w, screen->h);
214 0f0b7264 bellard
            break;
215 0f0b7264 bellard
        case SDL_KEYDOWN:
216 0f0b7264 bellard
        case SDL_KEYUP:
217 0f0b7264 bellard
            if (ev->type == SDL_KEYDOWN) {
218 0f0b7264 bellard
                if ((SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) ==
219 0f0b7264 bellard
                    (KMOD_LSHIFT | KMOD_LCTRL)) {
220 0f0b7264 bellard
                    /* exit/enter grab if pressing Ctrl-Shift */
221 0f0b7264 bellard
                    if (!gui_grab)
222 0f0b7264 bellard
                        sdl_grab_start();
223 0f0b7264 bellard
                    else
224 0f0b7264 bellard
                        sdl_grab_end();
225 0f0b7264 bellard
                }
226 0f0b7264 bellard
            }
227 0f0b7264 bellard
            sdl_process_key(&ev->key);
228 0f0b7264 bellard
            break;
229 0f0b7264 bellard
        case SDL_QUIT:
230 0f0b7264 bellard
            reset_requested = 1;
231 0f0b7264 bellard
            break;
232 0f0b7264 bellard
        case SDL_MOUSEMOTION:
233 0f0b7264 bellard
            if (gui_grab) {
234 0f0b7264 bellard
                sdl_send_mouse_event();
235 0f0b7264 bellard
            }
236 0f0b7264 bellard
            break;
237 0f0b7264 bellard
        case SDL_MOUSEBUTTONDOWN:
238 0f0b7264 bellard
        case SDL_MOUSEBUTTONUP:
239 0f0b7264 bellard
            {
240 0f0b7264 bellard
                SDL_MouseButtonEvent *bev = &ev->button;
241 0f0b7264 bellard
                if (!gui_grab) {
242 0f0b7264 bellard
                    if (ev->type == SDL_MOUSEBUTTONDOWN &&
243 0f0b7264 bellard
                        (bev->state & SDL_BUTTON_LMASK)) {
244 0f0b7264 bellard
                        /* start grabbing all events */
245 0f0b7264 bellard
                        sdl_grab_start();
246 0f0b7264 bellard
                    }
247 0f0b7264 bellard
                } else {
248 0f0b7264 bellard
                    sdl_send_mouse_event();
249 0f0b7264 bellard
                }
250 0f0b7264 bellard
            }
251 0f0b7264 bellard
            break;
252 0f0b7264 bellard
        default:
253 0f0b7264 bellard
            break;
254 0f0b7264 bellard
        }
255 0f0b7264 bellard
    }
256 0f0b7264 bellard
}
257 0f0b7264 bellard
258 0f0b7264 bellard
void sdl_display_init(DisplayState *ds)
259 0f0b7264 bellard
{
260 0f0b7264 bellard
    int flags;
261 0f0b7264 bellard
262 0f0b7264 bellard
    flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
263 0f0b7264 bellard
    if (SDL_Init (flags)) {
264 0f0b7264 bellard
        fprintf(stderr, "Could not initialize SDL - exiting\n");
265 0f0b7264 bellard
        exit(1);
266 0f0b7264 bellard
    }
267 0f0b7264 bellard
    ds->dpy_update = sdl_update;
268 0f0b7264 bellard
    ds->dpy_resize = sdl_resize;
269 0f0b7264 bellard
    ds->dpy_refresh = sdl_refresh;
270 0f0b7264 bellard
271 0f0b7264 bellard
    sdl_resize(ds, 640, 400);
272 0f0b7264 bellard
    SDL_WM_SetCaption("QEMU", "QEMU");
273 0f0b7264 bellard
    SDL_EnableKeyRepeat(250, 50);
274 0f0b7264 bellard
    gui_grab = 0;
275 0f0b7264 bellard
}