x11idle.c 951 B

123456789101112131415161718192021222324252627282930313233
  1. /* This header file is provided by the libxss-dev package on Debian,
  2. * by the libXss-devel rpm on openSUSE, and the libXScrnSaver-devel
  3. * rpm on Fedora.
  4. */
  5. #include <X11/extensions/scrnsaver.h>
  6. #include <stdio.h>
  7. /* Based on code from
  8. * http://coderrr.wordpress.com/2008/04/20/getting-idle-time-in-unix/
  9. *
  10. * compile with 'gcc -l Xss x11idle.c -o x11idle' and copy x11idle into your
  11. * path
  12. */
  13. int main() {
  14. XScreenSaverInfo *info = XScreenSaverAllocInfo();
  15. //open the display specified by the DISPLAY environment variable
  16. Display *display = XOpenDisplay(0);
  17. //display could be null if there is no X server running
  18. if (info == NULL || display == NULL) {
  19. return -1;
  20. }
  21. //X11 is running, try to retrieve info
  22. if (XScreenSaverQueryInfo(display, DefaultRootWindow(display), info) == 0) {
  23. return -1;
  24. }
  25. //info was retrieved successfully, print idle time
  26. printf("%lu\n", info->idle);
  27. return 0;
  28. }