home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 328_01 / wclock.c < prev    next >
C/C++ Source or Header  |  1991-03-17  |  4KB  |  185 lines

  1. /*! wclock routines
  2.  *
  3.  *
  4.  * PORTABILITY note: The clock function is an interrupt handler.
  5.  *    Turbo C, Microsoft, and a few others have this ability...
  6.  *
  7.  *
  8.  *
  9.  */
  10. #include "wsys.h"
  11.  
  12. #include <dos.h>
  13. #include <time.h>
  14.  
  15. #ifndef __TURBOC__
  16.     /* In microsoft C, interrupts must be explicitly declared 'far'
  17.      * and the compiler can't handle parenthesis on function prototypes
  18.      * so wclockold is defined as a generic ptr
  19.      * (which, I think, make the prototype dangerous)
  20.      */
  21.     static interrupt far wclock(void);
  22.     static void      far *wclockold =NULL;
  23. #else
  24.     /* in TurboC, interrupts  match the model.
  25.      * and the compiler is able to recognize ptrs to interrupt functions.
  26.      */
  27.     static void interrupt    wclock    (void);
  28.     static void interrupt  (*wclockold)(void) =NULL;
  29. #endif        /* Microsoft fix */
  30.  
  31. static int  ch10, ch, cm10, cm;
  32.  
  33.  
  34. static int clockx, clocky;
  35. static char clock_inst =0;        /* is clock currently operating ? */
  36. static char exit_inst  =0;        /* has atexit() been installed  ? */
  37.  
  38.  
  39. void wclockinstall (int x, int y)
  40.     {
  41.     time_t         t_now;
  42.     struct tm     *tm_now;
  43.     
  44.     if ( clock_inst )
  45.         {
  46.         return;
  47.         }
  48.     else
  49.         {
  50.         clock_inst = 1;
  51.         }
  52.         
  53.     if ( ! exit_inst )
  54.         {
  55.         exit_inst = 1;
  56.         atexit ( wclockremove );
  57.         }
  58.         
  59.  
  60.     /* save the screen location
  61.      * where the interrupt-driven clock can find it
  62.      */
  63.     clockx =x;
  64.     clocky = y;
  65.  
  66.     /* initialize time */
  67.     time( &t_now );
  68.     tm_now = localtime(&t_now);
  69.  
  70.  
  71.     if (( ch = tm_now-> tm_hour) >12)
  72.         {
  73.         /*convert from 24 hour clock */
  74.         ch -= 12;
  75.         }
  76.  
  77.     if ( ch > 9 )
  78.         {
  79.         ch10 = '1';
  80.         ch   = '0'-10 + ch;
  81.         }
  82.     else     {
  83.         ch10 = '0';
  84.         ch   = '0' + ch;
  85.         }
  86.  
  87.     cm = '0' + ( (tm_now->tm_min) - 10*((tm_now->tm_min)/10) );
  88.     cm10 = '0' + (tm_now->tm_min)/10;
  89.  
  90.  
  91.  
  92.     /* install clock */
  93.     wclockold = getvect (0x1c);
  94.     disable();
  95.     setvect (0x1c, wclock);
  96.     enable();
  97.  
  98.  
  99.     return;
  100.     }
  101.  
  102. void wclockremove (void)
  103.     {
  104.  
  105.     if ( ! clock_inst )
  106.         {
  107.         return;
  108.         }
  109.  
  110.     clock_inst = 0;
  111.     disable();
  112.     setvect (0x1c, wclockold);
  113.     enable();
  114.  
  115.     return;
  116.     }
  117.  
  118.  
  119. /* wclock() function body - 
  120.  * This is the actual clock calculator.
  121.  * (PORTABILITY note: make the function definition match the prototype.)
  122.  */
  123. #ifndef __TURBOC__
  124.     static interrupt far wclock(void)
  125. #else
  126.     static void interrupt    wclock    (void)
  127. #endif        /* Microsoft fix */
  128.     
  129.     /* function body follows 
  130.      */
  131.     {
  132.     #define TPS    18
  133.     static int tick = 0;
  134.     static int cs=0, cs10 = 0;
  135.  
  136.     ++tick;
  137.  
  138.     if (tick == TPS/2)
  139.         {
  140.         wputcabs (clockx+2   ,clocky, ' ',  wmenuattr, WGOVERWRITE);
  141.         }
  142.     else
  143.     if (tick == 18)  /* 18.2 ticks per second, correction every 10 s */
  144.         {
  145.         tick = 0;
  146.         if ( ++cs > 9 )
  147.             {
  148.             cs = 0;
  149.             tick -= 2;  /* need 2 extra ticks every 10 secs */
  150.             if ( ++cs10 == 6 ) /* 60 secs */
  151.                 {
  152.                 cs10 =0;
  153.                 if ( ++cm > '9' )
  154.                     {
  155.                     cm = '0';
  156.                     if ( ++(cm10) == '6')
  157.                         {
  158.                         cm10 = '0';
  159.                         if ( ++(ch) >'9')
  160.                            {
  161.                            ch10 = '1';
  162.                            ch =  '0';
  163.                            }
  164.                         if (  ch10 =='1'
  165.                            && ch == '3' )
  166.                            {
  167.                            ch10 = ' ';
  168.                            ch = '1';
  169.                            }
  170.                         }  /* end 1 hour */
  171.                     } /*end 10 minutes */
  172.                 }  /* end 1 minute*/
  173.             }  /* end 10 seconds*/
  174.         wputcabs (clockx     ,clocky, ch10, wmenuattr, WGOVERWRITE);
  175.         wputcabs (clockx+1   ,clocky, ch,   wmenuattr, WGOVERWRITE);
  176.         wputcabs (clockx+2   ,clocky, ':',  wmenuattr, WGOVERWRITE);
  177.         wputcabs (clockx+3   ,clocky, cm10, wmenuattr, WGOVERWRITE);
  178.         wputcabs (clockx+4   ,clocky, cm,   wmenuattr, WGOVERWRITE);
  179.         } /* end 18 ticks/sec*/
  180.  
  181.     return;        /* wclock */
  182.     } 
  183. /*--------------------------- END WCLOCK--------------------------*/
  184.  
  185.