Results 1 to 4 of 4

Thread: Find out how long an X session has been idle?

  1. #1
    Join Date
    Mar 2008
    Beans
    4,715
    Distro
    Ubuntu 9.10 Karmic Koala

    Find out how long an X session has been idle?

    Is there a command or program which can return the number of seconds an X session has been idle?

    Per this tutorial on dbus,
    http://foss-boss.blogspot.com/2008/1...x-desktop.html

    I've tried
    Code:
    sleep 5; qdbus org.gnome.ScreenSaver / GetSessionIdleTime
    but it always seems to return 0.

    I've also tried
    Code:
    sleep 5; qdbus org.freedesktop.ScreenSaver / GetSessionIdleTime
    Which returns
    Code:
    Service 'org.freedesktop.ScreenSaver' does not exist.

  2. #2
    Join Date
    Oct 2007
    Location
    Fort Collins, CO, USA
    Beans
    480
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Find out how long an X session has been idle?

    It seems that the gnome dbus method reports the time since gnome decided the screen was idle.
    That varies with the gnome screensaver time setting.
    This reports a 2 second idle time. But it turns on the screensaver.
    Code:
    qdbus org.gnome.ScreenSaver / org.gnome.ScreenSaver.SetActive False;sleep 2;qdbus org.gnome.ScreenSaver / org.gnome.ScreenSaver.GetSessionIdleTime
    You could use the X screensaver extension directly to read X server idle time.
    See http://linux.die.net/man/3/xscreensaverqueryinfo
    You may need to install the right development packages to build this.
    I needed "sudo apt-get install libxss-dev" on my system.
    Code:
    #include <stdio.h>
    #include <X11/Xlib.h>
    #include <X11/extensions/scrnsaver.h>
    
    /* Report amount of X server idle time. */
    /* Build with- */
    /* cc xidle.c -o xidle -lX11 -lXext -lXss */
    
    
    int main(int argc, char *argv[])
    {
        Display *display;
        int event_base, error_base;
        XScreenSaverInfo info;
        float seconds;
    
        display = XOpenDisplay("");
    
        if (XScreenSaverQueryExtension(display, &event_base, &error_base)) {
    	XScreenSaverQueryInfo(display, DefaultRootWindow(display), &info);
    
    	seconds = (float)info.idle/1000.0f;
    	printf("%f\n",seconds);
    	return(0);
        }
        else {
    	fprintf(stderr,"Error: XScreenSaver Extension not present\n");
    	return(1);
        }
    }

  3. #3
    Join Date
    Mar 2008
    Beans
    4,715
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Find out how long an X session has been idle?

    Thank you, stroyan. The C code works wonderfully.

  4. #4
    Join Date
    Mar 2013
    Beans
    1

    Re: Find out how long an X session has been idle?

    If i run this code into a while(1) loop and get the return value continuously i am getting the error "Maximum number of clients reached " , but if i use sleep(1) then it is working fine . is there a way to over come it without sleep one . Hear is my altered code .


    #include <stdio.h>
    #include <X11/Xlib.h>
    #include <X11/extensions/scrnsaver.h>

    /* Report amount of X server idle time. */
    /* Build with- */
    /* cc xidle.c -o xidle -lX11 -lXext -lXss */

    unsigned int abc();
    void main()
    {
    unsigned int idletime123;

    while(1)
    {
    idletime123=abc();
    printf("idletime : %u \n",idletime123);
    //sleep(1);
    }

    }

    unsigned int abc()
    //float time123(int argc, char *argv[])
    {
    Display *display;
    int event_base, error_base;
    XScreenSaverInfo info;
    unsigned int seconds;

    display = XOpenDisplay("");

    if (XScreenSaverQueryExtension(display, &event_base, &error_base)) {
    XScreenSaverQueryInfo(display, DefaultRootWindow(display), &info);

    seconds = (unsigned int)info.idle/1000.0f;
    //printf("%f\n",seconds);
    return(seconds);
    }
    else {
    fprintf(stderr,"Error: XScreenSaver Extension not present\n");
    return(1);
    }
    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •