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