/*CloseWindow v0.2 by Lars Dannenberg (21.03.94) L.DANNENBERG@KDS.ZER.SUB.ORG

 schließt ein beliebiges Fenster auf einem PubScreen

 Syntax: CloseWindow <Screenname> <Windowname>

                                      (Groß- und Kleinschreibung beachten)

 das Ganze läuft systemkonform ab:

 an das Fenster wird ein InputEvent IECLASS_CLOSEWINDOW
 gesendet, also kein intuition/CloseWindow()-Hack wie mit
 anderen Programmen.

*/


#include <exec/types.h>
#include <exec/memory.h>
#include <exec/io.h>
#include <devices/input.h>
#include <devices/inputevent.h>
#include <intuition/intuition.h>

#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>

#include <stdio.h>

#define LIBVER 37 /* Brauche OS>=2.0 !!! */

struct Library *IntuitionBase = NULL;


int main(int argc,char *argv[])
{
    struct MsgPort *InputMP;
    struct IOStdReq *InputIO;
    struct Screen *ScreenPtr;
    struct Window *WindowPtr;
    struct InputEvent myevent;

    BOOL    window_found = FALSE;

    if((argc==3))
    {
      if((IntuitionBase = (struct Library*) OpenLibrary("intuition.library",LIBVER)))
      {
        if((InputMP = (struct MsgPort*)  CreateMsgPort()))
        {
          if((InputIO = (struct IOStdReq*) CreateIORequest(InputMP,sizeof(struct IOStdReq))))
          {
            if(!(OpenDevice("input.device",0,(struct IORequest*) InputIO,0)))
            {
              if((ScreenPtr = (struct Screen*) LockPubScreen(argv[1])))
              {
                WindowPtr=ScreenPtr->FirstWindow;       /*Zeiger auf 1.Window*/
                while ((!window_found) && (WindowPtr))  /*Window suchen bis
                                                          gefunden oder letztes Window */

                {
                  if(!(strcmp(argv[2],WindowPtr->Title)))
                     window_found=TRUE;
                  /* Stimmt der Titel überein ? */
                  else WindowPtr=WindowPtr->NextWindow;
                  /* Nein, dann nächstes */
                }
                if (window_found)
                {
                    if (WindowPtr->Flags & WFLG_CLOSEGADGET)
                    {
                        Forbid();
                        ActivateWindow(WindowPtr);

                        InputIO->io_Data=&myevent;
                        InputIO->io_Length=sizeof (struct InputEvent);
                        InputIO->io_Command=IND_WRITEEVENT;
                        myevent.ie_Class=IECLASS_CLOSEWINDOW;
                        DoIO((struct IORequest*) InputIO);
                        /* IORequest abschicken */
                        Permit();
                    }
                    else printf("Fenster \"%s\" hat kein CloseGadget !\n",argv[2]);
                }
                else printf("Kann Fenster \"%s\" nicht finden !\n",argv[2]);
                UnlockPubScreen(NULL,ScreenPtr);
              }
              else printf("Bekomme keinen Lock auf Screen \"%s\" !\n",argv[1]);
              CloseDevice((struct IORequest*) InputIO);
            }
            else printf("Kann input.device nicht öffnen !\n");
          DeleteMsgPort(InputMP);
          DeleteIORequest(InputIO);
        }
        else printf("Kann keinen IORequest erstellen !\n");
      }
      else printf("Kann keinen MessagePort öffnen !\n");
      CloseLibrary(IntuitionBase);
    }
    else printf("Kann intuition.library v%d nicht öffnen !\n",LIBVER);
  }
  else printf("Usage: %s <ScreenName> <Windowname>\n",argv[0]);
}
