/* Test program to demonstrate the message interface to Post V1.0.
 * (C) Adrian Aylward 1989, 1990
 *
 * You may freely copy, use, and modify this file.  It was tested using
 * Lattice C V5.04
 *
 * To run this program, you need to be in a directory containing the files
 * "post", "init.ps", "prog1.ps", "prog2.ps".  Then type
 *
 *     run pmsg
 *
 * to the cli.  Wait until the disc has finished.  Then type
 *
 *     post screen size x640y400 messageport post.test
 *
 * To recompile this program type
 *
 *     lc -L pmsg.c
 */

# include <dos.h>
# include <exec/exec.h>
# include <exec/tasks.h>
# include <intuition/intuition.h>
# include <proto/dos.h>
# include <proto/exec.h>
# include <proto/graphics.h>
# include <proto/intuition.h>
# include "stdio.h"
# include "stdlib.h"

# include "postmsg.h"

struct MessagePort *testport;

struct NewScreen newscreen =
{   0, 0, 640, 400, 3, 0, 15, LACE|HIRES, CUSTOMSCREEN,
    NULL, NULL, NULL, NULL
};
struct Screen *screen;
static short ccolors[8] =
{   0xfff, 0x0ff, 0xf0f, 0x00f, /* White   cyan    magenta blue */
    0xff0, 0x0f0, 0xf00, 0x000  /* Yellow  green   red     black */
};


int returncode;
int count;

main(int argc, char **argv)
{   struct PSMessage *msg;
    int action;

    /* Open the libraries */

    GfxBase =
        (struct GfxBase *)       OpenLibrary("graphics.library", 0);
    IntuitionBase =
        (struct IntuitionBase *) OpenLibrary("intuition.library", 0);
    if (GfxBase == NULL || IntuitionBase == NULL)
    {   fprintf(stderr, "Can't open libraries\n");
        goto errorexit;
    }

    /* Open a screen and set up the colours */

    screen = OpenScreen(&newscreen);
    if (screen == NULL)
    {   fprintf(stderr, "Can't open screen\n");
        goto errorexit;
    }
    LoadRGB4(&screen->ViewPort, ccolors, 8);

    /* Create the message port */

    testport = CreatePort("post.test", 0);
    if (testport == NULL)
    {   fprintf(stderr, "Can't create port\n");
        goto errorexit;
    }

    /* Loop processing messages, until we get a CLOSE */

    for (;;)
    {   for (;;)
        {   msg = (struct PSMessage *) GetMsg(testport);
            if (msg) break;
            WaitPort(testport);
        }

        action = msg->action;
        switch(action)
        {   case PSACTOPEN:    /* Open */
                break;

            case PSACTCLOSE:   /* Close */
                break;

            case PSACTFLUSH:   /* Flush out the bitmap */
                BltBitMapRastPort(msg->bitmap,            /* Bitmap */
                                  0, msg->y1,             /* source pos */
                                  &screen->RastPort,      /* RastPort */
                                  0, msg->y1,             /* Target pos */
                                  640, msg->y2 - msg->y1, /* Size */
                                  0xC0);                  /* Copy */
                break;

            case PSACTPAUSE:   /* Pause at the end of a page */
                Delay(250);
                break;

            case PSACTCOMMAND: /* Get a command */
                count++;       /* Send the commands in sequence */
                if      (count == 1)
                {   msg->command = PSCOMFILEL;
                    msg->string = "init.ps";
                }
                else if (count == 2)
                {   msg->command = PSCOMSTRING;
                    msg->string = "(prog1.ps) run";
                    msg->length = -1;
                }
                else if (count == 3)
                    msg->command = PSCOMRESTART;
                else if (count == 4)
                {   msg->command = PSCOMFILEL;
                    msg->string = "init.ps";
                }
                else if (count == 5)
                {   msg->command = PSCOMFILER;
                    msg->string = "prog2.ps";
                }
                else
                    msg->command = PSCOMQUIT;
                break;

            case PSACTEXIT:    /* Exit */
                msg->command = PSCOMQUIT;
                break;
        }

        /* Comment this out to omit trace output */

        fprintf(stdout, "msg act %d com %d res %d err %d\n",
                msg->action, msg->command, msg->result, msg->errnum);

        ReplyMsg((struct Message *) msg);
        if (action == PSACTCLOSE) goto tidyup;
    }

errorexit:
    returncode = 20;

tidyup:
    if (testport) DeletePort(testport);
    if (screen) CloseScreen(screen);
    if (GfxBase) CloseLibrary((struct Library *) GfxBase);
    if (IntuitionBase) CloseLibrary((struct Library *) IntuitionBase);

    exit(returncode);
}
