blob: 431e5271195cf205dd7bc327d1c5be951ef55715 [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
34static SDL_Surface *screen;
35static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
bellard8a7ddc32004-03-31 19:00:16 +000036static int last_vm_running;
bellard8e9c4af2004-04-28 19:33:40 +000037static int gui_saved_grab;
38static int gui_fullscreen;
ths43523e92007-02-18 18:19:32 +000039static int gui_noframe;
bellard8e9c4af2004-04-28 19:33:40 +000040static int gui_key_modifier_pressed;
41static int gui_keysym;
bellardd63d3072004-10-03 13:29:03 +000042static int gui_fullscreen_initial_grab;
bellard32ff25b2004-10-03 14:33:54 +000043static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
44static uint8_t modifiers_state[256];
bellard09b26c52006-04-12 21:09:08 +000045static int width, height;
46static SDL_Cursor *sdl_cursor_normal;
47static SDL_Cursor *sdl_cursor_hidden;
48static int absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +000049static int guest_cursor = 0;
50static int guest_x, guest_y;
51static SDL_Cursor *guest_sprite = 0;
bellard0f0b7262003-08-09 18:26:36 +000052
53static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
54{
bellard898712a2004-02-06 19:56:42 +000055 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
bellard0f0b7262003-08-09 18:26:36 +000056 SDL_UpdateRect(screen, x, y, w, h);
57}
58
59static void sdl_resize(DisplayState *ds, int w, int h)
60{
61 int flags;
62
63 // printf("resizing to %d %d\n", w, h);
64
65 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
bellard8e9c4af2004-04-28 19:33:40 +000066 if (gui_fullscreen)
67 flags |= SDL_FULLSCREEN;
ths43523e92007-02-18 18:19:32 +000068 if (gui_noframe)
69 flags |= SDL_NOFRAME;
bellard9903da22005-10-30 23:19:10 +000070
bellard09b26c52006-04-12 21:09:08 +000071 width = w;
72 height = h;
73
bellard9903da22005-10-30 23:19:10 +000074 again:
bellard0f0b7262003-08-09 18:26:36 +000075 screen = SDL_SetVideoMode(w, h, 0, flags);
76 if (!screen) {
77 fprintf(stderr, "Could not open SDL display\n");
78 exit(1);
79 }
bellard9903da22005-10-30 23:19:10 +000080 if (!screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
81 flags &= ~SDL_HWSURFACE;
82 goto again;
83 }
84
85 if (!screen->pixels) {
86 fprintf(stderr, "Could not open SDL display\n");
87 exit(1);
88 }
bellard0f0b7262003-08-09 18:26:36 +000089 ds->data = screen->pixels;
90 ds->linesize = screen->pitch;
91 ds->depth = screen->format->BitsPerPixel;
blueswir1b29169d2007-06-10 16:07:38 +000092 if (screen->format->Bshift > screen->format->Rshift) {
bellardd3079cd2006-05-10 22:17:36 +000093 ds->bgr = 1;
94 } else {
95 ds->bgr = 0;
96 }
bellard457831f2004-07-14 17:22:33 +000097 ds->width = w;
98 ds->height = h;
bellard0f0b7262003-08-09 18:26:36 +000099}
100
bellard3d11d0e2004-12-12 16:56:30 +0000101/* generic keyboard conversion */
bellarde58d12e2004-07-05 22:13:07 +0000102
bellard3d11d0e2004-12-12 16:56:30 +0000103#include "sdl_keysym.h"
104#include "keymaps.c"
bellarde58d12e2004-07-05 22:13:07 +0000105
bellard3d11d0e2004-12-12 16:56:30 +0000106static kbd_layout_t *kbd_layout = NULL;
bellarde58d12e2004-07-05 22:13:07 +0000107
bellard3d11d0e2004-12-12 16:56:30 +0000108static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
bellarde58d12e2004-07-05 22:13:07 +0000109{
bellard3d11d0e2004-12-12 16:56:30 +0000110 int keysym;
111 /* workaround for X11+SDL bug with AltGR */
112 keysym = ev->keysym.sym;
113 if (keysym == 0 && ev->keysym.scancode == 113)
114 keysym = SDLK_MODE;
bellard60659e32006-08-19 14:27:31 +0000115 /* For Japanese key '\' and '|' */
116 if (keysym == 92 && ev->keysym.scancode == 133) {
117 keysym = 0xa5;
118 }
bellard3d11d0e2004-12-12 16:56:30 +0000119 return keysym2scancode(kbd_layout, keysym);
bellarde58d12e2004-07-05 22:13:07 +0000120}
121
bellard3d11d0e2004-12-12 16:56:30 +0000122/* specific keyboard conversions from scan codes */
123
124#if defined(_WIN32)
bellarde58d12e2004-07-05 22:13:07 +0000125
126static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
127{
128 return ev->keysym.scancode;
129}
130
131#else
132
bellarde58d12e2004-07-05 22:13:07 +0000133static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
134{
135 int keycode;
136
137 keycode = ev->keysym.scancode;
138
139 if (keycode < 9) {
140 keycode = 0;
141 } else if (keycode < 97) {
142 keycode -= 8; /* just an offset */
bellard60659e32006-08-19 14:27:31 +0000143 } else if (keycode < 212) {
bellarde58d12e2004-07-05 22:13:07 +0000144 /* use conversion table */
ths6070dd02007-01-24 21:40:21 +0000145 keycode = _translate_keycode(keycode - 97);
bellarde58d12e2004-07-05 22:13:07 +0000146 } else {
147 keycode = 0;
148 }
149 return keycode;
150}
151
152#endif
153
bellard32ff25b2004-10-03 14:33:54 +0000154static void reset_keys(void)
155{
156 int i;
157 for(i = 0; i < 256; i++) {
158 if (modifiers_state[i]) {
159 if (i & 0x80)
160 kbd_put_keycode(0xe0);
161 kbd_put_keycode(i | 0x80);
162 modifiers_state[i] = 0;
163 }
164 }
165}
166
bellard0f0b7262003-08-09 18:26:36 +0000167static void sdl_process_key(SDL_KeyboardEvent *ev)
168{
bellard32ff25b2004-10-03 14:33:54 +0000169 int keycode, v;
bellardde2200d2004-06-04 13:15:06 +0000170
171 if (ev->keysym.sym == SDLK_PAUSE) {
172 /* specific case */
173 v = 0;
174 if (ev->type == SDL_KEYUP)
175 v |= 0x80;
176 kbd_put_keycode(0xe1);
177 kbd_put_keycode(0x1d | v);
178 kbd_put_keycode(0x45 | v);
179 return;
180 }
181
bellard3d11d0e2004-12-12 16:56:30 +0000182 if (kbd_layout) {
183 keycode = sdl_keyevent_to_keycode_generic(ev);
184 } else {
185 keycode = sdl_keyevent_to_keycode(ev);
186 }
bellardde2200d2004-06-04 13:15:06 +0000187
188 switch(keycode) {
189 case 0x00:
190 /* sent when leaving window: reset the modifiers state */
bellard32ff25b2004-10-03 14:33:54 +0000191 reset_keys();
bellardde2200d2004-06-04 13:15:06 +0000192 return;
193 case 0x2a: /* Left Shift */
194 case 0x36: /* Right Shift */
195 case 0x1d: /* Left CTRL */
196 case 0x9d: /* Right CTRL */
197 case 0x38: /* Left ALT */
198 case 0xb8: /* Right ALT */
bellard0f0b7262003-08-09 18:26:36 +0000199 if (ev->type == SDL_KEYUP)
bellardde2200d2004-06-04 13:15:06 +0000200 modifiers_state[keycode] = 0;
201 else
202 modifiers_state[keycode] = 1;
203 break;
204 case 0x45: /* num lock */
205 case 0x3a: /* caps lock */
206 /* SDL does not send the key up event, so we generate it */
207 kbd_put_keycode(keycode);
208 kbd_put_keycode(keycode | 0x80);
209 return;
bellard0f0b7262003-08-09 18:26:36 +0000210 }
bellardde2200d2004-06-04 13:15:06 +0000211
212 /* now send the key code */
213 if (keycode & 0x80)
214 kbd_put_keycode(0xe0);
215 if (ev->type == SDL_KEYUP)
216 kbd_put_keycode(keycode | 0x80);
217 else
218 kbd_put_keycode(keycode & 0x7f);
bellard0f0b7262003-08-09 18:26:36 +0000219}
220
bellard8a7ddc32004-03-31 19:00:16 +0000221static void sdl_update_caption(void)
222{
223 char buf[1024];
thsc35734b2007-03-19 15:17:08 +0000224 const char *status = "";
225
226 if (!vm_running)
227 status = " [Stopped]";
ths3780e192007-06-21 21:08:02 +0000228 else if (gui_grab) {
229 if (!alt_grab)
230 status = " - Press Ctrl-Alt to exit grab";
231 else
232 status = " - Press Ctrl-Alt-Shift to exit grab";
233 }
thsc35734b2007-03-19 15:17:08 +0000234
235 if (qemu_name)
236 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
237 else
238 snprintf(buf, sizeof(buf), "QEMU%s", status);
239
bellard8a7ddc32004-03-31 19:00:16 +0000240 SDL_WM_SetCaption(buf, "QEMU");
241}
242
bellard09b26c52006-04-12 21:09:08 +0000243static void sdl_hide_cursor(void)
244{
balrog9467cd42007-05-01 01:34:14 +0000245 if (!cursor_hide)
246 return;
247
bellard8785a8d2006-06-13 10:49:12 +0000248 if (kbd_mouse_is_absolute()) {
249 SDL_ShowCursor(1);
250 SDL_SetCursor(sdl_cursor_hidden);
251 } else {
252 SDL_ShowCursor(0);
253 }
bellard09b26c52006-04-12 21:09:08 +0000254}
255
256static void sdl_show_cursor(void)
257{
balrog9467cd42007-05-01 01:34:14 +0000258 if (!cursor_hide)
259 return;
260
bellard09b26c52006-04-12 21:09:08 +0000261 if (!kbd_mouse_is_absolute()) {
bellard8785a8d2006-06-13 10:49:12 +0000262 SDL_ShowCursor(1);
thsd34cab92007-04-02 01:10:46 +0000263 if (guest_cursor &&
264 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
265 SDL_SetCursor(guest_sprite);
266 else
267 SDL_SetCursor(sdl_cursor_normal);
bellard09b26c52006-04-12 21:09:08 +0000268 }
269}
270
bellard0f0b7262003-08-09 18:26:36 +0000271static void sdl_grab_start(void)
272{
thsd34cab92007-04-02 01:10:46 +0000273 if (guest_cursor) {
274 SDL_SetCursor(guest_sprite);
275 SDL_WarpMouse(guest_x, guest_y);
276 } else
277 sdl_hide_cursor();
bellard0f0b7262003-08-09 18:26:36 +0000278 SDL_WM_GrabInput(SDL_GRAB_ON);
bellard0f0b7262003-08-09 18:26:36 +0000279 gui_grab = 1;
bellard8a7ddc32004-03-31 19:00:16 +0000280 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000281}
282
283static void sdl_grab_end(void)
284{
bellard0f0b7262003-08-09 18:26:36 +0000285 SDL_WM_GrabInput(SDL_GRAB_OFF);
bellard0f0b7262003-08-09 18:26:36 +0000286 gui_grab = 0;
thsd34cab92007-04-02 01:10:46 +0000287 sdl_show_cursor();
bellard8a7ddc32004-03-31 19:00:16 +0000288 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000289}
290
aurel324c44bdc2008-03-13 19:20:18 +0000291static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
bellard0f0b7262003-08-09 18:26:36 +0000292{
aurel324c44bdc2008-03-13 19:20:18 +0000293 int buttons;
bellard0f0b7262003-08-09 18:26:36 +0000294 buttons = 0;
295 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
296 buttons |= MOUSE_EVENT_LBUTTON;
297 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
298 buttons |= MOUSE_EVENT_RBUTTON;
299 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
300 buttons |= MOUSE_EVENT_MBUTTON;
bellard09b26c52006-04-12 21:09:08 +0000301
302 if (kbd_mouse_is_absolute()) {
303 if (!absolute_enabled) {
304 sdl_hide_cursor();
305 if (gui_grab) {
306 sdl_grab_end();
307 }
308 absolute_enabled = 1;
309 }
310
aurel324c44bdc2008-03-13 19:20:18 +0000311 dx = x * 0x7FFF / (width - 1);
312 dy = y * 0x7FFF / (height - 1);
ths455204e2007-01-05 16:42:13 +0000313 } else if (absolute_enabled) {
314 sdl_show_cursor();
315 absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000316 } else if (guest_cursor) {
aurel324c44bdc2008-03-13 19:20:18 +0000317 x -= guest_x;
318 y -= guest_y;
319 guest_x += x;
320 guest_y += y;
321 dx = x;
322 dy = y;
bellard09b26c52006-04-12 21:09:08 +0000323 }
324
bellard0f0b7262003-08-09 18:26:36 +0000325 kbd_mouse_event(dx, dy, dz, buttons);
326}
327
bellard8e9c4af2004-04-28 19:33:40 +0000328static void toggle_full_screen(DisplayState *ds)
329{
330 gui_fullscreen = !gui_fullscreen;
331 sdl_resize(ds, screen->w, screen->h);
332 if (gui_fullscreen) {
333 gui_saved_grab = gui_grab;
334 sdl_grab_start();
335 } else {
336 if (!gui_saved_grab)
337 sdl_grab_end();
338 }
pbrook95219892006-04-09 01:06:34 +0000339 vga_hw_invalidate();
340 vga_hw_update();
bellard8e9c4af2004-04-28 19:33:40 +0000341}
342
bellard0f0b7262003-08-09 18:26:36 +0000343static void sdl_refresh(DisplayState *ds)
344{
345 SDL_Event ev1, *ev = &ev1;
bellard8e9c4af2004-04-28 19:33:40 +0000346 int mod_state;
aurel324c44bdc2008-03-13 19:20:18 +0000347 int buttonstate = SDL_GetMouseState(NULL, NULL);
ths3b46e622007-09-17 08:09:54 +0000348
bellard8a7ddc32004-03-31 19:00:16 +0000349 if (last_vm_running != vm_running) {
350 last_vm_running = vm_running;
351 sdl_update_caption();
352 }
353
pbrook95219892006-04-09 01:06:34 +0000354 vga_hw_update();
bellard457831f2004-07-14 17:22:33 +0000355
bellard0f0b7262003-08-09 18:26:36 +0000356 while (SDL_PollEvent(ev)) {
357 switch (ev->type) {
358 case SDL_VIDEOEXPOSE:
359 sdl_update(ds, 0, 0, screen->w, screen->h);
360 break;
361 case SDL_KEYDOWN:
362 case SDL_KEYUP:
363 if (ev->type == SDL_KEYDOWN) {
ths3780e192007-06-21 21:08:02 +0000364 if (!alt_grab) {
365 mod_state = (SDL_GetModState() & gui_grab_code) ==
366 gui_grab_code;
367 } else {
368 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
369 (gui_grab_code | KMOD_LSHIFT);
370 }
bellard8e9c4af2004-04-28 19:33:40 +0000371 gui_key_modifier_pressed = mod_state;
bellard457831f2004-07-14 17:22:33 +0000372 if (gui_key_modifier_pressed) {
bellard32ff25b2004-10-03 14:33:54 +0000373 int keycode;
374 keycode = sdl_keyevent_to_keycode(&ev->key);
375 switch(keycode) {
376 case 0x21: /* 'f' key on US keyboard */
bellard457831f2004-07-14 17:22:33 +0000377 toggle_full_screen(ds);
378 gui_keysym = 1;
379 break;
ths5fafdf22007-09-16 21:08:06 +0000380 case 0x02 ... 0x0a: /* '1' to '9' keys */
bellarddfd92d32006-08-17 10:42:46 +0000381 /* Reset the modifiers sent to the current console */
382 reset_keys();
bellard32ff25b2004-10-03 14:33:54 +0000383 console_select(keycode - 0x02);
pbrook95219892006-04-09 01:06:34 +0000384 if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000385 /* display grab if going to a text console */
386 if (gui_grab)
387 sdl_grab_end();
388 }
389 gui_keysym = 1;
390 break;
391 default:
392 break;
393 }
pbrook95219892006-04-09 01:06:34 +0000394 } else if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000395 int keysym;
396 keysym = 0;
397 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
398 switch(ev->key.keysym.sym) {
399 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
400 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
401 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
402 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
403 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
404 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
405 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
406 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
407 default: break;
408 }
409 } else {
410 switch(ev->key.keysym.sym) {
411 case SDLK_UP: keysym = QEMU_KEY_UP; break;
412 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
413 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
414 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
415 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
416 case SDLK_END: keysym = QEMU_KEY_END; break;
417 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
418 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
thse91c8a72007-06-03 13:35:16 +0000419 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
420 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
bellard457831f2004-07-14 17:22:33 +0000421 default: break;
422 }
423 }
424 if (keysym) {
425 kbd_put_keysym(keysym);
426 } else if (ev->key.keysym.unicode != 0) {
427 kbd_put_keysym(ev->key.keysym.unicode);
428 }
bellard8e9c4af2004-04-28 19:33:40 +0000429 }
430 } else if (ev->type == SDL_KEYUP) {
ths3780e192007-06-21 21:08:02 +0000431 if (!alt_grab) {
432 mod_state = (ev->key.keysym.mod & gui_grab_code);
433 } else {
434 mod_state = (ev->key.keysym.mod &
435 (gui_grab_code | KMOD_LSHIFT));
436 }
bellard8e9c4af2004-04-28 19:33:40 +0000437 if (!mod_state) {
438 if (gui_key_modifier_pressed) {
pbrook5b311872006-03-11 20:07:45 +0000439 gui_key_modifier_pressed = 0;
bellard457831f2004-07-14 17:22:33 +0000440 if (gui_keysym == 0) {
bellard32ff25b2004-10-03 14:33:54 +0000441 /* exit/enter grab if pressing Ctrl-Alt */
bellardc66b0d42006-06-13 12:03:53 +0000442 if (!gui_grab) {
443 /* if the application is not active,
444 do not try to enter grab state. It
445 prevents
446 'SDL_WM_GrabInput(SDL_GRAB_ON)'
447 from blocking all the application
448 (SDL bug). */
449 if (SDL_GetAppState() & SDL_APPACTIVE)
450 sdl_grab_start();
451 } else {
bellard8e9c4af2004-04-28 19:33:40 +0000452 sdl_grab_end();
bellardc66b0d42006-06-13 12:03:53 +0000453 }
bellard32ff25b2004-10-03 14:33:54 +0000454 /* SDL does not send back all the
455 modifiers key, so we must correct it */
456 reset_keys();
bellard8e9c4af2004-04-28 19:33:40 +0000457 break;
458 }
bellard8e9c4af2004-04-28 19:33:40 +0000459 gui_keysym = 0;
460 }
bellard0f0b7262003-08-09 18:26:36 +0000461 }
462 }
ths5fafdf22007-09-16 21:08:06 +0000463 if (is_graphic_console() && !gui_keysym)
bellard457831f2004-07-14 17:22:33 +0000464 sdl_process_key(&ev->key);
bellard0f0b7262003-08-09 18:26:36 +0000465 break;
466 case SDL_QUIT:
ths667acca2006-12-11 02:08:05 +0000467 if (!no_quit) {
balrog731345e2007-06-21 17:56:50 +0000468 qemu_system_shutdown_request();
469 vm_start(); /* In case we're paused */
ths667acca2006-12-11 02:08:05 +0000470 }
bellard0f0b7262003-08-09 18:26:36 +0000471 break;
472 case SDL_MOUSEMOTION:
ths455204e2007-01-05 16:42:13 +0000473 if (gui_grab || kbd_mouse_is_absolute() ||
474 absolute_enabled) {
aurel324c44bdc2008-03-13 19:20:18 +0000475 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
476 ev->motion.x, ev->motion.y, ev->motion.state);
bellard0f0b7262003-08-09 18:26:36 +0000477 }
478 break;
479 case SDL_MOUSEBUTTONDOWN:
480 case SDL_MOUSEBUTTONUP:
481 {
482 SDL_MouseButtonEvent *bev = &ev->button;
bellard09b26c52006-04-12 21:09:08 +0000483 if (!gui_grab && !kbd_mouse_is_absolute()) {
bellard0f0b7262003-08-09 18:26:36 +0000484 if (ev->type == SDL_MOUSEBUTTONDOWN &&
aurel324c44bdc2008-03-13 19:20:18 +0000485 (bev->button == SDL_BUTTON_LEFT)) {
bellard0f0b7262003-08-09 18:26:36 +0000486 /* start grabbing all events */
487 sdl_grab_start();
488 }
489 } else {
bellard18a6d282005-01-17 22:32:23 +0000490 int dz;
491 dz = 0;
aurel324c44bdc2008-03-13 19:20:18 +0000492 if (ev->type == SDL_MOUSEBUTTONDOWN) {
493 buttonstate |= SDL_BUTTON(bev->button);
494 } else {
495 buttonstate &= ~SDL_BUTTON(bev->button);
496 }
bellard18a6d282005-01-17 22:32:23 +0000497#ifdef SDL_BUTTON_WHEELUP
bellard09b26c52006-04-12 21:09:08 +0000498 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000499 dz = -1;
bellard09b26c52006-04-12 21:09:08 +0000500 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000501 dz = 1;
502 }
ths3b46e622007-09-17 08:09:54 +0000503#endif
aurel324c44bdc2008-03-13 19:20:18 +0000504 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
bellard0f0b7262003-08-09 18:26:36 +0000505 }
506 }
507 break;
bellard0294ffb2004-04-29 22:15:15 +0000508 case SDL_ACTIVEEVENT:
pbrook5b311872006-03-11 20:07:45 +0000509 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
510 !ev->active.gain && !gui_fullscreen_initial_grab) {
bellard0294ffb2004-04-29 22:15:15 +0000511 sdl_grab_end();
512 }
aurel32f442e082008-03-13 19:20:33 +0000513 if (ev->active.state & SDL_APPACTIVE) {
514 if (ev->active.gain) {
515 /* Back to default interval */
516 ds->gui_timer_interval = 0;
517 } else {
518 /* Sleeping interval */
519 ds->gui_timer_interval = 500;
520 }
521 }
bellard0294ffb2004-04-29 22:15:15 +0000522 break;
bellard0f0b7262003-08-09 18:26:36 +0000523 default:
524 break;
525 }
526 }
527}
528
thsd34cab92007-04-02 01:10:46 +0000529static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
530{
531 SDL_Rect dst = { x, y, w, h };
532 SDL_FillRect(screen, &dst, c);
533}
534
535static void sdl_mouse_warp(int x, int y, int on)
536{
537 if (on) {
538 if (!guest_cursor)
539 sdl_show_cursor();
540 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
541 SDL_SetCursor(guest_sprite);
542 SDL_WarpMouse(x, y);
543 }
544 } else if (gui_grab)
545 sdl_hide_cursor();
546 guest_cursor = on;
547 guest_x = x, guest_y = y;
548}
549
550static void sdl_mouse_define(int width, int height, int bpp,
551 int hot_x, int hot_y,
552 uint8_t *image, uint8_t *mask)
553{
554 uint8_t sprite[256], *line;
555 int x, y, dst, bypl, src = 0;
556 if (guest_sprite)
557 SDL_FreeCursor(guest_sprite);
558
559 memset(sprite, 0, 256);
560 bypl = ((width * bpp + 31) >> 5) << 2;
561 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
562 line = image;
563 for (x = 0; x < width; x ++, dst ++) {
564 switch (bpp) {
565 case 24:
566 src = *(line ++); src |= *(line ++); src |= *(line ++);
567 break;
568 case 16:
569 case 15:
570 src = *(line ++); src |= *(line ++);
571 break;
572 case 8:
573 src = *(line ++);
574 break;
575 case 4:
576 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
577 break;
578 case 2:
579 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
580 break;
581 case 1:
582 src = 1 & (line[x >> 3] >> (x & 7));
583 break;
584 }
585 if (!src)
586 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
587 }
588 }
589 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
590
591 if (guest_cursor &&
592 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
593 SDL_SetCursor(guest_sprite);
594}
595
ths5fafdf22007-09-16 21:08:06 +0000596static void sdl_cleanup(void)
bellard898712a2004-02-06 19:56:42 +0000597{
thsd34cab92007-04-02 01:10:46 +0000598 if (guest_sprite)
599 SDL_FreeCursor(guest_sprite);
bellard898712a2004-02-06 19:56:42 +0000600 SDL_Quit();
601}
602
ths43523e92007-02-18 18:19:32 +0000603void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
bellard0f0b7262003-08-09 18:26:36 +0000604{
605 int flags;
bellard09b26c52006-04-12 21:09:08 +0000606 uint8_t data = 0;
bellard0f0b7262003-08-09 18:26:36 +0000607
bellard3d11d0e2004-12-12 16:56:30 +0000608#if defined(__APPLE__)
609 /* always use generic keymaps */
610 if (!keyboard_layout)
611 keyboard_layout = "en-us";
612#endif
613 if(keyboard_layout) {
614 kbd_layout = init_keyboard_layout(keyboard_layout);
615 if (!kbd_layout)
616 exit(1);
617 }
618
ths43523e92007-02-18 18:19:32 +0000619 if (no_frame)
620 gui_noframe = 1;
621
bellard0f0b7262003-08-09 18:26:36 +0000622 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
623 if (SDL_Init (flags)) {
624 fprintf(stderr, "Could not initialize SDL - exiting\n");
625 exit(1);
626 }
bellard67b915a2004-03-31 23:37:16 +0000627#ifndef _WIN32
bellard0ae04d72003-09-30 21:09:16 +0000628 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
629 signal(SIGINT, SIG_DFL);
630 signal(SIGQUIT, SIG_DFL);
bellard67b915a2004-03-31 23:37:16 +0000631#endif
bellard0ae04d72003-09-30 21:09:16 +0000632
bellard0f0b7262003-08-09 18:26:36 +0000633 ds->dpy_update = sdl_update;
634 ds->dpy_resize = sdl_resize;
635 ds->dpy_refresh = sdl_refresh;
thsd34cab92007-04-02 01:10:46 +0000636 ds->dpy_fill = sdl_fill;
637 ds->mouse_set = sdl_mouse_warp;
638 ds->cursor_define = sdl_mouse_define;
bellard0f0b7262003-08-09 18:26:36 +0000639
640 sdl_resize(ds, 640, 400);
bellard8a7ddc32004-03-31 19:00:16 +0000641 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000642 SDL_EnableKeyRepeat(250, 50);
bellard457831f2004-07-14 17:22:33 +0000643 SDL_EnableUNICODE(1);
bellard0f0b7262003-08-09 18:26:36 +0000644 gui_grab = 0;
bellard898712a2004-02-06 19:56:42 +0000645
bellard09b26c52006-04-12 21:09:08 +0000646 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
647 sdl_cursor_normal = SDL_GetCursor();
648
bellard898712a2004-02-06 19:56:42 +0000649 atexit(sdl_cleanup);
bellardd63d3072004-10-03 13:29:03 +0000650 if (full_screen) {
651 gui_fullscreen = 1;
652 gui_fullscreen_initial_grab = 1;
653 sdl_grab_start();
654 }
bellard0f0b7262003-08-09 18:26:36 +0000655}