diff pwplib/pwplib-unix.c @ 15:fa87f8897f21

Implement hacky and crummy SDL event handling in timer value fetching function; moved SDL general initialization into pwplib-unix.c. Quitting via ESC now works, as does signal handling (shouldn't segfault when sent SIGINT etc.) to some degree.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 22 May 2010 18:12:37 +0300
parents acb5694e93d9
children c60e531d19cd
line wrap: on
line diff
--- a/pwplib/pwplib-unix.c	Thu May 20 17:17:36 2010 +0300
+++ b/pwplib/pwplib-unix.c	Sat May 22 18:12:37 2010 +0300
@@ -5,6 +5,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#ifdef DRIVE_SDL
+#include <SDL.h>
+#endif
 #include <signal.h>
 #include <time.h>
 #include <sys/time.h>
@@ -12,10 +15,13 @@
 #include "pwplib.h"
 #include "tty.h"
 
-#define DESTRUCTORS 8
 
 /******************** random stuff *************************/
 
+#ifdef DRIVE_SDL
+static int sdl_init = 0;
+#endif
+
 extern void pwplib_dummy();
 #define pwp_dummy pwplib_dummy
 
@@ -33,9 +39,34 @@
 #define TIMERHZ 72
 int pwp_unix_tod()
 {
+#ifdef DRIVE_SDL
+    if (sdl_init) {
+        int quit = 0;
+        SDL_Event event;
+        if (SDL_PollEvent(&event) >= 0) {
+            switch (event.type) {
+            case SDL_KEYDOWN:
+                if (event.key.keysym.sym == SDLK_ESCAPE)
+                    quit = 1;
+                break;
+            case SDL_QUIT:
+                quit = 1;
+                break;
+            }
+        }
+        if (quit) {
+            SDL_Quit();
+            pwplib_shutdown();
+            exit(1);
+        }
+        return (SDL_GetTicks() * TIMERHZ) / 1000;
+    } else
+#endif
+    {
         struct timeval tod;
         gettimeofday(&tod,NULL);
         return (tod.tv_sec*TIMERHZ)+(tod.tv_usec*TIMERHZ/1000000);
+    }
 }
 
 /********************* destructors, signals etc **********/
@@ -44,6 +75,15 @@
 {
    char buf[40];
 
+   pwpwrite("* pwplib shutting down\n");
+
+#ifdef DRIVE_SDL
+   if (sdl_init) {
+      sdl_init = 0;
+      SDL_Quit();
+   }
+#endif
+
    pwplib_shutdown();
 
    sprintf(buf,"* died to signal %d\n",n);
@@ -56,23 +96,42 @@
 
 int pwplib_initcore()
 {
+   int sdl_flags = 0;
    pwplib_init_common();
 
    /*** set signals ***/
 
-   signal(SIGTERM,pwp_fatalsignal);
-   signal(SIGINT,pwp_fatalsignal);
-   signal(SIGQUIT,pwp_fatalsignal);
-   signal(SIGKILL,pwp_fatalsignal);
-   signal(SIGSEGV,pwp_fatalsignal);
+#ifdef DRIVE_SDL
+#ifdef DRIVE_VIDEO
+    sdl_flags |= SDL_INIT_VIDEO;
+#endif
+#ifdef DRIVE_AUDIO
+    sdl_flags |= SDL_INIT_AUDIO;
+#endif
+   /* With SDL, we let it handle the signals */
+    if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_EVENTTHREAD | /* SDL_INIT_NOPARACHUTE | */ sdl_flags) != 0)
+    {
+        pwpwrite("* SDL could not be initialized.\n");
+        sdl_init = 0;
+    } else
+        sdl_init = 1;
+#endif
+
+   if (!sdl_init) {
+       signal(SIGTERM,pwp_fatalsignal);
+       signal(SIGINT,pwp_fatalsignal);
+       signal(SIGQUIT,pwp_fatalsignal);
+       signal(SIGKILL,pwp_fatalsignal);
+       signal(SIGSEGV,pwp_fatalsignal);
+   }
 
    /*** video ***/
 
 #   ifdef DRIVE_VIDEO
 
 #   ifdef DRIVE_SDL
-   if(pwp_SDL_init())
-   {   
+   if(sdl_init && pwp_SDL_init())
+   {
    }
    else
 #   endif
@@ -93,7 +152,7 @@
    {
       int snd=0;
 #ifdef DRIVE_SDL
-        if(!snd) { if(pwp_sdlaudio_init())snd++; }
+        if(!snd) { if(sdl_init && pwp_sdlaudio_init())snd++; }
 #endif
 
 #ifdef DRIVE_OSS