
/*
 *  CBIO.C
 *
 *  This is the clipboard section of "snip". I am going to try and be as
 *  faithful as possible to the C-A example code (for now :-). Probably
 *  later I will economize and maybe add an extra bit of functionality
 *  here and there.
 *
 *  John Russell    03-07-88
 *
 */

#include "exec/types.h"
#include "exec/ports.h"
#include "exec/io.h"
#include "devices/clipboard.h"

/* never seen struct = 0 before */

struct IOClipReq cbIO = 0;
struct MsgPort cbMsgPort = 0, satisfyMsgPort = 0;

int CBOpen(unit)    /* do initial open of clipboard device */
register int unit;
{
    register int error;

    if ((error = OpenDevice("clipboard.device",unit,&cbIO,0)) != 0)
                return(error);

    /* open OK, need to set up MsgPort stuff (would be nice to have some support routines for this grunge work) */
        /* still it's easy enough to make a macro out of it */

    cbMsgPort.mp_Node.ln_Type = NT_MSGPORT;
    cbMsgPort.mp_Flags = 0;
    cbMsgPort.mp_SigBit = AllocSignal(-1);
    cbMsgPort.mp_SigTask = (struct Task *)FindTask((char *)NULL);
    AddPort(&cbMsgPort);

    cbIO.io_Message.mn_ReplyPort = &cbMsgPort;

    satisfyMsgPort.mp_Node.ln_Type = NT_MSGPORT;
    satisfyMsgPort.mp_Flags = 0;
    satisfyMsgPort.mp_SigBit = AllocSignal(-1);
    satisfyMsgPort.mp_SigTask = (struct Task *)FindTask((char *)NULL);
    AddPort(&satisfyMsgPort);

    return(0);
}

CBClose()
{
    RemPort(&satisfyMsgPort);
    RemPort(&cbMsgPort);
    CloseDevice(&cbIO);
}

CBSatisfyPost(string,length)
register char *string;
int length;
{
    cbIO.io_Offset = 0;
    writeLong("FORM");
    length += 12;
    writeLong(&length);
    writeLong("FTXT");
    writeLong("CHRS");
    length -= 12;
    writeLong(&length);

    cbIO.io_Command = CMD_WRITE;
    cbIO.io_Data = string;
    cbIO.io_Length = length;
    DoIO(&cbIO);

    cbIO.io_Command = CMD_UPDATE;
    return(DoIO(&cbIO));
}

writeLong(data)
register long *data;
{
    cbIO.io_Command = CMD_WRITE;
    cbIO.io_Data = data;
    cbIO.io_Length = 4;
    DoIO(&cbIO);
}

CBCut(string,length)
register char *string;
register int length;
{
    cbIO.io_ClipID = 0;
    return(CBSatisfyPost(string,length));
}

/* NB: since I only want to send things to the ClipBoard I don't include the "Paste" routine */

