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.
 
 
 
 

126 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 = 0;
  48. char status[MAXLEN];
  49. ARGBEGIN {
  50. case 's':
  51. sflag = 1;
  52. break;
  53. default:
  54. usage();
  55. } ARGEND
  56. if (argc) {
  57. usage();
  58. }
  59. setlocale(LC_ALL, "");
  60. memset(&act, 0, sizeof(act));
  61. act.sa_handler = terminate;
  62. sigaction(SIGINT, &act, NULL);
  63. sigaction(SIGTERM, &act, NULL);
  64. if (!sflag && !(dpy = XOpenDisplay(NULL))) {
  65. fprintf(stderr, "Cannot open display");
  66. return 1;
  67. }
  68. while (!done) {
  69. clock_gettime(CLOCK_MONOTONIC, &start);
  70. status[0] = '\0';
  71. for (i = len = 0; i < LEN(args); i++) {
  72. const char * res = args[i].func(args[i].args);
  73. res = (res == NULL) ? unknown_str : res;
  74. len += snprintf(status + len, sizeof(status) - len,
  75. args[i].fmt, res);
  76. if (len >= sizeof(status)) {
  77. status[sizeof(status) - 1] = '\0';
  78. }
  79. }
  80. if (sflag) {
  81. printf("%s\n", status);
  82. } else {
  83. XStoreName(dpy, DefaultRootWindow(dpy), status);
  84. XSync(dpy, False);
  85. }
  86. if (!done) {
  87. clock_gettime(CLOCK_MONOTONIC, &current);
  88. difftimespec(&diff, &current, &start);
  89. intspec.tv_sec = interval / 1000;
  90. intspec.tv_nsec = (interval % 1000) * 1000000;
  91. difftimespec(&wait, &intspec, &diff);
  92. if (wait.tv_sec >= 0) {
  93. nanosleep(&wait, NULL);
  94. }
  95. }
  96. }
  97. if (!sflag) {
  98. XStoreName(dpy, DefaultRootWindow(dpy), NULL);
  99. XCloseDisplay(dpy);
  100. }
  101. return 0;
  102. }