I know I can get user idle time using C winapi library as follows:
LASTINPUTINFO lii = {sizeof(LASTINPUTINFO), 0};
GetLastInputInfo(&lii); //error handling ommitted, not the point here
int CurIdleTime = (getTickCount() - (lii.dwTime))/1000;
It is also possible to do it in UNIX systems as the top
command displays user idle time correctly.
Is there an easy multiplatform way to get user idle time in C? if not, which libraries can help me doing that in order not to be OS-dependant?
Standard C has time.h
, in there are the functions time
and difftime
. These can be used to compute the time between
user inputs by storing the previous value between events.
Here is an example. (error checking has been removed for brevity)
void on_event() {
static time_t old = 0;
time_t new = time(NULL);
if (old != 0) {
double diff = difftime(new, old);
if (diff != 0)
printf("%g seconds idle\n", diff);
}
old = new;
}
Actually getting events requires platform-specific code, there are various libraries that have most, if not all, of that done already. GTK+ and SDL are popular libraries for C with different uses. There are ways to use Tk with C, and there are various cross-platform C++ libraries. It may also be helpful to note some of the questions regarding C GUI libraries that already exist.