x11idle.c 787 B

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