fix signed/unsigned overflows in SCSI disk (Rik van Riel)
[qemu.git] / sdl.c
blob17869926d2379ae357308a9a4fecefee979fa614
1 /*
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
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #include "console.h"
26 #include "sysemu.h"
28 #include <SDL.h>
30 #ifndef _WIN32
31 #include <signal.h>
32 #endif
34 static DisplayChangeListener *dcl;
35 static SDL_Surface *real_screen;
36 static SDL_Surface *guest_screen = NULL;
37 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
38 static int last_vm_running;
39 static int gui_saved_grab;
40 static int gui_fullscreen;
41 static int gui_noframe;
42 static int gui_key_modifier_pressed;
43 static int gui_keysym;
44 static int gui_fullscreen_initial_grab;
45 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
46 static uint8_t modifiers_state[256];
47 static int width, height;
48 static SDL_Cursor *sdl_cursor_normal;
49 static SDL_Cursor *sdl_cursor_hidden;
50 static int absolute_enabled = 0;
51 static int guest_cursor = 0;
52 static int guest_x, guest_y;
53 static SDL_Cursor *guest_sprite = 0;
55 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
57 SDL_Rect rec;
58 rec.x = x;
59 rec.y = y;
60 rec.w = w;
61 rec.h = h;
62 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
64 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
65 SDL_UpdateRect(real_screen, x, y, w, h);
68 static void sdl_setdata(DisplayState *ds)
70 SDL_Rect rec;
71 rec.x = 0;
72 rec.y = 0;
73 rec.w = real_screen->w;
74 rec.h = real_screen->h;
76 if (guest_screen != NULL) SDL_FreeSurface(guest_screen);
78 guest_screen = SDL_CreateRGBSurfaceFrom(ds_get_data(ds), ds_get_width(ds), ds_get_height(ds),
79 ds_get_bits_per_pixel(ds), ds_get_linesize(ds),
80 ds->surface->pf.rmask, ds->surface->pf.gmask,
81 ds->surface->pf.bmask, ds->surface->pf.amask);
84 static void sdl_resize(DisplayState *ds)
86 int flags;
88 // printf("resizing to %d %d\n", w, h);
90 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
91 if (gui_fullscreen)
92 flags |= SDL_FULLSCREEN;
93 if (gui_noframe)
94 flags |= SDL_NOFRAME;
96 real_screen = SDL_SetVideoMode(ds_get_width(ds), ds_get_height(ds), 0, flags);
97 if (!real_screen) {
98 fprintf(stderr, "Could not open SDL display\n");
99 exit(1);
102 sdl_setdata(ds);
105 /* generic keyboard conversion */
107 #include "sdl_keysym.h"
108 #include "keymaps.c"
110 static kbd_layout_t *kbd_layout = NULL;
112 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
114 int keysym;
115 /* workaround for X11+SDL bug with AltGR */
116 keysym = ev->keysym.sym;
117 if (keysym == 0 && ev->keysym.scancode == 113)
118 keysym = SDLK_MODE;
119 /* For Japanese key '\' and '|' */
120 if (keysym == 92 && ev->keysym.scancode == 133) {
121 keysym = 0xa5;
123 return keysym2scancode(kbd_layout, keysym);
126 /* specific keyboard conversions from scan codes */
128 #if defined(_WIN32)
130 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
132 return ev->keysym.scancode;
135 #else
137 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
139 int keycode;
141 keycode = ev->keysym.scancode;
143 if (keycode < 9) {
144 keycode = 0;
145 } else if (keycode < 97) {
146 keycode -= 8; /* just an offset */
147 } else if (keycode < 212) {
148 /* use conversion table */
149 keycode = _translate_keycode(keycode - 97);
150 } else {
151 keycode = 0;
153 return keycode;
156 #endif
158 static void reset_keys(void)
160 int i;
161 for(i = 0; i < 256; i++) {
162 if (modifiers_state[i]) {
163 if (i & 0x80)
164 kbd_put_keycode(0xe0);
165 kbd_put_keycode(i | 0x80);
166 modifiers_state[i] = 0;
171 static void sdl_process_key(SDL_KeyboardEvent *ev)
173 int keycode, v;
175 if (ev->keysym.sym == SDLK_PAUSE) {
176 /* specific case */
177 v = 0;
178 if (ev->type == SDL_KEYUP)
179 v |= 0x80;
180 kbd_put_keycode(0xe1);
181 kbd_put_keycode(0x1d | v);
182 kbd_put_keycode(0x45 | v);
183 return;
186 if (kbd_layout) {
187 keycode = sdl_keyevent_to_keycode_generic(ev);
188 } else {
189 keycode = sdl_keyevent_to_keycode(ev);
192 switch(keycode) {
193 case 0x00:
194 /* sent when leaving window: reset the modifiers state */
195 reset_keys();
196 return;
197 case 0x2a: /* Left Shift */
198 case 0x36: /* Right Shift */
199 case 0x1d: /* Left CTRL */
200 case 0x9d: /* Right CTRL */
201 case 0x38: /* Left ALT */
202 case 0xb8: /* Right ALT */
203 if (ev->type == SDL_KEYUP)
204 modifiers_state[keycode] = 0;
205 else
206 modifiers_state[keycode] = 1;
207 break;
208 case 0x45: /* num lock */
209 case 0x3a: /* caps lock */
210 /* SDL does not send the key up event, so we generate it */
211 kbd_put_keycode(keycode);
212 kbd_put_keycode(keycode | 0x80);
213 return;
216 /* now send the key code */
217 if (keycode & 0x80)
218 kbd_put_keycode(0xe0);
219 if (ev->type == SDL_KEYUP)
220 kbd_put_keycode(keycode | 0x80);
221 else
222 kbd_put_keycode(keycode & 0x7f);
225 static void sdl_update_caption(void)
227 char buf[1024];
228 const char *status = "";
230 if (!vm_running)
231 status = " [Stopped]";
232 else if (gui_grab) {
233 if (!alt_grab)
234 status = " - Press Ctrl-Alt to exit grab";
235 else
236 status = " - Press Ctrl-Alt-Shift to exit grab";
239 if (qemu_name)
240 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
241 else
242 snprintf(buf, sizeof(buf), "QEMU%s", status);
244 SDL_WM_SetCaption(buf, "QEMU");
247 static void sdl_hide_cursor(void)
249 if (!cursor_hide)
250 return;
252 if (kbd_mouse_is_absolute()) {
253 SDL_ShowCursor(1);
254 SDL_SetCursor(sdl_cursor_hidden);
255 } else {
256 SDL_ShowCursor(0);
260 static void sdl_show_cursor(void)
262 if (!cursor_hide)
263 return;
265 if (!kbd_mouse_is_absolute()) {
266 SDL_ShowCursor(1);
267 if (guest_cursor &&
268 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
269 SDL_SetCursor(guest_sprite);
270 else
271 SDL_SetCursor(sdl_cursor_normal);
275 static void sdl_grab_start(void)
277 if (guest_cursor) {
278 SDL_SetCursor(guest_sprite);
279 SDL_WarpMouse(guest_x, guest_y);
280 } else
281 sdl_hide_cursor();
283 if (SDL_WM_GrabInput(SDL_GRAB_ON) == SDL_GRAB_ON) {
284 gui_grab = 1;
285 sdl_update_caption();
286 } else
287 sdl_show_cursor();
290 static void sdl_grab_end(void)
292 SDL_WM_GrabInput(SDL_GRAB_OFF);
293 gui_grab = 0;
294 sdl_show_cursor();
295 sdl_update_caption();
298 static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
300 int buttons;
301 buttons = 0;
302 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
303 buttons |= MOUSE_EVENT_LBUTTON;
304 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
305 buttons |= MOUSE_EVENT_RBUTTON;
306 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
307 buttons |= MOUSE_EVENT_MBUTTON;
309 if (kbd_mouse_is_absolute()) {
310 if (!absolute_enabled) {
311 sdl_hide_cursor();
312 if (gui_grab) {
313 sdl_grab_end();
315 absolute_enabled = 1;
318 dx = x * 0x7FFF / (width - 1);
319 dy = y * 0x7FFF / (height - 1);
320 } else if (absolute_enabled) {
321 sdl_show_cursor();
322 absolute_enabled = 0;
323 } else if (guest_cursor) {
324 x -= guest_x;
325 y -= guest_y;
326 guest_x += x;
327 guest_y += y;
328 dx = x;
329 dy = y;
332 kbd_mouse_event(dx, dy, dz, buttons);
335 static void toggle_full_screen(DisplayState *ds)
337 gui_fullscreen = !gui_fullscreen;
338 sdl_resize(ds);
339 if (gui_fullscreen) {
340 gui_saved_grab = gui_grab;
341 sdl_grab_start();
342 } else {
343 if (!gui_saved_grab)
344 sdl_grab_end();
346 vga_hw_invalidate();
347 vga_hw_update();
350 static void sdl_refresh(DisplayState *ds)
352 SDL_Event ev1, *ev = &ev1;
353 int mod_state;
354 int buttonstate = SDL_GetMouseState(NULL, NULL);
356 if (last_vm_running != vm_running) {
357 last_vm_running = vm_running;
358 sdl_update_caption();
361 vga_hw_update();
362 SDL_EnableUNICODE(!is_graphic_console());
364 while (SDL_PollEvent(ev)) {
365 switch (ev->type) {
366 case SDL_VIDEOEXPOSE:
367 sdl_update(ds, 0, 0, real_screen->w, real_screen->h);
368 break;
369 case SDL_KEYDOWN:
370 case SDL_KEYUP:
371 if (ev->type == SDL_KEYDOWN) {
372 if (!alt_grab) {
373 mod_state = (SDL_GetModState() & gui_grab_code) ==
374 gui_grab_code;
375 } else {
376 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
377 (gui_grab_code | KMOD_LSHIFT);
379 gui_key_modifier_pressed = mod_state;
380 if (gui_key_modifier_pressed) {
381 int keycode;
382 keycode = sdl_keyevent_to_keycode(&ev->key);
383 switch(keycode) {
384 case 0x21: /* 'f' key on US keyboard */
385 toggle_full_screen(ds);
386 gui_keysym = 1;
387 break;
388 case 0x02 ... 0x0a: /* '1' to '9' keys */
389 /* Reset the modifiers sent to the current console */
390 reset_keys();
391 console_select(keycode - 0x02);
392 if (!is_graphic_console()) {
393 /* display grab if going to a text console */
394 if (gui_grab)
395 sdl_grab_end();
397 gui_keysym = 1;
398 break;
399 default:
400 break;
402 } else if (!is_graphic_console()) {
403 int keysym;
404 keysym = 0;
405 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
406 switch(ev->key.keysym.sym) {
407 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
408 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
409 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
410 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
411 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
412 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
413 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
414 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
415 default: break;
417 } else {
418 switch(ev->key.keysym.sym) {
419 case SDLK_UP: keysym = QEMU_KEY_UP; break;
420 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
421 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
422 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
423 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
424 case SDLK_END: keysym = QEMU_KEY_END; break;
425 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
426 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
427 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
428 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
429 default: break;
432 if (keysym) {
433 kbd_put_keysym(keysym);
434 } else if (ev->key.keysym.unicode != 0) {
435 kbd_put_keysym(ev->key.keysym.unicode);
438 } else if (ev->type == SDL_KEYUP) {
439 if (!alt_grab) {
440 mod_state = (ev->key.keysym.mod & gui_grab_code);
441 } else {
442 mod_state = (ev->key.keysym.mod &
443 (gui_grab_code | KMOD_LSHIFT));
445 if (!mod_state) {
446 if (gui_key_modifier_pressed) {
447 gui_key_modifier_pressed = 0;
448 if (gui_keysym == 0) {
449 /* exit/enter grab if pressing Ctrl-Alt */
450 if (!gui_grab) {
451 /* if the application is not active,
452 do not try to enter grab state. It
453 prevents
454 'SDL_WM_GrabInput(SDL_GRAB_ON)'
455 from blocking all the application
456 (SDL bug). */
457 if (SDL_GetAppState() & SDL_APPACTIVE)
458 sdl_grab_start();
459 } else {
460 sdl_grab_end();
462 /* SDL does not send back all the
463 modifiers key, so we must correct it */
464 reset_keys();
465 break;
467 gui_keysym = 0;
471 if (is_graphic_console() && !gui_keysym)
472 sdl_process_key(&ev->key);
473 break;
474 case SDL_QUIT:
475 if (!no_quit)
476 qemu_system_shutdown_request();
477 break;
478 case SDL_MOUSEMOTION:
479 if (gui_grab || kbd_mouse_is_absolute() ||
480 absolute_enabled) {
481 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
482 ev->motion.x, ev->motion.y, ev->motion.state);
484 break;
485 case SDL_MOUSEBUTTONDOWN:
486 case SDL_MOUSEBUTTONUP:
488 SDL_MouseButtonEvent *bev = &ev->button;
489 if (!gui_grab && !kbd_mouse_is_absolute()) {
490 if (ev->type == SDL_MOUSEBUTTONDOWN &&
491 (bev->button == SDL_BUTTON_LEFT)) {
492 /* start grabbing all events */
493 sdl_grab_start();
495 } else {
496 int dz;
497 dz = 0;
498 if (ev->type == SDL_MOUSEBUTTONDOWN) {
499 buttonstate |= SDL_BUTTON(bev->button);
500 } else {
501 buttonstate &= ~SDL_BUTTON(bev->button);
503 #ifdef SDL_BUTTON_WHEELUP
504 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
505 dz = -1;
506 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
507 dz = 1;
509 #endif
510 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
513 break;
514 case SDL_ACTIVEEVENT:
515 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
516 !ev->active.gain && !gui_fullscreen_initial_grab) {
517 sdl_grab_end();
519 if (ev->active.state & SDL_APPACTIVE) {
520 if (ev->active.gain) {
521 /* Back to default interval */
522 dcl->gui_timer_interval = 0;
523 dcl->idle = 0;
524 } else {
525 /* Sleeping interval */
526 dcl->gui_timer_interval = 500;
527 dcl->idle = 1;
530 break;
531 default:
532 break;
537 static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
539 SDL_Rect dst = { x, y, w, h };
540 SDL_FillRect(real_screen, &dst, c);
543 static void sdl_mouse_warp(int x, int y, int on)
545 if (on) {
546 if (!guest_cursor)
547 sdl_show_cursor();
548 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
549 SDL_SetCursor(guest_sprite);
550 SDL_WarpMouse(x, y);
552 } else if (gui_grab)
553 sdl_hide_cursor();
554 guest_cursor = on;
555 guest_x = x, guest_y = y;
558 static void sdl_mouse_define(int width, int height, int bpp,
559 int hot_x, int hot_y,
560 uint8_t *image, uint8_t *mask)
562 uint8_t sprite[256], *line;
563 int x, y, dst, bypl, src = 0;
564 if (guest_sprite)
565 SDL_FreeCursor(guest_sprite);
567 memset(sprite, 0, 256);
568 bypl = ((width * bpp + 31) >> 5) << 2;
569 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
570 line = image;
571 for (x = 0; x < width; x ++, dst ++) {
572 switch (bpp) {
573 case 24:
574 src = *(line ++); src |= *(line ++); src |= *(line ++);
575 break;
576 case 16:
577 case 15:
578 src = *(line ++); src |= *(line ++);
579 break;
580 case 8:
581 src = *(line ++);
582 break;
583 case 4:
584 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
585 break;
586 case 2:
587 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
588 break;
589 case 1:
590 src = 1 & (line[x >> 3] >> (x & 7));
591 break;
593 if (!src)
594 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
597 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
599 if (guest_cursor &&
600 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
601 SDL_SetCursor(guest_sprite);
604 static void sdl_cleanup(void)
606 if (guest_sprite)
607 SDL_FreeCursor(guest_sprite);
608 SDL_Quit();
611 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
613 int flags;
614 uint8_t data = 0;
616 #if defined(__APPLE__)
617 /* always use generic keymaps */
618 if (!keyboard_layout)
619 keyboard_layout = "en-us";
620 #endif
621 if(keyboard_layout) {
622 kbd_layout = init_keyboard_layout(keyboard_layout);
623 if (!kbd_layout)
624 exit(1);
627 if (no_frame)
628 gui_noframe = 1;
630 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
631 if (SDL_Init (flags)) {
632 fprintf(stderr, "Could not initialize SDL - exiting\n");
633 exit(1);
636 dcl = qemu_mallocz(sizeof(DisplayChangeListener));
637 if (!dcl)
638 exit(1);
639 dcl->dpy_update = sdl_update;
640 dcl->dpy_resize = sdl_resize;
641 dcl->dpy_refresh = sdl_refresh;
642 dcl->dpy_setdata = sdl_setdata;
643 dcl->dpy_fill = sdl_fill;
644 ds->mouse_set = sdl_mouse_warp;
645 ds->cursor_define = sdl_mouse_define;
646 register_displaychangelistener(ds, dcl);
648 sdl_update_caption();
649 SDL_EnableKeyRepeat(250, 50);
650 gui_grab = 0;
652 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
653 sdl_cursor_normal = SDL_GetCursor();
655 atexit(sdl_cleanup);
656 if (full_screen) {
657 gui_fullscreen = 1;
658 gui_fullscreen_initial_grab = 1;
659 sdl_grab_start();