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.
 
 
 
 

128 lines
2.3 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <locale.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 *argv0;
  18. char buf[1024];
  19. static unsigned short int done;
  20. static Display *dpy;
  21. #include "config.h"
  22. static void
  23. terminate(const int signo)
  24. {
  25. (void)signo;
  26. done = 1;
  27. }
  28. static void
  29. difftimespec(struct timespec *res, struct timespec *a, struct timespec *b)
  30. {
  31. res->tv_sec = a->tv_sec - b->tv_sec - (a->tv_nsec < b->tv_nsec);
  32. res->tv_nsec = a->tv_nsec - b->tv_nsec +
  33. (a->tv_nsec < b->tv_nsec) * 1000000000;
  34. }
  35. static void
  36. usage(void)
  37. {
  38. fprintf(stderr, "usage: %s [-s]\n", argv0);
  39. exit(1);
  40. }
  41. int
  42. main(int argc, char *argv[])
  43. {
  44. struct sigaction act;
  45. struct timespec start, current, diff, intspec, wait;
  46. size_t i, len;
  47. int sflag;
  48. char status[MAXLEN];
  49. sflag = 0;
  50. ARGBEGIN {
  51. case 's':
  52. sflag = 1;
  53. break;
  54. default:
  55. usage();
  56. } ARGEND
  57. if (argc) {
  58. usage();
  59. }
  60. setlocale(LC_ALL, "");
  61. memset(&act, 0, sizeof(act));
  62. act.sa_handler = terminate;
  63. sigaction(SIGINT, &act, NULL);
  64. sigaction(SIGTERM, &act, NULL);
  65. if (!sflag && !(dpy = XOpenDisplay(NULL))) {
  66. fprintf(stderr, "Cannot open display");
  67. return 1;
  68. }
  69. while (!done) {
  70. clock_gettime(CLOCK_MONOTONIC, &start);
  71. status[0] = '\0';
  72. for (i = len = 0; i < LEN(args); i++) {
  73. const char * res = args[i].func(args[i].args);
  74. res = (res == NULL) ? unknown_str : res;
  75. len += snprintf(status + len, sizeof(status) - len,
  76. args[i].fmt, res);
  77. if (len >= sizeof(status)) {
  78. status[sizeof(status) - 1] = '\0';
  79. }
  80. }
  81. if (sflag) {
  82. printf("%s\n", status);
  83. fflush(stdout);
  84. } else {
  85. XStoreName(dpy, DefaultRootWindow(dpy), status);
  86. XSync(dpy, False);
  87. }
  88. if (!done) {
  89. clock_gettime(CLOCK_MONOTONIC, &current);
  90. difftimespec(&diff, &current, &start);
  91. intspec.tv_sec = interval / 1000;
  92. intspec.tv_nsec = (interval % 1000) * 1000000;
  93. difftimespec(&wait, &intspec, &diff);
  94. if (wait.tv_sec >= 0) {
  95. nanosleep(&wait, NULL);
  96. }
  97. }
  98. }
  99. if (!sflag) {
  100. XStoreName(dpy, DefaultRootWindow(dpy), NULL);
  101. XCloseDisplay(dpy);
  102. }
  103. return 0;
  104. }