view 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 source

#define __PWPLIB_C

#include "config.h"

#include <stdio.h>
#include <stdlib.h>

#ifdef DRIVE_SDL
#include <SDL.h>
#endif
#include <signal.h>
#include <time.h>
#include <sys/time.h>

#include "pwplib.h"
#include "tty.h"


/******************** random stuff *************************/

#ifdef DRIVE_SDL
static int sdl_init = 0;
#endif

extern void pwplib_dummy();
#define pwp_dummy pwplib_dummy

void pwpwrite(char*z)
{
  if(!pwplib.setup[SETUP_SHUTUP])
  {
     fprintf(stderr,"%s",z);
     usleep(1000*pwplib.set.infodelay);
  }
}

/********************* timer  ******************************/

#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 **********/

void pwp_fatalsignal(int n)
{
   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);
   pwpwrite(buf);
   
   exit(1);
}

/******************** initialization *********************/

int pwplib_initcore()
{
   int sdl_flags = 0;
   pwplib_init_common();

   /*** set signals ***/

#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(sdl_init && pwp_SDL_init())
   {
   }
   else
#   endif
#   ifdef DRIVE_PVP
   if(pwplib.setup[SETUP_PVP])
     pvp_init();
   else
#   endif
   if(!pwplib.setup[SETUP_NOVIDEO])
   {
     tty_init();
   }
#   endif

   /*** audio ***/
#  ifdef DRIVE_AUDIO
   if(!pwplib.setup[SETUP_NOSOUND])
   {
      int snd=0;
#ifdef DRIVE_SDL
        if(!snd) { if(sdl_init && pwp_sdlaudio_init())snd++; }
#endif

#ifdef DRIVE_OSS
        if(!snd) { if(oss_init())snd++; }
#endif

#ifdef DRIVE_HPUX
        if(!snd) { if(hpuxsnd_init())snd++; }
#endif

        if(!snd)
        pwpwrite("* couldn't init sound. going silent.\n");

   }
   else
     pwpwrite("* no sound, as requested\n");
#  endif

   /*** fallbacks - remove? ***/

   if(pwplib.dump_rast==pwplib_dummy && pwplib.dump_attr!=pwplib_dummy)
      pwplib.dump_rast=pwplib_dump_rast_plain;

   /*** timer ***/

   if(pwplib.setup[SETUP_BPS]|pwplib.setup[SETUP_FPS])
   {
      char tmp[100];
      sprintf(tmp,"non-realtime output (bps=%d fps=%d)\n",
        pwplib.setup[SETUP_BPS],pwplib.setup[SETUP_FPS]);
      pwpwrite(tmp);

      pwplib.timerfunc=pwp_timer_nrt;
      pwplib.timer_counter=0;
   }
    else
      pwplib.timerfunc=pwp_unix_tod;

   /*** hmmm ***/

   if(pwplib.setup[SETUP_HALVE])
      pwplib.videobuf.height<<=1;

     /* only allow if smaller than original? */

   if(pwplib.setup[SETUP_USERHEIGHT])
      pwplib.videobuf.height=pwplib.setup[SETUP_USERHEIGHT];
   if(pwplib.setup[SETUP_USERWIDTH])
      pwplib.videobuf.width=pwplib.setup[SETUP_USERWIDTH];

   /*** done ***/
   
#   ifdef DRIVE_VIDEO
   if(!pwplib.setup[SETUP_NOVIDEO])
   {char tmp[100];
   sprintf(tmp,"* pwplib now controls a %d x %d framebuffer\n",
      pwplib.videobuf.width,pwplib.videobuf.height);
   pwpwrite(tmp);
   }
#  endif

   return 1;
}

/************************************/

const char*pwplogo=
"\033[2J\033[H\n"
"   \033[36;46mMMmmmmmmMM\033[0m\n"
"       \033[36;46mMM\033[0m\n"
"    \033[31;41mxxxxxxxxxxxx\033[0m\n"
"    \033[31;41mxxxxxxxxxxxxxxxxxxx\033[0m\n"
"   \033[31;41mxxxxxxxxx\033[0m\n"
"   \033[33;43mZZZZZ\033[34;44m....\033[0m        %%%%%%%%%  %%%        %%%  %%%%%%%%%\n"
"  \033[37;47m@@@@\033[34;44m......\033[0m        %%%%%%%%%. %%%....... %%%. %%%%%%%%%....\n"
"  \033[37;47m@@@@\033[34;44m....\033[37mo\033[34m.\033[0m   .....%%%   %%%  %%%   %%%  %%%  %%%   %%%\n"
" \033[37;47m@@@@@@@\033[34;44m....\033[0m        %%%   %%%  %%%   %%%  %%%  %%%   %%%\n"
" \033[37;47m@@@@@@@@@@@\033[0m        %%%%  %%%  %%%   %%%  %%%  %%%%  %%%\n"
" \033[37;47m@@@@@@@@@@@@@\033[0m      %%%%  %%%  %%%   %%%  %%%  %%%%  %%%\n"
" \033[37;47m@@@@@@@@@@@@@@@\033[0m    %%%%%%%%%  %%%   %%%  %%%  %%%%%%%%%\n"
"  \033[37;47m@@@@\033[0m              %%%%%%%%%  %%%   %%%  %%%  %%%%%%%%%\n"
"  \033[37;47m@@@@@@\033[0m            %%%        %%%%% %%%  %%%  %%%\n"
"  \033[37;47m@@@@@@@@@@\033[0m  \033[31;41mxx\033[0m    %%%        %%%%% %%%  %%%  %%%\n"
"  \033[37;47m@@@@@@@@@@@@@@\033[0m    %%%        %%%%% %%%% %%%  %%%\n"
"   \033[37;47m@@@@@@@@@@@\033[0m      %%%        %%%%% %%%% %%%  %%%\n"
"   \033[37;47m@@@@@@@@@\033[0m   .....%%%....... %%%%%%%%%%%%%%. %%%..........\n"
"     \033[37;47m@@@@@\033[0m          %%%        %%%%%%%%%%%%%%  %%%\n"
"     \033[37;47m@@@@\033[0m\n\n";


void pwplib_startup()
{
  if(!pwplib.setup[SETUP_SHUTUP])
  {
     pwpwrite("starting up.... 2sec delay for ^C");
     sleep(2);
  }

  /* send initstuff? */
}

void pwplib_end()
{
  if(pwplib.setup[SETUP_SHUTUP])return;

  pwplib_shutdown();
  sleep(2);

  fputs(pwplogo,stderr);
  pwpwrite("shutting down multimedia subsystems\n"  
           "operation finished - support pwp\n\n\n");
}

/***************************************************************/

int pwplib_init(int argc,char**argv)
{
   int i=0,quit=0;

   pwplib.argc=argc;
   pwplib.argv=argv;

   pwplib_getopts();

   if(pwplib.setup[SETUP_WANTHELP])
   {
      printhelp();
      exit(0);
      pwplib.setup[SETUP_SHUTUP]++;
   }

   if(!pwplib.setup[SETUP_SHUTUP])
   {
      fputs(pwplogo,stderr);
      pwpwrite("pwplib " PWPLIB_VERSION " initializing..\n\nrun with --help for some options\n\n");
   }
   
   {char buf[40];
    sprintf(buf,"* using locale '%s'\n",pwplib.set.lang);
    pwpwrite(buf);
   }

   if(!pwplib_initcore())
   {
      pwpwrite("initialization failed!\n");
      return 0;
   }

   if(pwplib.setup[SETUP_WANTHELP])
      return 0;

   return 1;
}