home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / stdwin / Ports / mac / timer.c < prev   
Encoding:
C/C++ Source or Header  |  1990-11-07  |  1.9 KB  |  84 lines  |  [TEXT/????]

  1. /* MAC STDWIN -- TIMER. */
  2.  
  3. /* XXX The timer mechanism should be coupled less strongly with the
  4.    stdwin implementation -- stdwin should support a single timer,
  5.    while thin layers on top could implement various timer strategies. */
  6.  
  7. #include "macwin.h"
  8. #ifdef MPW
  9. #include <Events.h>
  10. #endif
  11.  
  12. static unsigned long nexttimer;
  13.  
  14. /* Set a window's timer.
  15.    The second parameter is the number of deciseconds from now
  16.    when the timer is to go off, or 0 to clear the timer.
  17.    When the timer goes off (or sometime later),
  18.    a WE_TIMER event will be delivered.
  19.    As a service to 'checktimer' below, a NULL window parameter
  20.    is ignored, but causes the 'nexttimer' value to be recalculated.
  21.    (The inefficient simplicity of the code here is due to MINIX,
  22.    with some inventions of my own.) */
  23.  
  24. void
  25. wsettimer(win, deciseconds)
  26.     WINDOW *win;
  27.     int deciseconds;
  28. {
  29.     WindowPeek w;
  30.     
  31.     nexttimer= 0;
  32.     if (win != NULL) {
  33.         if (deciseconds == 0)
  34.             win->timer= 0;
  35.         else {
  36.             nexttimer= win->timer= TickCount() +
  37.                 deciseconds*(TICKSPERSECOND/10);
  38.         }
  39.     }
  40.     for (w= (WindowPeek)FrontWindow(); w != NULL; w= w->nextWindow) {
  41.         win= whichwin((WindowPtr)w);
  42.         if (win != NULL && win->timer != 0) {
  43.             if (nexttimer == 0 || win->timer < nexttimer)
  44.                 nexttimer= win->timer;
  45.         }
  46.     }
  47. }
  48.  
  49. /* Check if a timer went off.
  50.    If so, return TRUE and set the event record correspondingly;
  51.    otherwise, return FALSE. */
  52.  
  53. bool
  54. checktimer(ep)
  55.     EVENT *ep;
  56. {
  57.     WindowPtr w;
  58.     WINDOW *win;
  59.     unsigned long now;
  60.     
  61.     if (nexttimer == 0) {
  62.         return FALSE;
  63.     }
  64.     now= TickCount();
  65.     if (now < nexttimer) {
  66.         return FALSE;
  67.     }
  68.     ep->type= WE_NULL;
  69.     ep->window= NULL;
  70.     for (w= FrontWindow(); w != NULL;
  71.         w= (WindowPtr)((WindowPeek)w)->nextWindow) {
  72.         win= whichwin(w);
  73.         if (win != NULL && win->timer != 0) {
  74.             if (win->timer <= now) {
  75.                 ep->type= WE_TIMER;
  76.                 ep->window= win;
  77.                 now= win->timer;
  78.             }
  79.         }
  80.     }
  81.     wsettimer(ep->window, 0);
  82.     return ep->type == WE_TIMER;
  83. }
  84.