]> git.proxmox.com Git - qemu.git/blobdiff - sdl.c
BSD fix
[qemu.git] / sdl.c
diff --git a/sdl.c b/sdl.c
index e9c28f2a1a12fec591fcd8c4ded40f4f9d979539..7b3c3b5eab38cda3851240d577ed2ee928e1eccb 100644 (file)
--- a/sdl.c
+++ b/sdl.c
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdarg.h>
-#include <string.h>
-#include <getopt.h>
-#include <inttypes.h>
-#include <unistd.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <time.h>
-#include <sys/time.h>
-#include <malloc.h>
-#include <termios.h>
-#include <sys/poll.h>
-#include <errno.h>
-#include <sys/wait.h>
-#include <netinet/in.h>
+#include "vl.h"
 
 #include <SDL.h>
 
-#include "cpu-i386.h"
-#include "exec.h"
-
-#include "vl.h"
+#ifndef _WIN32
+#include <signal.h>
+#endif
 
 static SDL_Surface *screen;
 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
+static int last_vm_running;
+static int gui_saved_grab;
+static int gui_fullscreen;
+static int gui_key_modifier_pressed;
+static int gui_keysym;
 
 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
 {
+    //    printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
     SDL_UpdateRect(screen, x, y, w, h);
 }
 
@@ -63,6 +51,8 @@ static void sdl_resize(DisplayState *ds, int w, int h)
 
     flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
     flags |= SDL_RESIZABLE;
+    if (gui_fullscreen)
+        flags |= SDL_FULLSCREEN;
     screen = SDL_SetVideoMode(w, h, 0, flags);
     if (!screen) {
         fprintf(stderr, "Could not open SDL display\n");
@@ -97,20 +87,20 @@ static const uint32_t x_keycode_to_pc_keycode[61] = {
    0x0,         /* 117 */
    0x0,         /* 118 */
    0x0,         /* 119 */
-   0x0,         /* 120 */
+   0x70,         /* 120 Hiragana_Katakana */
    0x0,         /* 121 */
    0x0,         /* 122 */
-   0x0,         /* 123 */
+   0x73,         /* 123 backslash */
    0x0,         /* 124 */
    0x0,         /* 125 */
    0x0,         /* 126 */
    0x0,         /* 127 */
    0x0,         /* 128 */
-   0x0,         /* 129 */
+   0x79,         /* 129 Henkan */
    0x0,         /* 130 */
-   0x0,         /* 131 */
+   0x7b,         /* 131 Muhenkan */
    0x0,         /* 132 */
-   0x0,         /* 133 */
+   0x7d,         /* 133 Yen */
    0x0,         /* 134 */
    0x0,         /* 135 */
    0x47,         /* 136 KP_7 */
@@ -143,6 +133,8 @@ static void sdl_process_key(SDL_KeyboardEvent *ev)
     
     /* XXX: not portable, but avoids complicated mappings */
     keycode = ev->keysym.scancode;
+
+#ifndef _WIN32
     if (keycode < 9) {
         keycode = 0;
     } else if (keycode < 97) {
@@ -153,6 +145,7 @@ static void sdl_process_key(SDL_KeyboardEvent *ev)
     } else {
         keycode = 0;
     }
+#endif
     
     /* now send the key code */
     while (keycode != 0) {
@@ -164,22 +157,35 @@ static void sdl_process_key(SDL_KeyboardEvent *ev)
     }
 }
 
+static void sdl_update_caption(void)
+{
+    char buf[1024];
+    strcpy(buf, "QEMU");
+    if (!vm_running) {
+        strcat(buf, " [Stopped]");
+    }
+    if (gui_grab) {
+        strcat(buf, " - Press Ctrl-Shift to exit grab");
+    }
+    SDL_WM_SetCaption(buf, "QEMU");
+}
+
 static void sdl_grab_start(void)
 {
-    SDL_WM_SetCaption("QEMU - Press Ctrl-Shift to exit grab", "QEMU");
     SDL_ShowCursor(0);
     SDL_WM_GrabInput(SDL_GRAB_ON);
     /* dummy read to avoid moving the mouse */
     SDL_GetRelativeMouseState(NULL, NULL);
     gui_grab = 1;
+    sdl_update_caption();
 }
 
 static void sdl_grab_end(void)
 {
-    SDL_WM_SetCaption("QEMU", "QEMU");
     SDL_WM_GrabInput(SDL_GRAB_OFF);
     SDL_ShowCursor(1);
     gui_grab = 0;
+    sdl_update_caption();
 }
 
 static void sdl_send_mouse_event(void)
@@ -195,16 +201,39 @@ static void sdl_send_mouse_event(void)
         buttons |= MOUSE_EVENT_MBUTTON;
     /* XXX: test wheel */
     dz = 0;
+#ifdef SDL_BUTTON_WHEELUP
     if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP))
         dz--;
     if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN))
         dz++;
