home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d9xx / d995 / xprkermit.lha / XprKermit / source.lha / ckxtim.c < prev    next >
C/C++ Source or Header  |  1993-03-26  |  2KB  |  93 lines

  1. /*
  2.  * Timer.device routines from RKM, slightly modified.
  3.  *
  4.  * Version 1.88:  Renamed routine TimeDelay() to MyDelay(), since the former
  5.  * conflicts with a routine of the same name in the 2.0 amiga.lib.
  6.  *
  7.  * Version 1.90:  Created a GetSysTime() routine.  Made sure that if
  8.  * the CreateExtIO() in CreateTimer() fails, the timerport is deleted.
  9.  */
  10.  
  11. #ifndef _lint
  12. static char rcsid[] = "$Header$";
  13. #endif
  14.  
  15. #include "ckxtim.h"
  16.  
  17. void
  18. DeleteTimer(struct timerequest *tr) {
  19.    struct MsgPort *tp;
  20.  
  21.    if (tr != 0) {
  22.       tp = tr->tr_node.io_Message.mn_ReplyPort;
  23.       if (tp != 0)
  24.          DeletePort(tp);
  25.       CloseDevice((struct IORequest *) tr);
  26.       DeleteExtIO((struct IORequest *) tr);
  27.    }
  28. }
  29.  
  30. struct timerequest *
  31. CreateTimer(ULONG unit) {
  32.    /* return a pointer to a timer request.  If any problem, return NULL. */
  33.  
  34.    LONG error;
  35.    struct MsgPort *timerport;
  36.    struct timerequest *timermsg;
  37.    
  38.    timerport = CreatePort(0, 0);
  39.    if (timerport == NULL)
  40.       return NULL;
  41.    timermsg = (struct timerequest *)
  42.               CreateExtIO(timerport, sizeof(struct timerequest));
  43.    if (timermsg == NULL) {
  44.       DeletePort(timerport);
  45.       return NULL;
  46.    }
  47.    error = OpenDevice((UBYTE *) TIMERNAME, unit,
  48.                       (struct IORequest *) timermsg, 0L);
  49.    if (error != 0) {
  50.       DeleteTimer(timermsg);
  51.       return NULL;
  52.    }
  53.    return timermsg;
  54. }
  55.  
  56. void
  57. WaitForTimer(struct timerequest *tr, struct timeval *tv) {
  58.    /*----------------------------------------------*/
  59.    /* With the UNIT_MICROHZ timer, it is illegal   */
  60.    /* to wait for 0 or 1 microseconds!             */
  61.    /*----------------------------------------------*/
  62.    if (tv->tv_secs == 0L && tv->tv_micro < 2L) return;
  63.    
  64.    tr->tr_node.io_Command = TR_ADDREQUEST;    /* add a new timer request */
  65.  
  66.    /* Post request to the timer--will go to sleep till done. */
  67.    DoIO((struct IORequest *) tr);
  68. }
  69.  
  70. /* More precise timer than AmigaDOS Delay() */
  71.  
  72. LONG MyDelay(struct timeval *tv, LONG unit) {
  73.    struct timerequest *tr;
  74.    
  75.    tr = CreateTimer(unit);
  76.    if (tr == NULL) return -1L;
  77.    WaitForTimer(tr, tv);
  78.    DeleteTimer(tr);
  79.    return 0L;
  80. }
  81.  
  82. void
  83. GetSysTime(struct timeval *tv) {
  84.    struct timerequest *tr;
  85.  
  86.    if ((tr = CreateTimer(UNIT_MICROHZ)) == NULL)
  87.       return;
  88.    tr->tr_node.io_Command = TR_GETSYSTIME;
  89.    DoIO((struct IORequest *) tr);
  90.    *tv = tr->tr_time;
  91.    DeleteTimer(tr);
  92. }
  93.