slstatus.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* See LICENSE file for copyright and license details. */
  2. #include <errno.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #include <X11/Xlib.h>
  9. #include "arg.h"
  10. #include "slstatus.h"
  11. #include "util.h"
  12. struct arg {
  13. const char *(*func)(const char *);
  14. const char *fmt;
  15. const char *args;
  16. };
  17. char buf[1024];
  18. static volatile sig_atomic_t done;
  19. static Display *dpy;
  20. #include "config.h"
  21. static void
  22. terminate(const int signo)
  23. {
  24. if (signo != SIGUSR1)
  25. done = 1;
  26. }
  27. static void
  28. difftimespec(struct timespec *res, struct timespec *a, struct timespec *b)
  29. {
  30. res->tv_sec = a->tv_sec - b->tv_sec - (a->tv_nsec < b->tv_nsec);
  31. res->tv_nsec = a->tv_nsec - b->tv_nsec +
  32. (a->tv_nsec < b->tv_nsec) * 1E9;
  33. }
  34. static void
  35. usage(void)
  36. {
  37. die("usage: %s [-v] [-s] [-1]", argv0);
  38. }
  39. int
  40. main(int argc, char *argv[])
  41. {
  42. struct sigaction act;
  43. struct timespec start, current, diff, intspec, wait;
  44. size_t i, len;
  45. int sflag, ret;
  46. char status[MAXLEN];
  47. const char *res;
  48. sflag = 0;
  49. ARGBEGIN {
  50. case 'v':
  51. die("slstatus-"VERSION);
  52. case '1':
  53. done = 1;
  54. /* FALLTHROUGH */
  55. case 's':
  56. sflag = 1;
  57. break;
  58. default:
  59. usage();
  60. } ARGEND
  61. if (argc)
  62. usage();
  63. memset(&act, 0, sizeof(act));
  64. act.sa_handler = terminate;
  65. sigaction(SIGINT, &act, NULL);
  66. sigaction(SIGTERM, &act, NULL);
  67. act.sa_flags |= SA_RESTART;
  68. sigaction(SIGUSR1, &act, NULL);
  69. if (!sflag && !(dpy = XOpenDisplay(NULL)))
  70. die("XOpenDisplay: Failed to open display");
  71. do {
  72. if (clock_gettime(CLOCK_MONOTONIC, &start) < 0)
  73. die("clock_gettime:");
  74. status[0] = '\0';
  75. for (i = len = 0; i < LEN(args); i++) {
  76. if (!(res = args[i].func(args[i].args)))
  77. res = unknown_str;
  78. if ((ret = esnprintf(status + len, sizeof(status) - len,
  79. args[i].fmt, res)) < 0)
  80. break;
  81. len += ret;
  82. }
  83. if (sflag) {
  84. puts(status);
  85. fflush(stdout);
  86. if (ferror(stdout))
  87. die("puts:");
  88. } else {
  89. if (XStoreName(dpy, DefaultRootWindow(dpy), status) < 0)
  90. die("XStoreName: Allocation failed");
  91. XFlush(dpy);
  92. }
  93. if (!done) {
  94. if (clock_gettime(CLOCK_MONOTONIC, &current) < 0)
  95. die("clock_gettime:");
  96. difftimespec(&diff, &current, &start);
  97. intspec.tv_sec = interval / 1000;
  98. intspec.tv_nsec = (interval % 1000) * 1E6;
  99. difftimespec(&wait, &intspec, &diff);
  100. if (wait.tv_sec >= 0 &&
  101. nanosleep(&wait, NULL) < 0 &&
  102. errno != EINTR)
  103. die("nanosleep:");
  104. }
  105. } while (!done);
  106. if (!sflag) {
  107. XStoreName(dpy, DefaultRootWindow(dpy), NULL);
  108. if (XCloseDisplay(dpy) < 0)
  109. die("XCloseDisplay: Failed to close display");
  110. }
  111. return 0;
  112. }