]> git.proxmox.com Git - mirror_qemu.git/blame - sdl.c
ARM reabbot support (orginal patch by Aurelien Jarno).
[mirror_qemu.git] / sdl.c
CommitLineData
0f0b7264
FB
1/*
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 */
67b915a5 24#include "vl.h"
0f0b7264
FB
25
26#include <SDL.h>
27
67b915a5
FB
28#ifndef _WIN32
29#include <signal.h>
30#endif
0f0b7264
FB
31
32static SDL_Surface *screen;
33static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
8a7ddc38 34static int last_vm_running;
8e9c4afe
FB
35static int gui_saved_grab;
36static int gui_fullscreen;
43523e93 37static int gui_noframe;
8e9c4afe
FB
38static int gui_key_modifier_pressed;
39static int gui_keysym;
d63d307f 40static int gui_fullscreen_initial_grab;
32ff25bf
FB
41static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
42static uint8_t modifiers_state[256];
09b26c5e
FB
43static int width, height;
44static SDL_Cursor *sdl_cursor_normal;
45static SDL_Cursor *sdl_cursor_hidden;
46static int absolute_enabled = 0;
0f0b7264
FB
47
48static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
49{
898712a8 50 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
0f0b7264
FB
51 SDL_UpdateRect(screen, x, y, w, h);
52}
53
54static void sdl_resize(DisplayState *ds, int w, int h)
55{
56 int flags;
57
58 // printf("resizing to %d %d\n", w, h);
59
60 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
8e9c4afe
FB
61 if (gui_fullscreen)
62 flags |= SDL_FULLSCREEN;
43523e93
TS
63 if (gui_noframe)
64 flags |= SDL_NOFRAME;
9903da21 65
09b26c5e
FB
66 width = w;
67 height = h;
68
9903da21 69 again:
0f0b7264
FB
70 screen = SDL_SetVideoMode(w, h, 0, flags);
71 if (!screen) {
72 fprintf(stderr, "Could not open SDL display\n");
73 exit(1);
74 }
9903da21
FB
75 if (!screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
76 flags &= ~SDL_HWSURFACE;
77 goto again;
78 }
79
80 if (!screen->pixels) {
81 fprintf(stderr, "Could not open SDL display\n");
82 exit(1);
83 }
0f0b7264
FB
84 ds->data = screen->pixels;
85 ds->linesize = screen->pitch;
86 ds->depth = screen->format->BitsPerPixel;
d3079cd2
FB
87 if (ds->depth == 32 && screen->format->Rshift == 0) {
88 ds->bgr = 1;
89 } else {
90 ds->bgr = 0;
91 }
457831f4
FB
92 ds->width = w;
93 ds->height = h;
0f0b7264
FB
94}
95
3d11d0eb 96/* generic keyboard conversion */
e58d12ed 97
3d11d0eb
FB
98#include "sdl_keysym.h"
99#include "keymaps.c"
100
101static kbd_layout_t *kbd_layout = NULL;
102
103static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
e58d12ed 104{
3d11d0eb
FB
105 int keysym;
106 /* workaround for X11+SDL bug with AltGR */
107 keysym = ev->keysym.sym;
108 if (keysym == 0 && ev->keysym.scancode == 113)
109 keysym = SDLK_MODE;
60659e3b
FB
110 /* For Japanese key '\' and '|' */
111 if (keysym == 92 && ev->keysym.scancode == 133) {
112 keysym = 0xa5;
113 }
3d11d0eb 114 return keysym2scancode(kbd_layout, keysym);
e58d12ed
FB
115}
116
3d11d0eb
FB
117/* specific keyboard conversions from scan codes */
118
119#if defined(_WIN32)
e58d12ed
FB
120
121static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
122{
123 return ev->keysym.scancode;
124}
125
126#else
127
e58d12ed
FB
128static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
129{
130 int keycode;
131
132 keycode = ev->keysym.scancode;
133
134 if (keycode < 9) {
135 keycode = 0;
136 } else if (keycode < 97) {
137 keycode -= 8; /* just an offset */
60659e3b 138 } else if (keycode < 212) {
e58d12ed 139 /* use conversion table */
6070dd07 140 keycode = _translate_keycode(keycode - 97);
e58d12ed
FB
141 } else {
142 keycode = 0;
143 }
144 return keycode;
145}
146
147#endif
148
32ff25bf
FB
149static void reset_keys(void)
150{
151 int i;
152 for(i = 0; i < 256; i++) {
153 if (modifiers_state[i]) {
154 if (i & 0x80)
155 kbd_put_keycode(0xe0);
156 kbd_put_keycode(i | 0x80);
157 modifiers_state[i] = 0;
158 }
159 }
160}
161
0f0b7264
FB
162static void sdl_process_key(SDL_KeyboardEvent *ev)
163{
32ff25bf 164 int keycode, v;
de2200d3
FB
165
166 if (ev->keysym.sym == SDLK_PAUSE) {
167 /* specific case */
168 v = 0;
169 if (ev->type == SDL_KEYUP)
170 v |= 0x80;
171 kbd_put_keycode(0xe1);
172 kbd_put_keycode(0x1d | v);
173 kbd_put_keycode(0x45 | v);
174 return;
175 }
176
3d11d0eb
FB
177 if (kbd_layout) {
178 keycode = sdl_keyevent_to_keycode_generic(ev);
179 } else {
180 keycode = sdl_keyevent_to_keycode(ev);
181 }
de2200d3
FB
182
183 switch(keycode) {
184 case 0x00:
185 /* sent when leaving window: reset the modifiers state */
32ff25bf 186 reset_keys();
de2200d3
FB
187 return;
188 case 0x2a: /* Left Shift */
189 case 0x36: /* Right Shift */
190 case 0x1d: /* Left CTRL */
191 case 0x9d: /* Right CTRL */
192 case 0x38: /* Left ALT */
193 case 0xb8: /* Right ALT */
0f0b7264 194 if (ev->type == SDL_KEYUP)
de2200d3
FB
195 modifiers_state[keycode] = 0;
196 else
197 modifiers_state[keycode] = 1;
198 break;
199 case 0x45: /* num lock */
200 case 0x3a: /* caps lock */
201 /* SDL does not send the key up event, so we generate it */
202 kbd_put_keycode(keycode);
203 kbd_put_keycode(keycode | 0x80);
204 return;
0f0b7264 205 }
de2200d3
FB
206
207 /* now send the key code */
208 if (keycode & 0x80)
209 kbd_put_keycode(0xe0);
210 if (ev->type == SDL_KEYUP)
211 kbd_put_keycode(keycode | 0x80);
212 else
213 kbd_put_keycode(keycode & 0x7f);
0f0b7264
FB
214}
215
8a7ddc38
FB
216static void sdl_update_caption(void)
217{
218 char buf[1024];
219 strcpy(buf, "QEMU");
220 if (!vm_running) {
221 strcat(buf, " [Stopped]");
222 }
223 if (gui_grab) {
32ff25bf 224 strcat(buf, " - Press Ctrl-Alt to exit grab");
8a7ddc38
FB
225 }
226 SDL_WM_SetCaption(buf, "QEMU");
227}
228
09b26c5e
FB
229static void sdl_hide_cursor(void)
230{
8785a8dd
FB
231 if (kbd_mouse_is_absolute()) {
232 SDL_ShowCursor(1);
233 SDL_SetCursor(sdl_cursor_hidden);
234 } else {
235 SDL_ShowCursor(0);
236 }
09b26c5e
FB
237}
238
239static void sdl_show_cursor(void)
240{
241 if (!kbd_mouse_is_absolute()) {
8785a8dd 242 SDL_ShowCursor(1);
455204eb 243 SDL_SetCursor(sdl_cursor_normal);
09b26c5e
FB
244 }
245}
246
0f0b7264
FB
247static void sdl_grab_start(void)
248{
09b26c5e 249 sdl_hide_cursor();
0f0b7264
FB
250 SDL_WM_GrabInput(SDL_GRAB_ON);
251 /* dummy read to avoid moving the mouse */
252 SDL_GetRelativeMouseState(NULL, NULL);
253 gui_grab = 1;
8a7ddc38 254 sdl_update_caption();
0f0b7264
FB
255}
256
257static void sdl_grab_end(void)
258{
0f0b7264 259 SDL_WM_GrabInput(SDL_GRAB_OFF);
09b26c5e 260 sdl_show_cursor();
0f0b7264 261 gui_grab = 0;
8a7ddc38 262 sdl_update_caption();
0f0b7264
FB
263}
264
18a6d284 265static void sdl_send_mouse_event(int dz)
0f0b7264 266{
18a6d284 267 int dx, dy, state, buttons;
0f0b7264
FB
268 state = SDL_GetRelativeMouseState(&dx, &dy);
269 buttons = 0;
270 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
271 buttons |= MOUSE_EVENT_LBUTTON;
272 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
273 buttons |= MOUSE_EVENT_RBUTTON;
274 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
275 buttons |= MOUSE_EVENT_MBUTTON;
09b26c5e
FB
276
277 if (kbd_mouse_is_absolute()) {
278 if (!absolute_enabled) {
279 sdl_hide_cursor();
280 if (gui_grab) {
281 sdl_grab_end();
282 }
283 absolute_enabled = 1;
284 }
285
286 SDL_GetMouseState(&dx, &dy);
287 dx = dx * 0x7FFF / width;
288 dy = dy * 0x7FFF / height;
455204eb
TS
289 } else if (absolute_enabled) {
290 sdl_show_cursor();
291 absolute_enabled = 0;
09b26c5e
FB
292 }
293
0f0b7264
FB
294 kbd_mouse_event(dx, dy, dz, buttons);
295}
296
8e9c4afe
FB
297static void toggle_full_screen(DisplayState *ds)
298{
299 gui_fullscreen = !gui_fullscreen;
300 sdl_resize(ds, screen->w, screen->h);
301 if (gui_fullscreen) {
302 gui_saved_grab = gui_grab;
303 sdl_grab_start();
304 } else {
305 if (!gui_saved_grab)
306 sdl_grab_end();
307 }
95219897
PB
308 vga_hw_invalidate();
309 vga_hw_update();
8e9c4afe
FB
310}
311
0f0b7264
FB
312static void sdl_refresh(DisplayState *ds)
313{
314 SDL_Event ev1, *ev = &ev1;
8e9c4afe
FB
315 int mod_state;
316
8a7ddc38
FB
317 if (last_vm_running != vm_running) {
318 last_vm_running = vm_running;
319 sdl_update_caption();
320 }
321
95219897 322 vga_hw_update();
457831f4 323
0f0b7264
FB
324 while (SDL_PollEvent(ev)) {
325 switch (ev->type) {
326 case SDL_VIDEOEXPOSE:
327 sdl_update(ds, 0, 0, screen->w, screen->h);
328 break;
329 case SDL_KEYDOWN:
330 case SDL_KEYUP:
331 if (ev->type == SDL_KEYDOWN) {
32ff25bf
FB
332 mod_state = (SDL_GetModState() & gui_grab_code) ==
333 gui_grab_code;
8e9c4afe 334 gui_key_modifier_pressed = mod_state;
457831f4 335 if (gui_key_modifier_pressed) {
32ff25bf
FB
336 int keycode;
337 keycode = sdl_keyevent_to_keycode(&ev->key);
338 switch(keycode) {
339 case 0x21: /* 'f' key on US keyboard */
457831f4
FB
340 toggle_full_screen(ds);
341 gui_keysym = 1;
342 break;
32ff25bf 343 case 0x02 ... 0x0a: /* '1' to '9' keys */
dfd92d3a
FB
344 /* Reset the modifiers sent to the current console */
345 reset_keys();
32ff25bf 346 console_select(keycode - 0x02);
95219897 347 if (!is_graphic_console()) {
457831f4
FB
348 /* display grab if going to a text console */
349 if (gui_grab)
350 sdl_grab_end();
351 }
352 gui_keysym = 1;
353 break;
354 default:
355 break;
356 }
95219897 357 } else if (!is_graphic_console()) {
457831f4
FB
358 int keysym;
359 keysym = 0;
360 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
361 switch(ev->key.keysym.sym) {
362 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
363 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
364 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
365 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
366 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
367 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
368 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
369 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
370 default: break;
371 }
372 } else {
373 switch(ev->key.keysym.sym) {
374 case SDLK_UP: keysym = QEMU_KEY_UP; break;
375 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
376 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
377 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
378 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
379 case SDLK_END: keysym = QEMU_KEY_END; break;
380 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
381 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
382 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break; case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
383 default: break;
384 }
385 }
386 if (keysym) {
387 kbd_put_keysym(keysym);
388 } else if (ev->key.keysym.unicode != 0) {
389 kbd_put_keysym(ev->key.keysym.unicode);
390 }
8e9c4afe
FB
391 }
392 } else if (ev->type == SDL_KEYUP) {
bf2b84e4 393 mod_state = (ev->key.keysym.mod & gui_grab_code);
8e9c4afe
FB
394 if (!mod_state) {
395 if (gui_key_modifier_pressed) {
5b311878 396 gui_key_modifier_pressed = 0;
457831f4 397 if (gui_keysym == 0) {
32ff25bf 398 /* exit/enter grab if pressing Ctrl-Alt */
c66b0d4c
FB
399 if (!gui_grab) {
400 /* if the application is not active,
401 do not try to enter grab state. It
402 prevents
403 'SDL_WM_GrabInput(SDL_GRAB_ON)'
404 from blocking all the application
405 (SDL bug). */
406 if (SDL_GetAppState() & SDL_APPACTIVE)
407 sdl_grab_start();
408 } else {
8e9c4afe 409 sdl_grab_end();
c66b0d4c 410 }
32ff25bf
FB
411 /* SDL does not send back all the
412 modifiers key, so we must correct it */
413 reset_keys();
8e9c4afe
FB
414 break;
415 }
8e9c4afe
FB
416 gui_keysym = 0;
417 }
0f0b7264
FB
418 }
419 }
dfd92d3a 420 if (is_graphic_console() && !gui_keysym)
457831f4 421 sdl_process_key(&ev->key);
0f0b7264
FB
422 break;
423 case SDL_QUIT:
667accab
TS
424 if (!no_quit) {
425 qemu_system_shutdown_request();
426 }
0f0b7264
FB
427 break;
428 case SDL_MOUSEMOTION:
455204eb
TS
429 if (gui_grab || kbd_mouse_is_absolute() ||
430 absolute_enabled) {
18a6d284 431 sdl_send_mouse_event(0);
0f0b7264
FB
432 }
433 break;
434 case SDL_MOUSEBUTTONDOWN:
435 case SDL_MOUSEBUTTONUP:
436 {
437 SDL_MouseButtonEvent *bev = &ev->button;
09b26c5e 438 if (!gui_grab && !kbd_mouse_is_absolute()) {
0f0b7264
FB
439 if (ev->type == SDL_MOUSEBUTTONDOWN &&
440 (bev->state & SDL_BUTTON_LMASK)) {
441 /* start grabbing all events */
442 sdl_grab_start();
443 }
444 } else {
18a6d284
FB
445 int dz;
446 dz = 0;
447#ifdef SDL_BUTTON_WHEELUP
09b26c5e 448 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
18a6d284 449 dz = -1;
09b26c5e 450 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
18a6d284
FB
451 dz = 1;
452 }
453#endif
454 sdl_send_mouse_event(dz);
0f0b7264
FB
455 }
456 }
457 break;
0294ffb9 458 case SDL_ACTIVEEVENT:
5b311878
PB
459 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
460 !ev->active.gain && !gui_fullscreen_initial_grab) {
0294ffb9
FB
461 sdl_grab_end();
462 }
463 break;
0f0b7264
FB
464 default:
465 break;
466 }
467 }
468}
469
898712a8
FB
470static void sdl_cleanup(void)
471{
472 SDL_Quit();
473}
474
43523e93 475void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
0f0b7264
FB
476{
477 int flags;
09b26c5e 478 uint8_t data = 0;
0f0b7264 479
3d11d0eb
FB
480#if defined(__APPLE__)
481 /* always use generic keymaps */
482 if (!keyboard_layout)
483 keyboard_layout = "en-us";
484#endif
485 if(keyboard_layout) {
486 kbd_layout = init_keyboard_layout(keyboard_layout);
487 if (!kbd_layout)
488 exit(1);
489 }
490
43523e93
TS
491 if (no_frame)
492 gui_noframe = 1;
493
0f0b7264
FB
494 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
495 if (SDL_Init (flags)) {
496 fprintf(stderr, "Could not initialize SDL - exiting\n");
497 exit(1);
498 }
67b915a5 499#ifndef _WIN32
0ae04d73
FB
500 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
501 signal(SIGINT, SIG_DFL);
502 signal(SIGQUIT, SIG_DFL);
67b915a5 503#endif
0ae04d73 504
0f0b7264
FB
505 ds->dpy_update = sdl_update;
506 ds->dpy_resize = sdl_resize;
507 ds->dpy_refresh = sdl_refresh;
508
509 sdl_resize(ds, 640, 400);
8a7ddc38 510 sdl_update_caption();
0f0b7264 511 SDL_EnableKeyRepeat(250, 50);
457831f4 512 SDL_EnableUNICODE(1);
0f0b7264 513 gui_grab = 0;
898712a8 514
09b26c5e
FB
515 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
516 sdl_cursor_normal = SDL_GetCursor();
517
898712a8 518 atexit(sdl_cleanup);
d63d307f
FB
519 if (full_screen) {
520 gui_fullscreen = 1;
521 gui_fullscreen_initial_grab = 1;
522 sdl_grab_start();
523 }
0f0b7264 524}