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