7 #define LENGTH(X) (sizeof(X) / sizeof (X[0]))
13 unsigned int interval;
16 void sighandler(int num);
17 void replace(char *str, char old, char new);
18 void getcmds(int time);
20 void getsigcmds(int signal);
22 void sighandler(int signum);
24 int getstatus(char *str, char *last);
27 void termhandler(int signum);
35 static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
36 static char statusstr[2][256];
37 static int statusContinue = 1;
38 static void (*writestatus) () = setroot;
40 void replace(char *str, char old, char new)
43 for(int i = 0; i < N; i++)
48 //opens process *cmd and stores output in *output
49 void getcmd(const Block *block, char *output)
51 strcpy(output, block->icon);
52 char *cmd = block->command;
53 FILE *cmdf = popen(cmd,"r");
57 int i = strlen(block->icon);
58 fgets(output+i, CMDLENGTH-i, cmdf);
60 if (delim != '\0' && --i)
66 void getcmds(int time)
69 for(int i = 0; i < LENGTH(blocks); i++)
72 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
73 getcmd(current,statusbar[i]);
78 void getsigcmds(int signal)
81 for (int i = 0; i < LENGTH(blocks); i++)
84 if (current->signal == signal)
85 getcmd(current,statusbar[i]);
91 for(int i = 0; i < LENGTH(blocks); i++)
93 if (blocks[i].signal > 0)
94 signal(SIGRTMIN+blocks[i].signal, sighandler);
100 int getstatus(char *str, char *last)
104 for(int i = 0; i < LENGTH(blocks); i++)
105 strcat(str, statusbar[i]);
106 str[strlen(str)-1] = '\0';
107 return strcmp(str, last);//0 if they are the same
112 if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
114 Display *d = XOpenDisplay(NULL);
118 screen = DefaultScreen(dpy);
119 root = RootWindow(dpy, screen);
120 XStoreName(dpy, root, statusstr[0]);
126 if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
128 printf("%s\n",statusstr[0]);
140 while(statusContinue)
150 void sighandler(int signum)
152 getsigcmds(signum-SIGRTMIN);
157 void termhandler(int signum)
163 int main(int argc, char** argv)
165 for(int i = 0; i < argc; i++)
167 if (!strcmp("-d",argv[i]))
168 delim = argv[++i][0];
169 else if(!strcmp("-p",argv[i]))
170 writestatus = pstdout;
172 signal(SIGTERM, termhandler);
173 signal(SIGINT, termhandler);