]> git.proxmox.com Git - qemu.git/blame - sdl.c
Don't leak VLANClientState on PCI hot remove
[qemu.git] / sdl.c
CommitLineData
0f0b7264
FB
1/*
2 * QEMU SDL display driver
5fafdf24 3 *
0f0b7264 4 * Copyright (c) 2003 Fabrice Bellard
5fafdf24 5 *
0f0b7264
FB
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 */
87ecb68b
PB
24#include "qemu-common.h"
25#include "console.h"
26#include "sysemu.h"
5368a422 27#include "x_keymap.h"
0f0b7264
FB
28
29#include <SDL.h>
b96a313d 30#include <SDL_syswm.h>
0f0b7264 31
67b915a5
FB
32#ifndef _WIN32
33#include <signal.h>
34#endif
0f0b7264 35
7d957bd8
AL
36static DisplayChangeListener *dcl;
37static SDL_Surface *real_screen;
38static SDL_Surface *guest_screen = NULL;
0f0b7264 39static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
8a7ddc38 40static int last_vm_running;
8e9c4afe
FB
41static int gui_saved_grab;
42static int gui_fullscreen;
43523e93 43static int gui_noframe;
8e9c4afe
FB
44static int gui_key_modifier_pressed;
45static int gui_keysym;
d63d307f 46static int gui_fullscreen_initial_grab;
32ff25bf
FB
47static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
48static uint8_t modifiers_state[256];
09b26c5e
FB
49static int width, height;
50static SDL_Cursor *sdl_cursor_normal;
51static SDL_Cursor *sdl_cursor_hidden;
52static int absolute_enabled = 0;
d34cab9f
TS
53static int guest_cursor = 0;
54static int guest_x, guest_y;
55static SDL_Cursor *guest_sprite = 0;
0f0b7264
FB
56
57static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
58{
7d957bd8
AL
59 SDL_Rect rec;
60 rec.x = x;
61 rec.y = y;
62 rec.w = w;
63 rec.h = h;
898712a8 64 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
7d957bd8
AL
65
66 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
6e60065f 67 SDL_UpdateRect(real_screen, x, y, w, h);
7d957bd8
AL
68}
69
70static void sdl_setdata(DisplayState *ds)
71{
72 SDL_Rect rec;
73 rec.x = 0;
74 rec.y = 0;
75 rec.w = real_screen->w;
76 rec.h = real_screen->h;
77
78 if (guest_screen != NULL) SDL_FreeSurface(guest_screen);
79
80 guest_screen = SDL_CreateRGBSurfaceFrom(ds_get_data(ds), ds_get_width(ds), ds_get_height(ds),
81 ds_get_bits_per_pixel(ds), ds_get_linesize(ds),
82 ds->surface->pf.rmask, ds->surface->pf.gmask,
83 ds->surface->pf.bmask, ds->surface->pf.amask);
0f0b7264
FB
84}
85
7d957bd8 86static void sdl_resize(DisplayState *ds)
0f0b7264
FB
87{
88 int flags;
89
90 // printf("resizing to %d %d\n", w, h);
91
92 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
8e9c4afe
FB
93 if (gui_fullscreen)
94 flags |= SDL_FULLSCREEN;
43523e93
TS
95 if (gui_noframe)
96 flags |= SDL_NOFRAME;
9903da21 97
8bf66d40
AZ
98 width = ds_get_width(ds);
99 height = ds_get_height(ds);
100 real_screen = SDL_SetVideoMode(width, height, 0, flags);
7d957bd8 101 if (!real_screen) {
0f0b7264
FB
102 fprintf(stderr, "Could not open SDL display\n");
103 exit(1);
104 }
7d957bd8
AL
105
106 sdl_setdata(ds);
0f0b7264
FB
107}
108
3d11d0eb 109/* generic keyboard conversion */
e58d12ed 110
3d11d0eb
FB
111#include "sdl_keysym.h"
112#include "keymaps.c"
113
114static kbd_layout_t *kbd_layout = NULL;
115
116static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
e58d12ed 117{
3d11d0eb
FB
118 int keysym;
119 /* workaround for X11+SDL bug with AltGR */
120 keysym = ev->keysym.sym;
121 if (keysym == 0 && ev->keysym.scancode == 113)
122 keysym = SDLK_MODE;
60659e3b
FB
123 /* For Japanese key '\' and '|' */
124 if (keysym == 92 && ev->keysym.scancode == 133) {
125 keysym = 0xa5;
126 }
3d11d0eb 127 return keysym2scancode(kbd_layout, keysym);
e58d12ed
FB
128}
129
3d11d0eb
FB
130/* specific keyboard conversions from scan codes */
131
132#if defined(_WIN32)
e58d12ed
FB
133
134static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
135{
136 return ev->keysym.scancode;
137}
138
139#else
140
5368a422
AL
141#if defined(SDL_VIDEO_DRIVER_X11)
142#include <X11/XKBlib.h>
143
144static int check_for_evdev(void)
145{
146 SDL_SysWMinfo info;
fc78bd45 147 XkbDescPtr desc = NULL;
5368a422 148 int has_evdev = 0;
fc78bd45 149 char *keycodes = NULL;
5368a422
AL
150
151 SDL_VERSION(&info.version);
fc78bd45 152 if (!SDL_GetWMInfo(&info)) {
5368a422 153 return 0;
fc78bd45 154 }
5368a422
AL
155 desc = XkbGetKeyboard(info.info.x11.display,
156 XkbGBN_AllComponentsMask,
157 XkbUseCoreKbd);
fc78bd45
JK
158 if (desc && desc->names) {
159 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
160 if (keycodes == NULL) {
161 fprintf(stderr, "could not lookup keycode name\n");
162 } else if (strstart(keycodes, "evdev", NULL)) {
163 has_evdev = 1;
164 } else if (!strstart(keycodes, "xfree86", NULL)) {
165 fprintf(stderr, "unknown keycodes `%s', please report to "
166 "qemu-devel@nongnu.org\n", keycodes);
167 }
168 }
5368a422 169
fc78bd45
JK
170 if (desc) {
171 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
172 }
173 if (keycodes) {
174 XFree(keycodes);
175 }
5368a422
AL
176 return has_evdev;
177}
178#else
179static int check_for_evdev(void)
180{
181 return 0;
182}
183#endif
184
e58d12ed
FB
185static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
186{
187 int keycode;
5368a422
AL
188 static int has_evdev = -1;
189
190 if (has_evdev == -1)
191 has_evdev = check_for_evdev();
e58d12ed
FB
192
193 keycode = ev->keysym.scancode;
194
195 if (keycode < 9) {
196 keycode = 0;
197 } else if (keycode < 97) {
198 keycode -= 8; /* just an offset */
5368a422 199 } else if (keycode < 158) {
e58d12ed 200 /* use conversion table */
5368a422
AL
201 if (has_evdev)
202 keycode = translate_evdev_keycode(keycode - 97);
203 else
204 keycode = translate_xfree86_keycode(keycode - 97);
205 } else if (keycode == 208) { /* Hiragana_Katakana */
206 keycode = 0x70;
207 } else if (keycode == 211) { /* backslash */
208 keycode = 0x73;
e58d12ed
FB
209 } else {
210 keycode = 0;
211 }
212 return keycode;
213}
214
215#endif
216
32ff25bf
FB
217static void reset_keys(void)
218{
219 int i;
220 for(i = 0; i < 256; i++) {
221 if (modifiers_state[i]) {
222 if (i & 0x80)
223 kbd_put_keycode(0xe0);
224 kbd_put_keycode(i | 0x80);
225 modifiers_state[i] = 0;
226 }
227 }
228}
229
0f0b7264
FB
230static void sdl_process_key(SDL_KeyboardEvent *ev)
231{
32ff25bf 232 int keycode, v;
de2200d3
FB
233
234 if (ev->keysym.sym == SDLK_PAUSE) {
235 /* specific case */
236 v = 0;
237 if (ev->type == SDL_KEYUP)
238 v |= 0x80;
239 kbd_put_keycode(0xe1);
240 kbd_put_keycode(0x1d | v);
241 kbd_put_keycode(0x45 | v);
242 return;
243 }
244
3d11d0eb
FB
245 if (kbd_layout) {
246 keycode = sdl_keyevent_to_keycode_generic(ev);
247 } else {
248 keycode = sdl_keyevent_to_keycode(ev);
249 }
de2200d3
FB
250
251 switch(keycode) {
252 case 0x00:
253 /* sent when leaving window: reset the modifiers state */
32ff25bf 254 reset_keys();
de2200d3
FB
255 return;
256 case 0x2a: /* Left Shift */
257 case 0x36: /* Right Shift */
258 case 0x1d: /* Left CTRL */
259 case 0x9d: /* Right CTRL */
260 case 0x38: /* Left ALT */
261 case 0xb8: /* Right ALT */
0f0b7264 262 if (ev->type == SDL_KEYUP)
de2200d3
FB
263 modifiers_state[keycode] = 0;
264 else
265 modifiers_state[keycode] = 1;
266 break;
267 case 0x45: /* num lock */
268 case 0x3a: /* caps lock */
269 /* SDL does not send the key up event, so we generate it */
270 kbd_put_keycode(keycode);
271 kbd_put_keycode(keycode | 0x80);
272 return;
0f0b7264 273 }
de2200d3
FB
274
275 /* now send the key code */
276 if (keycode & 0x80)
277 kbd_put_keycode(0xe0);
278 if (ev->type == SDL_KEYUP)
279 kbd_put_keycode(keycode | 0x80);
280 else
281 kbd_put_keycode(keycode & 0x7f);
0f0b7264
FB
282}
283
8a7ddc38
FB
284static void sdl_update_caption(void)
285{
286 char buf[1024];
c35734b2
TS
287 const char *status = "";
288
289 if (!vm_running)
290 status = " [Stopped]";
3780e197
TS
291 else if (gui_grab) {
292 if (!alt_grab)
293 status = " - Press Ctrl-Alt to exit grab";
294 else
295 status = " - Press Ctrl-Alt-Shift to exit grab";
296 }
c35734b2
TS
297
298 if (qemu_name)
299 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
300 else
301 snprintf(buf, sizeof(buf), "QEMU%s", status);
302
8a7ddc38
FB
303 SDL_WM_SetCaption(buf, "QEMU");
304}
305
09b26c5e
FB
306static void sdl_hide_cursor(void)
307{
9467cd46
AZ
308 if (!cursor_hide)
309 return;
310
8785a8dd
FB
311 if (kbd_mouse_is_absolute()) {
312 SDL_ShowCursor(1);
313 SDL_SetCursor(sdl_cursor_hidden);
314 } else {
315 SDL_ShowCursor(0);
316 }
09b26c5e
FB
317}
318
319static void sdl_show_cursor(void)
320{
9467cd46
AZ
321 if (!cursor_hide)
322 return;
323
09b26c5e 324 if (!kbd_mouse_is_absolute()) {
8785a8dd 325 SDL_ShowCursor(1);
d34cab9f
TS
326 if (guest_cursor &&
327 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
328 SDL_SetCursor(guest_sprite);
329 else
330 SDL_SetCursor(sdl_cursor_normal);
09b26c5e
FB
331 }
332}
333
0f0b7264
FB
334static void sdl_grab_start(void)
335{
d34cab9f
TS
336 if (guest_cursor) {
337 SDL_SetCursor(guest_sprite);
08a2d4c4
AZ
338 if (!kbd_mouse_is_absolute() && !absolute_enabled)
339 SDL_WarpMouse(guest_x, guest_y);
d34cab9f
TS
340 } else
341 sdl_hide_cursor();
6bb81603
AL
342
343 if (SDL_WM_GrabInput(SDL_GRAB_ON) == SDL_GRAB_ON) {
344 gui_grab = 1;
345 sdl_update_caption();
346 } else
347 sdl_show_cursor();
0f0b7264
FB
348}
349
350static void sdl_grab_end(void)
351{
0f0b7264 352 SDL_WM_GrabInput(SDL_GRAB_OFF);
0f0b7264 353 gui_grab = 0;
d34cab9f 354 sdl_show_cursor();
8a7ddc38 355 sdl_update_caption();
0f0b7264
FB
356}
357
4c44bdcb 358static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
0f0b7264 359{
4c44bdcb 360 int buttons;
0f0b7264
FB
361 buttons = 0;
362 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
363 buttons |= MOUSE_EVENT_LBUTTON;
364 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
365 buttons |= MOUSE_EVENT_RBUTTON;
366 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
367 buttons |= MOUSE_EVENT_MBUTTON;
09b26c5e
FB
368
369 if (kbd_mouse_is_absolute()) {
370 if (!absolute_enabled) {
371 sdl_hide_cursor();
372 if (gui_grab) {
373 sdl_grab_end();
374 }
375 absolute_enabled = 1;
376 }
377
4c44bdcb
AJ
378 dx = x * 0x7FFF / (width - 1);
379 dy = y * 0x7FFF / (height - 1);
455204eb
TS
380 } else if (absolute_enabled) {
381 sdl_show_cursor();
382 absolute_enabled = 0;
d34cab9f 383 } else if (guest_cursor) {
4c44bdcb
AJ
384 x -= guest_x;
385 y -= guest_y;
386 guest_x += x;
387 guest_y += y;
388 dx = x;
389 dy = y;
09b26c5e
FB
390 }
391
0f0b7264
FB
392 kbd_mouse_event(dx, dy, dz, buttons);
393}
394
8e9c4afe
FB
395static void toggle_full_screen(DisplayState *ds)
396{
397 gui_fullscreen = !gui_fullscreen;
7d957bd8 398 sdl_resize(ds);
8e9c4afe
FB
399 if (gui_fullscreen) {
400 gui_saved_grab = gui_grab;
401 sdl_grab_start();
402 } else {
403 if (!gui_saved_grab)
404 sdl_grab_end();
405 }
95219897
PB
406 vga_hw_invalidate();
407 vga_hw_update();
8e9c4afe
FB
408}
409
0f0b7264
FB
410static void sdl_refresh(DisplayState *ds)
411{
412 SDL_Event ev1, *ev = &ev1;
8e9c4afe 413 int mod_state;
4c44bdcb 414 int buttonstate = SDL_GetMouseState(NULL, NULL);
3b46e624 415
8a7ddc38
FB
416 if (last_vm_running != vm_running) {
417 last_vm_running = vm_running;
418 sdl_update_caption();
419 }
420
95219897 421 vga_hw_update();
3bee8bd0 422 SDL_EnableUNICODE(!is_graphic_console());
457831f4 423
0f0b7264
FB
424 while (SDL_PollEvent(ev)) {
425 switch (ev->type) {
426 case SDL_VIDEOEXPOSE:
7d957bd8 427 sdl_update(ds, 0, 0, real_screen->w, real_screen->h);
0f0b7264
FB
428 break;
429 case SDL_KEYDOWN:
430 case SDL_KEYUP:
431 if (ev->type == SDL_KEYDOWN) {
3780e197
TS
432 if (!alt_grab) {
433 mod_state = (SDL_GetModState() & gui_grab_code) ==
434 gui_grab_code;
435 } else {
436 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
437 (gui_grab_code | KMOD_LSHIFT);
438 }
8e9c4afe 439 gui_key_modifier_pressed = mod_state;
457831f4 440 if (gui_key_modifier_pressed) {
32ff25bf
FB
441 int keycode;
442 keycode = sdl_keyevent_to_keycode(&ev->key);
443 switch(keycode) {
444 case 0x21: /* 'f' key on US keyboard */
457831f4
FB
445 toggle_full_screen(ds);
446 gui_keysym = 1;
447 break;
5fafdf24 448 case 0x02 ... 0x0a: /* '1' to '9' keys */
dfd92d3a
FB
449 /* Reset the modifiers sent to the current console */
450 reset_keys();
32ff25bf 451 console_select(keycode - 0x02);
95219897 452 if (!is_graphic_console()) {
457831f4
FB
453 /* display grab if going to a text console */
454 if (gui_grab)
455 sdl_grab_end();
456 }
457 gui_keysym = 1;
458 break;
459 default:
460 break;
461 }
95219897 462 } else if (!is_graphic_console()) {
457831f4
FB
463 int keysym;
464 keysym = 0;
465 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
466 switch(ev->key.keysym.sym) {
467 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
468 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
469 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
470 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
471 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
472 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
473 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
474 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
475 default: break;
476 }
477 } else {
478 switch(ev->key.keysym.sym) {
479 case SDLK_UP: keysym = QEMU_KEY_UP; break;
480 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
481 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
482 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
483 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
484 case SDLK_END: keysym = QEMU_KEY_END; break;
485 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
486 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
e91c8a77
TS
487 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
488 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
457831f4
FB
489 default: break;
490 }
491 }
492 if (keysym) {
493 kbd_put_keysym(keysym);
494 } else if (ev->key.keysym.unicode != 0) {
495 kbd_put_keysym(ev->key.keysym.unicode);
496 }
8e9c4afe
FB
497 }
498 } else if (ev->type == SDL_KEYUP) {
3780e197
TS
499 if (!alt_grab) {
500 mod_state = (ev->key.keysym.mod & gui_grab_code);
501 } else {
502 mod_state = (ev->key.keysym.mod &
503 (gui_grab_code | KMOD_LSHIFT));
504 }
8e9c4afe
FB
505 if (!mod_state) {
506 if (gui_key_modifier_pressed) {
5b311878 507 gui_key_modifier_pressed = 0;
457831f4 508 if (gui_keysym == 0) {
32ff25bf 509 /* exit/enter grab if pressing Ctrl-Alt */
c66b0d4c
FB
510 if (!gui_grab) {
511 /* if the application is not active,
512 do not try to enter grab state. It
513 prevents
514 'SDL_WM_GrabInput(SDL_GRAB_ON)'
515 from blocking all the application
516 (SDL bug). */
517 if (SDL_GetAppState() & SDL_APPACTIVE)
518 sdl_grab_start();
519 } else {
8e9c4afe 520 sdl_grab_end();
c66b0d4c 521 }
32ff25bf
FB
522 /* SDL does not send back all the
523 modifiers key, so we must correct it */
524 reset_keys();
8e9c4afe
FB
525 break;
526 }
8e9c4afe
FB
527 gui_keysym = 0;
528 }
0f0b7264
FB
529 }
530 }
5fafdf24 531 if (is_graphic_console() && !gui_keysym)
457831f4 532 sdl_process_key(&ev->key);
0f0b7264
FB
533 break;
534 case SDL_QUIT:
5b08fc10 535 if (!no_quit)
731345e1 536 qemu_system_shutdown_request();
0f0b7264
FB
537 break;
538 case SDL_MOUSEMOTION:
455204eb
TS
539 if (gui_grab || kbd_mouse_is_absolute() ||
540 absolute_enabled) {
4c44bdcb
AJ
541 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
542 ev->motion.x, ev->motion.y, ev->motion.state);
0f0b7264
FB
543 }
544 break;
545 case SDL_MOUSEBUTTONDOWN:
546 case SDL_MOUSEBUTTONUP:
547 {
548 SDL_MouseButtonEvent *bev = &ev->button;
09b26c5e 549 if (!gui_grab && !kbd_mouse_is_absolute()) {
0f0b7264 550 if (ev->type == SDL_MOUSEBUTTONDOWN &&
4c44bdcb 551 (bev->button == SDL_BUTTON_LEFT)) {
0f0b7264
FB
552 /* start grabbing all events */
553 sdl_grab_start();
554 }
555 } else {
18a6d284
FB
556 int dz;
557 dz = 0;
4c44bdcb
AJ
558 if (ev->type == SDL_MOUSEBUTTONDOWN) {
559 buttonstate |= SDL_BUTTON(bev->button);
560 } else {
561 buttonstate &= ~SDL_BUTTON(bev->button);
562 }
18a6d284 563#ifdef SDL_BUTTON_WHEELUP
09b26c5e 564 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
18a6d284 565 dz = -1;
09b26c5e 566 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
18a6d284
FB
567 dz = 1;
568 }
3b46e624 569#endif
4c44bdcb 570 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
0f0b7264
FB
571 }
572 }
573 break;
0294ffb9 574 case SDL_ACTIVEEVENT:
5b311878
PB
575 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
576 !ev->active.gain && !gui_fullscreen_initial_grab) {
0294ffb9
FB
577 sdl_grab_end();
578 }
f442e08b
AJ
579 if (ev->active.state & SDL_APPACTIVE) {
580 if (ev->active.gain) {
581 /* Back to default interval */
7d957bd8
AL
582 dcl->gui_timer_interval = 0;
583 dcl->idle = 0;
f442e08b
AJ
584 } else {
585 /* Sleeping interval */
7d957bd8
AL
586 dcl->gui_timer_interval = 500;
587 dcl->idle = 1;
f442e08b
AJ
588 }
589 }
0294ffb9 590 break;
0f0b7264
FB
591 default:
592 break;
593 }
594 }
595}
596
d34cab9f
TS
597static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
598{
599 SDL_Rect dst = { x, y, w, h };
7d957bd8 600 SDL_FillRect(real_screen, &dst, c);
d34cab9f
TS
601}
602
603static void sdl_mouse_warp(int x, int y, int on)
604{
605 if (on) {
606 if (!guest_cursor)
607 sdl_show_cursor();
608 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
609 SDL_SetCursor(guest_sprite);
08a2d4c4
AZ
610 if (!kbd_mouse_is_absolute() && !absolute_enabled)
611 SDL_WarpMouse(x, y);
d34cab9f
TS
612 }
613 } else if (gui_grab)
614 sdl_hide_cursor();
615 guest_cursor = on;
616 guest_x = x, guest_y = y;
617}
618
619static void sdl_mouse_define(int width, int height, int bpp,
620 int hot_x, int hot_y,
621 uint8_t *image, uint8_t *mask)
622{
623 uint8_t sprite[256], *line;
624 int x, y, dst, bypl, src = 0;
625 if (guest_sprite)
626 SDL_FreeCursor(guest_sprite);
627
628 memset(sprite, 0, 256);
629 bypl = ((width * bpp + 31) >> 5) << 2;
630 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
631 line = image;
632 for (x = 0; x < width; x ++, dst ++) {
633 switch (bpp) {
634 case 24:
635 src = *(line ++); src |= *(line ++); src |= *(line ++);
636 break;
637 case 16:
638 case 15:
639 src = *(line ++); src |= *(line ++);
640 break;
641 case 8:
642 src = *(line ++);
643 break;
644 case 4:
645 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
646 break;
647 case 2:
648 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
649 break;
650 case 1:
651 src = 1 & (line[x >> 3] >> (x & 7));
652 break;
653 }
654 if (!src)
655 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
656 }
657 }
658 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
659
660 if (guest_cursor &&
661 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
662 SDL_SetCursor(guest_sprite);
663}
664
5fafdf24 665static void sdl_cleanup(void)
898712a8 666{
d34cab9f
TS
667 if (guest_sprite)
668 SDL_FreeCursor(guest_sprite);
898712a8
FB
669 SDL_Quit();
670}
671
43523e93 672void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
0f0b7264
FB
673{
674 int flags;
09b26c5e 675 uint8_t data = 0;
0f0b7264 676
3d11d0eb
FB
677#if defined(__APPLE__)
678 /* always use generic keymaps */
679 if (!keyboard_layout)
680 keyboard_layout = "en-us";
681#endif
682 if(keyboard_layout) {
683 kbd_layout = init_keyboard_layout(keyboard_layout);
684 if (!kbd_layout)
685 exit(1);
686 }
687
43523e93
TS
688 if (no_frame)
689 gui_noframe = 1;
690
0f0b7264
FB
691 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
692 if (SDL_Init (flags)) {
693 fprintf(stderr, "Could not initialize SDL - exiting\n");
694 exit(1);
695 }
0ae04d73 696
7d957bd8 697 dcl = qemu_mallocz(sizeof(DisplayChangeListener));
7d957bd8
AL
698 dcl->dpy_update = sdl_update;
699 dcl->dpy_resize = sdl_resize;
700 dcl->dpy_refresh = sdl_refresh;
701 dcl->dpy_setdata = sdl_setdata;
702 dcl->dpy_fill = sdl_fill;
d34cab9f
TS
703 ds->mouse_set = sdl_mouse_warp;
704 ds->cursor_define = sdl_mouse_define;
7d957bd8 705 register_displaychangelistener(ds, dcl);
0f0b7264 706
8a7ddc38 707 sdl_update_caption();
0f0b7264
FB
708 SDL_EnableKeyRepeat(250, 50);
709 gui_grab = 0;
898712a8 710
09b26c5e
FB
711 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
712 sdl_cursor_normal = SDL_GetCursor();
713
898712a8 714 atexit(sdl_cleanup);
d63d307f
FB
715 if (full_screen) {
716 gui_fullscreen = 1;
717 gui_fullscreen_initial_grab = 1;
718 sdl_grab_start();
719 }
0f0b7264 720}