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
41 #include <netinet/in.h>
50 static SDL_Surface *screen;
51 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
52 static int last_vm_running;
54 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
56 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
57 SDL_UpdateRect(screen, x, y, w, h);
60 static void sdl_resize(DisplayState *ds, int w, int h)
64 // printf("resizing to %d %d\n", w, h);
66 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
67 flags |= SDL_RESIZABLE;
68 screen = SDL_SetVideoMode(w, h, 0, flags);
70 fprintf(stderr, "Could not open SDL display\n");
73 ds->data = screen->pixels;
74 ds->linesize = screen->pitch;
75 ds->depth = screen->format->BitsPerPixel;
78 static const uint32_t x_keycode_to_pc_keycode[61] = {
82 0x4be0, /* 100 Left */
84 0x4de0, /* 102 Right */
86 0x50e0, /* 104 Down */
87 0x51e0, /* 105 PgDn */
90 0x1ce0, /* 108 Enter */
91 0x1de0, /* 109 Ctrl-R */
92 0x451de1, /* 110 Pause */
93 0x37e0, /* 111 Print */
94 0x35e0, /* 112 Divide */
95 0x38e0, /* 113 Alt-R */
96 0x46e0, /* 114 Break */
129 0x47, /* 147 KP_HOME */
130 0x48, /* 148 KP_UP */
131 0x49, /* 149 KP_PgUp */
132 0x4b, /* 150 KP_Left */
134 0x4d, /* 152 KP_Right */
135 0x4f, /* 153 KP_End */
136 0x50, /* 154 KP_Down */
137 0x51, /* 155 KP_PgDn */
138 0x52, /* 156 KP_Ins */
139 0x53, /* 157 KP_Del */
142 static void sdl_process_key(SDL_KeyboardEvent *ev)
146 /* XXX: not portable, but avoids complicated mappings */
147 keycode = ev->keysym.scancode;
150 } else if (keycode < 97) {
151 keycode -= 8; /* just an offset */
152 } else if (keycode < 158) {
153 /* use conversion table */
154 keycode = x_keycode_to_pc_keycode[keycode - 97];
159 /* now send the key code */
160 while (keycode != 0) {
162 if (ev->type == SDL_KEYUP)
169 static void sdl_update_caption(void)
174 strcat(buf, " [Stopped]");
177 strcat(buf, " - Press Ctrl-Shift to exit grab");
179 SDL_WM_SetCaption(buf, "QEMU");
182 static void sdl_grab_start(void)
185 SDL_WM_GrabInput(SDL_GRAB_ON);
186 /* dummy read to avoid moving the mouse */
187 SDL_GetRelativeMouseState(NULL, NULL);
189 sdl_update_caption();
192 static void sdl_grab_end(void)
194 SDL_WM_GrabInput(SDL_GRAB_OFF);
197 sdl_update_caption();
200 static void sdl_send_mouse_event(void)
202 int dx, dy, dz, state, buttons;
203 state = SDL_GetRelativeMouseState(&dx, &dy);
205 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
206 buttons |= MOUSE_EVENT_LBUTTON;
207 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
208 buttons |= MOUSE_EVENT_RBUTTON;
209 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
210 buttons |= MOUSE_EVENT_MBUTTON;
211 /* XXX: test wheel */
213 #ifdef SDL_BUTTON_WHEELUP
214 if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP))
216 if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN))
219 kbd_mouse_event(dx, dy, dz, buttons);
222 static void sdl_refresh(DisplayState *ds)
224 SDL_Event ev1, *ev = &ev1;
226 if (last_vm_running != vm_running) {
227 last_vm_running = vm_running;
228 sdl_update_caption();
231 vga_update_display();
232 while (SDL_PollEvent(ev)) {
234 case SDL_VIDEOEXPOSE:
235 sdl_update(ds, 0, 0, screen->w, screen->h);
239 if (ev->type == SDL_KEYDOWN) {
240 if ((SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) ==
241 (KMOD_LSHIFT | KMOD_LCTRL)) {
242 /* exit/enter grab if pressing Ctrl-Shift */
249 sdl_process_key(&ev->key);
254 case SDL_MOUSEMOTION:
256 sdl_send_mouse_event();
259 case SDL_MOUSEBUTTONDOWN:
260 case SDL_MOUSEBUTTONUP:
262 SDL_MouseButtonEvent *bev = &ev->button;
264 if (ev->type == SDL_MOUSEBUTTONDOWN &&
265 (bev->state & SDL_BUTTON_LMASK)) {
266 /* start grabbing all events */
270 sdl_send_mouse_event();
280 static void sdl_cleanup(void)
285 void sdl_display_init(DisplayState *ds)
289 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
290 if (SDL_Init (flags)) {
291 fprintf(stderr, "Could not initialize SDL - exiting\n");
294 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
295 signal(SIGINT, SIG_DFL);
296 signal(SIGQUIT, SIG_DFL);
298 ds->dpy_update = sdl_update;
299 ds->dpy_resize = sdl_resize;
300 ds->dpy_refresh = sdl_refresh;
302 sdl_resize(ds, 640, 400);
303 sdl_update_caption();
304 SDL_EnableKeyRepeat(250, 50);