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