;/* Execute me to compile under SAS C 5.10...
asm -iIncdir: iesupport.a
lc wincloser
BLink from LIB:c.o+wincloser.o+iesupport.o LIB LIB:lcs.lib LIB:amiga.lib TO WinCloser
quit
*/

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/nodes.h>
#include <devices/input.h>
#include <intuition/intuition.h>
#include <exec/interrupts.h>
#include <proto/all.h>


/*
 * Defines and Structs...
 */
#define MYPORT "private.port"

struct PrivateMessage {
	struct Message pm_Message;
	char *TextPointer;
	};


/*
 * Puny Prototypes...
 */
int CXBRK(void) { return(0); }
int chkabort(void) { return(0); }
struct MsgPort *SendMessage(void);
void WaitForUser(void);
extern VOID AlternateCloseWin();


/*
 * Version info...
 */
char *versioninfo="\0$VER: WindowCloser version 1.0\n";


/*
 * Meat and Potatoes... 
 */
void main(void)
{
struct IOStdReq  *inputReqBlk;
struct MsgPort   *inputPort;
struct Interrupt *inputHandler;

if(SendMessage())return;	/* If other copy running abort both... */

    if (inputPort=CreatePort(NULL,NULL))
    {
        if (inputHandler=AllocMem(sizeof(struct Interrupt),
                                   MEMF_PUBLIC|MEMF_CLEAR))
        {
            if (inputReqBlk=(struct IOStdReq *)CreateExtIO(inputPort,
                                                     sizeof(struct IOStdReq)))
            {
                if (!OpenDevice("input.device",NULL,
                                 (struct IORequest *)inputReqBlk,NULL))
                {
                    inputHandler->is_Code=AlternateCloseWin;
                    inputHandler->is_Data=NULL;
                    inputHandler->is_Node.ln_Pri=100;
                    inputHandler->is_Node.ln_Name="BSIWindowCloser";
                    inputReqBlk->io_Data=(APTR)inputHandler;
                    inputReqBlk->io_Command=IND_ADDHANDLER;
                    DoIO((struct IORequest *)inputReqBlk);

                    WaitForUser();

                    inputReqBlk->io_Data=(APTR)inputHandler;
                    inputReqBlk->io_Command=IND_REMHANDLER;
                    DoIO((struct IORequest *)inputReqBlk);

                    CloseDevice((struct IORequest *)inputReqBlk);
                }
                DeleteExtIO((struct IORequest *)inputReqBlk);
            }
            FreeMem(inputHandler,sizeof(struct Interrupt));
        }
        DeletePort(inputPort);
    }
}


/*
 * This Waits for the user...
 */
void WaitForUser(void) {

struct MsgPort *export;
struct PrivateMessage *messy;

if(export=CreatePort(MYPORT,NULL)) {

for(;;) {
	WaitPort(export);
	if(messy=(struct PrivateMessage *)GetMsg(export))break;
	}
ReplyMsg((struct Message *)messy);
DeletePort(export);}
}


/*
 * SendMessage
 */
struct MsgPort *SendMessage(void) {

struct MsgPort *export,*inport;
struct PrivateMessage *mymessage;
char *samplemess="WindowCloser(w)1993 BSI";

Forbid();
if(export=FindPort(MYPORT)) {
if(mymessage=AllocMem(sizeof(struct PrivateMessage),MEMF_PUBLIC|MEMF_CLEAR)){
if(inport=CreatePort(NULL,NULL)){
mymessage->pm_Message.mn_Node.ln_Type=NT_MESSAGE;
mymessage->pm_Message.mn_Length=sizeof(struct PrivateMessage);
mymessage->pm_Message.mn_ReplyPort=inport;
mymessage->TextPointer=samplemess;

PutMsg(export,(struct Message *)mymessage);
WaitPort(inport);

DeletePort(inport);}
FreeMem(mymessage,sizeof(struct PrivateMessage));}
Permit();
return(export);
}
}
