slstatus.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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)();
  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 [-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 '1':
  51. done = 1;
  52. /* fallthrough */
  53. case 's':
  54. sflag = 1;
  55. break;
  56. default:
  57. usage();
  58. } ARGEND
  59. if (argc) {
  60. usage();
  61. }
  62. memset(&act, 0, sizeof(act));
  63. act.sa_handler = terminate;
  64. sigaction(SIGINT, &act, NULL);
  65. sigaction(SIGTERM, &act, NULL);
  66. act.sa_flags |= SA_RESTART;
  67. sigaction(SIGUSR1, &act, NULL);
  68. if (!sflag && !(dpy = XOpenDisplay(NULL))) {
  69. die("XOpenDisplay: Failed to open display");
  70. }
  71. do {
  72. if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) {
  73. die("clock_gettime:");
  74. }
  75. status[0] = '\0';
  76. for (i = len = 0; i < LEN(args); i++) {
  77. if (!(res = args[i].func(args[i].args))) {
  78. res = unknown_str;
  79. }
  80. if ((ret = esnprintf(status + len, sizeof(status) - len,
  81. args[i].fmt, res)) < 0) {
  82. break;
  83. }
  84. len += ret;
  85. }
  86. if (sflag) {
  87. puts(status);
  88. fflush(stdout);
  89. if (ferror(stdout))
  90. die("puts:");
  91. } else {
  92. if (XStoreName(dpy, DefaultRootWindow(dpy), status)
  93. < 0) {
  94. die("XStoreName: Allocation failed");
  95. }
  96. XFlush(dpy);
  97. }
  98. if (!done) {
  99. if (clock_gettime(CLOCK_MONOTONIC, &current) < 0) {
  100. die("clock_gettime:");
  101. }
  102. difftimespec(&diff, &current, &start);
  103. intspec.tv_sec = interval / 1000;
  104. intspec.tv_nsec = (interval % 1000) * 1E6;
  105. difftimespec(&wait, &intspec, &diff);
  106. if (wait.tv_sec >= 0) {
  107. if (nanosleep(&wait, NULL) < 0 &&
  108. errno != EINTR) {
  109. die("nanosleep:");
  110. }
  111. }
  112. }
  113. } while (!done);
  114. if (!sflag) {
  115. XStoreName(dpy, DefaultRootWindow(dpy), NULL);
  116. if (XCloseDisplay(dpy) < 0) {
  117. die("XCloseDisplay: Failed to close display");
  118. }
  119. }
  120. return 0;
  121. }