home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource1
/
cenvew
/
idletime.cmm
< prev
next >
Wrap
Text File
|
1993-11-03
|
4KB
|
115 lines
/**********************************************************************
*** IdleTime - CEnvi program to start a maximized clock after the ***
*** mouse and keyboard buttons have been idle, and then ***
*** terminate the clock when the buttons are active ***
*** again. This code checks for keyboard or mouse ***
*** activity by periodically checking the global ***
*** keyboard table and checking the mouse position ***
**********************************************************************/
#define DELAY_WHILE_WAITING 5
// Delay, in seconds, between each check for activity, while waiting
// for inactivity
#define INACTIVE_PERIOD 1
// How many minutes of keyboard inactivity will start the program
#define DELAY_WHILE_EXECUTING 750
// Delay, in milliseconds (1000 = 1 second), while waiting for
// keyboard or mouse activity to start again
main() // this is where the program begins. This will loop forever
{
// Uncomment ("remove "//") from following line to hide CEnvi icon
// HideThisCEnviProgram(); // make CEnvi icon invisible
while(TRUE) {
WaitForInactivity(); // wait for period of inactivity
window = StartProgram();// start program, and get handle
if ( window ) {
WaitForActivity(); // wait for a keyboard or mouse press
EndProgram(window);
}
}
}
StartProgram() // Start the clock program, maximize it and return
{ // a handle, or return 0 if cannot start it
window = spawn(P_NOWAIT,"CLOCK.EXE");
if ( !window || -1 == window )
return(0);
MaximizeWindow(window);
return(window);
}
EndProgram(handle) // terminate this program handle by sending
{ // it a WM_CLOSE message
#define WM_CLOSE 0x0010
DynamicLink("USER","POSTMESSAGE",SWORD16,PASCAL,
handle,WM_CLOSE,0,0,0);
}
WaitForInactivity() // check every once in a while, and return after a
{ // long period of inactivity
// check at every interval and set keyboard if it is not already set
PrevTable = GetGlobalKeyboardTable();
PrevPos = CursorPosition();
EndTime = time() + (INACTIVE_PERIOD * 60);
// stay in this block until period of inactivity has occurred
do {
suspend(DELAY_WHILE_WAITING * 1000); // wait a while
CurrentTable = GetGlobalKeyboardTable();
CurrentPos = CursorPosition();
if ( memcmp(CurrentTable,PrevTable,256) || CurrentPos != PrevPos ) {
// table has changed; reset EndTime and remember table
EndTime = time() + (INACTIVE_PERIOD * 60);
PrevTable = CurrentTable;
PrevPos = CurrentPos;
}
} while ( time() < EndTime );
}
WaitForActivity() // check every once in a while, and return after a
{ // change in the keyboard or mouse
PreviousTable = GetGlobalKeyboardTable();
PrevPos = CursorPosition();
while( !memcmp(PreviousTable,GetGlobalKeyboardTable(),256)
&& PrevPos == CursorPosition() ) {
suspend(DELAY_WHILE_EXECUTING);
}
}
GetGlobalKeyboardTable() // return 256 byte buffer for current
{ // keyboard state
keyBuf[255] = '\0'; // initialize a 256-byte array
DynamicLink("USER","GETKEYBOARDSTATE",SWORD16,PASCAL,keyBuf);
return(keyBuf);
}
HideThisCEnviProgram() // hide the CEnvi window icon
{
#define SW_HIDE 0
DynamicLink("USER","SHOWWINDOW",SWORD16,PASCAL,ScreenHandle(),SW_HIDE);
}
MaximizeWindow(handle)
{
#define SW_SHOWMAXIMIZED 3
DynamicLink("USER","SHOWWINDOW",SWORD16,PASCAL,handle,SW_SHOWMAXIMIZED);
}
CursorPosition() // return current cursor position in screen coordinates
{ // structure elements returned are .col and .row
BLObSize(point,4);
DynamicLink("USER","GETCURSORPOS",SWORD16,PASCAL,point);
position.col = BLObGet(point,0,SWORD16);
position.row = BLObGet(point,2,SWORD16);
return(position);
}