blob: 73396e86ad320e9fdf8dd126d80eeab14f221bd7 [file] [log] [blame]
bellard0f0b7262003-08-09 18:26:36 +00001/*
2 * QEMU SDL display driver
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard0f0b7262003-08-09 18:26:36 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard0f0b7262003-08-09 18:26:36 +00006 * 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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "qemu-common.h"
25#include "console.h"
26#include "sysemu.h"
bellard0f0b7262003-08-09 18:26:36 +000027
28#include <SDL.h>
29
bellard67b915a2004-03-31 23:37:16 +000030#ifndef _WIN32
31#include <signal.h>
32#endif
bellard0f0b7262003-08-09 18:26:36 +000033
aliguori7d957bd2009-01-15 22:14:11 +000034static DisplayChangeListener *dcl;
35static SDL_Surface *real_screen;
36static SDL_Surface *guest_screen = NULL;
bellard0f0b7262003-08-09 18:26:36 +000037static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
bellard8a7ddc32004-03-31 19:00:16 +000038static int last_vm_running;
bellard8e9c4af2004-04-28 19:33:40 +000039static int gui_saved_grab;
40static int gui_fullscreen;
ths43523e92007-02-18 18:19:32 +000041static int gui_noframe;
bellard8e9c4af2004-04-28 19:33:40 +000042static int gui_key_modifier_pressed;
43static int gui_keysym;
bellardd63d3072004-10-03 13:29:03 +000044static int gui_fullscreen_initial_grab;
bellard32ff25b2004-10-03 14:33:54 +000045static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
46static uint8_t modifiers_state[256];
bellard09b26c52006-04-12 21:09:08 +000047static int width, height;
48static SDL_Cursor *sdl_cursor_normal;
49static SDL_Cursor *sdl_cursor_hidden;
50static int absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +000051static int guest_cursor = 0;
52static int guest_x, guest_y;
53static SDL_Cursor *guest_sprite = 0;
bellard0f0b7262003-08-09 18:26:36 +000054
55static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
56{
aliguori7d957bd2009-01-15 22:14:11 +000057 SDL_Rect rec;
58 rec.x = x;
59 rec.y = y;
60 rec.w = w;
61 rec.h = h;
bellard898712a2004-02-06 19:56:42 +000062 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
aliguori7d957bd2009-01-15 22:14:11 +000063
64 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
65 SDL_Flip(real_screen);
bellard0f0b7262003-08-09 18:26:36 +000066}
67
aliguori7d957bd2009-01-15 22:14:11 +000068static void sdl_setdata(DisplayState *ds)
69{
70 SDL_Rect rec;
71 rec.x = 0;
72 rec.y = 0;
73 rec.w = real_screen->w;
74 rec.h = real_screen->h;
75
76 if (guest_screen != NULL) SDL_FreeSurface(guest_screen);
77
78 guest_screen = SDL_CreateRGBSurfaceFrom(ds_get_data(ds), ds_get_width(ds), ds_get_height(ds),
79 ds_get_bits_per_pixel(ds), ds_get_linesize(ds),
80 ds->surface->pf.rmask, ds->surface->pf.gmask,
81 ds->surface->pf.bmask, ds->surface->pf.amask);
82}
83
84static void sdl_resize(DisplayState *ds)
bellard0f0b7262003-08-09 18:26:36 +000085{
86 int flags;
87
88 // printf("resizing to %d %d\n", w, h);
89
90 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
bellard8e9c4af2004-04-28 19:33:40 +000091 if (gui_fullscreen)
92 flags |= SDL_FULLSCREEN;
ths43523e92007-02-18 18:19:32 +000093 if (gui_noframe)
94 flags |= SDL_NOFRAME;
bellard9903da22005-10-30 23:19:10 +000095
96 again:
aliguori7d957bd2009-01-15 22:14:11 +000097 real_screen = SDL_SetVideoMode(ds_get_width(ds), ds_get_height(ds), 0, flags);
98 if (!real_screen) {
bellard0f0b7262003-08-09 18:26:36 +000099 fprintf(stderr, "Could not open SDL display\n");
100 exit(1);
101 }
aliguori7d957bd2009-01-15 22:14:11 +0000102 if (!real_screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
bellard9903da22005-10-30 23:19:10 +0000103 flags &= ~SDL_HWSURFACE;
104 goto again;
105 }
106
aliguori7d957bd2009-01-15 22:14:11 +0000107 if (!real_screen->pixels) {
bellard9903da22005-10-30 23:19:10 +0000108 fprintf(stderr, "Could not open SDL display\n");
109 exit(1);
110 }
blueswir1749ecd92008-07-24 11:25:30 +0000111
aliguori7d957bd2009-01-15 22:14:11 +0000112 sdl_setdata(ds);
bellard0f0b7262003-08-09 18:26:36 +0000113}
114
bellard3d11d0e2004-12-12 16:56:30 +0000115/* generic keyboard conversion */
bellarde58d12e2004-07-05 22:13:07 +0000116
bellard3d11d0e2004-12-12 16:56:30 +0000117#include "sdl_keysym.h"
118#include "keymaps.c"
bellarde58d12e2004-07-05 22:13:07 +0000119
bellard3d11d0e2004-12-12 16:56:30 +0000120static kbd_layout_t *kbd_layout = NULL;
bellarde58d12e2004-07-05 22:13:07 +0000121
bellard3d11d0e2004-12-12 16:56:30 +0000122static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
bellarde58d12e2004-07-05 22:13:07 +0000123{
bellard3d11d0e2004-12-12 16:56:30 +0000124 int keysym;
125 /* workaround for X11+SDL bug with AltGR */
126 keysym = ev->keysym.sym;
127 if (keysym == 0 && ev->keysym.scancode == 113)
128 keysym = SDLK_MODE;
bellard60659e32006-08-19 14:27:31 +0000129 /* For Japanese key '\' and '|' */
130 if (keysym == 92 && ev->keysym.scancode == 133) {
131 keysym = 0xa5;
132 }
bellard3d11d0e2004-12-12 16:56:30 +0000133 return keysym2scancode(kbd_layout, keysym);
bellarde58d12e2004-07-05 22:13:07 +0000134}
135
bellard3d11d0e2004-12-12 16:56:30 +0000136/* specific keyboard conversions from scan codes */
137
138#if defined(_WIN32)
bellarde58d12e2004-07-05 22:13:07 +0000139
140static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
141{
142 return ev->keysym.scancode;
143}
144
145#else
146
bellarde58d12e2004-07-05 22:13:07 +0000147static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
148{
149 int keycode;
150
151 keycode = ev->keysym.scancode;
152
153 if (keycode < 9) {
154 keycode = 0;
155 } else if (keycode < 97) {
156 keycode -= 8; /* just an offset */
bellard60659e32006-08-19 14:27:31 +0000157 } else if (keycode < 212) {
bellarde58d12e2004-07-05 22:13:07 +0000158 /* use conversion table */
ths6070dd02007-01-24 21:40:21 +0000159 keycode = _translate_keycode(keycode - 97);
bellarde58d12e2004-07-05 22:13:07 +0000160 } else {
161 keycode = 0;
162 }
163 return keycode;
164}
165
166#endif
167
bellard32ff25b2004-10-03 14:33:54 +0000168static void reset_keys(void)
169{
170 int i;
171 for(i = 0; i < 256; i++) {
172 if (modifiers_state[i]) {
173 if (i & 0x80)
174 kbd_put_keycode(0xe0);
175 kbd_put_keycode(i | 0x80);
176 modifiers_state[i] = 0;
177 }
178 }
179}
180
bellard0f0b7262003-08-09 18:26:36 +0000181static void sdl_process_key(SDL_KeyboardEvent *ev)
182{
bellard32ff25b2004-10-03 14:33:54 +0000183 int keycode, v;
bellardde2200d2004-06-04 13:15:06 +0000184
185 if (ev->keysym.sym == SDLK_PAUSE) {
186 /* specific case */
187 v = 0;
188 if (ev->type == SDL_KEYUP)
189 v |= 0x80;
190 kbd_put_keycode(0xe1);
191 kbd_put_keycode(0x1d | v);
192 kbd_put_keycode(0x45 | v);
193 return;
194 }
195
bellard3d11d0e2004-12-12 16:56:30 +0000196 if (kbd_layout) {
197 keycode = sdl_keyevent_to_keycode_generic(ev);
198 } else {
199 keycode = sdl_keyevent_to_keycode(ev);
200 }
bellardde2200d2004-06-04 13:15:06 +0000201
202 switch(keycode) {
203 case 0x00:
204 /* sent when leaving window: reset the modifiers state */
bellard32ff25b2004-10-03 14:33:54 +0000205 reset_keys();
bellardde2200d2004-06-04 13:15:06 +0000206 return;
207 case 0x2a: /* Left Shift */
208 case 0x36: /* Right Shift */
209 case 0x1d: /* Left CTRL */
210 case 0x9d: /* Right CTRL */
211 case 0x38: /* Left ALT */
212 case 0xb8: /* Right ALT */
bellard0f0b7262003-08-09 18:26:36 +0000213 if (ev->type == SDL_KEYUP)
bellardde2200d2004-06-04 13:15:06 +0000214 modifiers_state[keycode] = 0;
215 else
216 modifiers_state[keycode] = 1;
217 break;
218 case 0x45: /* num lock */
219 case 0x3a: /* caps lock */
220 /* SDL does not send the key up event, so we generate it */
221 kbd_put_keycode(keycode);
222 kbd_put_keycode(keycode | 0x80);
223 return;
bellard0f0b7262003-08-09 18:26:36 +0000224 }
bellardde2200d2004-06-04 13:15:06 +0000225
226 /* now send the key code */
227 if (keycode & 0x80)
228 kbd_put_keycode(0xe0);
229 if (ev->type == SDL_KEYUP)
230 kbd_put_keycode(keycode | 0x80);
231 else
232 kbd_put_keycode(keycode & 0x7f);
bellard0f0b7262003-08-09 18:26:36 +0000233}
234
bellard8a7ddc32004-03-31 19:00:16 +0000235static void sdl_update_caption(void)
236{
237 char buf[1024];
thsc35734b2007-03-19 15:17:08 +0000238 const char *status = "";
239
240 if (!vm_running)
241 status = " [Stopped]";
ths3780e192007-06-21 21:08:02 +0000242 else if (gui_grab) {
243 if (!alt_grab)
244 status = " - Press Ctrl-Alt to exit grab";
245 else
246 status = " - Press Ctrl-Alt-Shift to exit grab";
247 }
thsc35734b2007-03-19 15:17:08 +0000248
249 if (qemu_name)
250 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
251 else
252 snprintf(buf, sizeof(buf), "QEMU%s", status);
253
bellard8a7ddc32004-03-31 19:00:16 +0000254 SDL_WM_SetCaption(buf, "QEMU");
255}
256
bellard09b26c52006-04-12 21:09:08 +0000257static void sdl_hide_cursor(void)
258{
balrog9467cd42007-05-01 01:34:14 +0000259 if (!cursor_hide)
260 return;
261
bellard8785a8d2006-06-13 10:49:12 +0000262 if (kbd_mouse_is_absolute()) {
263 SDL_ShowCursor(1);
264 SDL_SetCursor(sdl_cursor_hidden);
265 } else {
266 SDL_ShowCursor(0);
267 }
bellard09b26c52006-04-12 21:09:08 +0000268}
269
270static void sdl_show_cursor(void)
271{
balrog9467cd42007-05-01 01:34:14 +0000272 if (!cursor_hide)
273 return;
274
bellard09b26c52006-04-12 21:09:08 +0000275 if (!kbd_mouse_is_absolute()) {
bellard8785a8d2006-06-13 10:49:12 +0000276 SDL_ShowCursor(1);
thsd34cab92007-04-02 01:10:46 +0000277 if (guest_cursor &&
278 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
279 SDL_SetCursor(guest_sprite);
280 else
281 SDL_SetCursor(sdl_cursor_normal);
bellard09b26c52006-04-12 21:09:08 +0000282 }
283}
284
bellard0f0b7262003-08-09 18:26:36 +0000285static void sdl_grab_start(void)
286{
thsd34cab92007-04-02 01:10:46 +0000287 if (guest_cursor) {
288 SDL_SetCursor(guest_sprite);
289 SDL_WarpMouse(guest_x, guest_y);
290 } else
291 sdl_hide_cursor();
aliguori6bb81602009-01-15 20:47:45 +0000292
293 if (SDL_WM_GrabInput(SDL_GRAB_ON) == SDL_GRAB_ON) {
294 gui_grab = 1;
295 sdl_update_caption();
296 } else
297 sdl_show_cursor();
bellard0f0b7262003-08-09 18:26:36 +0000298}
299
300static void sdl_grab_end(void)
301{
bellard0f0b7262003-08-09 18:26:36 +0000302 SDL_WM_GrabInput(SDL_GRAB_OFF);
bellard0f0b7262003-08-09 18:26:36 +0000303 gui_grab = 0;
thsd34cab92007-04-02 01:10:46 +0000304 sdl_show_cursor();
bellard8a7ddc32004-03-31 19:00:16 +0000305 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000306}
307
aurel324c44bdc2008-03-13 19:20:18 +0000308static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
bellard0f0b7262003-08-09 18:26:36 +0000309{
aurel324c44bdc2008-03-13 19:20:18 +0000310 int buttons;
bellard0f0b7262003-08-09 18:26:36 +0000311 buttons = 0;
312 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
313 buttons |= MOUSE_EVENT_LBUTTON;
314 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
315 buttons |= MOUSE_EVENT_RBUTTON;
316 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
317 buttons |= MOUSE_EVENT_MBUTTON;
bellard09b26c52006-04-12 21:09:08 +0000318
319 if (kbd_mouse_is_absolute()) {
320 if (!absolute_enabled) {
321 sdl_hide_cursor();
322 if (gui_grab) {
323 sdl_grab_end();
324 }
325 absolute_enabled = 1;
326 }
327
aurel324c44bdc2008-03-13 19:20:18 +0000328 dx = x * 0x7FFF / (width - 1);
329 dy = y * 0x7FFF / (height - 1);
ths455204e2007-01-05 16:42:13 +0000330 } else if (absolute_enabled) {
331 sdl_show_cursor();
332 absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000333 } else if (guest_cursor) {
aurel324c44bdc2008-03-13 19:20:18 +0000334 x -= guest_x;
335 y -= guest_y;
336 guest_x += x;
337 guest_y += y;
338 dx = x;
339 dy = y;
bellard09b26c52006-04-12 21:09:08 +0000340 }
341
bellard0f0b7262003-08-09 18:26:36 +0000342 kbd_mouse_event(dx, dy, dz, buttons);
343}
344
bellard8e9c4af2004-04-28 19:33:40 +0000345static void toggle_full_screen(DisplayState *ds)
346{
347 gui_fullscreen = !gui_fullscreen;
aliguori7d957bd2009-01-15 22:14:11 +0000348 sdl_resize(ds);
bellard8e9c4af2004-04-28 19:33:40 +0000349 if (gui_fullscreen) {
350 gui_saved_grab = gui_grab;
351 sdl_grab_start();
352 } else {
353 if (!gui_saved_grab)
354 sdl_grab_end();
355 }
pbrook95219892006-04-09 01:06:34 +0000356 vga_hw_invalidate();
357 vga_hw_update();
bellard8e9c4af2004-04-28 19:33:40 +0000358}
359
bellard0f0b7262003-08-09 18:26:36 +0000360static void sdl_refresh(DisplayState *ds)
361{
362 SDL_Event ev1, *ev = &ev1;
bellard8e9c4af2004-04-28 19:33:40 +0000363 int mod_state;
aurel324c44bdc2008-03-13 19:20:18 +0000364 int buttonstate = SDL_GetMouseState(NULL, NULL);
ths3b46e622007-09-17 08:09:54 +0000365
bellard8a7ddc32004-03-31 19:00:16 +0000366 if (last_vm_running != vm_running) {
367 last_vm_running = vm_running;
368 sdl_update_caption();
369 }
370
pbrook95219892006-04-09 01:06:34 +0000371 vga_hw_update();
aurel323bee8bd2008-04-13 16:08:37 +0000372 SDL_EnableUNICODE(!is_graphic_console());
bellard457831f2004-07-14 17:22:33 +0000373
bellard0f0b7262003-08-09 18:26:36 +0000374 while (SDL_PollEvent(ev)) {
375 switch (ev->type) {
376 case SDL_VIDEOEXPOSE:
aliguori7d957bd2009-01-15 22:14:11 +0000377 sdl_update(ds, 0, 0, real_screen->w, real_screen->h);
bellard0f0b7262003-08-09 18:26:36 +0000378 break;
379 case SDL_KEYDOWN:
380 case SDL_KEYUP:
381 if (ev->type == SDL_KEYDOWN) {
ths3780e192007-06-21 21:08:02 +0000382 if (!alt_grab) {
383 mod_state = (SDL_GetModState() & gui_grab_code) ==
384 gui_grab_code;
385 } else {
386 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
387 (gui_grab_code | KMOD_LSHIFT);
388 }
bellard8e9c4af2004-04-28 19:33:40 +0000389 gui_key_modifier_pressed = mod_state;
bellard457831f2004-07-14 17:22:33 +0000390 if (gui_key_modifier_pressed) {
bellard32ff25b2004-10-03 14:33:54 +0000391 int keycode;
392 keycode = sdl_keyevent_to_keycode(&ev->key);
393 switch(keycode) {
394 case 0x21: /* 'f' key on US keyboard */
bellard457831f2004-07-14 17:22:33 +0000395 toggle_full_screen(ds);
396 gui_keysym = 1;
397 break;
ths5fafdf22007-09-16 21:08:06 +0000398 case 0x02 ... 0x0a: /* '1' to '9' keys */
bellarddfd92d32006-08-17 10:42:46 +0000399 /* Reset the modifiers sent to the current console */
400 reset_keys();
bellard32ff25b2004-10-03 14:33:54 +0000401 console_select(keycode - 0x02);
pbrook95219892006-04-09 01:06:34 +0000402 if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000403 /* display grab if going to a text console */
404 if (gui_grab)
405 sdl_grab_end();
406 }
407 gui_keysym = 1;
408 break;
409 default:
410 break;
411 }
pbrook95219892006-04-09 01:06:34 +0000412 } else if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000413 int keysym;
414 keysym = 0;
415 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
416 switch(ev->key.keysym.sym) {
417 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
418 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
419 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
420 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
421 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
422 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
423 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
424 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
425 default: break;
426 }
427 } else {
428 switch(ev->key.keysym.sym) {
429 case SDLK_UP: keysym = QEMU_KEY_UP; break;
430 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
431 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
432 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
433 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
434 case SDLK_END: keysym = QEMU_KEY_END; break;
435 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
436 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
thse91c8a72007-06-03 13:35:16 +0000437 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
438 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
bellard457831f2004-07-14 17:22:33 +0000439 default: break;
440 }
441 }
442 if (keysym) {
443 kbd_put_keysym(keysym);
444 } else if (ev->key.keysym.unicode != 0) {
445 kbd_put_keysym(ev->key.keysym.unicode);
446 }
bellard8e9c4af2004-04-28 19:33:40 +0000447 }
448 } else if (ev->type == SDL_KEYUP) {
ths3780e192007-06-21 21:08:02 +0000449 if (!alt_grab) {
450 mod_state = (ev->key.keysym.mod & gui_grab_code);
451 } else {
452 mod_state = (ev->key.keysym.mod &
453 (gui_grab_code | KMOD_LSHIFT));
454 }
bellard8e9c4af2004-04-28 19:33:40 +0000455 if (!mod_state) {
456 if (gui_key_modifier_pressed) {
pbrook5b311872006-03-11 20:07:45 +0000457 gui_key_modifier_pressed = 0;
bellard457831f2004-07-14 17:22:33 +0000458 if (gui_keysym == 0) {
bellard32ff25b2004-10-03 14:33:54 +0000459 /* exit/enter grab if pressing Ctrl-Alt */
bellardc66b0d42006-06-13 12:03:53 +0000460 if (!gui_grab) {
461 /* if the application is not active,
462 do not try to enter grab state. It
463 prevents
464 'SDL_WM_GrabInput(SDL_GRAB_ON)'
465 from blocking all the application
466 (SDL bug). */
467 if (SDL_GetAppState() & SDL_APPACTIVE)
468 sdl_grab_start();
469 } else {
bellard8e9c4af2004-04-28 19:33:40 +0000470 sdl_grab_end();
bellardc66b0d42006-06-13 12:03:53 +0000471 }
bellard32ff25b2004-10-03 14:33:54 +0000472 /* SDL does not send back all the
473 modifiers key, so we must correct it */
474 reset_keys();
bellard8e9c4af2004-04-28 19:33:40 +0000475 break;
476 }
bellard8e9c4af2004-04-28 19:33:40 +0000477 gui_keysym = 0;
478 }
bellard0f0b7262003-08-09 18:26:36 +0000479 }
480 }
ths5fafdf22007-09-16 21:08:06 +0000481 if (is_graphic_console() && !gui_keysym)
bellard457831f2004-07-14 17:22:33 +0000482 sdl_process_key(&ev->key);
bellard0f0b7262003-08-09 18:26:36 +0000483 break;
484 case SDL_QUIT:
aliguori5b08fc12008-08-21 20:08:03 +0000485 if (!no_quit)
balrog731345e2007-06-21 17:56:50 +0000486 qemu_system_shutdown_request();
bellard0f0b7262003-08-09 18:26:36 +0000487 break;
488 case SDL_MOUSEMOTION:
ths455204e2007-01-05 16:42:13 +0000489 if (gui_grab || kbd_mouse_is_absolute() ||
490 absolute_enabled) {
aurel324c44bdc2008-03-13 19:20:18 +0000491 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
492 ev->motion.x, ev->motion.y, ev->motion.state);
bellard0f0b7262003-08-09 18:26:36 +0000493 }
494 break;
495 case SDL_MOUSEBUTTONDOWN:
496 case SDL_MOUSEBUTTONUP:
497 {
498 SDL_MouseButtonEvent *bev = &ev->button;
bellard09b26c52006-04-12 21:09:08 +0000499 if (!gui_grab && !kbd_mouse_is_absolute()) {
bellard0f0b7262003-08-09 18:26:36 +0000500 if (ev->type == SDL_MOUSEBUTTONDOWN &&
aurel324c44bdc2008-03-13 19:20:18 +0000501 (bev->button == SDL_BUTTON_LEFT)) {
bellard0f0b7262003-08-09 18:26:36 +0000502 /* start grabbing all events */
503 sdl_grab_start();
504 }
505 } else {
bellard18a6d282005-01-17 22:32:23 +0000506 int dz;
507 dz = 0;
aurel324c44bdc2008-03-13 19:20:18 +0000508 if (ev->type == SDL_MOUSEBUTTONDOWN) {
509 buttonstate |= SDL_BUTTON(bev->button);
510 } else {
511 buttonstate &= ~SDL_BUTTON(bev->button);
512 }
bellard18a6d282005-01-17 22:32:23 +0000513#ifdef SDL_BUTTON_WHEELUP
bellard09b26c52006-04-12 21:09:08 +0000514 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000515 dz = -1;
bellard09b26c52006-04-12 21:09:08 +0000516 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000517 dz = 1;
518 }
ths3b46e622007-09-17 08:09:54 +0000519#endif
aurel324c44bdc2008-03-13 19:20:18 +0000520 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
bellard0f0b7262003-08-09 18:26:36 +0000521 }
522 }
523 break;
bellard0294ffb2004-04-29 22:15:15 +0000524 case SDL_ACTIVEEVENT:
pbrook5b311872006-03-11 20:07:45 +0000525 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
526 !ev->active.gain && !gui_fullscreen_initial_grab) {
bellard0294ffb2004-04-29 22:15:15 +0000527 sdl_grab_end();
528 }
aurel32f442e082008-03-13 19:20:33 +0000529 if (ev->active.state & SDL_APPACTIVE) {
530 if (ev->active.gain) {
531 /* Back to default interval */
aliguori7d957bd2009-01-15 22:14:11 +0000532 dcl->gui_timer_interval = 0;
533 dcl->idle = 0;
aurel32f442e082008-03-13 19:20:33 +0000534 } else {
535 /* Sleeping interval */
aliguori7d957bd2009-01-15 22:14:11 +0000536 dcl->gui_timer_interval = 500;
537 dcl->idle = 1;
aurel32f442e082008-03-13 19:20:33 +0000538 }
539 }
bellard0294ffb2004-04-29 22:15:15 +0000540 break;
bellard0f0b7262003-08-09 18:26:36 +0000541 default:
542 break;
543 }
544 }
545}
546
thsd34cab92007-04-02 01:10:46 +0000547static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
548{
549 SDL_Rect dst = { x, y, w, h };
aliguori7d957bd2009-01-15 22:14:11 +0000550 SDL_FillRect(real_screen, &dst, c);
thsd34cab92007-04-02 01:10:46 +0000551}
552
553static void sdl_mouse_warp(int x, int y, int on)
554{
555 if (on) {
556 if (!guest_cursor)
557 sdl_show_cursor();
558 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
559 SDL_SetCursor(guest_sprite);
560 SDL_WarpMouse(x, y);
561 }
562 } else if (gui_grab)
563 sdl_hide_cursor();
564 guest_cursor = on;
565 guest_x = x, guest_y = y;
566}
567
568static void sdl_mouse_define(int width, int height, int bpp,
569 int hot_x, int hot_y,
570 uint8_t *image, uint8_t *mask)
571{
572 uint8_t sprite[256], *line;
573 int x, y, dst, bypl, src = 0;
574 if (guest_sprite)
575 SDL_FreeCursor(guest_sprite);
576
577 memset(sprite, 0, 256);
578 bypl = ((width * bpp + 31) >> 5) << 2;
579 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
580 line = image;
581 for (x = 0; x < width; x ++, dst ++) {
582 switch (bpp) {
583 case 24:
584 src = *(line ++); src |= *(line ++); src |= *(line ++);
585 break;
586 case 16:
587 case 15:
588 src = *(line ++); src |= *(line ++);
589 break;
590 case 8:
591 src = *(line ++);
592 break;
593 case 4:
594 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
595 break;
596 case 2:
597 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
598 break;
599 case 1:
600 src = 1 & (line[x >> 3] >> (x & 7));
601 break;
602 }
603 if (!src)
604 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
605 }
606 }
607 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
608
609 if (guest_cursor &&
610 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
611 SDL_SetCursor(guest_sprite);
612}
613
ths5fafdf22007-09-16 21:08:06 +0000614static void sdl_cleanup(void)
bellard898712a2004-02-06 19:56:42 +0000615{
thsd34cab92007-04-02 01:10:46 +0000616 if (guest_sprite)
617 SDL_FreeCursor(guest_sprite);
bellard898712a2004-02-06 19:56:42 +0000618 SDL_Quit();
619}
620
ths43523e92007-02-18 18:19:32 +0000621void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
bellard0f0b7262003-08-09 18:26:36 +0000622{
623 int flags;
bellard09b26c52006-04-12 21:09:08 +0000624 uint8_t data = 0;
bellard0f0b7262003-08-09 18:26:36 +0000625
bellard3d11d0e2004-12-12 16:56:30 +0000626#if defined(__APPLE__)
627 /* always use generic keymaps */
628 if (!keyboard_layout)
629 keyboard_layout = "en-us";
630#endif
631 if(keyboard_layout) {
632 kbd_layout = init_keyboard_layout(keyboard_layout);
633 if (!kbd_layout)
634 exit(1);
635 }
636
ths43523e92007-02-18 18:19:32 +0000637 if (no_frame)
638 gui_noframe = 1;
639
bellard0f0b7262003-08-09 18:26:36 +0000640 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
641 if (SDL_Init (flags)) {
642 fprintf(stderr, "Could not initialize SDL - exiting\n");
643 exit(1);
644 }
bellard0ae04d72003-09-30 21:09:16 +0000645
aliguori7d957bd2009-01-15 22:14:11 +0000646 dcl = qemu_mallocz(sizeof(DisplayChangeListener));
647 if (!dcl)
648 exit(1);
649 dcl->dpy_update = sdl_update;
650 dcl->dpy_resize = sdl_resize;
651 dcl->dpy_refresh = sdl_refresh;
652 dcl->dpy_setdata = sdl_setdata;
653 dcl->dpy_fill = sdl_fill;
thsd34cab92007-04-02 01:10:46 +0000654 ds->mouse_set = sdl_mouse_warp;
655 ds->cursor_define = sdl_mouse_define;
aliguori7d957bd2009-01-15 22:14:11 +0000656 register_displaychangelistener(ds, dcl);
bellard0f0b7262003-08-09 18:26:36 +0000657
bellard8a7ddc32004-03-31 19:00:16 +0000658 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000659 SDL_EnableKeyRepeat(250, 50);
660 gui_grab = 0;
bellard898712a2004-02-06 19:56:42 +0000661
bellard09b26c52006-04-12 21:09:08 +0000662 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
663 sdl_cursor_normal = SDL_GetCursor();
664
bellard898712a2004-02-06 19:56:42 +0000665 atexit(sdl_cleanup);
bellardd63d3072004-10-03 13:29:03 +0000666 if (full_screen) {
667 gui_fullscreen = 1;
668 gui_fullscreen_initial_grab = 1;
669 sdl_grab_start();
670 }
bellard0f0b7262003-08-09 18:26:36 +0000671}