My slstatus configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

slstatus.c 2.6 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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) * 1000000000;
  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) {
  64. setbuf(stdout, NULL);
  65. }
  66. if (!sflag && !(dpy = XOpenDisplay(NULL))) {
  67. die("XOpenDisplay: Failed to open display");
  68. }
  69. while (!done) {
  70. if (clock_gettime(CLOCK_MONOTONIC, &start) < 0) {
  71. die("clock_gettime:");
  72. }
  73. status[0] = '\0';
  74. for (i = len = 0; i < LEN(args); i++) {
  75. if (!(res = args[i].func(args[i].args))) {
  76. res = unknown_str;
  77. }
  78. if ((ret = snprintf(status + len, sizeof(status) - len,
  79. args[i].fmt, res)) < 0) {
  80. warn("snprintf:");
  81. break;
  82. } else if ((size_t)ret >= sizeof(status) - len) {
  83. warn("snprintf: Output truncated");
  84. break;
  85. }
  86. len += ret;
  87. }
  88. if (sflag) {
  89. printf("%s\n", status);
  90. } else {
  91. if (XStoreName(dpy, DefaultRootWindow(dpy), status) < 0) {
  92. die("XStoreName: Allocation failed");
  93. }
  94. XFlush(dpy);
  95. }
  96. if (!done) {
  97. if (clock_gettime(CLOCK_MONOTONIC, &current) < 0) {
  98. die("clock_gettime:");
  99. }
  100. difftimespec(&diff, &current, &start);
  101. intspec.tv_sec = interval / 1000;
  102. intspec.tv_nsec = (interval % 1000) * 1000000;
  103. difftimespec(&wait, &intspec, &diff);
  104. if (wait.tv_sec >= 0) {
  105. if (nanosleep(&wait, NULL) < 0 &&
  106. errno != EINTR) {
  107. die("nanosleep:");
  108. }
  109. }
  110. }
  111. }
  112. if (!sflag) {
  113. XStoreName(dpy, DefaultRootWindow(dpy), NULL);
  114. if (XCloseDisplay(dpy) < 0) {
  115. die("XCloseDisplay: Failed to close display");
  116. }
  117. }
  118. return 0;
  119. }