Question

I have found quite a few C fragments on this topic. I modified and was able to get my X display idle time:

#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;
        unsigned int seconds;

        display = XOpenDisplay("");

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

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

                }
        }else {
                fprintf(stderr,"Error: Invalid Display\n");
                return(1);
        }
}

From XOrg's documentation the XOpenDisplay("") function will use the DISPLAY env variable if it is set to NULL (which it is). If I hard set XOpenDisplay(":18.0"), it works well since 18.0 is my display. However when I try another user's display, for example XOpenDisplay(":14.0") I get:

No protocol specified
Error: Invalid Display

I even tried running this script as root to no avail.

I am running RHEL6 with gnome if that means anything.

Was it helpful?

Solution

Even as root you may still need to tell X to allow access, e.g. with xhost local:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top