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.

179 lines
3.2 KiB

  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<unistd.h>
  5. #include<signal.h>
  6. #include<X11/Xlib.h>
  7. #define LENGTH(X) (sizeof(X) / sizeof (X[0]))
  8. typedef struct {
  9. char* icon;
  10. char* command;
  11. unsigned int interval;
  12. unsigned int signal;
  13. } Block;
  14. void sighandler(int num);
  15. void replace(char *str, char old, char new);
  16. void getcmds(int time);
  17. void getsigcmds(int signal);
  18. void setupsignals();
  19. int getstatus(char *str, char *last);
  20. void setroot();
  21. void statusloop();
  22. void statusinit();
  23. void sighandler(int signum);
  24. void termhandler(int signum);
  25. #include "blocks.h"
  26. static Display *dpy;
  27. static int screen;
  28. static Window root;
  29. static char statusbar[LENGTH(blocks)][50] = {0};
  30. static char statusstr[2][256];
  31. static int statusContinue = 1;
  32. static void (*writestatus) () = setroot;
  33. void replace(char *str, char old, char new)
  34. {
  35. int N = strlen(str);
  36. for(int i = 0; i < N; i++)
  37. if(str[i] == old)
  38. str[i] = new;
  39. }
  40. //opens process *cmd and stores output in *output
  41. void getcmd(const Block *block, char *output)
  42. {
  43. strcpy(output, block->icon);
  44. char *cmd = block->command;
  45. FILE *cmdf = popen(cmd,"r");
  46. if (!cmdf)
  47. return;
  48. char c;
  49. int i = strlen(block->icon);
  50. while((c = fgetc(cmdf)) != EOF)
  51. {
  52. output[i++] = c;
  53. if(c == '\n')
  54. break;
  55. }
  56. if (delim != '\0' && --i)
  57. output[i++] = delim;
  58. output[i++] = '\0';
  59. pclose(cmdf);
  60. }
  61. void getcmds(int time)
  62. {
  63. const Block* current;
  64. for(int i = 0; i < LENGTH(blocks); i++)
  65. {
  66. current = blocks + i;
  67. if ((current->interval != 0 && time % current->interval == 0) || time == -1)
  68. getcmd(current,statusbar[i]);
  69. }
  70. }
  71. void getsigcmds(int signal)
  72. {
  73. const Block *current;
  74. for (int i = 0; i < LENGTH(blocks); i++)
  75. {
  76. current = blocks + i;
  77. if (current->signal == signal)
  78. getcmd(current,statusbar[i]);
  79. }
  80. }
  81. void setupsignals()
  82. {
  83. for(int i = 0; i < LENGTH(blocks); i++)
  84. {
  85. if (blocks[i].signal > 0)
  86. signal(SIGRTMIN+blocks[i].signal, sighandler);
  87. }
  88. }
  89. int getstatus(char *str, char *last)
  90. {
  91. strcpy(last, str);
  92. str[0] = '\0';
  93. for(int i = 0; i < LENGTH(blocks); i++)
  94. {
  95. strcat(str, statusbar[i]);
  96. }
  97. str[strlen(str)-1] = '\0';
  98. return strcmp(str, last);//0 if they are the same
  99. }
  100. void setroot()
  101. {
  102. if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
  103. return;
  104. Display *d = XOpenDisplay(NULL);
  105. if (d) {
  106. dpy = d;
  107. }
  108. screen = DefaultScreen(dpy);
  109. root = RootWindow(dpy, screen);
  110. XStoreName(dpy, root, statusstr[0]);
  111. XCloseDisplay(dpy);
  112. }
  113. void pstdout()
  114. {
  115. if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
  116. return;
  117. printf("%s\n",statusstr[0]);
  118. fflush(stdout);
  119. }
  120. void statusloop()
  121. {
  122. setupsignals();
  123. int i = 0;
  124. getcmds(-1);
  125. while(statusContinue)
  126. {
  127. getcmds(i);
  128. writestatus();
  129. sleep(1.0);
  130. i++;
  131. }
  132. }
  133. void statusinit()
  134. {
  135. statusloop();
  136. }
  137. void sighandler(int signum)
  138. {
  139. getsigcmds(signum-SIGRTMIN);
  140. writestatus();
  141. }
  142. void termhandler(int signum)
  143. {
  144. statusContinue = 0;
  145. exit(0);
  146. }
  147. int main(int argc, char** argv)
  148. {
  149. for(int i = 0; i < argc; i++)
  150. {
  151. if (!strcmp("-d",argv[i]))
  152. delim = argv[++i][0];
  153. else if(!strcmp("-p",argv[i]))
  154. writestatus = pstdout;
  155. }
  156. signal(SIGTERM, termhandler);
  157. signal(SIGINT, termhandler);
  158. statusinit();
  159. }