blob: 7b3c3b5eab38cda3851240d577ed2ee928e1eccb [file] [log] [blame]
bellard0f0b7262003-08-09 18:26:36 +00001/*
2 * QEMU SDL display driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
bellard67b915a2004-03-31 23:37:16 +000024#include "vl.h"
bellard0f0b7262003-08-09 18:26:36 +000025
26#include <SDL.h>
27
bellard67b915a2004-03-31 23:37:16 +000028#ifndef _WIN32
29#include <signal.h>
30#endif
bellard0f0b7262003-08-09 18:26:36 +000031
32static SDL_Surface *screen;
33static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
bellard8a7ddc32004-03-31 19:00:16 +000034static int last_vm_running;
bellard8e9c4af2004-04-28 19:33:40 +000035static int gui_saved_grab;
36static int gui_fullscreen;
37static int gui_key_modifier_pressed;
38static int gui_keysym;
bellard0f0b7262003-08-09 18:26:36 +000039
40static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
41{
bellard898712a2004-02-06 19:56:42 +000042 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
bellard0f0b7262003-08-09 18:26:36 +000043 SDL_UpdateRect(screen, x, y, w, h);
44}
45
46static void sdl_resize(DisplayState *ds, int w, int h)
47{
48 int flags;
49
50 // printf("resizing to %d %d\n", w, h);
51
52 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
53 flags |= SDL_RESIZABLE;
bellard8e9c4af2004-04-28 19:33:40 +000054 if (gui_fullscreen)
55 flags |= SDL_FULLSCREEN;
bellard0f0b7262003-08-09 18:26:36 +000056 screen = SDL_SetVideoMode(w, h, 0, flags);
57 if (!screen) {
58 fprintf(stderr, "Could not open SDL display\n");
59 exit(1);
60 }
61 ds->data = screen->pixels;
62 ds->linesize = screen->pitch;
63 ds->depth = screen->format->BitsPerPixel;
64}
65
66static const uint32_t x_keycode_to_pc_keycode[61] = {
67 0x47e0, /* 97 Home */
68 0x48e0, /* 98 Up */
69 0x49e0, /* 99 PgUp */
70 0x4be0, /* 100 Left */
71 0x4c, /* 101 KP-5 */
72 0x4de0, /* 102 Right */
73 0x4fe0, /* 103 End */
74 0x50e0, /* 104 Down */
75 0x51e0, /* 105 PgDn */
76 0x52e0, /* 106 Ins */
77 0x53e0, /* 107 Del */
78 0x1ce0, /* 108 Enter */
79 0x1de0, /* 109 Ctrl-R */
80 0x451de1, /* 110 Pause */
81 0x37e0, /* 111 Print */
82 0x35e0, /* 112 Divide */
83 0x38e0, /* 113 Alt-R */
84 0x46e0, /* 114 Break */
85 0x0, /* 115 */
86 0x0, /* 116 */
87 0x0, /* 117 */
88 0x0, /* 118 */
89 0x0, /* 119 */
bellardb71e95f2004-05-20 13:08:06 +000090 0x70, /* 120 Hiragana_Katakana */
bellard0f0b7262003-08-09 18:26:36 +000091 0x0, /* 121 */
92 0x0, /* 122 */
bellardb71e95f2004-05-20 13:08:06 +000093 0x73, /* 123 backslash */
bellard0f0b7262003-08-09 18:26:36 +000094 0x0, /* 124 */
95 0x0, /* 125 */
96 0x0, /* 126 */
97 0x0, /* 127 */
98 0x0, /* 128 */
bellardb71e95f2004-05-20 13:08:06 +000099 0x79, /* 129 Henkan */
bellard0f0b7262003-08-09 18:26:36 +0000100 0x0, /* 130 */
bellardb71e95f2004-05-20 13:08:06 +0000101 0x7b, /* 131 Muhenkan */
bellard0f0b7262003-08-09 18:26:36 +0000102 0x0, /* 132 */
bellardb71e95f2004-05-20 13:08:06 +0000103 0x7d, /* 133 Yen */
bellard0f0b7262003-08-09 18:26:36 +0000104 0x0, /* 134 */
105 0x0, /* 135 */
106 0x47, /* 136 KP_7 */
107 0x48, /* 137 KP_8 */
108 0x49, /* 138 KP_9 */
109 0x4b, /* 139 KP_4 */
110 0x4c, /* 140 KP_5 */
111 0x4d, /* 141 KP_6 */
112 0x4f, /* 142 KP_1 */
113 0x50, /* 143 KP_2 */
114 0x51, /* 144 KP_3 */
115 0x52, /* 145 KP_0 */
116 0x53, /* 146 KP_. */
117 0x47, /* 147 KP_HOME */
118 0x48, /* 148 KP_UP */
119 0x49, /* 149 KP_PgUp */
120 0x4b, /* 150 KP_Left */
121 0x4c, /* 151 KP_ */
122 0x4d, /* 152 KP_Right */
123 0x4f, /* 153 KP_End */
124 0x50, /* 154 KP_Down */
125 0x51, /* 155 KP_PgDn */
126 0x52, /* 156 KP_Ins */
127 0x53, /* 157 KP_Del */
128};
129
130static void sdl_process_key(SDL_KeyboardEvent *ev)
131{
132 int keycode, v;
133
134 /* XXX: not portable, but avoids complicated mappings */
135 keycode = ev->keysym.scancode;
bellardb71e95f2004-05-20 13:08:06 +0000136
137#ifndef _WIN32
bellard0f0b7262003-08-09 18:26:36 +0000138 if (keycode < 9) {
139 keycode = 0;
140 } else if (keycode < 97) {
141 keycode -= 8; /* just an offset */
bellardb71e95f2004-05-20 13:08:06 +0000142 } else if (keycode < 158) {
bellard0f0b7262003-08-09 18:26:36 +0000143 /* use conversion table */
144 keycode = x_keycode_to_pc_keycode[keycode - 97];
145 } else {
146 keycode = 0;
147 }
bellardb71e95f2004-05-20 13:08:06 +0000148#endif
bellard0f0b7262003-08-09 18:26:36 +0000149
150 /* now send the key code */
151 while (keycode != 0) {
152 v = keycode & 0xff;
153 if (ev->type == SDL_KEYUP)
154 v |= 0x80;
155 kbd_put_keycode(v);
156 keycode >>= 8;
157 }
158}
159
bellard8a7ddc32004-03-31 19:00:16 +0000160static void sdl_update_caption(void)
161{
162 char buf[1024];
163 strcpy(buf, "QEMU");
164 if (!vm_running) {
165 strcat(buf, " [Stopped]");
166 }
167 if (gui_grab) {
168 strcat(buf, " - Press Ctrl-Shift to exit grab");
169 }
170 SDL_WM_SetCaption(buf, "QEMU");
171}
172
bellard0f0b7262003-08-09 18:26:36 +0000173static void sdl_grab_start(void)
174{
bellard0f0b7262003-08-09 18:26:36 +0000175 SDL_ShowCursor(0);
176 SDL_WM_GrabInput(SDL_GRAB_ON);
177 /* dummy read to avoid moving the mouse */
178 SDL_GetRelativeMouseState(NULL, NULL);
179 gui_grab = 1;
bellard8a7ddc32004-03-31 19:00:16 +0000180 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000181}
182
183static void sdl_grab_end(void)
184{
bellard0f0b7262003-08-09 18:26:36 +0000185 SDL_WM_GrabInput(SDL_GRAB_OFF);
186 SDL_ShowCursor(1);
187 gui_grab = 0;
bellard8a7ddc32004-03-31 19:00:16 +0000188 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000189}
190
191static void sdl_send_mouse_event(void)
192{
193 int dx, dy, dz, state, buttons;
194 state = SDL_GetRelativeMouseState(&dx, &dy);
195 buttons = 0;
196 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
197 buttons |= MOUSE_EVENT_LBUTTON;
198 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
199 buttons |= MOUSE_EVENT_RBUTTON;
200 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
201 buttons |= MOUSE_EVENT_MBUTTON;
202 /* XXX: test wheel */
203 dz = 0;
bellard8351d2d2004-01-04 18:15:01 +0000204#ifdef SDL_BUTTON_WHEELUP
bellard0f0b7262003-08-09 18:26:36 +0000205 if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP))
206 dz--;
207 if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN))
208 dz++;
bellard8351d2d2004-01-04 18:15:01 +0000209#endif
bellard0f0b7262003-08-09 18:26:36 +0000210 kbd_mouse_event(dx, dy, dz, buttons);
211}
212
bellard8e9c4af2004-04-28 19:33:40 +0000213static void toggle_full_screen(DisplayState *ds)
214{
215 gui_fullscreen = !gui_fullscreen;
216 sdl_resize(ds, screen->w, screen->h);
217 if (gui_fullscreen) {
218 gui_saved_grab = gui_grab;
219 sdl_grab_start();
220 } else {
221 if (!gui_saved_grab)
222 sdl_grab_end();
223 }
224 vga_update_display();
225 sdl_update(ds, 0, 0, screen->w, screen->h);
226}
227
bellard0f0b7262003-08-09 18:26:36 +0000228static void sdl_refresh(DisplayState *ds)
229{
230 SDL_Event ev1, *ev = &ev1;
bellard8e9c4af2004-04-28 19:33:40 +0000231 int mod_state;
232
bellard8a7ddc32004-03-31 19:00:16 +0000233 if (last_vm_running != vm_running) {
234 last_vm_running = vm_running;
235 sdl_update_caption();
236 }
237
bellard0f0b7262003-08-09 18:26:36 +0000238 vga_update_display();
239 while (SDL_PollEvent(ev)) {
240 switch (ev->type) {
241 case SDL_VIDEOEXPOSE:
242 sdl_update(ds, 0, 0, screen->w, screen->h);
243 break;
244 case SDL_KEYDOWN:
245 case SDL_KEYUP:
246 if (ev->type == SDL_KEYDOWN) {
bellard8e9c4af2004-04-28 19:33:40 +0000247 mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) ==
248 (KMOD_LSHIFT | KMOD_LCTRL);
249 gui_key_modifier_pressed = mod_state;
250 if (gui_key_modifier_pressed &&
251 ev->key.keysym.sym == SDLK_f) {
252 gui_keysym = ev->key.keysym.sym;
253 }
254 } else if (ev->type == SDL_KEYUP) {
255 mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL));
256 if (!mod_state) {
257 if (gui_key_modifier_pressed) {
258 switch(gui_keysym) {
259 case SDLK_f:
260 toggle_full_screen(ds);
261 break;
262 case 0:
263 /* exit/enter grab if pressing Ctrl-Shift */
264 if (!gui_grab)
265 sdl_grab_start();
266 else
267 sdl_grab_end();
268 break;
269 }
270 gui_key_modifier_pressed = 0;
271 gui_keysym = 0;
272 }
bellard0f0b7262003-08-09 18:26:36 +0000273 }
274 }
275 sdl_process_key(&ev->key);
276 break;
277 case SDL_QUIT:
278 reset_requested = 1;
279 break;
280 case SDL_MOUSEMOTION:
281 if (gui_grab) {
282 sdl_send_mouse_event();
283 }
284 break;
285 case SDL_MOUSEBUTTONDOWN:
286 case SDL_MOUSEBUTTONUP:
287 {
288 SDL_MouseButtonEvent *bev = &ev->button;
289 if (!gui_grab) {
290 if (ev->type == SDL_MOUSEBUTTONDOWN &&
291 (bev->state & SDL_BUTTON_LMASK)) {
292 /* start grabbing all events */
293 sdl_grab_start();
294 }
295 } else {
296 sdl_send_mouse_event();
297 }
298 }
299 break;
bellard0294ffb2004-04-29 22:15:15 +0000300 case SDL_ACTIVEEVENT:
301 if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0) {
302 sdl_grab_end();
303 }
304 break;
bellard0f0b7262003-08-09 18:26:36 +0000305 default:
306 break;
307 }
308 }
309}
310
bellard898712a2004-02-06 19:56:42 +0000311static void sdl_cleanup(void)
312{
313 SDL_Quit();
314}
315
bellard0f0b7262003-08-09 18:26:36 +0000316void sdl_display_init(DisplayState *ds)
317{
318 int flags;
319
320 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
321 if (SDL_Init (flags)) {
322 fprintf(stderr, "Could not initialize SDL - exiting\n");
323 exit(1);
324 }
bellard67b915a2004-03-31 23:37:16 +0000325
326#ifndef _WIN32
bellard0ae04d72003-09-30 21:09:16 +0000327 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
328 signal(SIGINT, SIG_DFL);
329 signal(SIGQUIT, SIG_DFL);
bellard67b915a2004-03-31 23:37:16 +0000330#endif
bellard0ae04d72003-09-30 21:09:16 +0000331
bellard0f0b7262003-08-09 18:26:36 +0000332 ds->dpy_update = sdl_update;
333 ds->dpy_resize = sdl_resize;
334 ds->dpy_refresh = sdl_refresh;
335
336 sdl_resize(ds, 640, 400);
bellard8a7ddc32004-03-31 19:00:16 +0000337 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000338 SDL_EnableKeyRepeat(250, 50);
339 gui_grab = 0;
bellard898712a2004-02-06 19:56:42 +0000340
341 atexit(sdl_cleanup);
bellard0f0b7262003-08-09 18:26:36 +0000342}