SCSI TCQ support.
[qemu/mini2440.git] / sdl.c
blobe65dcac9c5d4ebadb23444198caf6a10dab4bbe7
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:
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
22 * THE SOFTWARE.
24 #include "vl.h"
26 #include <SDL.h>
28 #ifndef _WIN32
29 #include <signal.h>
30 #endif
32 static SDL_Surface *screen;
33 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
34 static int last_vm_running;
35 static int gui_saved_grab;
36 static int gui_fullscreen;
37 static int gui_key_modifier_pressed;
38 static int gui_keysym;
39 static int gui_fullscreen_initial_grab;
40 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
41 static uint8_t modifiers_state[256];
42 static int width, height;
43 static SDL_Cursor *sdl_cursor_normal;
44 static SDL_Cursor *sdl_cursor_hidden;
45 static int absolute_enabled = 0;
47 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
49 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
50 SDL_UpdateRect(screen, x, y, w, h);
53 static void sdl_resize(DisplayState *ds, int w, int h)
55 int flags;
57 // printf("resizing to %d %d\n", w, h);
59 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
60 if (gui_fullscreen)
61 flags |= SDL_FULLSCREEN;
63 width = w;
64 height = h;
66 again:
67 screen = SDL_SetVideoMode(w, h, 0, flags);
68 if (!screen) {
69 fprintf(stderr, "Could not open SDL display\n");
70 exit(1);
72 if (!screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
73 flags &= ~SDL_HWSURFACE;
74 goto again;
77 if (!screen->pixels) {
78 fprintf(stderr, "Could not open SDL display\n");
79 exit(1);
81 ds->data = screen->pixels;
82 ds->linesize = screen->pitch;
83 ds->depth = screen->format->BitsPerPixel;
84 if (ds->depth == 32 && screen->format->Rshift == 0) {
85 ds->bgr = 1;
86 } else {
87 ds->bgr = 0;
89 ds->width = w;
90 ds->height = h;
93 /* generic keyboard conversion */
95 #include "sdl_keysym.h"
96 #include "keymaps.c"
98 static kbd_layout_t *kbd_layout = NULL;
100 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
102 int keysym;
103 /* workaround for X11+SDL bug with AltGR */
104 keysym = ev->keysym.sym;
105 if (keysym == 0 && ev->keysym.scancode == 113)
106 keysym = SDLK_MODE;
107 /* For Japanese key '\' and '|' */
108 if (keysym == 92 && ev->keysym.scancode == 133) {
109 keysym = 0xa5;
111 return keysym2scancode(kbd_layout, keysym);
114 /* specific keyboard conversions from scan codes */
116 #if defined(_WIN32)
118 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
120 return ev->keysym.scancode;
123 #else
125 static const uint8_t x_keycode_to_pc_keycode[115] = {
126 0xc7, /* 97 Home */
127 0xc8, /* 98 Up */
128 0xc9, /* 99 PgUp */
129 0xcb, /* 100 Left */
130 0x4c, /* 101 KP-5 */
131 0xcd, /* 102 Right */
132 0xcf, /* 103 End */
133 0xd0, /* 104 Down */
134 0xd1, /* 105 PgDn */
135 0xd2, /* 106 Ins */
136 0xd3, /* 107 Del */
137 0x9c, /* 108 Enter */
138 0x9d, /* 109 Ctrl-R */
139 0x0, /* 110 Pause */
140 0xb7, /* 111 Print */
141 0xb5, /* 112 Divide */
142 0xb8, /* 113 Alt-R */
143 0xc6, /* 114 Break */
144 0x0, /* 115 */
145 0x0, /* 116 */
146 0x0, /* 117 */
147 0x0, /* 118 */
148 0x0, /* 119 */
149 0x0, /* 120 */
150 0x0, /* 121 */
151 0x0, /* 122 */
152 0x0, /* 123 */
153 0x0, /* 124 */
154 0x0, /* 125 */
155 0x0, /* 126 */
156 0x0, /* 127 */
157 0x0, /* 128 */
158 0x79, /* 129 Henkan */
159 0x0, /* 130 */
160 0x7b, /* 131 Muhenkan */
161 0x0, /* 132 */
162 0x7d, /* 133 Yen */
163 0x0, /* 134 */
164 0x0, /* 135 */
165 0x47, /* 136 KP_7 */
166 0x48, /* 137 KP_8 */
167 0x49, /* 138 KP_9 */
168 0x4b, /* 139 KP_4 */
169 0x4c, /* 140 KP_5 */
170 0x4d, /* 141 KP_6 */
171 0x4f, /* 142 KP_1 */
172 0x50, /* 143 KP_2 */
173 0x51, /* 144 KP_3 */
174 0x52, /* 145 KP_0 */
175 0x53, /* 146 KP_. */
176 0x47, /* 147 KP_HOME */
177 0x48, /* 148 KP_UP */
178 0x49, /* 149 KP_PgUp */
179 0x4b, /* 150 KP_Left */
180 0x4c, /* 151 KP_ */
181 0x4d, /* 152 KP_Right */
182 0x4f, /* 153 KP_End */
183 0x50, /* 154 KP_Down */
184 0x51, /* 155 KP_PgDn */
185 0x52, /* 156 KP_Ins */
186 0x53, /* 157 KP_Del */
187 0x0, /* 158 */
188 0x0, /* 159 */
189 0x0, /* 160 */
190 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* 170 */
191 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* 180 */
192 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* 190 */
193 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* 200 */
194 0x0, /* 201 */
195 0x0, /* 202 */
196 0x0, /* 203 */
197 0x0, /* 204 */
198 0x0, /* 205 */
199 0x0, /* 206 */
200 0x0, /* 207 */
201 0x70, /* 208 Hiragana_Katakana */
202 0x0, /* 209 */
203 0x0, /* 210 */
204 0x73, /* 211 backslash */
207 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
209 int keycode;
211 keycode = ev->keysym.scancode;
213 if (keycode < 9) {
214 keycode = 0;
215 } else if (keycode < 97) {
216 keycode -= 8; /* just an offset */
217 } else if (keycode < 212) {
218 /* use conversion table */
219 keycode = x_keycode_to_pc_keycode[keycode - 97];
220 } else {
221 keycode = 0;
223 return keycode;
226 #endif
228 static void reset_keys(void)
230 int i;
231 for(i = 0; i < 256; i++) {
232 if (modifiers_state[i]) {
233 if (i & 0x80)
234 kbd_put_keycode(0xe0);
235 kbd_put_keycode(i | 0x80);
236 modifiers_state[i] = 0;
241 static void sdl_process_key(SDL_KeyboardEvent *ev)
243 int keycode, v;
245 if (ev->keysym.sym == SDLK_PAUSE) {
246 /* specific case */
247 v = 0;
248 if (ev->type == SDL_KEYUP)
249 v |= 0x80;
250 kbd_put_keycode(0xe1);
251 kbd_put_keycode(0x1d | v);
252 kbd_put_keycode(0x45 | v);
253 return;
256 if (kbd_layout) {
257 keycode = sdl_keyevent_to_keycode_generic(ev);
258 } else {
259 keycode = sdl_keyevent_to_keycode(ev);
262 switch(keycode) {
263 case 0x00:
264 /* sent when leaving window: reset the modifiers state */
265 reset_keys();
266 return;
267 case 0x2a: /* Left Shift */
268 case 0x36: /* Right Shift */
269 case 0x1d: /* Left CTRL */
270 case 0x9d: /* Right CTRL */
271 case 0x38: /* Left ALT */
272 case 0xb8: /* Right ALT */
273 if (ev->type == SDL_KEYUP)
274 modifiers_state[keycode] = 0;
275 else
276 modifiers_state[keycode] = 1;
277 break;
278 case 0x45: /* num lock */
279 case 0x3a: /* caps lock */
280 /* SDL does not send the key up event, so we generate it */
281 kbd_put_keycode(keycode);
282 kbd_put_keycode(keycode | 0x80);
283 return;
286 /* now send the key code */
287 if (keycode & 0x80)
288 kbd_put_keycode(0xe0);
289 if (ev->type == SDL_KEYUP)
290 kbd_put_keycode(keycode | 0x80);
291 else
292 kbd_put_keycode(keycode & 0x7f);
295 static void sdl_update_caption(void)
297 char buf[1024];
298 strcpy(buf, "QEMU");
299 if (!vm_running) {
300 strcat(buf, " [Stopped]");
302 if (gui_grab) {
303 strcat(buf, " - Press Ctrl-Alt to exit grab");
305 SDL_WM_SetCaption(buf, "QEMU");
308 static void sdl_hide_cursor(void)
310 if (kbd_mouse_is_absolute()) {
311 SDL_ShowCursor(1);
312 SDL_SetCursor(sdl_cursor_hidden);
313 } else {
314 SDL_ShowCursor(0);
318 static void sdl_show_cursor(void)
320 if (!kbd_mouse_is_absolute()) {
321 SDL_ShowCursor(1);
325 static void sdl_grab_start(void)
327 sdl_hide_cursor();
328 SDL_WM_GrabInput(SDL_GRAB_ON);
329 /* dummy read to avoid moving the mouse */
330 SDL_GetRelativeMouseState(NULL, NULL);
331 gui_grab = 1;
332 sdl_update_caption();
335 static void sdl_grab_end(void)
337 SDL_WM_GrabInput(SDL_GRAB_OFF);
338 sdl_show_cursor();
339 gui_grab = 0;
340 sdl_update_caption();
343 static void sdl_send_mouse_event(int dz)
345 int dx, dy, state, buttons;
346 state = SDL_GetRelativeMouseState(&dx, &dy);
347 buttons = 0;
348 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
349 buttons |= MOUSE_EVENT_LBUTTON;
350 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
351 buttons |= MOUSE_EVENT_RBUTTON;
352 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
353 buttons |= MOUSE_EVENT_MBUTTON;
355 if (kbd_mouse_is_absolute()) {
356 if (!absolute_enabled) {
357 sdl_hide_cursor();
358 if (gui_grab) {
359 sdl_grab_end();
361 absolute_enabled = 1;
364 SDL_GetMouseState(&dx, &dy);
365 dx = dx * 0x7FFF / width;
366 dy = dy * 0x7FFF / height;
369 kbd_mouse_event(dx, dy, dz, buttons);
372 static void toggle_full_screen(DisplayState *ds)
374 gui_fullscreen = !gui_fullscreen;
375 sdl_resize(ds, screen->w, screen->h);
376 if (gui_fullscreen) {
377 gui_saved_grab = gui_grab;
378 sdl_grab_start();
379 } else {
380 if (!gui_saved_grab)
381 sdl_grab_end();
383 vga_hw_invalidate();
384 vga_hw_update();
387 static void sdl_refresh(DisplayState *ds)
389 SDL_Event ev1, *ev = &ev1;
390 int mod_state;
392 if (last_vm_running != vm_running) {
393 last_vm_running = vm_running;
394 sdl_update_caption();
397 vga_hw_update();
399 while (SDL_PollEvent(ev)) {
400 switch (ev->type) {
401 case SDL_VIDEOEXPOSE:
402 sdl_update(ds, 0, 0, screen->w, screen->h);
403 break;
404 case SDL_KEYDOWN:
405 case SDL_KEYUP:
406 if (ev->type == SDL_KEYDOWN) {
407 mod_state = (SDL_GetModState() & gui_grab_code) ==
408 gui_grab_code;
409 gui_key_modifier_pressed = mod_state;
410 if (gui_key_modifier_pressed) {
411 int keycode;
412 keycode = sdl_keyevent_to_keycode(&ev->key);
413 switch(keycode) {
414 case 0x21: /* 'f' key on US keyboard */
415 toggle_full_screen(ds);
416 gui_keysym = 1;
417 break;
418 case 0x02 ... 0x0a: /* '1' to '9' keys */
419 /* Reset the modifiers sent to the current console */
420 reset_keys();
421 console_select(keycode - 0x02);
422 if (!is_graphic_console()) {
423 /* display grab if going to a text console */
424 if (gui_grab)
425 sdl_grab_end();
427 gui_keysym = 1;
428 break;
429 default:
430 break;
432 } else if (!is_graphic_console()) {
433 int keysym;
434 keysym = 0;
435 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
436 switch(ev->key.keysym.sym) {
437 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
438 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
439 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
440 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
441 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
442 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
443 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
444 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
445 default: break;
447 } else {
448 switch(ev->key.keysym.sym) {
449 case SDLK_UP: keysym = QEMU_KEY_UP; break;
450 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
451 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
452 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
453 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
454 case SDLK_END: keysym = QEMU_KEY_END; break;
455 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
456 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
457 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break; case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
458 default: break;
461 if (keysym) {
462 kbd_put_keysym(keysym);
463 } else if (ev->key.keysym.unicode != 0) {
464 kbd_put_keysym(ev->key.keysym.unicode);
467 } else if (ev->type == SDL_KEYUP) {
468 mod_state = (ev->key.keysym.mod & gui_grab_code);
469 if (!mod_state) {
470 if (gui_key_modifier_pressed) {
471 gui_key_modifier_pressed = 0;
472 if (gui_keysym == 0) {
473 /* exit/enter grab if pressing Ctrl-Alt */
474 if (!gui_grab) {
475 /* if the application is not active,
476 do not try to enter grab state. It
477 prevents
478 'SDL_WM_GrabInput(SDL_GRAB_ON)'
479 from blocking all the application
480 (SDL bug). */
481 if (SDL_GetAppState() & SDL_APPACTIVE)
482 sdl_grab_start();
483 } else {
484 sdl_grab_end();
486 /* SDL does not send back all the
487 modifiers key, so we must correct it */
488 reset_keys();
489 break;
491 gui_keysym = 0;
495 if (is_graphic_console() && !gui_keysym)
496 sdl_process_key(&ev->key);
497 break;
498 case SDL_QUIT:
499 qemu_system_shutdown_request();
500 break;
501 case SDL_MOUSEMOTION:
502 if (gui_grab || kbd_mouse_is_absolute()) {
503 sdl_send_mouse_event(0);
505 break;
506 case SDL_MOUSEBUTTONDOWN:
507 case SDL_MOUSEBUTTONUP:
509 SDL_MouseButtonEvent *bev = &ev->button;
510 if (!gui_grab && !kbd_mouse_is_absolute()) {
511 if (ev->type == SDL_MOUSEBUTTONDOWN &&
512 (bev->state & SDL_BUTTON_LMASK)) {
513 /* start grabbing all events */
514 sdl_grab_start();
516 } else {
517 int dz;
518 dz = 0;
519 #ifdef SDL_BUTTON_WHEELUP
520 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
521 dz = -1;
522 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
523 dz = 1;
525 #endif
526 sdl_send_mouse_event(dz);
529 break;
530 case SDL_ACTIVEEVENT:
531 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
532 !ev->active.gain && !gui_fullscreen_initial_grab) {
533 sdl_grab_end();
535 break;
536 default:
537 break;
542 static void sdl_cleanup(void)
544 SDL_Quit();
547 void sdl_display_init(DisplayState *ds, int full_screen)
549 int flags;
550 uint8_t data = 0;
552 #if defined(__APPLE__)
553 /* always use generic keymaps */
554 if (!keyboard_layout)
555 keyboard_layout = "en-us";
556 #endif
557 if(keyboard_layout) {
558 kbd_layout = init_keyboard_layout(keyboard_layout);
559 if (!kbd_layout)
560 exit(1);
563 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
564 if (SDL_Init (flags)) {
565 fprintf(stderr, "Could not initialize SDL - exiting\n");
566 exit(1);
568 #ifndef _WIN32
569 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
570 signal(SIGINT, SIG_DFL);
571 signal(SIGQUIT, SIG_DFL);
572 #endif
574 ds->dpy_update = sdl_update;
575 ds->dpy_resize = sdl_resize;
576 ds->dpy_refresh = sdl_refresh;
578 sdl_resize(ds, 640, 400);
579 sdl_update_caption();
580 SDL_EnableKeyRepeat(250, 50);
581 SDL_EnableUNICODE(1);
582 gui_grab = 0;
584 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
585 sdl_cursor_normal = SDL_GetCursor();
587 atexit(sdl_cleanup);
588 if (full_screen) {
589 gui_fullscreen = 1;
590 gui_fullscreen_initial_grab = 1;
591 sdl_grab_start();