x11idle.c 838 B

1234567891011121314151617181920212223242526272829303132
  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. Status querry = 0;
  11. XScreenSaverInfo *info = XScreenSaverAllocInfo();
  12. //open the display specified by the DISPLAY environment variable
  13. Display *display = XOpenDisplay(0);
  14. //display could be null if there is no X server running
  15. if (info == NULL || display == NULL) {
  16. return -1;
  17. }
  18. //X11 is running, retrieve and print idle time
  19. querry = XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
  20. if (querry == 0) {
  21. return -1;
  22. }
  23. //idle time was retrieved successfully, print it
  24. printf("%u\n", info->idle);
  25. return 0;
  26. }