home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / cknalm.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  3KB  |  108 lines

  1. /* C K N A L M  --  Kermit alarm functions for Windows 95 systems */
  2.  
  3. /*
  4.   Author: Jeffrey Altman (jaltman@secure-endpoints.com),
  5.             Secure Endpoints Inc., New York City.
  6.  
  7.   Copyright (C) 1996,2004 Trustees of Columbia University in the City of New
  8.   York.
  9. */
  10.  
  11. /* We are performing this ugly hack only because Windows 95 can not implement 
  12.    a timer that works accurately on all computer systems with Console
  13.    Applications.  This code should not be used for Windows NT or other Win32
  14.    implementations
  15. */
  16.  
  17. #include <windows.h>
  18.  
  19. #define TIMER_COUNT 65
  20. struct _timerInfo
  21. {
  22.    UINT   id ;
  23.    ULONG  interval ;
  24.    LPTIMECALLBACK pTimeProc ;
  25.    DWORD  userdata ;
  26.    LONG   event ;
  27.    BOOL   inuse ;
  28. } timers[TIMER_COUNT] =
  29. {{0,0,0,0,0,0},{1,0,0,0,0,0},{2,0,0,0,0,0},{3,0,0,0,0,0},{4,0,0,0,0,0},{5,0,0,0,0,0},
  30.  {6,0,0,0,0,0},{7,0,0,0,0,0},{8,0,0,0,0,0},{9,0,0,0,0,0},
  31.  {10,0,0,0,0,0},{11,0,0,0,0,0},{12,0,0,0,0,0},{13,0,0,0,0,0},{14,0,0,0,0,0},{15,0,0,0,0,0},
  32.  {16,0,0,0,0,0},{17,0,0,0,0,0},{18,0,0,0,0,0},{19,0,0,0,0,0},
  33.  {20,0,0,0,0,0},{21,0,0,0,0,0},{22,0,0,0,0,0},{23,0,0,0,0,0},{24,0,0,0,0,0},{25,0,0,0,0,0},
  34.  {26,0,0,0,0,0},{27,0,0,0,0,0},{28,0,0,0,0,0},{29,0,0,0,0,0},
  35.  {30,0,0,0,0,0},{31,0,0,0,0,0},{32,0,0,0,0,0},{33,0,0,0,0,0},{34,0,0,0,0,0},{35,0,0,0,0,0},
  36.  {36,0,0,0,0,0},{37,0,0,0,0,0},{38,0,0,0,0,0},{39,0,0,0,0,0},
  37.  {40,0,0,0,0,0},{41,0,0,0,0,0},{42,0,0,0,0,0},{43,0,0,0,0,0},{44,0,0,0,0,0},{45,0,0,0,0,0},
  38.  {46,0,0,0,0,0},{47,0,0,0,0,0},{48,0,0,0,0,0},{49,0,0,0,0,0},
  39.  {50,0,0,0,0,0},{51,0,0,0,0,0},{52,0,0,0,0,0},{53,0,0,0,0,0},{54,0,0,0,0,0},{55,0,0,0,0,0},
  40.  {56,0,0,0,0,0},{57,0,0,0,0,0},{58,0,0,0,0,0},{59,0,0,0,0,0},
  41.  {60,0,0,0,0,0},{61,0,0,0,0,0},{62,0,0,0,0,0},{63,0,0,0,0,0},{64,0,0,0,0,0}
  42. };
  43.  
  44. void
  45. TimerThread( void * data )
  46. {
  47.    int tt = 0, interval = 250 , t = 0, tr = 0 ;
  48.    struct _timerInfo * ti = (struct _timerInfo *) data ;
  49.    tt = (ti->interval / interval) ;
  50.    tr = (ti->interval % interval) ;
  51.    if ( tt )
  52.       interval += tr / tt ;
  53.    else
  54.    {
  55.       tt = 1 ;
  56.       interval = tr ;
  57.    }
  58.  
  59.    do {
  60.       for ( t = 0 ; t < tt ; t++ )
  61.       {
  62.          Sleep( interval ) ;
  63.          if ( ti->event < 0 )
  64.          {
  65.             ti->inuse = 0 ;
  66.             _endthread();
  67.          }
  68.       }
  69.       if ( ti->event < 0 )
  70.          break;
  71.       (*ti->pTimeProc)( ti->id, 0, ti->userdata, 0, 0 ) ;
  72.    } while ( ti->event == TIME_PERIODIC );
  73.    ti->inuse = 0 ;
  74.    _endthread() ;
  75. }
  76.  
  77. UINT
  78. ckTimerStart( UINT interval, UINT precision, LPTIMECALLBACK pTimeProc, DWORD userdata, UINT event )
  79. {
  80.    int i = 1;
  81.    while ( timers[i].inuse )
  82.    {
  83.       if ( ++i >= TIMER_COUNT )
  84.       {
  85.          i = 1 ;
  86.          Sleep(100) ;
  87.       }
  88.    }
  89.    timers[i].interval = interval ;
  90.    timers[i].pTimeProc = pTimeProc ;
  91.    timers[i].userdata = userdata ;
  92.    timers[i].event = event ;
  93.    timers[i].inuse = 1 ;
  94.  
  95.    if ( (HANDLE) _beginthread( TimerThread, 65536, &timers[i] ) != (HANDLE) INVALID_HANDLE_VALUE )
  96.       return i ;
  97.    else
  98.       return 0 ;
  99. }
  100.  
  101. UINT
  102. ckTimerKill( UINT i )
  103. {
  104.    if ( timers[i].inuse )
  105.        timers[i].event = -1 ;
  106.    return 1;
  107. }
  108.