/* > main.c

 * SJ Middleton, 1992

 */

#include "akbd.h"
#include "alarm.h"
#include "baricon.h"
#include "dbox.h"
#include "flex.h"
#include "help.h"
#include "menu.h"
#include "msgs.h"
#include "res.h"
#include "template.h"
#include "visdelay.h"
#include "werr.h"
#include "wimp.h"
#include "wimpt.h"
#include "win.h"
#include "xferrecv.h"

#include <stdlib.h>

#include "armovie.h"
#include "arsound.h"
#include "utils.h"

#define TASK_TITLE      msgs_lookup("taskname:ARSound2")
#define APP_TITLE       "ARSound2"

#define VERSION_NUMBER  100

/* External functions (should use header file really) */

extern void demo_open(void);

/* ---------------------------------------------------------------------------------- */

#define PROG_INFO_IDENT         "progInfo"

static void wmisc_popup_info(int version_number, const char *date)
{
    dbox  d;

    if ((d = dbox_new(PROG_INFO_IDENT)) != NULL)
    {
        char b[64];
        sprintf(b, "%d.%02d - %s", version_number / 100, version_number % 100, date);
        dbox_setfield(d, 4, b);
        dbox_show(d);

        dbox_fillin(d);

        dbox_dispose(&d);
    }
}

/* ---------------------------------------------------------------------------------- */

#define ICONBAR_MENU    msgs_lookup("imenu:>Info,Demo ...,Quit")

enum
{
    mmenu_Info = 1,
    mmenu_Demo = 2,
    mmenu_Quit
};

static menu mmenu;

static void mmenuproc(void *handle, char *hit)
{
    switch (hit[0])
    {
        case mmenu_Info:
            wmisc_popup_info(VERSION_NUMBER, __DATE__);
            break;

        case mmenu_Demo:
            demo_open();
            break;

        case mmenu_Quit:
            exit(EXIT_SUCCESS);
    }
    handle = handle;
}


static menu makemenus(void *handle)
{                
    return mmenu;
    handle = handle;
}


static BOOL init_iconbar_menu(void)
{
    if ((mmenu = menu_new(APP_TITLE, ICONBAR_MENU)) == NULL)
        return FALSE;

    if (!event_attachmenumaker(win_ICONBAR, makemenus, mmenuproc, 0))
        return FALSE;

    return TRUE;
}

/* ---------------------------------------------------------------------------------- */

static BOOL movie_playing = FALSE;
static int movie_reference = 1;

/*
 * The movie event handler - showing the return types you can get

 * Any code <= armovie_Aborted means that playback has stoped.
 * Any code > armovie_Aborted is for information - playback still continues.

 */

static void movie_events(int reference, armovie_returncode code, void *handle)
{
    switch (code)
    {
        case armovie_Completed:     /* finished successfully */
            werr(0, "Movie %d completed successfully", reference);
            movie_playing = FALSE;
            break;

        case armovie_Error:         /* error occurred so stopped playback */
            werr(0, "Movie %d stopped becase of error", reference);
            movie_playing = FALSE;
            break;

        case armovie_ModeChange:    /* finished because a mode change happened */
            werr(0, "Movie %d stopped because of mode change", reference);
            movie_playing = FALSE;
            break;

        case armovie_Aborted:
            werr(0, "Movie %d stopped due to device claim or other abort situation (%d)", reference, code);
            movie_playing = FALSE;
            break;

        default:
            if (code <= armovie_Aborted)
            {
                werr(0, "Movie %d stopped due to unknown reason %d", reference, code);
                movie_playing = FALSE;
            }
            break;
    }

    handle = handle;
}

/* ---------------------------------------------------------------------------------- */

static void iconclick(wimp_i icon)
{
    armovie_stopsound();    /* safe to call at any time - even if no movie playing */
    icon = icon;
}

/*
 * Drag movie file to iconbar to play it
 * If SHIFT held down then queue it after current finishes - else play immediately.
 * If CTRL held down then loop it 3 times - else just play once.
 */

static void iconbar_loadproc(wimp_eventstr *e, void *handle)
{
    char        *filename;
    if (xferrecv_checkinsert(&filename) == filetype_ARMovie)
    {                     
        armovie_playflag playflags = 0;

        if (akbd_pollsh())
            playflags |= arplay_QueueSound;
        if (akbd_pollctl())
            playflags |= arplay_Loop;

        if (armovie_playsound(filename,
                                0,                  /* start time in cs */
                                0,                  /* end time in cs */
                                3,                  /* number of loops */
                                playflags,          /* flags */
                                1,                  /* soundtrack number */
                                movie_reference     /* reference code for movie */
                                ) != NULL)
        {
            movie_playing = TRUE;
            movie_reference++;

/* Register an event handler */
		    armovie_eventhandler(movie_events, NULL);
        }

        xferrecv_insertfileok();
    }
    e = e;
    handle = handle;
}

/* ---------------------------------------------------------------------------------- */

static BOOL initialise(void)
{
    res_init(APP_TITLE);            /* Resources */
    msgs_init();

    wimpt_init(TASK_TITLE);         /* Main Wimp initialisation */
    visdelay_init();

    template_init();                /* Templates */
    dbox_init();                    /* Dialogue boxes */
    alarm_init();

    flex_init();

    baricon("!"APP_TITLE, 1, iconclick);

/* ------------------ Init Iconbar menu ----------------- */

    if (!init_iconbar_menu())
        return FALSE;

    win_register_event_handler(win_ICONBARLOAD, iconbar_loadproc, NULL);

/* Init system and let it set up its own event handler for MODECHANGE events */
    armovie_init();

/* Use flex to allocate temporary memory for each sound chunk */
    armovie_registermemfns(f_alloc, f_free);

/* We won't use flex for catalogues because we are going to use
 * them whilst allocating memory in demo.c - in normal use it
 * would be fine to do this.
 */

/* 	armovie_registercataloguememfns(f_alloc, f_free); */

/* Check for dynamic areas */
    if (dynamicarea_init())
       armovie_registersoundmemfns(d_alloc, d_free);

    return TRUE;
}

int main(int argc, char *argv[])
{
    if (initialise())
        while (TRUE)
            event_process();

    return EXIT_FAILURE;

    argc = argc;
    argv = argv;
}


/* eof main.c */
