/* StarGateExample 2.0
   (c) 1999-2000 by Felix Schwarz (felix@innovative-web.de).
   All rights reserved.
   This sourcecode is placed in the Public Domain:
    Use it for whatever you want, but don`t make
    me responsible, if anything doesn`t work as
    it should. This sourcecode is supplied "as is"
    and I cannot be held responsible, if it causes
    any damages and/or errors, loss of money, health
    or whatever. Use it fully at your own risk.

   The StarGate Plugin may be included into an archive
   with previous permission by Felix Schwarz. Drop me
   (felix@innovative-web.de) a mail and ask for a free
   license.
*/

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

#include <exec/ports.h>
#include <clib/exec_protos.h>
#include "stargate.h"

struct fsbitmap
{
        APTR location;   // Pointer to the RGB-data
        long width;      // width of the image to transmit
        long height;     // height of the image to transmit
        long type;       // Set this to 3
};

// Send an image to fxPAINT without saving it inbetween
BOOL sendimg_to_stargate(struct fsbitmap *fsb)
{
        struct SGMessage msg;
        struct Message *reply;
        struct MsgPort *ReplyPort;
        struct MsgPort *SGPort;
        BOOL ok=FALSE;

        if (ReplyPort=CreateMsgPort())
        {
                Forbid();
                if (SGPort=FindPort("Stargate Airport"))
                {
                        msg.msg.mn_Node.ln_Type = NT_MESSAGE;
                        msg.msg.mn_Length       = sizeof(struct SGMessage);
                        msg.msg.mn_ReplyPort    = ReplyPort;
                        msg.fsb                 = fsb;

                        PutMsg(SGPort, (struct Message *) &msg);
                }
                Permit();

                if (SGPort)
                {
                        Wait(1L << ReplyPort->mp_SigBit);
                        reply=GetMsg(ReplyPort);
                        ok=TRUE;
                }
                else
                {
                        // Give out this (or similiar) error message here:
                        // "Stargate not found or fxPAINT not started"
                }

                DeleteMsgPort(ReplyPort);
        }

        return(ok);
}

// Send a filename to fxPAINT in order to fxPAINT loading that file
BOOL sendfile_to_stargate (char *file)
{
        struct SGMessageExt msg;
        struct Message *reply;
        struct MsgPort *ReplyPort;
        struct MsgPort *SGPort;
        BOOL ok=FALSE;

        if (ReplyPort=CreateMsgPort())
        {
                Forbid();
                if (SGPort=FindPort("Stargate Airport"))
                {
                        msg.msg.mn_Node.ln_Type = NT_MESSAGE;
                        msg.msg.mn_Length       = sizeof(struct SGMessageExt);
                        msg.msg.mn_ReplyPort    = ReplyPort;
                        msg.type                = SG_LOAD_IMAGE;
                        msg.data                = (APTR) file;

                        PutMsg(SGPort, (struct Message *) &msg);
                }
                Permit();

                if (SGPort)
                {
                        Wait(1L << ReplyPort->mp_SigBit);
                        reply=GetMsg(ReplyPort);
                        ok=TRUE;
                }
                else
                {
                        // Give out this (or similiar) error message here:
                        // "Stargate not found or fxPAINT not started"
                }

                DeleteMsgPort(ReplyPort);
        }

        return(ok);
}

// Get number of pictures in memory of fxPAINT
long num_of_pics_in_stargate (void)
{
        struct SGMessageExt msg;
        struct Message *reply;
        struct MsgPort *ReplyPort;
        struct MsgPort *SGPort;
        long res=0;

        if (ReplyPort=CreateMsgPort())
        {
                Forbid();
                if (SGPort=FindPort("Stargate Airport"))
                {
                        msg.msg.mn_Node.ln_Type = NT_MESSAGE;
                        msg.msg.mn_Length       = sizeof(struct SGMessageExt);
                        msg.msg.mn_ReplyPort    = ReplyPort;
                        msg.type                = SG_NUM_OF_IMGS;
                        msg.data                = NULL;

                        PutMsg(SGPort, (struct Message *) &msg);
                }
                Permit();

                if (SGPort)
                {
                        Wait(1L << ReplyPort->mp_SigBit);
                        reply=GetMsg(ReplyPort);
                        res=(ULONG) msg.data;
                }
                else
                {
                        // Give out this (or similiar) error message here:
                        // "Stargate not found or fxPAINT not started"
                }

                DeleteMsgPort(ReplyPort);
        }

        return(res);
}

// Get pointer to picture n`s fsb
struct fsbitmap *get_fsb_in_stargate (long n)
{
        struct SGMessageExt msg;
        struct Message *reply;
        struct MsgPort *ReplyPort;
        struct MsgPort *SGPort;
        struct fsbitmap *res=NULL;

        if (ReplyPort=CreateMsgPort())
        {
                Forbid();
                if (SGPort=FindPort("Stargate Airport"))
                {
                        msg.msg.mn_Node.ln_Type = NT_MESSAGE;
                        msg.msg.mn_Length       = sizeof(struct SGMessageExt);
                        msg.msg.mn_ReplyPort    = ReplyPort;
                        msg.type                = SG_GET_BITMAP;
                        msg.data                = (ULONG) n;

                        PutMsg(SGPort, (struct Message *) &msg);
                }
                Permit();

                if (SGPort)
                {
                        Wait(1L << ReplyPort->mp_SigBit);
                        reply=GetMsg(ReplyPort);
                        res=(ULONG) msg.data;
                }
                else
                {
                        // Give out this (or similiar) error message here:
                        // "Stargate not found or fxPAINT not started"
                }

                DeleteMsgPort(ReplyPort);
        }

        return(res);
}

/* EXAMPLE 1 */

void main(int argc, char *argv[])
{
        if (argc > 1)
        {
                printf("Sending %s to fxPAINT ..\n",argv[1]);
                if (sendfile_to_stargate(argv[1]))
                {
                        printf(".. sent!\n");
                        exit(0);
                }
                else
                {
                        printf("Stargate not found or fxPAINT not running ..\n");
                        exit(5);
                }
        }
        else
        {
                printf("Usage: StarGateExample [file]\n");
                exit(5);
        }
}

/* EXAMPLE 2 */
/* void main(int argc, char *argv[])
{
        long num;
        struct fsbitmap *fsb;
        FILE *p_out;

        if (argc > 2)
        {
                if (num = num_of_pics_in_stargate ())
                {
                        printf("Pictures in memory: %ld\n",num);

                        if ((atol(argv[2])>=0) && (atol(argv[2])<num))
                        {
                                if (fsb = get_fsb_in_stargate(atol(argv[2])))
                                {
                                        if (p_out=fopen(argv[1],"wb"))
                                        {
                                                fprintf (p_out,"P6\n%ld %ld\n255\n", fsb->width, fsb->height);
                                                fwrite  (fsb->location, fsb->width*fsb->height*3, 1, p_out);
                                                fclose  (p_out);
                                        }
                                }
                        }
                }
                else
                {
                        printf("Stargate not found, no pictures available or fxPAINT not running ..\n");
                        exit(5);
                }
        }
        else
        {
                printf("Usage: StarGateExample [file] [num]\n");
                exit(5);
        }
} */
