In a C or C++ program, what is the best way to connect to all X displays on a computer? I would like to check the X idle time of each display, to determine whether the computer is idle across all displays.
My current way of doing it is to put a filesystem watch on
/tmp/.X11-unix to detect new X sockets. When a new display is detected, it waits a few seconds, then tries to connect to it. Usually, this will fail, since the process doesn't have permissions to access the display. So, to fix this, I added
xhost +SI:localuser:root to the GDM presession script (display-setup-script for LightDM). This seems to work most of the time.
However, this method fails every so often, and I don't know why. I'm guessing this is because the
xhost command is not reliable. I'm considering extracting the Xauthority cookie from each individual display, but I cannot find any reliable/portable way of doing so.