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