/* ------------------------------------------------------------------------------------------------------------ */
/*                                                                                                              *
 *   LimitWhite V1.0 Main Module
 *                                                                                                              */
/* ------------------------------------------------------------------------------------------------------------ */

#include "LimitWhite.h"
#include "Peripherel.C"
#include "globals.h"
#include "protos.h"

/* ------------------------------------------------------------------------------------------------------------ */

int main(int ac, char *av[])
{
    CheckOSVer();                                                               /* Check for OS3.0+             */
    InstallPatches();                                                           /* Patch the OS functions       */

    DoIDCMP();                                                                  /* Enter IDCMP loop             */

    RemovePatches();                                                            /* Attempt to remove OS patches */

    CleanUp();                                                                  /* Free system resources        */
} /* if */

/* ------------------------------------------------------------------------------------------------------------ */

void wbmain(struct WBStartup *wbmsg)
{
    /* Program started from WB. Pass control over to main() */

    main(0, NULL);
} /* wbmain() */

/* ------------------------------------------------------------------------------------------------------------ */

void CheckOSVer()
{
    struct Library *VersionBase;

    /* Check OS version is at least V39 (OS 3.0) */

    if (VersionBase = OpenLibrary("version.library", 0))
    {
        if (VersionBase->lib_Version < 39)
        {
            /* OS is 2.1 or lower */

            DoEasyReq("LimitWhite V1.0 requires at least OS3.0");

            CloseLibrary(VersionBase);
            CleanUp();
        } /* if */

        CloseLibrary(VersionBase);
    } /* if */
} /* CheckOSVer() */

/* ------------------------------------------------------------------------------------------------------------ */

void DoIDCMP()
{
    CxMsg *msg;
    ULONG sigs = 0, msgid, msgtype;

    /* IDCMP loop */

    /* First, create and activate the commodity */

    SetupCx();

    /* Enter IDCMP loop */

    do
    {
        /* Wait for Cx and Ctrl-C signals */

        sigs = Wait((1L << Cx_Broker_Mp->mp_SigBit) | SIGBREAKF_CTRL_C);

        if (sigs & SIGBREAKF_CTRL_C)
        {
            AttemptExit();
        } /* if */

        else
        {
            /* Cx message */

            while (msg = (CxMsg *)GetMsg(Cx_Broker_Mp))
            {
                /* Get message details and reply to it */

                msgid = CxMsgID(msg);
                msgtype = CxMsgType(msg);

                ReplyMsg((struct Message *)msg);

                /* Act upon the message */

                switch(msgtype)
                {
                    case CXM_COMMAND:

                        /* Commodities command */

                        switch(msgid)
                        {
                            case CXCMD_KILL:

                                /* Attempt to exit program */

                                AttemptExit();

                                break;

                            case CXCMD_UNIQUE:

                                /* User attempted to start another copy of LimitWhite */

                                DoEasyReq("LimitWhite is already running");

                                break;

                            default:

                                /* Show/Hide interface/Enable/Disable. Not relevant */

                                break;
                        } /* switch */

                        break;

                    default:

                        /* Show/Hide interface. Not relevant */

                        break;
                } /* switch */
            } /* while */
        } /* else */
    } while (Running == TRUE); /* do */
} /* DoIDCMP() */

/* ------------------------------------------------------------------------------------------------------------ */

void SetupCx()
{
    struct NewBroker Cx_NewBroker = {
        NB_VERSION,
        "LimitWhite",
        "LimitWhite V1.0 - ToneMaster, 2000",
        "Reduces white colours to grey",
        NBU_UNIQUE | NBU_NOTIFY,
        0,
        0,
        NULL,
        0
    };

    /* Set up commodities port */

    if (!(Cx_Broker_Mp = CreateMsgPort()))
    {
        RemovePatches();
        CleanUp();
    } /* if */

    Cx_NewBroker.nb_Port = Cx_Broker_Mp;

    if (!(Cx_Broker = (CxObj *)CxBroker(&Cx_NewBroker, NULL)))
    {
        RemovePatches();
        CleanUp();
    } /* if */

    ActivateCxObj(Cx_Broker, 1L);
} /* SetupCx() */

/* ------------------------------------------------------------------------------------------------------------ */

void AttemptExit()
{
    /* Attempt to remove the patches */

    if (RemovePatches())
    {
        Running = FALSE;
    } /* if */

    else
    {
        DoEasyReq("A program has patched LimitWhite's patches.\nLimitWhite cannot quit, but can be disabled");
    } /* else */
} /* AttemptExit() */

/* ------------------------------------------------------------------------------------------------------------ */

void CleanUp()
{
    CxMsg *msg;

    /* Free system resources */

    if (Cx_Broker)
    {
        DeleteCxObjAll(Cx_Broker);
    } /* if */

    while (msg = (CxMsg *)GetMsg(Cx_Broker_Mp))
    {
        ReplyMsg((struct Message *)msg);
    } /* while */

    DeleteMsgPort(Cx_Broker_Mp);

    exit(0);
} /* CleanUp() */

/* ------------------------------------------------------------------------------------------------------------ */

/* End Of Text */