+#endif
     kbd_mouse_event(dx, dy, dz, buttons);
 }
 
+static void toggle_full_screen(DisplayState *ds)
+{
+    gui_fullscreen = !gui_fullscreen;
+    sdl_resize(ds, screen->w, screen->h);
+    if (gui_fullscreen) {
+        gui_saved_grab = gui_grab;
+        sdl_grab_start();
+    } else {
+        if (!gui_saved_grab)
+            sdl_grab_end();
+    }
+    vga_update_display();
+    sdl_update(ds, 0, 0, screen->w, screen->h);
+}
+
 static void sdl_refresh(DisplayState *ds)
 {
     SDL_Event ev1, *ev = &ev1;
+    int mod_state;
+                     
+    if (last_vm_running != vm_running) {
+        last_vm_running = vm_running;
+        sdl_update_caption();
+    }
 
     vga_update_display();
     while (SDL_PollEvent(ev)) {
@@ -215,13 +244,32 @@ static void sdl_refresh(DisplayState *ds)
         case SDL_KEYDOWN:
         case SDL_KEYUP:
             if (ev->type == SDL_KEYDOWN) {
-                if ((SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) ==
-                    (KMOD_LSHIFT | KMOD_LCTRL)) {
-                    /* exit/enter grab if pressing Ctrl-Shift */
-                    if (!gui_grab)
-                        sdl_grab_start();
-                    else
-                        sdl_grab_end();
+                mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) ==
+                    (KMOD_LSHIFT | KMOD_LCTRL);
+                gui_key_modifier_pressed = mod_state;
+                if (gui_key_modifier_pressed && 
+                    ev->key.keysym.sym == SDLK_f) {
+                    gui_keysym = ev->key.keysym.sym;
+                }
+            } else if (ev->type == SDL_KEYUP) {
+                mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL));
+                if (!mod_state) {
+                    if (gui_key_modifier_pressed) {
+                        switch(gui_keysym) {
+                        case SDLK_f:
+                            toggle_full_screen(ds);
+                            break;
+                        case 0:
+                            /* exit/enter grab if pressing Ctrl-Shift */
+                            if (!gui_grab)
+                                sdl_grab_start();
+                            else
+                                sdl_grab_end();
+                            break;
+                        }
+                        gui_key_modifier_pressed = 0;
+                        gui_keysym = 0;
+                    }
                 }
             }
             sdl_process_key(&ev->key);
@@ -249,12 +297,22 @@ static void sdl_refresh(DisplayState *ds)
                 }
             }
             break;
+        case SDL_ACTIVEEVENT:
+            if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0) {
+                sdl_grab_end();
+            }
+            break;
         default:
             break;
         }
     }
 }
 
+static void sdl_cleanup(void) 
+{
+    SDL_Quit();
+}
+
 void sdl_display_init(DisplayState *ds)
 {
     int flags;
@@ -264,12 +322,21 @@ void sdl_display_init(DisplayState *ds)
         fprintf(stderr, "Could not initialize SDL - exiting\n");
         exit(1);
     }
+
+#ifndef _WIN32
+    /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
+    signal(SIGINT, SIG_DFL);
+    signal(SIGQUIT, SIG_DFL);
+#endif
+
     ds->dpy_update = sdl_update;
     ds->dpy_resize = sdl_resize;
     ds->dpy_refresh = sdl_refresh;
 
     sdl_resize(ds, 640, 400);
-    SDL_WM_SetCaption("QEMU", "QEMU");
+    sdl_update_caption();
     SDL_EnableKeyRepeat(250, 50);
     gui_grab = 0;
+
+    atexit(sdl_cleanup);
 }