slstatus.c 2.5 KB

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