/* These functions has been moved to a separate file for avoiding conflicts
 * with some declarations in chess.h such as Read()
 */

#include <exec/types.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/timer.h>
#define RAW 1L
#define CON 0L
#include <stdio.h>
#include <sys/time.h>
#include <sys/times.h>

struct Device *TimerBase = NULL ;
/* this is from chess.h */
typedef enum {cpu, elapsed, microseconds} TIME_TYPE;
typedef unsigned long long BITBOARD ;

/* timer.device routines */

static struct MsgPort *port = NULL ;
static struct timerequest *tr = NULL ;

void inittimer(void)
{
LONG error ;
    port = CreateMsgPort();
    if (port) {
        tr = (struct timerequest *)
            CreateIORequest(port, sizeof(struct timerequest));
        if (tr) {
            error = OpenDevice("timer.device", UNIT_VBLANK,
                (struct IORequest *)tr, 0);
            if (!error) {
                TimerBase = tr->tr_node.io_Device;
            }
            else {
                TimerBase = NULL ;
                DeleteIORequest((struct IORequest *)tr);
                tr = NULL ;
                DeleteMsgPort(port) ;
                port = NULL ;
            }
        }
        else {
            DeleteMsgPort(port);
            port = NULL ;
        }
    }
}

void closetimer(void)
{
    if (TimerBase) CloseDevice((struct IORequest *)tr);
    if (tr) DeleteIORequest((struct IORequest *)tr);
    if (port) DeleteMsgPort(port) ;
}



int CheckInput(void)
{
  BPTR  inp;
  BOOL  ret;

  inp=Input();
  if(!IsInteractive(inp)) return FALSE;
  Flush(inp);
  SetMode(inp,RAW);
  ret=WaitForChar(inp,1);
  SetMode(inp,CON);
  return ret;
}


unsigned int ReadClock(TIME_TYPE type)
{
struct timeval ti ;
struct tms t;
BITBOARD cputime=0;

    switch (type) {
    case cpu:
      (void) times(&t);
      cputime=t.tms_utime+t.tms_stime+t.tms_cutime+t.tms_cstime;
      cputime=cputime*100/CLK_TCK;
      return((unsigned int) cputime);
    case elapsed:
    default:
        GetSysTime(&ti) ;
        return ((ti.tv_secs * 100) + (ti.tv_micro /10000)) ;
    }
}

