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