1

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?

Magix
  • 4,989
  • 7
  • 26
  • 50

1 Answers1

1

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.

Community
  • 1
  • 1
kdhp
  • 2,096
  • 14
  • 15
  • Doesn't answer the question as it only provides a way to get the user *input* idle time, not whether he has moved his mouse/lowered the volume/any other input the OS can handle – Magix Aug 23 '15 at 18:32
  • @MagixKiller Looking at it again what the example was trying to show was not obvious, it has been replaced with one that is hopefully more helpful. – kdhp Aug 23 '15 at 18:52
  • Well a problem remains, how can you get the OS to call *on_event()* when an event is triggered in a multiplatform way? this is also part of the question, thanks for answering anyway – Magix Aug 23 '15 at 22:25