-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckActivity.cpp
More file actions
80 lines (67 loc) · 2.28 KB
/
checkActivity.cpp
File metadata and controls
80 lines (67 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// checkActivity.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Windows.h"
#include <string>
#include <fstream>
/*typedef struct tagLASTINPUTINFO {
UINT cbSize;
DWORD dwTime;
} LASTINPUTINFO, *PLASTINPUTINFO;
*/
int main(int argc, char* argv[])
{
//possible command params
unsigned long timeoutSec = 293; // ~5 mins +/- sleepSec // after this limit is reached machine is available
std::string outputfileName; // outputfile that will contain either FREE or BUSY
unsigned int sleepSec = 15; // how often to check if we're busy or not.
//internal variables
DWORD currentTick; // check current tick against lastinputinfo tick
LASTINPUTINFO plii; // set by GetLastInputInfo()
plii.cbSize = sizeof(LASTINPUTINFO); //need to set buffer size to appropriate size
DWORD tickdiff; // holds difference between currentTick and last Input
DWORD timeoutInTicks = timeoutSec*1000;
outputfileName = "freebusy.dat";
std::ofstream ofile;
//stop us from uneccesarily writing file with same info
// initialise to false to force run on first iteration
// will this always fire, even if no recent input
// e.g. started automatically with system start or task scheduler?
bool lastStatusBusy = false;
printf("Checking every %ld sec if active within last %ld sec, outputting to file %s\n",
sleepSec, timeoutSec, outputfileName.c_str());
printf("Ctrl+C a couple of times to exit\n");
while (true) {
currentTick = GetTickCount();
if (GetLastInputInfo(&plii)) {
tickdiff = currentTick - plii.dwTime;
if (tickdiff >= timeoutInTicks) {
printf("FREE since %lds\r",(unsigned long)(tickdiff/1000));
//do stuff to show computer is free
//update output file
if (lastStatusBusy) {
ofile.open(outputfileName);
ofile << "FREE";
ofile.close();
lastStatusBusy = false;
}
}
else {
printf("BUSY \r");
//do stuff to show computer is in use
//update output file
if (!lastStatusBusy) {
ofile.open(outputfileName);
ofile << "BUSY";
ofile.close();
lastStatusBusy = true;
}
}
}
else {
printf("getLastInputInfo failed.\n");
}
Sleep(1000 * sleepSec);
}
return 0;
}