kvm: libkvm: remove some unused parameters
[qemu-kvm/fedora.git] / sdl.c
blobd242ccefd622e3a7894fc2498323645d6a87e770
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 uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
127 int keycode;
129 keycode = ev->keysym.scancode;
131 if (keycode < 9) {
132 keycode = 0;
133 } else if (keycode < 97) {
134 keycode -= 8; /* just an offset */
135 } else if (keycode < 212) {
136 /* use conversion table */
137 keycode = _translate_keycode(keycode - 97);
138 } else {
139 keycode = 0;
141 return keycode;
144 #endif
146 static void reset_keys(void)
148 int i;
149 for(i = 0; i < 256; i++) {
150 if (modifiers_state[i]) {
151 if (i & 0x80)
152 kbd_put_keycode(0xe0);
153 kbd_put_keycode(i | 0x80);
154 modifiers_state[i] = 0;
159 static void sdl_process_key(SDL_KeyboardEvent *ev)
161 int keycode, v;
163 if (ev->keysym.sym == SDLK_PAUSE) {
164 /* specific case */
165 v = 0;
166 if (ev->type == SDL_KEYUP)
167 v |= 0x80;
168 kbd_put_keycode(0xe1);
169 kbd_put_keycode(0x1d | v);
170 kbd_put_keycode(0x45 | v);
171 return;
174 if (kbd_layout) {
175 keycode = sdl_keyevent_to_keycode_generic(ev);
176 } else {
177 keycode = sdl_keyevent_to_keycode(ev);
180 switch(keycode) {
181 case 0x00:
182 /* sent when leaving window: reset the modifiers state */
183 reset_keys();
184 return;
185 case 0x2a: /* Left Shift */
186 case 0x36: /* Right Shift */
187 case 0x1d: /* Left CTRL */
188 case 0x9d: /* Right CTRL */
189 case 0x38: /* Left ALT */
190 case 0xb8: /* Right ALT */
191 if (ev->type == SDL_KEYUP)
192 modifiers_state[keycode] = 0;
193 else
194 modifiers_state[keycode] = 1;
195 break;
196 case 0x45: /* num lock */
197 case 0x3a: /* caps lock */
198 /* SDL does not send the key up event, so we generate it */
199 kbd_put_keycode(keycode);
200 kbd_put_keycode(keycode | 0x80);
201 return;
204 /* now send the key code */
205 if (keycode & 0x80)
206 kbd_put_keycode(0xe0);
207 if (ev->type == SDL_KEYUP)
208 kbd_put_keycode(keycode | 0x80);
209 else
210 kbd_put_keycode(keycode & 0x7f);
213 static void sdl_update_caption(void)
215 char buf[1024];
216 strcpy(buf, "QEMU");
217 #if USE_KVM
218 if (kvm_allowed) {
219 strcat(buf, "/KVM");
221 #endif
222 if (!vm_running) {
223 strcat(buf, " [Stopped]");
225 if (gui_grab) {
226 strcat(buf, " - Press Ctrl-Alt to exit grab");
228 SDL_WM_SetCaption(buf, "QEMU");
231 static void sdl_hide_cursor(void)
233 if (kbd_mouse_is_absolute()) {
234 SDL_ShowCursor(1);
235 SDL_SetCursor(sdl_cursor_hidden);
236 } else {
237 SDL_ShowCursor(0);
241 static void sdl_show_cursor(void)
243 if (!kbd_mouse_is_absolute()) {
244 SDL_ShowCursor(1);
245 SDL_SetCursor(sdl_cursor_normal);
249 static void sdl_grab_start(void)
251 sdl_hide_cursor();
252 SDL_WM_GrabInput(SDL_GRAB_ON);
253 /* dummy read to avoid moving the mouse */
254 SDL_GetRelativeMouseState(NULL, NULL);
255 gui_grab = 1;
256 sdl_update_caption();
259 static void sdl_grab_end(void)
261 SDL_WM_GrabInput(SDL_GRAB_OFF);
262 sdl_show_cursor();
263 gui_grab = 0;
264 sdl_update_caption();
267 static void sdl_send_mouse_event(int dz)
269 int dx, dy, state, buttons;
270 state = SDL_GetRelativeMouseState(&dx, &dy);
271 buttons = 0;
272 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
273 buttons |= MOUSE_EVENT_LBUTTON;
274 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
275 buttons |= MOUSE_EVENT_RBUTTON;
276 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
277 buttons |= MOUSE_EVENT_MBUTTON;
279 if (kbd_mouse_is_absolute()) {
280 if (!absolute_enabled) {
281 sdl_hide_cursor();
282 if (gui_grab) {
283 sdl_grab_end();
285 absolute_enabled = 1;
288 SDL_GetMouseState(&dx, &dy);
289 dx = dx * 0x7FFF / width;
290 dy = dy * 0x7FFF / height;
291 } else if (absolute_enabled) {
292 sdl_show_cursor();
293 absolute_enabled = 0;
296 kbd_mouse_event(dx, dy, dz, buttons);
299 static void toggle_full_screen(DisplayState *ds)
301 gui_fullscreen = !gui_fullscreen;
302 sdl_resize(ds, screen->w, screen->h);
303 if (gui_fullscreen) {
304 gui_saved_grab = gui_grab;
305 sdl_grab_start();
306 } else {
307 if (!gui_saved_grab)
308 sdl_grab_end();
310 vga_hw_invalidate();
311 vga_hw_update();
314 static void sdl_refresh(DisplayState *ds)
316 SDL_Event ev1, *ev = &ev1;
317 int mod_state;
319 if (last_vm_running != vm_running) {
320 last_vm_running = vm_running;
321 sdl_update_caption();
324 vga_hw_update();
326 while (SDL_PollEvent(ev)) {
327 switch (ev->type) {
328 case SDL_VIDEOEXPOSE:
329 sdl_update(ds, 0, 0, screen->w, screen->h);
330 break;
331 case SDL_KEYDOWN:
332 case SDL_KEYUP:
333 if (ev->type == SDL_KEYDOWN) {
334 mod_state = (SDL_GetModState() & gui_grab_code) ==
335 gui_grab_code;
336 gui_key_modifier_pressed = mod_state;
337 if (gui_key_modifier_pressed) {
338 int keycode;
339 keycode = sdl_keyevent_to_keycode(&ev->key);
340 switch(keycode) {
341 case 0x21: /* 'f' key on US keyboard */
342 toggle_full_screen(ds);
343 gui_keysym = 1;
344 break;
345 case 0x02 ... 0x0a: /* '1' to '9' keys */
346 /* Reset the modifiers sent to the current console */
347 reset_keys();
348 console_select(keycode - 0x02);
349 if (!is_graphic_console()) {
350 /* display grab if going to a text console */
351 if (gui_grab)
352 sdl_grab_end();
354 gui_keysym = 1;
355 break;
356 default:
357 break;
359 } else if (!is_graphic_console()) {
360 int keysym;
361 keysym = 0;
362 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
363 switch(ev->key.keysym.sym) {
364 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
365 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
366 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
367 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
368 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
369 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
370 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
371 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
372 default: break;
374 } else {
375 switch(ev->key.keysym.sym) {
376 case SDLK_UP: keysym = QEMU_KEY_UP; break;
377 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
378 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
379 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
380 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
381 case SDLK_END: keysym = QEMU_KEY_END; break;
382 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
383 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
384 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break; case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
385 default: break;
388 if (keysym) {
389 kbd_put_keysym(keysym);
390 } else if (ev->key.keysym.unicode != 0) {
391 kbd_put_keysym(ev->key.keysym.unicode);
394 } else if (ev->type == SDL_KEYUP) {
395 mod_state = (ev->key.keysym.mod & gui_grab_code);
396 if (!mod_state) {
397 if (gui_key_modifier_pressed) {
398 gui_key_modifier_pressed = 0;
399 if (gui_keysym == 0) {
400 /* exit/enter grab if pressing Ctrl-Alt */
401 if (!gui_grab) {
402 /* if the application is not active,
403 do not try to enter grab state. It
404 prevents
405 'SDL_WM_GrabInput(SDL_GRAB_ON)'
406 from blocking all the application
407 (SDL bug). */
408 if (SDL_GetAppState() & SDL_APPACTIVE)
409 sdl_grab_start();
410 } else {
411 sdl_grab_end();
413 /* SDL does not send back all the
414 modifiers key, so we must correct it */
415 reset_keys();
416 break;
418 gui_keysym = 0;
422 if (is_graphic_console() && !gui_keysym)
423 sdl_process_key(&ev->key);
424 break;
425 case SDL_QUIT:
426 if (!no_quit) {
427 qemu_system_shutdown_request();
429 break;
430 case SDL_MOUSEMOTION:
431 if (gui_grab || kbd_mouse_is_absolute() ||
432 absolute_enabled) {
433 sdl_send_mouse_event(0);
435 break;
436 case SDL_MOUSEBUTTONDOWN:
437 case SDL_MOUSEBUTTONUP:
439 SDL_MouseButtonEvent *bev = &ev->button;
440 if (!gui_grab && !kbd_mouse_is_absolute()) {
441 if (ev->type == SDL_MOUSEBUTTONDOWN &&
442 (bev->state & SDL_BUTTON_LMASK)) {
443 /* start grabbing all events */
444 sdl_grab_start();
446 } else {
447 int dz;
448 dz = 0;
449 #ifdef SDL_BUTTON_WHEELUP
450 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
451 dz = -1;
452 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
453 dz = 1;
455 #endif
456 sdl_send_mouse_event(dz);
459 break;
460 case SDL_ACTIVEEVENT:
461 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
462 !ev->active.gain && !gui_fullscreen_initial_grab) {
463 sdl_grab_end();
465 break;
466 default:
467 break;
472 static void sdl_cleanup(void)
474 SDL_Quit();
477 void sdl_display_init(DisplayState *ds, int full_screen)
479 int flags;
480 uint8_t data = 0;
482 #if defined(__APPLE__)
483 /* always use generic keymaps */
484 if (!keyboard_layout)
485 keyboard_layout = "en-us";
486 #endif
487 if(keyboard_layout) {
488 kbd_layout = init_keyboard_layout(keyboard_layout);
489 if (!kbd_layout)
490 exit(1);
493 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
494 if (SDL_Init (flags)) {
495 fprintf(stderr, "Could not initialize SDL - exiting\n");
496 exit(1);
498 #ifndef _WIN32
499 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
500 signal(SIGINT, SIG_DFL);
501 signal(SIGQUIT, SIG_DFL);
502 #endif
504 ds->dpy_update = sdl_update;
505 ds->dpy_resize = sdl_resize;
506 ds->dpy_refresh = sdl_refresh;
508 sdl_resize(ds, 640, 400);
509 sdl_update_caption();
510 SDL_EnableKeyRepeat(250, 50);
511 SDL_EnableUNICODE(1);
512 gui_grab = 0;
514 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
515 sdl_cursor_normal = SDL_GetCursor();
517 atexit(sdl_cleanup);
518 if (full_screen) {
519 gui_fullscreen = 1;
520 gui_fullscreen_initial_grab = 1;
521 sdl_grab_start();