/*  timerdev.c - open/close timer.device

    ©1999 Henryk Richter <tfa652@uni-rostock.de>

    This software is distributed under the terms of the GNU General Public
    License; either version 2 of the License, or (at your option) any
    later version.
*/

/*
    08-Apr-1999 Restructured by Joseph Walton
                    (hopefully without breaking anything)
*/

#include "timerdev.h"

#include <devices/timer.h>
#include <proto/timer.h>
#include <proto/exec.h>

struct Library *TimerBase;

static struct timerequest *timerIO;
static struct MsgPort *timerMP;
static BOOL timerIsOpen;

BOOL td_opentimer()
{
    if (timerMP = CreateMsgPort()) {
        if (timerIO = (struct timerequest *)CreateIORequest(timerMP,
            sizeof(struct timerequest)))
        {
            if (0 == OpenDevice(TIMERNAME, UNIT_VBLANK,
                (struct IORequest *)timerIO, 0L))
            {
                timerIsOpen = TRUE;
                TimerBase = (struct Library *)timerIO->tr_node.io_Device;
            }
        }
    }

    return timerIsOpen;
}

void td_closetimer()
{
    if (timerIO) {
        if (timerIsOpen) {
            CloseDevice( (struct IORequest *) timerIO);
            timerIsOpen = FALSE;
        }
        DeleteIORequest(timerIO);
        timerIO = NULL;
    }
    if (timerMP) {
        DeleteMsgPort(timerMP);
        timerMP = NULL;
    }
}

