-
Idle time of the user
Hello together,
i write a little programm, which checks, if a user is sitting in front of his computer or not.
Now i want to find out, how long the user is idle, means how long no input has been made.
Any suggestions how to solve this problem in C?
I really dont have an idea where to start, maybe with the x-Window api or so...
I hope you are able to help me. Thank you.
-
Try reading up on how screen savers work.
-
Code:
#include <sys/stat.h>
#include <time.h>
/*
Take the difference between current time in seconds and access time of the
users terminal device
*/
int main(int argc, char *argv[])
{
struct stat f_info;
/* terminal device for the user */
const char *file_name = "/dev/pts/5";
stat(file_name, &f_info);
printf("%d\n", time(NULL) - f_info.st_atime);
return 0;
}
-
Thank you guys for your help and the piece of code.
Is there also an other way than the one mix0matt described?
How can i find out the terminal device of the user? Is it always /dev/pts/5 ?
-
I've found another solution, but there is a problem while i try to compile the source code.
This is the code:
Code:
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/scrnsaver.h>
#include <gdk/gdkx.h>
int main(void){
static XScreenSaverInfo *mit_info = NULL;
int event_base, error_base;
if (XScreenSaverQueryExtension(GDK_DISPLAY(), &event_base, &error_base)) {
mit_info = XScreenSaverAllocInfo();
XScreenSaverQueryInfo(GDK_DISPLAY(), GDK_ROOT_WINDOW(), mit_info);
printf("Idle Time: %i\n", mit_info->idle);
} else {
printf("No Display available");
}
return 0;
}
And this is my compilercall:
Code:
gcc -I/usr/X11R6/include -I/${GTK-DIR}/1.2.10/include/gtk-1.2 -I${GLIB-DIR}/1.2.10/include/glib-1.2 -I${GLIB-DIR}/lib/glib/include -L${GTK-DIR}/1.2.10/i686-pc-linux-gnuoldld/lib -L/usr/X11R6/lib -L/usr/X11R6/lib/X11 -lXss -lX11 -lgdk idle.c
Now the compiler throws the following error:
Code:
/tmp/ccYXOUio.o(.text+0x33): In function `main':
: undefined reference to `XScreenSaverQueryExtension'
/tmp/ccYXOUio.o(.text+0x3c): In function `main':
: undefined reference to `XScreenSaverAllocInfo'
/tmp/ccYXOUio.o(.text+0x60): In function `main':
: undefined reference to `XScreenSaverQueryInfo'
collect2: ld returned 1 exit status
Anybody got an idea why this happens?
-
> -lXss -lX11 -lgdk
These go after .c files - otherwise the linker doesn't see any unresolved symbols when it gets to the libraries, therefore it doesn't think the libraries are necessary.
-
Hello,
well, the compilation worked, an so does the code. But now, i've got another problem... ;)
These XScreenSaver functions only work, if the XscreenSaver extension is present.
But i want code, which can run standalone, so i need to know how X gets the IdleTime.
Does anybody know this? I dont want to read the whole source for xscreensaver to find out...
This would help me very mutch. Thank you guys.