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 */
53 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
55 SDL_UpdateRect(screen, x, y, w, h);
58 static void sdl_resize(DisplayState *ds, int w, int h)
62 // printf("resizing to %d %d\n", w, h);
64 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
65 flags |= SDL_RESIZABLE;
66 screen = SDL_SetVideoMode(w, h, 0, flags);
68 fprintf(stderr, "Could not open SDL display\n");
71 ds->data = screen->pixels;
72 ds->linesize = screen->pitch;
73 ds->depth = screen->format->BitsPerPixel;
76 static const uint32_t x_keycode_to_pc_keycode[61] = {
80 0x4be0, /* 100 Left */
82 0x4de0, /* 102 Right */
84 0x50e0, /* 104 Down */
85 0x51e0, /* 105 PgDn */
88 0x1ce0, /* 108 Enter */
89 0x1de0, /* 109 Ctrl-R */
90 0x451de1, /* 110 Pause */
91 0x37e0, /* 111 Print */
92 0x35e0, /* 112 Divide */
93 0x38e0, /* 113 Alt-R */
94 0x46e0, /* 114 Break */
127 0x47, /* 147 KP_HOME */
128 0x48, /* 148 KP_UP */
129 0x49, /* 149 KP_PgUp */
130 0x4b, /* 150 KP_Left */
132 0x4d, /* 152 KP_Right */
133 0x4f, /* 153 KP_End */
134 0x50, /* 154 KP_Down */
135 0x51, /* 155 KP_PgDn */
136 0x52, /* 156 KP_Ins */
137 0x53, /* 157 KP_Del */
140 static void sdl_process_key(SDL_KeyboardEvent *ev)
144 /* XXX: not portable, but avoids complicated mappings */
145 keycode = ev->keysym.scancode;
148 } else if (keycode < 97) {
149 keycode -= 8; /* just an offset */
150 } else if (keycode < 158) {
151 /* use conversion table */
152 keycode = x_keycode_to_pc_keycode[keycode - 97];
157 /* now send the key code */
158 while (keycode != 0) {
160 if (ev->type == SDL_KEYUP)
167 static void sdl_grab_start(void)
169 SDL_WM_SetCaption("QEMU - Press Ctrl-Shift to exit grab", "QEMU");
171 SDL_WM_GrabInput(SDL_GRAB_ON);
172 /* dummy read to avoid moving the mouse */
173 SDL_GetRelativeMouseState(NULL, NULL);
177 static void sdl_grab_end(void)
179 SDL_WM_SetCaption("QEMU", "QEMU");
180 SDL_WM_GrabInput(SDL_GRAB_OFF);
185 static void sdl_send_mouse_event(void)
187 int dx, dy, dz, state, buttons;
188 state = SDL_GetRelativeMouseState(&dx, &dy);
190 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
191 buttons |= MOUSE_EVENT_LBUTTON;
192 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
193 buttons |= MOUSE_EVENT_RBUTTON;
194 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
195 buttons |= MOUSE_EVENT_MBUTTON;
196 /* XXX: test wheel */
198 #ifdef SDL_BUTTON_WHEELUP
199 if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP))
201 if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN))
204 kbd_mouse_event(dx, dy, dz, buttons);
207 static void sdl_refresh(DisplayState *ds)
209 SDL_Event ev1, *ev = &ev1;
211 vga_update_display();
212 while (SDL_PollEvent(ev)) {
214 case SDL_VIDEOEXPOSE:
215 sdl_update(ds, 0, 0, screen->w, screen->h);
219 if (ev->type == SDL_KEYDOWN) {
220 if ((SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) ==
221 (KMOD_LSHIFT | KMOD_LCTRL)) {
222 /* exit/enter grab if pressing Ctrl-Shift */
229 sdl_process_key(&ev->key);
234 case SDL_MOUSEMOTION:
236 sdl_send_mouse_event();
239 case SDL_MOUSEBUTTONDOWN:
240 case SDL_MOUSEBUTTONUP:
242 SDL_MouseButtonEvent *bev = &ev->button;
244 if (ev->type == SDL_MOUSEBUTTONDOWN &&
245 (bev->state & SDL_BUTTON_LMASK)) {
246 /* start grabbing all events */
250 sdl_send_mouse_event();
260 void sdl_display_init(DisplayState *ds)
264 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
265 if (SDL_Init (flags)) {
266 fprintf(stderr, "Could not initialize SDL - exiting\n");
269 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
270 signal(SIGINT, SIG_DFL);
271 signal(SIGQUIT, SIG_DFL);
273 ds->dpy_update = sdl_update;
274 ds->dpy_resize = sdl_resize;
275 ds->dpy_refresh = sdl_refresh;
277 sdl_resize(ds, 640, 400);
278 SDL_WM_SetCaption("QEMU", "QEMU");
279 SDL_EnableKeyRepeat(250, 50);