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