
/***************************************************************************
    1/89

        DIRK v2.0 -- Tune workbench colors to system performance

        Copyright (C) 1989 by Daniel Elbaum

        This software is freely redistributable provided that:
        the four files which comprise it (dirk, dirk.doc, dirk.h,
        adjust.c, main.c, sys, window.c) remain intact; all
        copyright notices contained in any of the aforementioned
        files remain intact; and no fee beyond reasonable remuneration
        for collation and distribution be charged for use and/or
        distribution.

***************************************************************************/

#define MAIN
#include "dirk.h"
#define ODD_BEHAVIOR

main(c, v)
char **v;
{
    register i, active;
    register ULONG tm;
    register wtog;
    ULONG Class;
    USHORT Code;
    struct Screen *sp;
    struct Window *wp;
    register struct IntuiMessage *msg=NULL;
    t_gf g_f;
    struct Window *getwin();
    ULONG totmem();
    void redowin(), setcolors(), fillwin();

    if (getargs(++v, &g_f)<0) exit(10);

    if (!(IntuitionBase=(t_ib *)OpenLibrary(INAM, IREV))){
        printf("Couldn't open intuition library\n");
        exit(99);
    }
    if (!(GfxBase=(t_gb *)OpenLibrary(GNAM, GREV))){
        printf("Couldn't open graphics library\n");
        CloseLibrary(IntuitionBase);
        exit(98);
    }
    if (!(sp=IntuitionBase->FirstScreen)){
        printf("Couldn't find any screens\n");
        CloseLibrary(IntuitionBase);
        CloseLibrary(GfxBase);
        exit(97);
    }
    for (; sp; sp=sp->NextScreen)
        if ((sp->Flags&SCREENTYPE)==WBENCHSCREEN)
            break;
    if (!sp) {
        printf("Couldn't find Workbench screen\n");
        CloseLibrary(IntuitionBase);
        CloseLibrary(GfxBase);
        exit(96);
    }
    msg=NULL;
    wtog=W_CLOSED;
    active=0;   /* ACTIVATE not requested by getwin()   */
    if (!(wp=getwin(sp))){
        printf("Couldn't open Dirk's window\n");
        CloseLibrary(IntuitionBase);
        CloseLibrary(GfxBase);
        exit(95);
    }
    if (g_f.bstog) tm=totmem();
    for (;;) {
        while (wp&&(msg||(msg=GetMsg(wp->UserPort)))){
            Class=msg->Class;
            Code=msg->Code;
            ReplyMsg(msg);
            msg=NULL;
            switch(Class){      /* assume sizeof(int)==sizeof(ULONG)    */
                case ACTIVEWINDOW:
                    active=1;
                    break;
                case INACTIVEWINDOW:
                    active=0;
                    break;
                case MOUSEBUTTONS:
                    if ((Code&0xFFFF)==MENUDOWN){
                        redowin(wp, wtog);
                        if (wtog==W_CLOSED) {fillwin(wp); wtog=W_OPEN;}
                        else wtog=W_CLOSED;
                    }
                    break;
                case CLOSEWINDOW:
                    if (wp){
                        while (msg=GetMsg(wp->UserPort))
                            ReplyMsg(msg);
                        CloseWindow(wp);
                        wp=NULL;
                    }
                    break;
            }
        }
        if (!wp) {
            CloseLibrary(IntuitionBase);
            CloseLibrary(GfxBase);
            exit(0);
        }
        if (active)
            WaitTOF();
        else{
            for (i=0; i<g_f.intvl; ++i)
                if (msg=GetMsg(wp->UserPort)) break;
                else WaitTOF();
        }
        if (wtog==W_OPEN) fillwin(wp);
        setcolor(sp, tm, &g_f);
    }
    /* NOTREACHED   */
}


