fixed full screen refresh
[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 uint8_t x_keycode_to_pc_keycode[61] = {
67    0xc7,      /*  97  Home   */
68    0xc8,      /*  98  Up     */
69    0xc9,      /*  99  PgUp   */
70    0xcb,      /* 100  Left   */
71    0x4c,        /* 101  KP-5   */
72    0xcd,      /* 102  Right  */
73    0xcf,      /* 103  End    */
74    0xd0,      /* 104  Down   */
75    0xd1,      /* 105  PgDn   */
76    0xd2,      /* 106  Ins    */
77    0xd3,      /* 107  Del    */
78    0x9c,      /* 108  Enter  */
79    0x9d,      /* 109  Ctrl-R */
80    0x0,       /* 110  Pause  */
81    0xb7,      /* 111  Print  */
82    0xb5,      /* 112  Divide */
83    0xb8,      /* 113  Alt-R  */
84    0xc6,      /* 114  Break  */   
85    0x0,         /* 115 */
86    0x0,         /* 116 */
87    0x0,         /* 117 */
88    0x0,         /* 118 */
89    0x0,         /* 119 */
90    0x70,         /* 120 Hiragana_Katakana */
91    0x0,         /* 121 */
92    0x0,         /* 122 */
93    0x73,         /* 123 backslash */
94    0x0,         /* 124 */
95    0x0,         /* 125 */
96    0x0,         /* 126 */
97    0x0,         /* 127 */
98    0x0,         /* 128 */
99    0x79,         /* 129 Henkan */
100    0x0,         /* 130 */
101    0x7b,         /* 131 Muhenkan */
102    0x0,         /* 132 */
103    0x7d,         /* 133 Yen */
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, i;
133     static uint8_t modifiers_state[256];
134
135     if (ev->keysym.sym == SDLK_PAUSE) {
136         /* specific case */
137         v = 0;
138         if (ev->type == SDL_KEYUP)
139             v |= 0x80;
140         kbd_put_keycode(0xe1);
141         kbd_put_keycode(0x1d | v);
142         kbd_put_keycode(0x45 | v);
143         return;
144     }
145
146     /* XXX: not portable, but avoids complicated mappings */
147     keycode = ev->keysym.scancode;
148
149     /* XXX: windows version may not work: 0xe0/0xe1 should be trapped
150        ? */
151 #ifndef _WIN32
152     if (keycode < 9) {
153         keycode = 0;
154     } else if (keycode < 97) {
155         keycode -= 8; /* just an offset */
156     } else if (keycode < 158) {
157         /* use conversion table */
158         keycode = x_keycode_to_pc_keycode[keycode - 97];
159     } else {
160         keycode = 0;
161     }
162 #endif
163
164     switch(keycode) {
165     case 0x00:
166         /* sent when leaving window: reset the modifiers state */
167         for(i = 0; i < 256; i++) {
168             if (modifiers_state[i]) {
169                 if (i & 0x80)
170                     kbd_put_keycode(0xe0);
171                 kbd_put_keycode(i | 0x80);
172             }
173         }
174         return;
175     case 0x2a:                          /* Left Shift */
176     case 0x36:                          /* Right Shift */
177     case 0x1d:                          /* Left CTRL */
178     case 0x9d:                          /* Right CTRL */
179     case 0x38:                          /* Left ALT */
180     case 0xb8:                         /* Right ALT */
181         if (ev->type == SDL_KEYUP)
182             modifiers_state[keycode] = 0;
183         else
184             modifiers_state[keycode] = 1;
185         break;
186     case 0x45: /* num lock */
187     case 0x3a: /* caps lock */
188         /* SDL does not send the key up event, so we generate it */
189         kbd_put_keycode(keycode);
190         kbd_put_keycode(keycode | 0x80);
191         return;
192     }
193
194     /* now send the key code */
195     if (keycode & 0x80)
196         kbd_put_keycode(0xe0);
197     if (ev->type == SDL_KEYUP)
198         kbd_put_keycode(keycode | 0x80);
199     else
200         kbd_put_keycode(keycode & 0x7f);
201 }
202
203 static void sdl_update_caption(void)
204 {
205     char buf[1024];
206     strcpy(buf, "QEMU");
207     if (!vm_running) {
208         strcat(buf, " [Stopped]");
209     }
210     if (gui_grab) {
211         strcat(buf, " - Press Ctrl-Shift to exit grab");
212     }
213     SDL_WM_SetCaption(buf, "QEMU");
214 }
215
216 static void sdl_grab_start(void)
217 {
218     SDL_ShowCursor(0);
219     SDL_WM_GrabInput(SDL_GRAB_ON);
220     /* dummy read to avoid moving the mouse */
221     SDL_GetRelativeMouseState(NULL, NULL);
222     gui_grab = 1;
223     sdl_update_caption();
224 }
225
226 static void sdl_grab_end(void)
227 {
228     SDL_WM_GrabInput(SDL_GRAB_OFF);
229     SDL_ShowCursor(1);
230     gui_grab = 0;
231     sdl_update_caption();
232 }
233
234 static void sdl_send_mouse_event(void)
235 {
236     int dx, dy, dz, state, buttons;
237     state = SDL_GetRelativeMouseState(&dx, &dy);
238     buttons = 0;
239     if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
240         buttons |= MOUSE_EVENT_LBUTTON;
241     if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
242         buttons |= MOUSE_EVENT_RBUTTON;
243     if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
244         buttons |= MOUSE_EVENT_MBUTTON;
245     /* XXX: test wheel */
246     dz = 0;
247 #ifdef SDL_BUTTON_WHEELUP
248     if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP))
249         dz--;
250     if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN))
251         dz++;
252 #endif
253     kbd_mouse_event(dx, dy, dz, buttons);
254 }
255
256 static void toggle_full_screen(DisplayState *ds)
257 {
258     gui_fullscreen = !gui_fullscreen;
259     sdl_resize(ds, screen->w, screen->h);
260     if (gui_fullscreen) {
261         gui_saved_grab = gui_grab;
262         sdl_grab_start();
263     } else {
264         if (!gui_saved_grab)
265             sdl_grab_end();
266     }
267     vga_invalidate_display();
268     vga_update_display();
269 }
270
271 static void sdl_refresh(DisplayState *ds)
272 {
273     SDL_Event ev1, *ev = &ev1;
274     int mod_state;
275                      
276     if (last_vm_running != vm_running) {
277         last_vm_running = vm_running;
278         sdl_update_caption();
279     }
280
281     vga_update_display();
282     while (SDL_PollEvent(ev)) {
283         switch (ev->type) {
284         case SDL_VIDEOEXPOSE:
285             sdl_update(ds, 0, 0, screen->w, screen->h);
286             break;
287         case SDL_KEYDOWN:
288         case SDL_KEYUP:
289             if (ev->type == SDL_KEYDOWN) {
290                 mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) ==
291                     (KMOD_LSHIFT | KMOD_LCTRL);
292                 gui_key_modifier_pressed = mod_state;
293                 if (gui_key_modifier_pressed && 
294                     ev->key.keysym.sym == SDLK_f) {
295                     gui_keysym = ev->key.keysym.sym;
296                 }
297             } else if (ev->type == SDL_KEYUP) {
298                 mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL));
299                 if (!mod_state) {
300                     if (gui_key_modifier_pressed) {
301                         switch(gui_keysym) {
302                         case SDLK_f:
303                             toggle_full_screen(ds);
304                             break;
305                         case 0:
306                             /* exit/enter grab if pressing Ctrl-Shift */
307                             if (!gui_grab)
308                                 sdl_grab_start();
309                             else
310                                 sdl_grab_end();
311                             break;
312                         }
313                         gui_key_modifier_pressed = 0;
314                         gui_keysym = 0;
315                     }
316                 }
317             }
318             sdl_process_key(&ev->key);
319             break;
320         case SDL_QUIT:
321             reset_requested = 1;
322             break;
323         case SDL_MOUSEMOTION:
324             if (gui_grab) {
325                 sdl_send_mouse_event();
326             }
327             break;
328         case SDL_MOUSEBUTTONDOWN:
329         case SDL_MOUSEBUTTONUP:
330             {
331                 SDL_MouseButtonEvent *bev = &ev->button;
332                 if (!gui_grab) {
333                     if (ev->type == SDL_MOUSEBUTTONDOWN &&
334                         (bev->state & SDL_BUTTON_LMASK)) {
335                         /* start grabbing all events */
336                         sdl_grab_start();
337                     }
338                 } else {
339                     sdl_send_mouse_event();
340                 }
341             }
342             break;
343         case SDL_ACTIVEEVENT:
344             if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0) {
345                 sdl_grab_end();
346             }
347             break;
348         default:
349             break;
350         }
351     }
352 }
353
354 static void sdl_cleanup(void) 
355 {
356     SDL_Quit();
357 }
358
359 void sdl_display_init(DisplayState *ds)
360 {
361     int flags;
362
363     flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
364     if (SDL_Init (flags)) {
365         fprintf(stderr, "Could not initialize SDL - exiting\n");
366         exit(1);
367     }
368
369 #ifndef _WIN32
370     /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
371     signal(SIGINT, SIG_DFL);
372     signal(SIGQUIT, SIG_DFL);
373 #endif
374
375     ds->dpy_update = sdl_update;
376     ds->dpy_resize = sdl_resize;
377     ds->dpy_refresh = sdl_refresh;
378
379     sdl_resize(ds, 640, 400);
380     sdl_update_caption();
381     SDL_EnableKeyRepeat(250, 50);
382     gui_grab = 0;
383
384     atexit(sdl_cleanup);
385 }