]> git.proxmox.com Git - mirror_qemu.git/blame - ui/sdl.c
virtio-ccw: range check in READ_VQ_CONF
[mirror_qemu.git] / ui / 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 */
cdfb017e
SW
24
25/* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
26#undef WIN32_LEAN_AND_MEAN
27
0f0b7264 28#include <SDL.h>
c9985aa8 29#include <SDL_syswm.h>
0f0b7264 30
511d2b14 31#include "qemu-common.h"
28ecbaee 32#include "ui/console.h"
a25f545d 33#include "ui/input.h"
9c17d615 34#include "sysemu/sysemu.h"
511d2b14 35#include "x_keymap.h"
c18a2c36 36#include "sdl_zoom.h"
511d2b14 37
7d957bd8 38static DisplayChangeListener *dcl;
8db9bae9 39static DisplaySurface *surface;
7d957bd8
AL
40static SDL_Surface *real_screen;
41static SDL_Surface *guest_screen = NULL;
0f0b7264 42static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
8a7ddc38 43static int last_vm_running;
f9977897
JK
44static bool gui_saved_scaling;
45static int gui_saved_width;
46static int gui_saved_height;
8e9c4afe
FB
47static int gui_saved_grab;
48static int gui_fullscreen;
43523e93 49static int gui_noframe;
8e9c4afe
FB
50static int gui_key_modifier_pressed;
51static int gui_keysym;
32ff25bf
FB
52static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
53static uint8_t modifiers_state[256];
09b26c5e
FB
54static SDL_Cursor *sdl_cursor_normal;
55static SDL_Cursor *sdl_cursor_hidden;
56static int absolute_enabled = 0;
d34cab9f
TS
57static int guest_cursor = 0;
58static int guest_x, guest_y;
660f11be 59static SDL_Cursor *guest_sprite = NULL;
c18a2c36
SS
60static SDL_PixelFormat host_format;
61static int scaling_active = 0;
3af12c86 62static Notifier mouse_mode_notifier;
0f0b7264 63
5f5d82dd
BH
64#if 0
65#define DEBUG_SDL
66#endif
67
7c20b4a3 68static void sdl_update(DisplayChangeListener *dcl,
7c20b4a3 69 int x, int y, int w, int h)
0f0b7264 70{
c18a2c36
SS
71 SDL_Rect rec;
72 rec.x = x;
73 rec.y = y;
74 rec.w = w;
75 rec.h = h;
76
5f5d82dd
BH
77#ifdef DEBUG_SDL
78 printf("SDL: Updating x=%d y=%d w=%d h=%d (scaling: %d)\n",
79 x, y, w, h, scaling_active);
80#endif
81
7b5d76da 82 if (guest_screen) {
c18a2c36
SS
83 if (!scaling_active) {
84 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
85 } else {
86 if (sdl_zoom_blit(guest_screen, real_screen, SMOOTHING_ON, &rec) < 0) {
87 fprintf(stderr, "Zoom blit failed\n");
88 exit(1);
89 }
90 }
91 }
92 SDL_UpdateRect(real_screen, rec.x, rec.y, rec.w, rec.h);
7d957bd8
AL
93}
94
9510a486 95static void do_sdl_resize(int width, int height, int bpp)
0f0b7264
FB
96{
97 int flags;
898ae284 98 SDL_Surface *tmp_screen;
0f0b7264 99
5f5d82dd
BH
100#ifdef DEBUG_SDL
101 printf("SDL: Resizing to %dx%d bpp %d\n", width, height, bpp);
102#endif
0f0b7264 103
91ada980
JK
104 flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
105 if (gui_fullscreen) {
8e9c4afe 106 flags |= SDL_FULLSCREEN;
91ada980
JK
107 } else {
108 flags |= SDL_RESIZABLE;
109 }
43523e93
TS
110 if (gui_noframe)
111 flags |= SDL_NOFRAME;
9903da21 112
898ae284 113 tmp_screen = SDL_SetVideoMode(width, height, bpp, flags);
7d957bd8 114 if (!real_screen) {
898ae284
LL
115 if (!tmp_screen) {
116 fprintf(stderr, "Could not open SDL display (%dx%dx%d): %s\n",
117 width, height, bpp, SDL_GetError());
118 exit(1);
119 }
120 } else {
121 /*
122 * Revert to the previous video mode if the change of resizing or
123 * resolution failed.
124 */
125 if (!tmp_screen) {
126 fprintf(stderr, "Failed to set SDL display (%dx%dx%d): %s\n",
127 width, height, bpp, SDL_GetError());
128 return;
129 }
0f0b7264 130 }
898ae284
LL
131
132 real_screen = tmp_screen;
7b5d76da
AL
133}
134
c12aeb86 135static void sdl_switch(DisplayChangeListener *dcl,
8db9bae9 136 DisplaySurface *new_surface)
7b5d76da 137{
d28d6505 138 PixelFormat pf;
8db9bae9
GH
139
140 /* temporary hack: allows to call sdl_switch to handle scaling changes */
141 if (new_surface) {
142 surface = new_surface;
143 }
d28d6505 144 pf = qemu_pixelformat_from_pixman(surface->format);
8db9bae9 145
187cd1d9 146 if (!scaling_active) {
8db9bae9
GH
147 do_sdl_resize(surface_width(surface), surface_height(surface), 0);
148 } else if (real_screen->format->BitsPerPixel !=
149 surface_bits_per_pixel(surface)) {
187cd1d9 150 do_sdl_resize(real_screen->w, real_screen->h,
8db9bae9 151 surface_bits_per_pixel(surface));
c18a2c36 152 }
8db9bae9
GH
153
154 if (guest_screen != NULL) {
155 SDL_FreeSurface(guest_screen);
156 }
5f5d82dd
BH
157
158#ifdef DEBUG_SDL
159 printf("SDL: Creating surface with masks: %08x %08x %08x %08x\n",
160 pf.rmask, pf.gmask, pf.bmask, pf.amask);
161#endif
162
8db9bae9
GH
163 guest_screen = SDL_CreateRGBSurfaceFrom
164 (surface_data(surface),
165 surface_width(surface), surface_height(surface),
166 surface_bits_per_pixel(surface), surface_stride(surface),
30f1e661
GH
167 pf.rmask, pf.gmask,
168 pf.bmask, pf.amask);
0f0b7264
FB
169}
170
7dd93291
BH
171static bool sdl_check_format(DisplayChangeListener *dcl,
172 pixman_format_code_t format)
173{
174 /*
175 * We let SDL convert for us a few more formats than,
176 * the native ones. Thes are the ones I have tested.
177 */
178 return (format == PIXMAN_x8r8g8b8 ||
179 format == PIXMAN_b8g8r8x8 ||
180 format == PIXMAN_x1r5g5b5 ||
181 format == PIXMAN_r5g6b5);
182}
183
3d11d0eb 184/* generic keyboard conversion */
e58d12ed 185
3d11d0eb 186#include "sdl_keysym.h"
3d11d0eb 187
c227f099 188static kbd_layout_t *kbd_layout = NULL;
3d11d0eb
FB
189
190static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
e58d12ed 191{
3d11d0eb
FB
192 int keysym;
193 /* workaround for X11+SDL bug with AltGR */
194 keysym = ev->keysym.sym;
195 if (keysym == 0 && ev->keysym.scancode == 113)
196 keysym = SDLK_MODE;
60659e3b
FB
197 /* For Japanese key '\' and '|' */
198 if (keysym == 92 && ev->keysym.scancode == 133) {
199 keysym = 0xa5;
200 }
44bb61c8 201 return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK;
e58d12ed
FB
202}
203
3d11d0eb
FB
204/* specific keyboard conversions from scan codes */
205
206#if defined(_WIN32)
e58d12ed
FB
207
208static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
209{
210 return ev->keysym.scancode;
211}
212
213#else
214
5368a422
AL
215#if defined(SDL_VIDEO_DRIVER_X11)
216#include <X11/XKBlib.h>
217
218static int check_for_evdev(void)
219{
220 SDL_SysWMinfo info;
229609dd 221 XkbDescPtr desc = NULL;
5368a422 222 int has_evdev = 0;
229609dd 223 char *keycodes = NULL;
5368a422
AL
224
225 SDL_VERSION(&info.version);
229609dd 226 if (!SDL_GetWMInfo(&info)) {
5368a422 227 return 0;
229609dd 228 }
5368a422
AL
229 desc = XkbGetKeyboard(info.info.x11.display,
230 XkbGBN_AllComponentsMask,
231 XkbUseCoreKbd);
229609dd
JK
232 if (desc && desc->names) {
233 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
234 if (keycodes == NULL) {
235 fprintf(stderr, "could not lookup keycode name\n");
236 } else if (strstart(keycodes, "evdev", NULL)) {
237 has_evdev = 1;
238 } else if (!strstart(keycodes, "xfree86", NULL)) {
239 fprintf(stderr, "unknown keycodes `%s', please report to "
240 "qemu-devel@nongnu.org\n", keycodes);
241 }
242 }
5368a422 243
229609dd
JK
244 if (desc) {
245 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
246 }
247 if (keycodes) {
248 XFree(keycodes);
249 }
5368a422
AL
250 return has_evdev;
251}
252#else
253static int check_for_evdev(void)
254{
255 return 0;
256}
257#endif
258
e58d12ed
FB
259static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
260{
261 int keycode;
5368a422
AL
262 static int has_evdev = -1;
263
264 if (has_evdev == -1)
265 has_evdev = check_for_evdev();
e58d12ed
FB
266
267 keycode = ev->keysym.scancode;
268
269 if (keycode < 9) {
270 keycode = 0;
271 } else if (keycode < 97) {
272 keycode -= 8; /* just an offset */
5368a422 273 } else if (keycode < 158) {
e58d12ed 274 /* use conversion table */
5368a422
AL
275 if (has_evdev)
276 keycode = translate_evdev_keycode(keycode - 97);
277 else
278 keycode = translate_xfree86_keycode(keycode - 97);
279 } else if (keycode == 208) { /* Hiragana_Katakana */
280 keycode = 0x70;
281 } else if (keycode == 211) { /* backslash */
282 keycode = 0x73;
e58d12ed
FB
283 } else {
284 keycode = 0;
285 }
286 return keycode;
287}
288
289#endif
290
32ff25bf
FB
291static void reset_keys(void)
292{
293 int i;
294 for(i = 0; i < 256; i++) {
295 if (modifiers_state[i]) {
a25f545d 296 qemu_input_event_send_key_number(dcl->con, i, false);
32ff25bf
FB
297 modifiers_state[i] = 0;
298 }
299 }
300}
301
0f0b7264
FB
302static void sdl_process_key(SDL_KeyboardEvent *ev)
303{
a25f545d 304 int keycode;
de2200d3
FB
305
306 if (ev->keysym.sym == SDLK_PAUSE) {
307 /* specific case */
a25f545d
GH
308 qemu_input_event_send_key_qcode(dcl->con, Q_KEY_CODE_PAUSE,
309 ev->type == SDL_KEYDOWN);
de2200d3
FB
310 return;
311 }
312
3d11d0eb
FB
313 if (kbd_layout) {
314 keycode = sdl_keyevent_to_keycode_generic(ev);
315 } else {
316 keycode = sdl_keyevent_to_keycode(ev);
317 }
de2200d3
FB
318
319 switch(keycode) {
320 case 0x00:
321 /* sent when leaving window: reset the modifiers state */
32ff25bf 322 reset_keys();
de2200d3
FB
323 return;
324 case 0x2a: /* Left Shift */
325 case 0x36: /* Right Shift */
326 case 0x1d: /* Left CTRL */
327 case 0x9d: /* Right CTRL */
328 case 0x38: /* Left ALT */
329 case 0xb8: /* Right ALT */
0f0b7264 330 if (ev->type == SDL_KEYUP)
de2200d3
FB
331 modifiers_state[keycode] = 0;
332 else
333 modifiers_state[keycode] = 1;
334 break;
4e79bcbb
SW
335#define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
336#if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
337 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
de2200d3
FB
338 case 0x45: /* num lock */
339 case 0x3a: /* caps lock */
340 /* SDL does not send the key up event, so we generate it */
a25f545d
GH
341 qemu_input_event_send_key_number(dcl->con, keycode, true);
342 qemu_input_event_send_key_number(dcl->con, keycode, false);
de2200d3 343 return;
4e79bcbb 344#endif
0f0b7264 345 }
de2200d3
FB
346
347 /* now send the key code */
a25f545d
GH
348 qemu_input_event_send_key_number(dcl->con, keycode,
349 ev->type == SDL_KEYDOWN);
0f0b7264
FB
350}
351
8a7ddc38
FB
352static void sdl_update_caption(void)
353{
b4ed5d18
DE
354 char win_title[1024];
355 char icon_title[1024];
c35734b2
TS
356 const char *status = "";
357
1354869c 358 if (!runstate_is_running())
c35734b2 359 status = " [Stopped]";
3780e197 360 else if (gui_grab) {
0ca9f8a4 361 if (alt_grab)
4e75b342 362 status = " - Press Ctrl-Alt-Shift to exit mouse grab";
0ca9f8a4 363 else if (ctrl_grab)
4e75b342 364 status = " - Press Right-Ctrl to exit mouse grab";
0ca9f8a4 365 else
4e75b342 366 status = " - Press Ctrl-Alt to exit mouse grab";
3780e197 367 }
c35734b2 368
b4ed5d18
DE
369 if (qemu_name) {
370 snprintf(win_title, sizeof(win_title), "QEMU (%s)%s", qemu_name, status);
371 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
372 } else {
373 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
374 snprintf(icon_title, sizeof(icon_title), "QEMU");
375 }
c35734b2 376
b4ed5d18 377 SDL_WM_SetCaption(win_title, icon_title);
8a7ddc38
FB
378}
379
09b26c5e
FB
380static void sdl_hide_cursor(void)
381{
9467cd46
AZ
382 if (!cursor_hide)
383 return;
384
3ab193e6 385 if (qemu_input_is_absolute()) {
8785a8dd
FB
386 SDL_ShowCursor(1);
387 SDL_SetCursor(sdl_cursor_hidden);
388 } else {
389 SDL_ShowCursor(0);
390 }
09b26c5e
FB
391}
392
393static void sdl_show_cursor(void)
394{
9467cd46
AZ
395 if (!cursor_hide)
396 return;
397
3ab193e6 398 if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL)) {
8785a8dd 399 SDL_ShowCursor(1);
d34cab9f 400 if (guest_cursor &&
3ab193e6 401 (gui_grab || qemu_input_is_absolute() || absolute_enabled))
d34cab9f
TS
402 SDL_SetCursor(guest_sprite);
403 else
404 SDL_SetCursor(sdl_cursor_normal);
09b26c5e
FB
405 }
406}
407
0f0b7264
FB
408static void sdl_grab_start(void)
409{
85f94f86
JK
410 /*
411 * If the application is not active, do not try to enter grab state. This
412 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
413 * application (SDL bug).
414 */
415 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS)) {
416 return;
417 }
d34cab9f
TS
418 if (guest_cursor) {
419 SDL_SetCursor(guest_sprite);
3ab193e6 420 if (!qemu_input_is_absolute() && !absolute_enabled) {
08a2d4c4 421 SDL_WarpMouse(guest_x, guest_y);
3ab193e6 422 }
d34cab9f
TS
423 } else
424 sdl_hide_cursor();
eaa2e027
JK
425 SDL_WM_GrabInput(SDL_GRAB_ON);
426 gui_grab = 1;
427 sdl_update_caption();
0f0b7264
FB
428}
429
430static void sdl_grab_end(void)
431{
0f0b7264 432 SDL_WM_GrabInput(SDL_GRAB_OFF);
0f0b7264 433 gui_grab = 0;
d34cab9f 434 sdl_show_cursor();
8a7ddc38 435 sdl_update_caption();
0f0b7264
FB
436}
437
66596356
JK
438static void absolute_mouse_grab(void)
439{
440 int mouse_x, mouse_y;
441
85f94f86
JK
442 SDL_GetMouseState(&mouse_x, &mouse_y);
443 if (mouse_x > 0 && mouse_x < real_screen->w - 1 &&
444 mouse_y > 0 && mouse_y < real_screen->h - 1) {
445 sdl_grab_start();
66596356
JK
446 }
447}
448
9e8dd451 449static void sdl_mouse_mode_change(Notifier *notify, void *data)
3af12c86 450{
3ab193e6 451 if (qemu_input_is_absolute()) {
3af12c86 452 if (!absolute_enabled) {
3af12c86 453 absolute_enabled = 1;
81c0d5a6 454 if (qemu_console_is_graphic(NULL)) {
66596356
JK
455 absolute_mouse_grab();
456 }
3af12c86
AL
457 }
458 } else if (absolute_enabled) {
f8b8d633
JK
459 if (!gui_fullscreen) {
460 sdl_grab_end();
461 }
35b0f237 462 absolute_enabled = 0;
3af12c86
AL
463 }
464}
465
3ab193e6 466static void sdl_send_mouse_event(int dx, int dy, int x, int y, int state)
0f0b7264 467{
3ab193e6
GH
468 static uint32_t bmap[INPUT_BUTTON_MAX] = {
469 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
470 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
471 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
472 [INPUT_BUTTON_WHEEL_UP] = SDL_BUTTON(SDL_BUTTON_WHEELUP),
473 [INPUT_BUTTON_WHEEL_DOWN] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN),
474 };
475 static uint32_t prev_state;
476
477 if (prev_state != state) {
478 qemu_input_update_buttons(dcl->con, bmap, prev_state, state);
479 prev_state = state;
480 }
481
482 if (qemu_input_is_absolute()) {
483 qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, x,
484 real_screen->w);
485 qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, y,
486 real_screen->h);
c3aa84b6
GH
487 } else {
488 if (guest_cursor) {
489 x -= guest_x;
490 y -= guest_y;
491 guest_x += x;
492 guest_y += y;
493 dx = x;
494 dy = y;
495 }
496 qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, dx);
497 qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, dy);
09b26c5e 498 }
3ab193e6 499 qemu_input_event_sync();
0f0b7264
FB
500}
501
8db9bae9 502static void sdl_scale(int width, int height)
f9977897
JK
503{
504 int bpp = real_screen->format->BitsPerPixel;
505
5f5d82dd
BH
506#ifdef DEBUG_SDL
507 printf("SDL: Scaling to %dx%d bpp %d\n", width, height, bpp);
508#endif
509
f9977897
JK
510 if (bpp != 16 && bpp != 32) {
511 bpp = 32;
512 }
513 do_sdl_resize(width, height, bpp);
514 scaling_active = 1;
f9977897
JK
515}
516
8db9bae9 517static void toggle_full_screen(void)
8e9c4afe 518{
8db9bae9
GH
519 int width = surface_width(surface);
520 int height = surface_height(surface);
521 int bpp = surface_bits_per_pixel(surface);
522
8e9c4afe 523 gui_fullscreen = !gui_fullscreen;
8e9c4afe 524 if (gui_fullscreen) {
f9977897
JK
525 gui_saved_width = real_screen->w;
526 gui_saved_height = real_screen->h;
527 gui_saved_scaling = scaling_active;
528
8db9bae9 529 do_sdl_resize(width, height, bpp);
c18a2c36 530 scaling_active = 0;
f9977897 531
8e9c4afe
FB
532 gui_saved_grab = gui_grab;
533 sdl_grab_start();
534 } else {
f9977897 535 if (gui_saved_scaling) {
8db9bae9 536 sdl_scale(gui_saved_width, gui_saved_height);
f9977897 537 } else {
8db9bae9 538 do_sdl_resize(width, height, 0);
f9977897 539 }
81c0d5a6 540 if (!gui_saved_grab || !qemu_console_is_graphic(NULL)) {
8e9c4afe 541 sdl_grab_end();
f8558100 542 }
8e9c4afe 543 }
1dbfa005
GH
544 graphic_hw_invalidate(NULL);
545 graphic_hw_update(NULL);
8e9c4afe
FB
546}
547
8db9bae9 548static void handle_keydown(SDL_Event *ev)
0f0b7264 549{
8e9c4afe 550 int mod_state;
1ae1caf1
JK
551 int keycode;
552
553 if (alt_grab) {
554 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
555 (gui_grab_code | KMOD_LSHIFT);
556 } else if (ctrl_grab) {
557 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
558 } else {
559 mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
560 }
561 gui_key_modifier_pressed = mod_state;
562
563 if (gui_key_modifier_pressed) {
564 keycode = sdl_keyevent_to_keycode(&ev->key);
565 switch (keycode) {
566 case 0x21: /* 'f' key on US keyboard */
8db9bae9 567 toggle_full_screen();
1ae1caf1
JK
568 gui_keysym = 1;
569 break;
570 case 0x16: /* 'u' key on US keyboard */
571 if (scaling_active) {
572 scaling_active = 0;
bc2ed970 573 sdl_switch(dcl, NULL);
1dbfa005
GH
574 graphic_hw_invalidate(NULL);
575 graphic_hw_update(NULL);
1ae1caf1
JK
576 }
577 gui_keysym = 1;
578 break;
579 case 0x02 ... 0x0a: /* '1' to '9' keys */
580 /* Reset the modifiers sent to the current console */
581 reset_keys();
582 console_select(keycode - 0x02);
583 gui_keysym = 1;
584 if (gui_fullscreen) {
585 break;
586 }
81c0d5a6 587 if (!qemu_console_is_graphic(NULL)) {
1ae1caf1
JK
588 /* release grab if going to a text console */
589 if (gui_grab) {
590 sdl_grab_end();
591 } else if (absolute_enabled) {
592 sdl_show_cursor();
593 }
594 } else if (absolute_enabled) {
595 sdl_hide_cursor();
596 absolute_mouse_grab();
597 }
598 break;
599 case 0x1b: /* '+' */
600 case 0x35: /* '-' */
601 if (!gui_fullscreen) {
602 int width = MAX(real_screen->w + (keycode == 0x1b ? 50 : -50),
603 160);
8db9bae9
GH
604 int height = (surface_height(surface) * width) /
605 surface_width(surface);
1ae1caf1 606
8db9bae9 607 sdl_scale(width, height);
1dbfa005
GH
608 graphic_hw_invalidate(NULL);
609 graphic_hw_update(NULL);
1ae1caf1
JK
610 gui_keysym = 1;
611 }
612 default:
613 break;
614 }
81c0d5a6 615 } else if (!qemu_console_is_graphic(NULL)) {
1ae1caf1
JK
616 int keysym = 0;
617
618 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
619 switch (ev->key.keysym.sym) {
620 case SDLK_UP:
621 keysym = QEMU_KEY_CTRL_UP;
622 break;
623 case SDLK_DOWN:
624 keysym = QEMU_KEY_CTRL_DOWN;
625 break;
626 case SDLK_LEFT:
627 keysym = QEMU_KEY_CTRL_LEFT;
628 break;
629 case SDLK_RIGHT:
630 keysym = QEMU_KEY_CTRL_RIGHT;
631 break;
632 case SDLK_HOME:
633 keysym = QEMU_KEY_CTRL_HOME;
634 break;
635 case SDLK_END:
636 keysym = QEMU_KEY_CTRL_END;
637 break;
638 case SDLK_PAGEUP:
639 keysym = QEMU_KEY_CTRL_PAGEUP;
640 break;
641 case SDLK_PAGEDOWN:
642 keysym = QEMU_KEY_CTRL_PAGEDOWN;
643 break;
644 default:
645 break;
646 }
647 } else {
648 switch (ev->key.keysym.sym) {
649 case SDLK_UP:
650 keysym = QEMU_KEY_UP;
651 break;
652 case SDLK_DOWN:
653 keysym = QEMU_KEY_DOWN;
654 break;
655 case SDLK_LEFT:
656 keysym = QEMU_KEY_LEFT;
657 break;
658 case SDLK_RIGHT:
659 keysym = QEMU_KEY_RIGHT;
660 break;
661 case SDLK_HOME:
662 keysym = QEMU_KEY_HOME;
663 break;
664 case SDLK_END:
665 keysym = QEMU_KEY_END;
666 break;
667 case SDLK_PAGEUP:
668 keysym = QEMU_KEY_PAGEUP;
669 break;
670 case SDLK_PAGEDOWN:
671 keysym = QEMU_KEY_PAGEDOWN;
672 break;
673 case SDLK_BACKSPACE:
674 keysym = QEMU_KEY_BACKSPACE;
675 break;
676 case SDLK_DELETE:
677 keysym = QEMU_KEY_DELETE;
678 break;
679 default:
680 break;
681 }
682 }
683 if (keysym) {
684 kbd_put_keysym(keysym);
685 } else if (ev->key.keysym.unicode != 0) {
686 kbd_put_keysym(ev->key.keysym.unicode);
687 }
688 }
81c0d5a6 689 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
1ae1caf1
JK
690 sdl_process_key(&ev->key);
691 }
692}
693
8db9bae9 694static void handle_keyup(SDL_Event *ev)
1ae1caf1
JK
695{
696 int mod_state;
697
698 if (!alt_grab) {
699 mod_state = (ev->key.keysym.mod & gui_grab_code);
700 } else {
701 mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
702 }
703 if (!mod_state && gui_key_modifier_pressed) {
704 gui_key_modifier_pressed = 0;
705 if (gui_keysym == 0) {
706 /* exit/enter grab if pressing Ctrl-Alt */
707 if (!gui_grab) {
81c0d5a6 708 if (qemu_console_is_graphic(NULL)) {
1ae1caf1
JK
709 sdl_grab_start();
710 }
711 } else if (!gui_fullscreen) {
712 sdl_grab_end();
713 }
714 /* SDL does not send back all the modifiers key, so we must
715 * correct it. */
716 reset_keys();
717 return;
718 }
719 gui_keysym = 0;
720 }
81c0d5a6 721 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
1ae1caf1
JK
722 sdl_process_key(&ev->key);
723 }
724}
725
8db9bae9 726static void handle_mousemotion(SDL_Event *ev)
1ae1caf1
JK
727{
728 int max_x, max_y;
729
81c0d5a6 730 if (qemu_console_is_graphic(NULL) &&
3ab193e6 731 (qemu_input_is_absolute() || absolute_enabled)) {
1ae1caf1
JK
732 max_x = real_screen->w - 1;
733 max_y = real_screen->h - 1;
734 if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
735 ev->motion.x == max_x || ev->motion.y == max_y)) {
736 sdl_grab_end();
737 }
85f94f86 738 if (!gui_grab &&
1ae1caf1
JK
739 (ev->motion.x > 0 && ev->motion.x < max_x &&
740 ev->motion.y > 0 && ev->motion.y < max_y)) {
741 sdl_grab_start();
742 }
743 }
3ab193e6
GH
744 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
745 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel,
1ae1caf1
JK
746 ev->motion.x, ev->motion.y, ev->motion.state);
747 }
748}
749
8db9bae9 750static void handle_mousebutton(SDL_Event *ev)
1ae1caf1 751{
4c44bdcb 752 int buttonstate = SDL_GetMouseState(NULL, NULL);
1ae1caf1 753 SDL_MouseButtonEvent *bev;
1ae1caf1 754
81c0d5a6 755 if (!qemu_console_is_graphic(NULL)) {
1ae1caf1
JK
756 return;
757 }
758
759 bev = &ev->button;
3ab193e6 760 if (!gui_grab && !qemu_input_is_absolute()) {
822f98d2 761 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
1ae1caf1
JK
762 /* start grabbing all events */
763 sdl_grab_start();
764 }
765 } else {
1ae1caf1
JK
766 if (ev->type == SDL_MOUSEBUTTONDOWN) {
767 buttonstate |= SDL_BUTTON(bev->button);
768 } else {
769 buttonstate &= ~SDL_BUTTON(bev->button);
770 }
3ab193e6 771 sdl_send_mouse_event(0, 0, bev->x, bev->y, buttonstate);
1ae1caf1
JK
772 }
773}
774
8db9bae9 775static void handle_activation(SDL_Event *ev)
1ae1caf1 776{
02df4d6f
JK
777#ifdef _WIN32
778 /* Disable grab if the window no longer has the focus
779 * (Windows-only workaround) */
1ae1caf1
JK
780 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
781 !ev->active.gain && !gui_fullscreen) {
782 sdl_grab_end();
783 }
02df4d6f 784#endif
81c0d5a6 785 if (!gui_grab && ev->active.gain && qemu_console_is_graphic(NULL) &&
3ab193e6 786 (qemu_input_is_absolute() || absolute_enabled)) {
1ae1caf1
JK
787 absolute_mouse_grab();
788 }
789 if (ev->active.state & SDL_APPACTIVE) {
790 if (ev->active.gain) {
791 /* Back to default interval */
0f7b2864 792 update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT);
1ae1caf1 793 } else {
0f7b2864
GH
794 /* Sleeping interval. Not using the long default here as
795 * sdl_refresh does not only update the guest screen, but
796 * also checks for gui events. */
797 update_displaychangelistener(dcl, 500);
1ae1caf1
JK
798 }
799 }
800}
801
bc2ed970 802static void sdl_refresh(DisplayChangeListener *dcl)
1ae1caf1
JK
803{
804 SDL_Event ev1, *ev = &ev1;
3b46e624 805
1354869c
LC
806 if (last_vm_running != runstate_is_running()) {
807 last_vm_running = runstate_is_running();
8a7ddc38
FB
808 sdl_update_caption();
809 }
810
1dbfa005 811 graphic_hw_update(NULL);
81c0d5a6 812 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL));
457831f4 813
0f0b7264
FB
814 while (SDL_PollEvent(ev)) {
815 switch (ev->type) {
816 case SDL_VIDEOEXPOSE:
bc2ed970 817 sdl_update(dcl, 0, 0, real_screen->w, real_screen->h);
0f0b7264
FB
818 break;
819 case SDL_KEYDOWN:
8db9bae9 820 handle_keydown(ev);
1ae1caf1 821 break;
0f0b7264 822 case SDL_KEYUP:
8db9bae9 823 handle_keyup(ev);
0f0b7264
FB
824 break;
825 case SDL_QUIT:
941f511a
JK
826 if (!no_quit) {
827 no_shutdown = 0;
731345e1 828 qemu_system_shutdown_request();
941f511a 829 }
0f0b7264
FB
830 break;
831 case SDL_MOUSEMOTION:
8db9bae9 832 handle_mousemotion(ev);
0f0b7264
FB
833 break;
834 case SDL_MOUSEBUTTONDOWN:
835 case SDL_MOUSEBUTTONUP:
8db9bae9 836 handle_mousebutton(ev);
0f0b7264 837 break;
0294ffb9 838 case SDL_ACTIVEEVENT:
8db9bae9 839 handle_activation(ev);
0294ffb9 840 break;
f9977897 841 case SDL_VIDEORESIZE:
8db9bae9 842 sdl_scale(ev->resize.w, ev->resize.h);
1dbfa005
GH
843 graphic_hw_invalidate(NULL);
844 graphic_hw_update(NULL);
c18a2c36 845 break;
0f0b7264
FB
846 default:
847 break;
848 }
849 }
850}
851
7c20b4a3 852static void sdl_mouse_warp(DisplayChangeListener *dcl,
7c20b4a3 853 int x, int y, int on)
d34cab9f
TS
854{
855 if (on) {
856 if (!guest_cursor)
857 sdl_show_cursor();
3ab193e6 858 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
d34cab9f 859 SDL_SetCursor(guest_sprite);
3ab193e6 860 if (!qemu_input_is_absolute() && !absolute_enabled) {
08a2d4c4 861 SDL_WarpMouse(x, y);
3ab193e6 862 }
d34cab9f
TS
863 }
864 } else if (gui_grab)
865 sdl_hide_cursor();
866 guest_cursor = on;
867 guest_x = x, guest_y = y;
868}
869
7c20b4a3 870static void sdl_mouse_define(DisplayChangeListener *dcl,
7c20b4a3 871 QEMUCursor *c)
d34cab9f 872{
fbe6d7a4
GH
873 uint8_t *image, *mask;
874 int bpl;
875
d34cab9f
TS
876 if (guest_sprite)
877 SDL_FreeCursor(guest_sprite);
878
fbe6d7a4 879 bpl = cursor_get_mono_bpl(c);
7267c094
AL
880 image = g_malloc0(bpl * c->height);
881 mask = g_malloc0(bpl * c->height);
fbe6d7a4
GH
882 cursor_get_mono_image(c, 0x000000, image);
883 cursor_get_mono_mask(c, 0, mask);
884 guest_sprite = SDL_CreateCursor(image, mask, c->width, c->height,
885 c->hot_x, c->hot_y);
7267c094
AL
886 g_free(image);
887 g_free(mask);
d34cab9f
TS
888
889 if (guest_cursor &&
3ab193e6 890 (gui_grab || qemu_input_is_absolute() || absolute_enabled))
d34cab9f
TS
891 SDL_SetCursor(guest_sprite);
892}
893
28695489 894static void sdl_cleanup(void)
898712a8 895{
d34cab9f
TS
896 if (guest_sprite)
897 SDL_FreeCursor(guest_sprite);
d8ee7665 898 SDL_QuitSubSystem(SDL_INIT_VIDEO);
898712a8
FB
899}
900
7c20b4a3 901static const DisplayChangeListenerOps dcl_ops = {
7dd93291
BH
902 .dpy_name = "sdl",
903 .dpy_gfx_update = sdl_update,
904 .dpy_gfx_switch = sdl_switch,
905 .dpy_gfx_check_format = sdl_check_format,
906 .dpy_refresh = sdl_refresh,
907 .dpy_mouse_set = sdl_mouse_warp,
908 .dpy_cursor_define = sdl_mouse_define,
7c20b4a3
GH
909};
910
43523e93 911void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
0f0b7264
FB
912{
913 int flags;
09b26c5e 914 uint8_t data = 0;
7b5d76da 915 const SDL_VideoInfo *vi;
09cec717 916 char *filename;
0f0b7264 917
3d11d0eb
FB
918#if defined(__APPLE__)
919 /* always use generic keymaps */
920 if (!keyboard_layout)
921 keyboard_layout = "en-us";
922#endif
923 if(keyboard_layout) {
0483755a 924 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
3d11d0eb
FB
925 if (!kbd_layout)
926 exit(1);
927 }
928
43523e93
TS
929 if (no_frame)
930 gui_noframe = 1;
931
111f8ec9
JK
932 if (!full_screen) {
933 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
934 }
1de9756b
MT
935#ifdef __linux__
936 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
937 * accessible $DISPLAY to open X11 window. This is often the case
938 * when qemu is run using sudo. But in this case, and when actually
939 * run in X11 environment, SDL fights with X11 for the video card,
940 * making current display unavailable, often until reboot.
941 * So make x11 the default SDL video driver if this variable is unset.
942 * This is a bit hackish but saves us from bigger problem.
943 * Maybe it's a good idea to fix this in SDL instead.
944 */
945 setenv("SDL_VIDEODRIVER", "x11", 0);
946#endif
111f8ec9 947
4e79bcbb
SW
948 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
949 * This requires SDL >= 1.2.14. */
950 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
951
0f0b7264
FB
952 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
953 if (SDL_Init (flags)) {
3caf2562 954 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
955 SDL_GetError());
0f0b7264
FB
956 exit(1);
957 }
7b5d76da 958 vi = SDL_GetVideoInfo();
c18a2c36 959 host_format = *(vi->vfmt);
0ae04d73 960
09cec717
SW
961 /* Load a 32x32x4 image. White pixels are transparent. */
962 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
963 if (filename) {
964 SDL_Surface *image = SDL_LoadBMP(filename);
965 if (image) {
966 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
967 SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
968 SDL_WM_SetIcon(image, NULL);
969 }
7267c094 970 g_free(filename);
09cec717
SW
971 }
972
110defd7
JK
973 if (full_screen) {
974 gui_fullscreen = 1;
975 sdl_grab_start();
976 }
977
7267c094 978 dcl = g_malloc0(sizeof(DisplayChangeListener));
7c20b4a3 979 dcl->ops = &dcl_ops;
5209089f 980 register_displaychangelistener(dcl);
0f0b7264 981
3af12c86
AL
982 mouse_mode_notifier.notify = sdl_mouse_mode_change;
983 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
984
8a7ddc38 985 sdl_update_caption();
0f0b7264
FB
986 SDL_EnableKeyRepeat(250, 50);
987 gui_grab = 0;
898712a8 988
09b26c5e
FB
989 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
990 sdl_cursor_normal = SDL_GetCursor();
991
28695489 992 atexit(sdl_cleanup);
0f0b7264 993}