Another copy of my dotfiles. Because I don't completely trust GitHub.
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.

256 lines
5.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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. #define CMDLENGTH 50
  9. typedef struct {
  10. char* icon;
  11. char* command;
  12. unsigned int interval;
  13. unsigned int signal;
  14. } Block;
  15. char** last_updates;
  16. void sighandler(int num);
  17. void buttonhandler(int sig, siginfo_t *si, void *ucontext);
  18. void replace(char *str, char old, char new);
  19. void remove_all(char *str, char to_remove);
  20. void getcmds(int time);
  21. #ifndef __OpenBSD__
  22. void getsigcmds(int signal);
  23. void setupsignals();
  24. void sighandler(int signum);
  25. #endif
  26. int getstatus(char *str, char *last);
  27. void setroot();
  28. void statusloop();
  29. void termhandler(int signum);
  30. #include "config.h"
  31. static Display *dpy;
  32. static int screen;
  33. static Window root;
  34. static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
  35. static char statusstr[2][1024];
  36. static int statusContinue = 1;
  37. static void (*writestatus) () = setroot;
  38. void replace(char *str, char old, char new)
  39. {
  40. int N = strlen(str);
  41. for(int i = 0; i < N; i++)
  42. if(str[i] == old)
  43. str[i] = new;
  44. }
  45. void remove_all(char *str, char to_remove) {
  46. char *read = str;
  47. char *write = str;
  48. while (*read) {
  49. if (*read == to_remove) {
  50. read++;
  51. *write = *read;
  52. }
  53. read++;
  54. write++;
  55. }
  56. }
  57. //opens process *cmd and stores output in *output
  58. void getcmd(const Block *block, char* last_update , char *output)
  59. {
  60. if (block->signal)
  61. {
  62. output[0] = block->signal;
  63. output++;
  64. }
  65. strcpy(output, block->icon);
  66. char *cmd = block->command;
  67. FILE *cmdf = popen(cmd,"r");
  68. if (!cmdf)
  69. return;
  70. char c;
  71. int i = strlen(block->icon);
  72. fgets(output+i, CMDLENGTH-(strlen(delim)+1), cmdf);
  73. remove_all(output, '\n');
  74. if(i == strlen(output))
  75. strcpy(output+i, last_update);
  76. else
  77. strcpy(last_update, output+i);
  78. i = strlen(output);
  79. if ((i > 0 && block != &blocks[LENGTH(blocks) - 1]))
  80. strcat(output, delim);
  81. i+=strlen(delim);
  82. output[i++] = '\0';
  83. pclose(cmdf);
  84. }
  85. void getcmds(int time)
  86. {
  87. const Block* current;
  88. for(int i = 0; i < LENGTH(blocks); i++)
  89. {
  90. current = blocks + i;
  91. if ((current->interval != 0 && time % current->interval == 0) || time == -1)
  92. getcmd(current,last_updates[i],statusbar[i]);
  93. }
  94. }
  95. #ifndef __OpenBSD__
  96. void getsigcmds(int signal)
  97. {
  98. const Block *current;
  99. for (int i = 0; i < LENGTH(blocks); i++)
  100. {
  101. current = blocks + i;
  102. if (current->signal == signal)
  103. getcmd(current,last_updates[i],statusbar[i]);
  104. }
  105. }
  106. void setupsignals()
  107. {
  108. struct sigaction sa;
  109. for(int i = SIGRTMIN; i <= SIGRTMAX; i++)
  110. signal(i, SIG_IGN);
  111. for(int i = 0; i < LENGTH(blocks); i++)
  112. {
  113. if (blocks[i].signal > 0)
  114. {
  115. signal(SIGRTMIN+blocks[i].signal, sighandler);
  116. sigaddset(&sa.sa_mask, SIGRTMIN+blocks[i].signal);
  117. }
  118. }
  119. sa.sa_sigaction = buttonhandler;
  120. sa.sa_flags = SA_SIGINFO;
  121. sigaction(SIGUSR1, &sa, NULL);
  122. struct sigaction sigchld_action = {
  123. .sa_handler = SIG_DFL,
  124. .sa_flags = SA_NOCLDWAIT
  125. };
  126. sigaction(SIGCHLD, &sigchld_action, NULL);
  127. }
  128. #endif
  129. int getstatus(char *str, char *last)
  130. {
  131. strcpy(last, str);
  132. str[0] = '\0';
  133. for(int i = 0; i < LENGTH(blocks); i++) {
  134. strcat(str, statusbar[i]);
  135. if (i == LENGTH(blocks) - 1)
  136. strcat(str, " ");
  137. }
  138. str[strlen(str)-1] = '\0';
  139. return strcmp(str, last);//0 if they are the same
  140. }
  141. void setroot()
  142. {
  143. if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
  144. return;
  145. Display *d = XOpenDisplay(NULL);
  146. if (d) {
  147. dpy = d;
  148. }
  149. screen = DefaultScreen(dpy);
  150. root = RootWindow(dpy, screen);
  151. XStoreName(dpy, root, statusstr[0]);
  152. XCloseDisplay(dpy);
  153. }
  154. void pstdout()
  155. {
  156. if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
  157. return;
  158. printf("%s\n",statusstr[0]);
  159. fflush(stdout);
  160. }
  161. void statusloop()
  162. {
  163. #ifndef __OpenBSD__
  164. setupsignals();
  165. #endif
  166. last_updates = malloc(sizeof(char*) * LENGTH(blocks));
  167. for(int i = 0; i < LENGTH(blocks); i++) {
  168. last_updates[i] = malloc(sizeof(char) * CMDLENGTH);
  169. strcpy(last_updates[i],"");
  170. }
  171. int i = 0;
  172. getcmds(-1);
  173. while(statusContinue)
  174. {
  175. getcmds(i);
  176. writestatus();
  177. sleep(1.0);
  178. i++;
  179. }
  180. }
  181. #ifndef __OpenBSD__
  182. void sighandler(int signum)
  183. {
  184. getsigcmds(signum-SIGRTMIN);
  185. writestatus();
  186. }
  187. void buttonhandler(int sig, siginfo_t *si, void *ucontext)
  188. {
  189. char button[2] = {'0' + si->si_value.sival_int & 0xff, '\0'};
  190. pid_t process_id = getpid();
  191. sig = si->si_value.sival_int >> 8;
  192. if (fork() == 0)
  193. {
  194. const Block *current;
  195. for (int i = 0; i < LENGTH(blocks); i++)
  196. {
  197. current = blocks + i;
  198. if (current->signal == sig)
  199. break;
  200. }
  201. char shcmd[1024];
  202. sprintf(shcmd,"%s && kill -%d %d",current->command, current->signal+34,process_id);
  203. char *command[] = { "/bin/sh", "-c", shcmd, NULL };
  204. setenv("BLOCK_BUTTON", button, 1);
  205. setsid();
  206. execvp(command[0], command);
  207. exit(EXIT_SUCCESS);
  208. }
  209. }
  210. #endif
  211. void termhandler(int signum)
  212. {
  213. for(int i = 0; i < LENGTH(blocks); i++) {
  214. free(last_updates[i]);
  215. }
  216. free(last_updates);
  217. statusContinue = 0;
  218. exit(0);
  219. }
  220. int main(int argc, char** argv)
  221. {
  222. for(int i = 0; i < argc; i++)
  223. {
  224. if (!strcmp("-d",argv[i]))
  225. delim = argv[++i];
  226. else if(!strcmp("-p",argv[i]))
  227. writestatus = pstdout;
  228. }
  229. signal(SIGTERM, termhandler);
  230. signal(SIGINT, termhandler);
  231. statusloop();
  232. }