2 * QEMU SDL display driver
4 * Copyright (c) 2003 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 static SDL_Surface *screen;
33 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
34 static int last_vm_running;
36 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
38 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
39 SDL_UpdateRect(screen, x, y, w, h);
42 static void sdl_resize(DisplayState *ds, int w, int h)
46 // printf("resizing to %d %d\n", w, h);
48 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
49 flags |= SDL_RESIZABLE;
50 screen = SDL_SetVideoMode(w, h, 0, flags);
52 fprintf(stderr, "Could not open SDL display\n");
55 ds->data = screen->pixels;
56 ds->linesize = screen->pitch;
57 ds->depth = screen->format->BitsPerPixel;
60 static const uint32_t x_keycode_to_pc_keycode[61] = {
64 0x4be0, /* 100 Left */
66 0x4de0, /* 102 Right */
68 0x50e0, /* 104 Down */
69 0x51e0, /* 105 PgDn */
72 0x1ce0, /* 108 Enter */
73 0x1de0, /* 109 Ctrl-R */
74 0x451de1, /* 110 Pause */
75 0x37e0, /* 111 Print */
76 0x35e0, /* 112 Divide */
77 0x38e0, /* 113 Alt-R */
78 0x46e0, /* 114 Break */
111 0x47, /* 147 KP_HOME */
112 0x48, /* 148 KP_UP */
113 0x49, /* 149 KP_PgUp */
114 0x4b, /* 150 KP_Left */
116 0x4d, /* 152 KP_Right */
117 0x4f, /* 153 KP_End */
118 0x50, /* 154 KP_Down */
119 0x51, /* 155 KP_PgDn */
120 0x52, /* 156 KP_Ins */
121 0x53, /* 157 KP_Del */
124 static void sdl_process_key(SDL_KeyboardEvent *ev)
128 /* XXX: not portable, but avoids complicated mappings */
129 keycode = ev->keysym.scancode;
137 } else if (keycode < 97) {
138 keycode -= 8; /* just an offset */
142 /* use conversion table */
143 keycode = x_keycode_to_pc_keycode[keycode - 97];
148 /* now send the key code */
149 while (keycode != 0) {
151 if (ev->type == SDL_KEYUP)
158 static void sdl_update_caption(void)
163 strcat(buf, " [Stopped]");
166 strcat(buf, " - Press Ctrl-Shift to exit grab");
168 SDL_WM_SetCaption(buf, "QEMU");
171 static void sdl_grab_start(void)
174 SDL_WM_GrabInput(SDL_GRAB_ON);
175 /* dummy read to avoid moving the mouse */
176 SDL_GetRelativeMouseState(NULL, NULL);
178 sdl_update_caption();
181 static void sdl_grab_end(void)
183 SDL_WM_GrabInput(SDL_GRAB_OFF);
186 sdl_update_caption();
189 static void sdl_send_mouse_event(void)
191 int dx, dy, dz, state, buttons;
192 state = SDL_GetRelativeMouseState(&dx, &dy);
194 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
195 buttons |= MOUSE_EVENT_LBUTTON;
196 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
197 buttons |= MOUSE_EVENT_RBUTTON;
198 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
199 buttons |= MOUSE_EVENT_MBUTTON;
200 /* XXX: test wheel */
202 #ifdef SDL_BUTTON_WHEELUP
203 if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP))
205 if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN))
208 kbd_mouse_event(dx, dy, dz, buttons);
211 static void sdl_refresh(DisplayState *ds)
213 SDL_Event ev1, *ev = &ev1;
215 if (last_vm_running != vm_running) {
216 last_vm_running = vm_running;
217 sdl_update_caption();
220 vga_update_display();
221 while (SDL_PollEvent(ev)) {
223 case SDL_VIDEOEXPOSE:
224 sdl_update(ds, 0, 0, screen->w, screen->h);
228 if (ev->type == SDL_KEYDOWN) {
229 if ((SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) ==
230 (KMOD_LSHIFT | KMOD_LCTRL)) {
231 /* exit/enter grab if pressing Ctrl-Shift */
238 sdl_process_key(&ev->key);
243 case SDL_MOUSEMOTION:
245 sdl_send_mouse_event();
248 case SDL_MOUSEBUTTONDOWN:
249 case SDL_MOUSEBUTTONUP:
251 SDL_MouseButtonEvent *bev = &ev->button;
253 if (ev->type == SDL_MOUSEBUTTONDOWN &&
254 (bev->state & SDL_BUTTON_LMASK)) {
255 /* start grabbing all events */
259 sdl_send_mouse_event();
269 static void sdl_cleanup(void)
274 void sdl_display_init(DisplayState *ds)
278 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
279 if (SDL_Init (flags)) {
280 fprintf(stderr, "Could not initialize SDL - exiting\n");
285 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
286 signal(SIGINT, SIG_DFL);
287 signal(SIGQUIT, SIG_DFL);
290 ds->dpy_update = sdl_update;
291 ds->dpy_resize = sdl_resize;
292 ds->dpy_refresh = sdl_refresh;
294 sdl_resize(ds, 640, 400);
295 sdl_update_caption();
296 SDL_EnableKeyRepeat(250, 50);