Thread: Idle time of the user

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    17

    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.

  2. #2
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    Try reading up on how screen savers work.

  3. #3
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    Posts
    144
    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;
    }
    THIS IS NOT JUST A CHRONICLING OF THINGS WE HAVE DONE IN THE PAST BUT OUR RISE TO POWER.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    17
    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 ?

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    17
    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?

  6. #6
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    > -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.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    17
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird Times I'm getting
    By afflictedd2 in forum Linux Programming
    Replies: 8
    Last Post: 07-23-2008, 07:18 AM
  2. What is the best way to record a process execution time?
    By hanash in forum Linux Programming
    Replies: 7
    Last Post: 03-15-2006, 07:17 AM
  3. give user time to read messages
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 04-21-2002, 12:54 AM
  4. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM