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