view pwplib/pwplib-unix.c @ 71:a87eb778f225

Improvements to the MinGW crossbuild. Should now build with default tools from Debian mingw packages, though you need Win32 version of libSDL with the necessary headers and so on in addition. 64-bit builds not tested and probably won't work. Tested on Debian 7.0, earlier won't work. binutils-mingw-w64-i686 gcc-mingw-w64-i686 mingw-w64-i686-dev
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 14 Aug 2012 03:08:10 +0300
parents 5d819ba6891c
children
line wrap: on
line source

#define __PWPLIB_C

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>

#ifdef DRIVE_SDL
#include <SDL.h>
#endif

#include "pwplib.h"
#include "sound.h"
#include "video.h"
#include "tty.h"


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

static int sdl_init = 0;

extern void pwplib_dummy();

void pwpwrite(const char *fmt, ...)
{
    if (!pwplib.setup[SETUP_SHUTUP]) {
        va_list ap;
        va_start(ap, fmt);
        vfprintf(stderr, fmt, ap);
        va_end(ap);

        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;
#ifdef FISI_HACK
              case SDL_QUIT:
                  quit = 1;
                  break;
#endif
            }
        }
        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)
{
    pwpwrite("* pwplib shutting down\n");

#ifdef DRIVE_SDL
    if (sdl_init) {
        sdl_init = 0;
        SDL_Quit();
    }
#endif

    pwplib_shutdown();

    pwpwrite("* died to signal %d\n", n);

    exit(1);
}

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

int pwplib_initcore()
{
    int sdl_flags = 0;

    pwplib_init_common();

   /*** setup signals, etc ***/
#ifdef DRIVE_SDL
#ifdef DRIVE_VIDEO
    sdl_flags |= SDL_INIT_VIDEO;
#endif
#ifdef DRIVE_AUDIO
    sdl_flags |= SDL_INIT_AUDIO;
#endif
#ifdef FISI_HACK
    sdl_flags |= SDL_INIT_NOPARACHUTE;
#endif
    /* With SDL, we let it handle the signals */
    if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_EVENTTHREAD | sdl_flags) != 0) {
        pwpwrite("* SDL could not be initialized (%s).\n", SDL_GetError());
        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_SDL_audio_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])
    {
        pwpwrite("non-realtime output (bps=%d fps=%d)\n", pwplib.setup[SETUP_BPS], pwplib.setup[SETUP_FPS]);
        pwplib.timerfunc = pwp_timer_nrt;
        pwplib.timer_counter = 0;
    }
    else
        pwplib.timerfunc = pwp_unix_tod;

   /*** hmmm ***/

    if (!sdl_init && 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])
    {
        pwpwrite("* pwplib now controls a %d x %d framebuffer\n", pwplib.videobuf.width, pwplib.videobuf.height);
    }
#  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)
{
    pwplib.argc = argc;
    pwplib.argv = argv;

    pwplib_getopts();

    if (pwplib.setup[SETUP_WANTHELP])
    {
        pwplib_printhelp();
        exit(0);
    }

    if (!pwplib.setup[SETUP_SHUTUP])
    {
        fputs(pwplogo, stderr);
        pwpwrite("pwplib " PWPLIB_VERSION " initializing..\n\nrun with --help for some options\n\n");
    }

    pwpwrite("* using locale '%s'\n", pwplib.set.lang);

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

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

    return 1;
}