disable grab if the window no longer has the focus (Windows case) (Mike Nordell)
[qemu] / sdl.c
1 /*
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  */
24 #include "vl.h"
25
26 #include <SDL.h>
27
28 #ifndef _WIN32
29 #include <signal.h>
30 #endif
31
32 static SDL_Surface *screen;
33 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
34 static int last_vm_running;
35 static int gui_saved_grab;
36 static int gui_fullscreen;
37 static int gui_key_modifier_pressed;
38 static int gui_keysym;
39
40 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
41 {
42     //    printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
43     SDL_UpdateRect(screen, x, y, w, h);
44 }
45
46 static void sdl_resize(DisplayState *ds, int w, int h)
47 {
48     int flags;
49
50     //    printf("resizing to %d %d\n", w, h);
51
52     flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
53     flags |= SDL_RESIZABLE;
54     if (gui_fullscreen)
55         flags |= SDL_FULLSCREEN;
56     screen = SDL_SetVideoMode(w, h, 0, flags);
57     if (!screen) {
58         fprintf(stderr, "Could not open SDL display\n");
59         exit(1);
60     }
61     ds->data = screen->pixels;
62     ds->linesize = screen->pitch;
63     ds->depth = screen->format->BitsPerPixel;
64 }
65
66 static const uint32_t x_keycode_to_pc_keycode[61] = {
67    0x47e0,      /*  97  Home   */
68    0x48e0,      /*  98  Up     */
69    0x49e0,      /*  99  PgUp   */
70    0x4be0,      /* 100  Left   */
71    0x4c,        /* 101  KP-5   */
72    0x4de0,      /* 102  Right  */
73    0x4fe0,      /* 103  End    */
74    0x50e0,      /* 104  Down   */
75    0x51e0,      /* 105  PgDn   */
76    0x52e0,      /* 106  Ins    */
77    0x53e0,      /* 107  Del    */
78    0x1ce0,      /* 108  Enter  */
79    0x1de0,      /* 109  Ctrl-R */
80    0x451de1,    /* 110  Pause  */
81    0x37e0,      /* 111  Print  */
82    0x35e0,      /* 112  Divide */
83    0x38e0,      /* 113  Alt-R  */
84    0x46e0,      /* 114  Break  */   
85    0x0,         /* 115 */
86    0x0,         /* 116 */
87    0x0,         /* 117 */
88    0x0,         /* 118 */
89    0x0,         /* 119 */
90    0x0,         /* 120 */
91    0x0,         /* 121 */
92    0x0,         /* 122 */
93    0x0,         /* 123 */
94    0x0,         /* 124 */
95    0x0,         /* 125 */
96    0x0,         /* 126 */
97    0x0,         /* 127 */
98    0x0,         /* 128 */
99    0x0,         /* 129 */
100    0x0,         /* 130 */
101    0x0,         /* 131 */
102    0x0,         /* 132 */
103    0x0,         /* 133 */
104    0x0,         /* 134 */
105    0x0,         /* 135 */
106    0x47,         /* 136 KP_7 */
107    0x48,         /* 137 KP_8 */
108    0x49,         /* 138 KP_9 */
109    0x4b,         /* 139 KP_4 */
110    0x4c,         /* 140 KP_5 */
111    0x4d,         /* 141 KP_6 */
112    0x4f,         /* 142 KP_1 */
113    0x50,         /* 143 KP_2 */
114    0x51,         /* 144 KP_3 */
115    0x52,         /* 145 KP_0 */
116    0x53,         /* 146 KP_. */
117    0x47,         /* 147 KP_HOME */
118    0x48,         /* 148 KP_UP */
119    0x49,         /* 149 KP_PgUp */
120    0x4b,         /* 150 KP_Left */
121    0x4c,         /* 151 KP_ */
122    0x4d,         /* 152 KP_Right */
123    0x4f,         /* 153 KP_End */
124    0x50,         /* 154 KP_Down */
125    0x51,         /* 155 KP_PgDn */
126    0x52,         /* 156 KP_Ins */
127    0x53,         /* 157 KP_Del */
128 };
129
130 static void sdl_process_key(SDL_KeyboardEvent *ev)
131 {
132     int keycode, v;
133     
134     /* XXX: not portable, but avoids complicated mappings */
135     keycode = ev->keysym.scancode;
136 #ifdef _WIN32
137     if (keycode < 97) {
138         /* nothing to do */
139     } else 
140 #else
141     if (keycode < 9) {
142         keycode = 0;
143     } else if (keycode < 97) {
144         keycode -= 8; /* just an offset */
145     } else 
146 #endif
147     if (keycode < 158) {
148         /* use conversion table */
149         keycode = x_keycode_to_pc_keycode[keycode - 97];
150     } else {
151         keycode = 0;
152     }
153     
154     /* now send the key code */
155     while (keycode != 0) {
156         v = keycode & 0xff;
157         if (ev->type == SDL_KEYUP)
158             v |= 0x80;
159         kbd_put_keycode(v);
160         keycode >>= 8;
161     }
162 }
163
164 static void sdl_update_caption(void)
165 {
166     char buf[1024];
167     strcpy(buf, "QEMU");
168     if (!vm_running) {
169         strcat(buf, " [Stopped]");
170     }
171     if (gui_grab) {
172         strcat(buf, " - Press Ctrl-Shift to exit grab");
173     }
174     SDL_WM_SetCaption(buf, "QEMU");
175 }
176
177 static void sdl_grab_start(void)
178 {
179     SDL_ShowCursor(0);
180     SDL_WM_GrabInput(SDL_GRAB_ON);
181     /* dummy read to avoid moving the mouse */
182     SDL_GetRelativeMouseState(NULL, NULL);
183     gui_grab = 1;
184     sdl_update_caption();
185 }
186
187 static void sdl_grab_end(void)
188 {
189     SDL_WM_GrabInput(SDL_GRAB_OFF);
190     SDL_ShowCursor(1);
191     gui_grab = 0;
192     sdl_update_caption();
193 }
194
195 static void sdl_send_mouse_event(void)
196 {
197     int dx, dy, dz, state, buttons;
198     state = SDL_GetRelativeMouseState(&dx, &dy);
199     buttons = 0;
200     if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
201         buttons |= MOUSE_EVENT_LBUTTON;
202     if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
203         buttons |= MOUSE_EVENT_RBUTTON;
204     if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
205         buttons |= MOUSE_EVENT_MBUTTON;
206     /* XXX: test wheel */
207     dz = 0;
208 #ifdef SDL_BUTTON_WHEELUP
209     if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP))
210         dz--;
211     if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN))
212         dz++;
213 #endif
214     kbd_mouse_event(dx, dy, dz, buttons);
215 }
216
217 static void toggle_full_screen(DisplayState *ds)
218 {
219     gui_fullscreen = !gui_fullscreen;
220     sdl_resize(ds, screen->w, screen->h);
221     if (gui_fullscreen) {
222         gui_saved_grab = gui_grab;
223         sdl_grab_start();
224     } else {
225         if (!gui_saved_grab)
226             sdl_grab_end();
227     }
228     vga_update_display();
229     sdl_update(ds, 0, 0, screen->w, screen->h);
230 }
231
232 static void sdl_refresh(DisplayState *ds)
233 {
234     SDL_Event ev1, *ev = &ev1;
235     int mod_state;
236                      
237     if (last_vm_running != vm_running) {
238         last_vm_running = vm_running;
239         sdl_update_caption();
240     }
241
242     vga_update_display();
243     while (SDL_PollEvent(ev)) {
244         switch (ev->type) {
245         case SDL_VIDEOEXPOSE:
246             sdl_update(ds, 0, 0, screen->w, screen->h);
247             break;
248         case SDL_KEYDOWN:
249         case SDL_KEYUP:
250             if (ev->type == SDL_KEYDOWN) {
251                 mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) ==
252                     (KMOD_LSHIFT | KMOD_LCTRL);
253                 gui_key_modifier_pressed = mod_state;
254                 if (gui_key_modifier_pressed && 
255                     ev->key.keysym.sym == SDLK_f) {
256                     gui_keysym = ev->key.keysym.sym;
257                 }
258             } else if (ev->type == SDL_KEYUP) {
259                 mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL));
260                 if (!mod_state) {
261                     if (gui_key_modifier_pressed) {
262                         switch(gui_keysym) {
263                         case SDLK_f:
264                             toggle_full_screen(ds);
265                             break;
266                         case 0:
267                             /* exit/enter grab if pressing Ctrl-Shift */
268                             if (!gui_grab)
269                                 sdl_grab_start();
270                             else
271                                 sdl_grab_end();
272                             break;
273                         }
274                         gui_key_modifier_pressed = 0;
275                         gui_keysym = 0;
276                     }
277                 }
278             }
279             sdl_process_key(&ev->key);
280             break;
281         case SDL_QUIT:
282             reset_requested = 1;
283             break;
284         case SDL_MOUSEMOTION:
285             if (gui_grab) {
286                 sdl_send_mouse_event();
287             }
288             break;
289         case SDL_MOUSEBUTTONDOWN:
290         case SDL_MOUSEBUTTONUP:
291             {
292                 SDL_MouseButtonEvent *bev = &ev->button;
293                 if (!gui_grab) {
294                     if (ev->type == SDL_MOUSEBUTTONDOWN &&
295                         (bev->state & SDL_BUTTON_LMASK)) {
296                         /* start grabbing all events */
297                         sdl_grab_start();
298                     }
299                 } else {
300                     sdl_send_mouse_event();
301                 }
302             }
303             break;
304         case SDL_ACTIVEEVENT:
305             if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0) {
306                 sdl_grab_end();
307             }
308             break;
309         default:
310             break;
311         }
312     }
313 }
314
315 static void sdl_cleanup(void) 
316 {
317     SDL_Quit();
318 }
319
320 void sdl_display_init(DisplayState *ds)
321 {
322     int flags;
323
324     flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
325     if (SDL_Init (flags)) {
326         fprintf(stderr, "Could not initialize SDL - exiting\n");
327         exit(1);
328     }
329
330 #ifndef _WIN32
331     /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
332     signal(SIGINT, SIG_DFL);
333     signal(SIGQUIT, SIG_DFL);
334 #endif
335
336     ds->dpy_update = sdl_update;
337     ds->dpy_resize = sdl_resize;
338     ds->dpy_refresh = sdl_refresh;
339
340     sdl_resize(ds, 640, 400);
341     sdl_update_caption();
342     SDL_EnableKeyRepeat(250, 50);
343     gui_grab = 0;
344
345     atexit(sdl_cleanup);
346 }