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