blob: a8dbc2c735617c5baab7ead70a079e20e6b71077 [file] [log] [blame]
bellard0f0b7262003-08-09 18:26:36 +00001/*
2 * QEMU SDL display driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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 */
bellard67b915a2004-03-31 23:37:16 +000024#include "vl.h"
bellard0f0b7262003-08-09 18:26:36 +000025
26#include <SDL.h>
27
bellard67b915a2004-03-31 23:37:16 +000028#ifndef _WIN32
29#include <signal.h>
30#endif
bellard0f0b7262003-08-09 18:26:36 +000031
32static SDL_Surface *screen;
33static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
bellard8a7ddc32004-03-31 19:00:16 +000034static int last_vm_running;
bellard8e9c4af2004-04-28 19:33:40 +000035static int gui_saved_grab;
36static int gui_fullscreen;
ths43523e92007-02-18 18:19:32 +000037static int gui_noframe;
bellard8e9c4af2004-04-28 19:33:40 +000038static int gui_key_modifier_pressed;
39static int gui_keysym;
bellardd63d3072004-10-03 13:29:03 +000040static int gui_fullscreen_initial_grab;
bellard32ff25b2004-10-03 14:33:54 +000041static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
42static uint8_t modifiers_state[256];
bellard09b26c52006-04-12 21:09:08 +000043static int width, height;
44static SDL_Cursor *sdl_cursor_normal;
45static SDL_Cursor *sdl_cursor_hidden;
46static int absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +000047static int guest_cursor = 0;
48static int guest_x, guest_y;
49static SDL_Cursor *guest_sprite = 0;
bellard0f0b7262003-08-09 18:26:36 +000050
51static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
52{
bellard898712a2004-02-06 19:56:42 +000053 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
bellard0f0b7262003-08-09 18:26:36 +000054 SDL_UpdateRect(screen, x, y, w, h);
55}
56
57static void sdl_resize(DisplayState *ds, int w, int h)
58{
59 int flags;
60
61 // printf("resizing to %d %d\n", w, h);
62
63 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
bellard8e9c4af2004-04-28 19:33:40 +000064 if (gui_fullscreen)
65 flags |= SDL_FULLSCREEN;
ths43523e92007-02-18 18:19:32 +000066 if (gui_noframe)
67 flags |= SDL_NOFRAME;
bellard9903da22005-10-30 23:19:10 +000068
bellard09b26c52006-04-12 21:09:08 +000069 width = w;
70 height = h;
71
bellard9903da22005-10-30 23:19:10 +000072 again:
bellard0f0b7262003-08-09 18:26:36 +000073 screen = SDL_SetVideoMode(w, h, 0, flags);
74 if (!screen) {
75 fprintf(stderr, "Could not open SDL display\n");
76 exit(1);
77 }
bellard9903da22005-10-30 23:19:10 +000078 if (!screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
79 flags &= ~SDL_HWSURFACE;
80 goto again;
81 }
82
83 if (!screen->pixels) {
84 fprintf(stderr, "Could not open SDL display\n");
85 exit(1);
86 }
bellard0f0b7262003-08-09 18:26:36 +000087 ds->data = screen->pixels;
88 ds->linesize = screen->pitch;
89 ds->depth = screen->format->BitsPerPixel;
bellardd3079cd2006-05-10 22:17:36 +000090 if (ds->depth == 32 && screen->format->Rshift == 0) {
91 ds->bgr = 1;
92 } else {
93 ds->bgr = 0;
94 }
bellard457831f2004-07-14 17:22:33 +000095 ds->width = w;
96 ds->height = h;
bellard0f0b7262003-08-09 18:26:36 +000097}
98
bellard3d11d0e2004-12-12 16:56:30 +000099/* generic keyboard conversion */
bellarde58d12e2004-07-05 22:13:07 +0000100
bellard3d11d0e2004-12-12 16:56:30 +0000101#include "sdl_keysym.h"
102#include "keymaps.c"
bellarde58d12e2004-07-05 22:13:07 +0000103
bellard3d11d0e2004-12-12 16:56:30 +0000104static kbd_layout_t *kbd_layout = NULL;
bellarde58d12e2004-07-05 22:13:07 +0000105
bellard3d11d0e2004-12-12 16:56:30 +0000106static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
bellarde58d12e2004-07-05 22:13:07 +0000107{
bellard3d11d0e2004-12-12 16:56:30 +0000108 int keysym;
109 /* workaround for X11+SDL bug with AltGR */
110 keysym = ev->keysym.sym;
111 if (keysym == 0 && ev->keysym.scancode == 113)
112 keysym = SDLK_MODE;
bellard60659e32006-08-19 14:27:31 +0000113 /* For Japanese key '\' and '|' */
114 if (keysym == 92 && ev->keysym.scancode == 133) {
115 keysym = 0xa5;
116 }
bellard3d11d0e2004-12-12 16:56:30 +0000117 return keysym2scancode(kbd_layout, keysym);
bellarde58d12e2004-07-05 22:13:07 +0000118}
119
bellard3d11d0e2004-12-12 16:56:30 +0000120/* specific keyboard conversions from scan codes */
121
122#if defined(_WIN32)
bellarde58d12e2004-07-05 22:13:07 +0000123
124static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
125{
126 return ev->keysym.scancode;
127}
128
129#else
130
bellarde58d12e2004-07-05 22:13:07 +0000131static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
132{
133 int keycode;
134
135 keycode = ev->keysym.scancode;
136
137 if (keycode < 9) {
138 keycode = 0;
139 } else if (keycode < 97) {
140 keycode -= 8; /* just an offset */
bellard60659e32006-08-19 14:27:31 +0000141 } else if (keycode < 212) {
bellarde58d12e2004-07-05 22:13:07 +0000142 /* use conversion table */
ths6070dd02007-01-24 21:40:21 +0000143 keycode = _translate_keycode(keycode - 97);
bellarde58d12e2004-07-05 22:13:07 +0000144 } else {
145 keycode = 0;
146 }
147 return keycode;
148}
149
150#endif
151
bellard32ff25b2004-10-03 14:33:54 +0000152static void reset_keys(void)
153{
154 int i;
155 for(i = 0; i < 256; i++) {
156 if (modifiers_state[i]) {
157 if (i & 0x80)
158 kbd_put_keycode(0xe0);
159 kbd_put_keycode(i | 0x80);
160 modifiers_state[i] = 0;
161 }
162 }
163}
164
bellard0f0b7262003-08-09 18:26:36 +0000165static void sdl_process_key(SDL_KeyboardEvent *ev)
166{
bellard32ff25b2004-10-03 14:33:54 +0000167 int keycode, v;
bellardde2200d2004-06-04 13:15:06 +0000168
169 if (ev->keysym.sym == SDLK_PAUSE) {
170 /* specific case */
171 v = 0;
172 if (ev->type == SDL_KEYUP)
173 v |= 0x80;
174 kbd_put_keycode(0xe1);
175 kbd_put_keycode(0x1d | v);
176 kbd_put_keycode(0x45 | v);
177 return;
178 }
179
bellard3d11d0e2004-12-12 16:56:30 +0000180 if (kbd_layout) {
181 keycode = sdl_keyevent_to_keycode_generic(ev);
182 } else {
183 keycode = sdl_keyevent_to_keycode(ev);
184 }
bellardde2200d2004-06-04 13:15:06 +0000185
186 switch(keycode) {
187 case 0x00:
188 /* sent when leaving window: reset the modifiers state */
bellard32ff25b2004-10-03 14:33:54 +0000189 reset_keys();
bellardde2200d2004-06-04 13:15:06 +0000190 return;
191 case 0x2a: /* Left Shift */
192 case 0x36: /* Right Shift */
193 case 0x1d: /* Left CTRL */
194 case 0x9d: /* Right CTRL */
195 case 0x38: /* Left ALT */
196 case 0xb8: /* Right ALT */
bellard0f0b7262003-08-09 18:26:36 +0000197 if (ev->type == SDL_KEYUP)
bellardde2200d2004-06-04 13:15:06 +0000198 modifiers_state[keycode] = 0;
199 else
200 modifiers_state[keycode] = 1;
201 break;
202 case 0x45: /* num lock */
203 case 0x3a: /* caps lock */
204 /* SDL does not send the key up event, so we generate it */
205 kbd_put_keycode(keycode);
206 kbd_put_keycode(keycode | 0x80);
207 return;
bellard0f0b7262003-08-09 18:26:36 +0000208 }
bellardde2200d2004-06-04 13:15:06 +0000209
210 /* now send the key code */
211 if (keycode & 0x80)
212 kbd_put_keycode(0xe0);
213 if (ev->type == SDL_KEYUP)
214 kbd_put_keycode(keycode | 0x80);
215 else
216 kbd_put_keycode(keycode & 0x7f);
bellard0f0b7262003-08-09 18:26:36 +0000217}
218
bellard8a7ddc32004-03-31 19:00:16 +0000219static void sdl_update_caption(void)
220{
221 char buf[1024];
thsc35734b2007-03-19 15:17:08 +0000222 const char *status = "";
223
224 if (!vm_running)
225 status = " [Stopped]";
226 else if (gui_grab)
227 status = " - Press Ctrl-Alt to exit grab";
228
229 if (qemu_name)
230 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
231 else
232 snprintf(buf, sizeof(buf), "QEMU%s", status);
233
bellard8a7ddc32004-03-31 19:00:16 +0000234 SDL_WM_SetCaption(buf, "QEMU");
235}
236
bellard09b26c52006-04-12 21:09:08 +0000237static void sdl_hide_cursor(void)
238{
bellard8785a8d2006-06-13 10:49:12 +0000239 if (kbd_mouse_is_absolute()) {
240 SDL_ShowCursor(1);
241 SDL_SetCursor(sdl_cursor_hidden);
242 } else {
243 SDL_ShowCursor(0);
244 }
bellard09b26c52006-04-12 21:09:08 +0000245}
246
247static void sdl_show_cursor(void)
248{
249 if (!kbd_mouse_is_absolute()) {
bellard8785a8d2006-06-13 10:49:12 +0000250 SDL_ShowCursor(1);
thsd34cab92007-04-02 01:10:46 +0000251 if (guest_cursor &&
252 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
253 SDL_SetCursor(guest_sprite);
254 else
255 SDL_SetCursor(sdl_cursor_normal);
bellard09b26c52006-04-12 21:09:08 +0000256 }
257}
258
bellard0f0b7262003-08-09 18:26:36 +0000259static void sdl_grab_start(void)
260{
thsd34cab92007-04-02 01:10:46 +0000261 if (guest_cursor) {
262 SDL_SetCursor(guest_sprite);
263 SDL_WarpMouse(guest_x, guest_y);
264 } else
265 sdl_hide_cursor();
bellard0f0b7262003-08-09 18:26:36 +0000266 SDL_WM_GrabInput(SDL_GRAB_ON);
267 /* dummy read to avoid moving the mouse */
268 SDL_GetRelativeMouseState(NULL, NULL);
269 gui_grab = 1;
bellard8a7ddc32004-03-31 19:00:16 +0000270 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000271}
272
273static void sdl_grab_end(void)
274{
bellard0f0b7262003-08-09 18:26:36 +0000275 SDL_WM_GrabInput(SDL_GRAB_OFF);
bellard0f0b7262003-08-09 18:26:36 +0000276 gui_grab = 0;
thsd34cab92007-04-02 01:10:46 +0000277 sdl_show_cursor();
bellard8a7ddc32004-03-31 19:00:16 +0000278 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000279}
280
bellard18a6d282005-01-17 22:32:23 +0000281static void sdl_send_mouse_event(int dz)
bellard0f0b7262003-08-09 18:26:36 +0000282{
bellard18a6d282005-01-17 22:32:23 +0000283 int dx, dy, state, buttons;
bellard0f0b7262003-08-09 18:26:36 +0000284 state = SDL_GetRelativeMouseState(&dx, &dy);
285 buttons = 0;
286 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
287 buttons |= MOUSE_EVENT_LBUTTON;
288 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
289 buttons |= MOUSE_EVENT_RBUTTON;
290 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
291 buttons |= MOUSE_EVENT_MBUTTON;
bellard09b26c52006-04-12 21:09:08 +0000292
293 if (kbd_mouse_is_absolute()) {
294 if (!absolute_enabled) {
295 sdl_hide_cursor();
296 if (gui_grab) {
297 sdl_grab_end();
298 }
299 absolute_enabled = 1;
300 }
301
302 SDL_GetMouseState(&dx, &dy);
303 dx = dx * 0x7FFF / width;
304 dy = dy * 0x7FFF / height;
ths455204e2007-01-05 16:42:13 +0000305 } else if (absolute_enabled) {
306 sdl_show_cursor();
307 absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000308 } else if (guest_cursor) {
309 SDL_GetMouseState(&dx, &dy);
310 dx -= guest_x;
311 dy -= guest_y;
312 guest_x += dx;
313 guest_y += dy;
bellard09b26c52006-04-12 21:09:08 +0000314 }
315
bellard0f0b7262003-08-09 18:26:36 +0000316 kbd_mouse_event(dx, dy, dz, buttons);
317}
318
bellard8e9c4af2004-04-28 19:33:40 +0000319static void toggle_full_screen(DisplayState *ds)
320{
321 gui_fullscreen = !gui_fullscreen;
322 sdl_resize(ds, screen->w, screen->h);
323 if (gui_fullscreen) {
324 gui_saved_grab = gui_grab;
325 sdl_grab_start();
326 } else {
327 if (!gui_saved_grab)
328 sdl_grab_end();
329 }
pbrook95219892006-04-09 01:06:34 +0000330 vga_hw_invalidate();
331 vga_hw_update();
bellard8e9c4af2004-04-28 19:33:40 +0000332}
333
bellard0f0b7262003-08-09 18:26:36 +0000334static void sdl_refresh(DisplayState *ds)
335{
336 SDL_Event ev1, *ev = &ev1;
bellard8e9c4af2004-04-28 19:33:40 +0000337 int mod_state;
338
bellard8a7ddc32004-03-31 19:00:16 +0000339 if (last_vm_running != vm_running) {
340 last_vm_running = vm_running;
341 sdl_update_caption();
342 }
343
pbrook95219892006-04-09 01:06:34 +0000344 vga_hw_update();
bellard457831f2004-07-14 17:22:33 +0000345
bellard0f0b7262003-08-09 18:26:36 +0000346 while (SDL_PollEvent(ev)) {
347 switch (ev->type) {
348 case SDL_VIDEOEXPOSE:
349 sdl_update(ds, 0, 0, screen->w, screen->h);
350 break;
351 case SDL_KEYDOWN:
352 case SDL_KEYUP:
353 if (ev->type == SDL_KEYDOWN) {
bellard32ff25b2004-10-03 14:33:54 +0000354 mod_state = (SDL_GetModState() & gui_grab_code) ==
355 gui_grab_code;
bellard8e9c4af2004-04-28 19:33:40 +0000356 gui_key_modifier_pressed = mod_state;
bellard457831f2004-07-14 17:22:33 +0000357 if (gui_key_modifier_pressed) {
bellard32ff25b2004-10-03 14:33:54 +0000358 int keycode;
359 keycode = sdl_keyevent_to_keycode(&ev->key);
360 switch(keycode) {
361 case 0x21: /* 'f' key on US keyboard */
bellard457831f2004-07-14 17:22:33 +0000362 toggle_full_screen(ds);
363 gui_keysym = 1;
364 break;
bellard32ff25b2004-10-03 14:33:54 +0000365 case 0x02 ... 0x0a: /* '1' to '9' keys */
bellarddfd92d32006-08-17 10:42:46 +0000366 /* Reset the modifiers sent to the current console */
367 reset_keys();
bellard32ff25b2004-10-03 14:33:54 +0000368 console_select(keycode - 0x02);
pbrook95219892006-04-09 01:06:34 +0000369 if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000370 /* display grab if going to a text console */
371 if (gui_grab)
372 sdl_grab_end();
373 }
374 gui_keysym = 1;
375 break;
376 default:
377 break;
378 }
pbrook95219892006-04-09 01:06:34 +0000379 } else if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000380 int keysym;
381 keysym = 0;
382 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
383 switch(ev->key.keysym.sym) {
384 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
385 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
386 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
387 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
388 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
389 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
390 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
391 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
392 default: break;
393 }
394 } else {
395 switch(ev->key.keysym.sym) {
396 case SDLK_UP: keysym = QEMU_KEY_UP; break;
397 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
398 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
399 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
400 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
401 case SDLK_END: keysym = QEMU_KEY_END; break;
402 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
403 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
404 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break; case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
405 default: break;
406 }
407 }
408 if (keysym) {
409 kbd_put_keysym(keysym);
410 } else if (ev->key.keysym.unicode != 0) {
411 kbd_put_keysym(ev->key.keysym.unicode);
412 }
bellard8e9c4af2004-04-28 19:33:40 +0000413 }
414 } else if (ev->type == SDL_KEYUP) {
bellardbf2b84e2004-11-14 19:46:35 +0000415 mod_state = (ev->key.keysym.mod & gui_grab_code);
bellard8e9c4af2004-04-28 19:33:40 +0000416 if (!mod_state) {
417 if (gui_key_modifier_pressed) {
pbrook5b311872006-03-11 20:07:45 +0000418 gui_key_modifier_pressed = 0;
bellard457831f2004-07-14 17:22:33 +0000419 if (gui_keysym == 0) {
bellard32ff25b2004-10-03 14:33:54 +0000420 /* exit/enter grab if pressing Ctrl-Alt */
bellardc66b0d42006-06-13 12:03:53 +0000421 if (!gui_grab) {
422 /* if the application is not active,
423 do not try to enter grab state. It
424 prevents
425 'SDL_WM_GrabInput(SDL_GRAB_ON)'
426 from blocking all the application
427 (SDL bug). */
428 if (SDL_GetAppState() & SDL_APPACTIVE)
429 sdl_grab_start();
430 } else {
bellard8e9c4af2004-04-28 19:33:40 +0000431 sdl_grab_end();
bellardc66b0d42006-06-13 12:03:53 +0000432 }
bellard32ff25b2004-10-03 14:33:54 +0000433 /* SDL does not send back all the
434 modifiers key, so we must correct it */
435 reset_keys();
bellard8e9c4af2004-04-28 19:33:40 +0000436 break;
437 }
bellard8e9c4af2004-04-28 19:33:40 +0000438 gui_keysym = 0;
439 }
bellard0f0b7262003-08-09 18:26:36 +0000440 }
441 }
bellarddfd92d32006-08-17 10:42:46 +0000442 if (is_graphic_console() && !gui_keysym)
bellard457831f2004-07-14 17:22:33 +0000443 sdl_process_key(&ev->key);
bellard0f0b7262003-08-09 18:26:36 +0000444 break;
445 case SDL_QUIT:
ths667acca2006-12-11 02:08:05 +0000446 if (!no_quit) {
447 qemu_system_shutdown_request();
448 }
bellard0f0b7262003-08-09 18:26:36 +0000449 break;
450 case SDL_MOUSEMOTION:
ths455204e2007-01-05 16:42:13 +0000451 if (gui_grab || kbd_mouse_is_absolute() ||
452 absolute_enabled) {
bellard18a6d282005-01-17 22:32:23 +0000453 sdl_send_mouse_event(0);
bellard0f0b7262003-08-09 18:26:36 +0000454 }
455 break;
456 case SDL_MOUSEBUTTONDOWN:
457 case SDL_MOUSEBUTTONUP:
458 {
459 SDL_MouseButtonEvent *bev = &ev->button;
bellard09b26c52006-04-12 21:09:08 +0000460 if (!gui_grab && !kbd_mouse_is_absolute()) {
bellard0f0b7262003-08-09 18:26:36 +0000461 if (ev->type == SDL_MOUSEBUTTONDOWN &&
462 (bev->state & SDL_BUTTON_LMASK)) {
463 /* start grabbing all events */
464 sdl_grab_start();
465 }
466 } else {
bellard18a6d282005-01-17 22:32:23 +0000467 int dz;
468 dz = 0;
469#ifdef SDL_BUTTON_WHEELUP
bellard09b26c52006-04-12 21:09:08 +0000470 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000471 dz = -1;
bellard09b26c52006-04-12 21:09:08 +0000472 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000473 dz = 1;
474 }
475#endif
476 sdl_send_mouse_event(dz);
bellard0f0b7262003-08-09 18:26:36 +0000477 }
478 }
479 break;
bellard0294ffb2004-04-29 22:15:15 +0000480 case SDL_ACTIVEEVENT:
pbrook5b311872006-03-11 20:07:45 +0000481 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
482 !ev->active.gain && !gui_fullscreen_initial_grab) {
bellard0294ffb2004-04-29 22:15:15 +0000483 sdl_grab_end();
484 }
485 break;
bellard0f0b7262003-08-09 18:26:36 +0000486 default:
487 break;
488 }
489 }
490}
491
thsd34cab92007-04-02 01:10:46 +0000492static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
493{
494 SDL_Rect dst = { x, y, w, h };
495 SDL_FillRect(screen, &dst, c);
496}
497
498static void sdl_mouse_warp(int x, int y, int on)
499{
500 if (on) {
501 if (!guest_cursor)
502 sdl_show_cursor();
503 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
504 SDL_SetCursor(guest_sprite);
505 SDL_WarpMouse(x, y);
506 }
507 } else if (gui_grab)
508 sdl_hide_cursor();
509 guest_cursor = on;
510 guest_x = x, guest_y = y;
511}
512
513static void sdl_mouse_define(int width, int height, int bpp,
514 int hot_x, int hot_y,
515 uint8_t *image, uint8_t *mask)
516{
517 uint8_t sprite[256], *line;
518 int x, y, dst, bypl, src = 0;
519 if (guest_sprite)
520 SDL_FreeCursor(guest_sprite);
521
522 memset(sprite, 0, 256);
523 bypl = ((width * bpp + 31) >> 5) << 2;
524 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
525 line = image;
526 for (x = 0; x < width; x ++, dst ++) {
527 switch (bpp) {
528 case 24:
529 src = *(line ++); src |= *(line ++); src |= *(line ++);
530 break;
531 case 16:
532 case 15:
533 src = *(line ++); src |= *(line ++);
534 break;
535 case 8:
536 src = *(line ++);
537 break;
538 case 4:
539 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
540 break;
541 case 2:
542 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
543 break;
544 case 1:
545 src = 1 & (line[x >> 3] >> (x & 7));
546 break;
547 }
548 if (!src)
549 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
550 }
551 }
552 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
553
554 if (guest_cursor &&
555 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
556 SDL_SetCursor(guest_sprite);
557}
558
bellard898712a2004-02-06 19:56:42 +0000559static void sdl_cleanup(void)
560{
thsd34cab92007-04-02 01:10:46 +0000561 if (guest_sprite)
562 SDL_FreeCursor(guest_sprite);
bellard898712a2004-02-06 19:56:42 +0000563 SDL_Quit();
564}
565
ths43523e92007-02-18 18:19:32 +0000566void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
bellard0f0b7262003-08-09 18:26:36 +0000567{
568 int flags;
bellard09b26c52006-04-12 21:09:08 +0000569 uint8_t data = 0;
bellard0f0b7262003-08-09 18:26:36 +0000570
bellard3d11d0e2004-12-12 16:56:30 +0000571#if defined(__APPLE__)
572 /* always use generic keymaps */
573 if (!keyboard_layout)
574 keyboard_layout = "en-us";
575#endif
576 if(keyboard_layout) {
577 kbd_layout = init_keyboard_layout(keyboard_layout);
578 if (!kbd_layout)
579 exit(1);
580 }
581
ths43523e92007-02-18 18:19:32 +0000582 if (no_frame)
583 gui_noframe = 1;
584
bellard0f0b7262003-08-09 18:26:36 +0000585 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
586 if (SDL_Init (flags)) {
587 fprintf(stderr, "Could not initialize SDL - exiting\n");
588 exit(1);
589 }
bellard67b915a2004-03-31 23:37:16 +0000590#ifndef _WIN32
bellard0ae04d72003-09-30 21:09:16 +0000591 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
592 signal(SIGINT, SIG_DFL);
593 signal(SIGQUIT, SIG_DFL);
bellard67b915a2004-03-31 23:37:16 +0000594#endif
bellard0ae04d72003-09-30 21:09:16 +0000595
bellard0f0b7262003-08-09 18:26:36 +0000596 ds->dpy_update = sdl_update;
597 ds->dpy_resize = sdl_resize;
598 ds->dpy_refresh = sdl_refresh;
thsd34cab92007-04-02 01:10:46 +0000599 ds->dpy_fill = sdl_fill;
600 ds->mouse_set = sdl_mouse_warp;
601 ds->cursor_define = sdl_mouse_define;
bellard0f0b7262003-08-09 18:26:36 +0000602
603 sdl_resize(ds, 640, 400);
bellard8a7ddc32004-03-31 19:00:16 +0000604 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000605 SDL_EnableKeyRepeat(250, 50);
bellard457831f2004-07-14 17:22:33 +0000606 SDL_EnableUNICODE(1);
bellard0f0b7262003-08-09 18:26:36 +0000607 gui_grab = 0;
bellard898712a2004-02-06 19:56:42 +0000608
bellard09b26c52006-04-12 21:09:08 +0000609 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
610 sdl_cursor_normal = SDL_GetCursor();
611
bellard898712a2004-02-06 19:56:42 +0000612 atexit(sdl_cleanup);
bellardd63d3072004-10-03 13:29:03 +0000613 if (full_screen) {
614 gui_fullscreen = 1;
615 gui_fullscreen_initial_grab = 1;
616 sdl_grab_start();
617 }
bellard0f0b7262003-08-09 18:26:36 +0000618}