home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / CENVIW9.ZIP / IDLETIME.CMM < prev    next >
Text File  |  1994-03-08  |  5KB  |  157 lines

  1. /**********************************************************************
  2.  *** IdleTime - CEnvi program to start a maximized clock after the  ***
  3.  *** ver.1      mouse and keyboard buttons have been idle, and then ***
  4.  ***            terminate the clock when the buttons are active     ***
  5.  ***            again.  This code checks for keyboard or mouse      ***
  6.  ***            activity by periodically checking the global        ***
  7.  ***            keyboard table and checking the mouse position      ***
  8.  **********************************************************************/
  9.  
  10.  
  11. #define  DELAY_WHILE_WAITING     5
  12.    // Delay, in seconds, between each check for activity, while waiting
  13.    // for inactivity
  14.  
  15. #define  INACTIVE_PERIOD         3
  16.    // How many minutes of keyboard inactivity will start the program
  17.  
  18. #define  DELAY_WHILE_EXECUTING   500
  19.    // Delay, in milliseconds (1000 = 1 second), while waiting for
  20.    // keyboard or mouse activity to start again
  21.  
  22.  
  23. #include <WinTools.lib>
  24. #include <MenuCtrl.lib>
  25. #include <KeyPush.lib>
  26.  
  27. main()   // this is where the program begins. This will loop forever
  28. {
  29.    // Uncomment ("remove "//") from following line to hide CEnvi icon
  30.    // HideThisCEnviProgram(); // make CEnvi icon invisible
  31.    while(TRUE) {
  32.       WaitForInactivity();    // wait for period of inactivity
  33.       window = StartProgram();// start program, and get handle
  34.       if ( window ) {
  35.          WaitForActivity();      // wait for a keyboard or mouse press
  36.          EndProgram(window);
  37.       }
  38.    }
  39. }
  40.  
  41. ShowDigital, ShowSeconds, ShowTitle;
  42. ShowMaximized, ShowMinimized;
  43. IStartedClock;
  44.  
  45. StartProgram() // Start the clock program, maximize it and return
  46. {              // a handle, or return 0 if cannot start it
  47.    if ( IStartedClock = !(window = GetWindowHandle("clock")) ) {
  48.       spawn(P_NOWAIT,"CLOCK.EXE");
  49.       if ( !(window = GetWindowHandle("clock")) )
  50.          return(0);
  51.    }
  52.  
  53.    // remember state of clock for later restoration
  54.    ShowMaximized = IsMaximized(window);
  55.    ShowMinimized = IsMinimized(window);
  56.    if ( !ShowMaximized )
  57.       ShowWindow(window,SW_SHOWMAXIMIZED);
  58.  
  59.    // remember menu selections to be restored
  60.    if ( !(ShowTitle = (Menu = GetMenu(window))) ) {
  61.       // there is no menu; escape key toggles menu on clock
  62.       KeyPushFocusID(window);
  63.       KeyStroke(VK_ESCAPE);
  64.       Menu = GetMenu(window);
  65.    }
  66.  
  67.    ShowDigital = MF_CHECKED & GetMenuState(Menu,FindMenuString(Menu,"Digital"),MF_BYCOMMAND);
  68.    ShowSeconds = MF_CHECKED & GetMenuState(Menu,FindMenuString(Menu,"Seconds"),MF_BYCOMMAND);
  69.  
  70.    // my favorite clock settings
  71.    if ( ShowDigital )
  72.       MenuCommand(window,"Analog",True);
  73.    if ( !ShowSeconds )
  74.       MenuCommand(window,"Seconds",True);
  75.    MenuCommand(window,"No Title",True);   // hide the title bar
  76.  
  77.    return(window);
  78. }
  79.  
  80.  
  81. EndProgram(handle)   // reset window settings and close window
  82. {
  83.    if ( IsWindow(handle) ) {
  84.       // if not title now then make one so we can use the menu
  85.       if ( !GetMenu(handle) ) {
  86.          KeyPushFocusID(handle);
  87.          KeyStroke(VK_ESCAPE);
  88.       }
  89.       if ( ShowDigital )
  90.          MenuCommand(handle,"Digital",True);
  91.       if ( !ShowSeconds )
  92.          MenuCommand(handle,"Seconds",True);
  93.       if ( !ShowMaximized )
  94.          ShowWindow(handle,ShowMinimized ? SW_SHOWMINIMIZED : SW_RESTORE);
  95.       if ( !ShowTitle )
  96.          MenuCommand(handle,"No Title",True);
  97.       if ( IStartedClock )
  98.          SystemMenuCommand(handle,"Close");
  99.    }
  100. }
  101.  
  102.  
  103. WaitForInactivity() // check every once in a while, and return after a
  104. {                   // long period of inactivity
  105.    // check at every interval and set keyboard if it is not already set
  106.    PrevTable = GetGlobalKeyboardTable();
  107.    PrevPos = CursorPosition();
  108.    EndTime = time() + (INACTIVE_PERIOD * 60);
  109.    // stay in this block until period of inactivity has occurred
  110.    do {
  111.       suspend(DELAY_WHILE_WAITING * 1000); // wait a while
  112.       CurrentTable = GetGlobalKeyboardTable();
  113.       CurrentPos = CursorPosition();
  114.       if ( memcmp(CurrentTable,PrevTable,256) || CurrentPos != PrevPos ) {
  115.          // table has changed; reset EndTime and remember table
  116.          EndTime = time() + (INACTIVE_PERIOD * 60);
  117.          PrevTable = CurrentTable;
  118.          PrevPos = CurrentPos;
  119.       }
  120.    } while ( time() < EndTime );
  121. }
  122.  
  123.  
  124. WaitForActivity() // check every once in a while, and return after a
  125. {                 // change in the keyboard or mouse
  126.    PreviousTable = GetGlobalKeyboardTable();
  127.    PrevPos = CursorPosition();
  128.    while( !memcmp(PreviousTable,GetGlobalKeyboardTable(),256)
  129.        && PrevPos == CursorPosition() ) {
  130.       suspend(DELAY_WHILE_EXECUTING);
  131.    }
  132. }
  133.  
  134.  
  135. GetGlobalKeyboardTable()   // return 256 byte buffer for current
  136. {                          // keyboard state
  137.    keyBuf[255] = '\0'; // initialize a 256-byte array
  138.    DynamicLink("USER","GETKEYBOARDSTATE",SWORD16,PASCAL,keyBuf);
  139.    return(keyBuf);
  140. }
  141.  
  142.  
  143. HideThisCEnviProgram()  // hide the CEnvi window icon
  144. {
  145.    ShowWindow(ScreenHandle(),SW_HIDE);
  146. }
  147.  
  148. CursorPosition()  // return current cursor position in screen coordinates
  149. {                 // structure elements returned are .col and .row
  150.    BLObSize(point,4);
  151.    DynamicLink("USER","GETCURSORPOS",SWORD16,PASCAL,point);
  152.    position.col = BLObGet(point,0,SWORD16);
  153.    position.row = BLObGet(point,2,SWORD16);
  154.    return(position);
  155. }
  156.  
  157.