home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 199.lha / GimmeLib / timerstuff.c < prev    next >
C/C++ Source or Header  |  1988-12-27  |  4KB  |  175 lines

  1. /*
  2.  *  FILE: timerstuff.c
  3.  *  Support routines for dealing with timerequests and additional timer support.
  4.  *
  5.  *  Public Domain, but keep my name in it as the original author.
  6.  *  31-Oct-88    Jan Sven Trabandt   split from timer.c
  7.  */
  8.  
  9.  
  10. #define I_AM_TIMERSTUFF
  11. #include "gimmelib/gimmefuncs.h"
  12.  
  13. extern struct Device *TimerBase;
  14.  
  15.  
  16. LONG timeDelay( secs, micros, unit, mytr )
  17.     ULONG        secs, micros;
  18.     ULONG        unit;
  19.     struct timerequest    *mytr;
  20. {
  21.     register struct timerequest *tr;
  22.     LONG    err;
  23.  
  24.     if( !secs && !micros ) {
  25.     return( 0L );           /* so we don't crash, return right away */
  26.     }
  27.     if( mytr ) {
  28.     tr = mytr;
  29.     } else {
  30. #ifdef GIMME_WIMPY
  31.     if( unit != UNIT_VBLANK && unit != UNIT_MICROHZ ) {
  32.         return( -1L );
  33.     }
  34. #endif
  35.     if( !secs ) {
  36.         unit = UNIT_MICROHZ;        /* force more efficient method */
  37.     }
  38.     if( !(tr = accessTimer(unit, NULL)) ) {
  39.         return( -1L );
  40.     }
  41.     }
  42.     tr->tr_node.io_Command = TR_ADDREQUEST;
  43.     tr->tr_time.tv_secs = secs;
  44.     tr->tr_time.tv_micro = micros;
  45.     err = DoIO( tr );
  46.     tr->tr_node.io_Message.mn_Node.ln_Type = NT_FREEMSG;
  47.     if( !mytr ) {
  48.     releaseTimer( tr, NULL );
  49.     }
  50.     return( err );
  51. } /* timeDelay */
  52.  
  53.  
  54. struct timerequest *timeDelayAsync( secs, micros, unit, tr )
  55.     ULONG   secs, micros;
  56.     ULONG   unit;
  57.     register struct timerequest *tr;
  58. {
  59.     if( !tr ) {
  60. #ifdef GIMME_WIMPY
  61.     if( unit != UNIT_VBLANK && unit != UNIT_MICROHZ ) {
  62.         return( NULL );
  63.     }
  64. #endif
  65.     if( !secs ) {
  66.         unit = UNIT_MICROHZ;        /* force more efficient method */
  67.     }
  68.     if( !(tr = accessTimer(unit, NULL)) ) {
  69.         return( NULL );
  70.     }
  71.     }
  72.     if( !secs && !micros ) {
  73.     micros = 1L;            /* so we don't crash on zero-delay */
  74.     }
  75.     tr->tr_node.io_Command = TR_ADDREQUEST;
  76.     tr->tr_time.tv_secs = secs;
  77.     tr->tr_time.tv_micro = micros;
  78.     SendIO( tr );
  79.     return( tr );
  80. } /* timeDelayAsync */
  81.  
  82.  
  83. LONG waitTimer( tr )
  84.     struct timerequest    *tr;
  85. {
  86.     LONG    err = 0L;
  87.  
  88. #ifdef GIMME_WIMPY
  89.     if( !tr ) {
  90.     return( err );
  91.     }
  92. #endif
  93.     err = WaitIO( tr );
  94.     tr->tr_node.io_Message.mn_Node.ln_Type = NT_FREEMSG;
  95.     return( err );
  96. } /* waitTimer */
  97.  
  98.  
  99. LONG killTimeDelay( tr )
  100.     register struct timerequest *tr;
  101. {
  102.     LONG    err;
  103.  
  104. #ifdef GIMME_WIMPY
  105.     if( !tr ) {
  106.     return( 0L );
  107.     }
  108. #endif
  109.     Forbid();
  110.     if( tr->tr_node.io_Message.mn_Node.ln_Type == NT_MESSAGE ) {
  111.     if( err = AbortIO(tr) ) {
  112.         Permit();
  113.         return( err );
  114.     }
  115.     Remove( tr );
  116.     tr->tr_node.io_Message.mn_Node.ln_Type = NT_FREEMSG;
  117.     }
  118.     Permit();
  119.     return( 0L );
  120. } /* killTimeDelay */
  121.  
  122.  
  123. short getSysTime( secs, micros, mytr )
  124.     ULONG        *secs, *micros;
  125.     struct timerequest    *mytr;
  126. {
  127.     register struct timerequest *tr;
  128.  
  129.     if( mytr ) {
  130.     tr = mytr;
  131.     } else {
  132.     if( !(tr = accessTimer(UNIT_MICROHZ, NULL)) ) {
  133.         return( -1 );
  134.     }
  135.     }
  136.     tr->tr_node.io_Command = TR_GETSYSTIME;
  137.     DoIO( tr );
  138.     tr->tr_node.io_Message.mn_Node.ln_Type = NT_FREEMSG;
  139.     if( secs ) {
  140.     *secs = tr->tr_time.tv_secs;
  141.     }
  142.     if( micros ) {
  143.     *micros = tr->tr_time.tv_micro;
  144.     }
  145.     if( !mytr ) {
  146.     releaseTimer( tr, NULL );
  147.     }
  148.     return( 0 );
  149. } /* getSysTime */
  150.  
  151.  
  152. short setSysTime( secs, micros, mytr )
  153.     ULONG        secs, micros;
  154.     struct timerequest    *mytr;
  155. {
  156.     register struct timerequest *tr;
  157.  
  158.     if( mytr ) {
  159.     tr = mytr;
  160.     } else {
  161.     if( !(tr = accessTimer(UNIT_MICROHZ, NULL)) ) {
  162.         return( -1 );
  163.     }
  164.     }
  165.     tr->tr_node.io_Command = TR_SETSYSTIME;
  166.     tr->tr_time.tv_secs = secs;
  167.     tr->tr_time.tv_micro = micros;
  168.     DoIO( tr );
  169.     tr->tr_node.io_Message.mn_Node.ln_Type = NT_FREEMSG;
  170.     if( !mytr ) {
  171.     releaseTimer( tr, NULL );
  172.     }
  173.     return( 0 );
  174. } /* setSysTime */
  175.