7 #define LENGTH(X) (sizeof(X) / sizeof (X[0]))
12 unsigned int interval;
15 void sighandler(int num);
16 void replace(char *str, char old, char new);
17 void getcmds(int time);
18 void getsigcmds(int signal);
20 int getstatus(char *str, char *last);
24 void sighandler(int signum);
25 void termhandler(int signum);
33 static char statusbar[LENGTH(blocks)][50] = {0};
34 static char statusstr[2][256];
35 static int statusContinue = 1;
37 void replace(char *str, char old, char new)
40 for(int i = 0; i < N; i++)
45 //opens process *cmd and stores output in *output
46 void getcmd(const Block *block, char *output)
48 strcpy(output, block->icon);
49 char *cmd = block->command;
50 FILE *cmdf = popen(cmd,"r");
53 //int N = strlen(output);
55 int i = strlen(block->icon);
56 while((c = fgetc(cmdf)) != EOF)
58 if (delim != '\0' && --i)
64 void getcmds(int time)
67 for(int i = 0; i < LENGTH(blocks); i++)
70 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
71 getcmd(current,statusbar[i]);
75 void getsigcmds(int signal)
78 for (int i = 0; i < LENGTH(blocks); i++)
81 if (current->signal == signal)
82 getcmd(current,statusbar[i]);
88 for(int i = 0; i < LENGTH(blocks); i++)
90 if (blocks[i].signal > 0)
91 signal(SIGRTMIN+blocks[i].signal, sighandler);
96 int getstatus(char *str, char *last)
100 for(int i = 0; i < LENGTH(blocks); j+=strlen(statusbar[i++]))
102 strcpy(str + j, statusbar[i]);
105 return strcmp(str, last);//0 if they are the same
110 if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
112 Display *d = XOpenDisplay(NULL);
116 screen = DefaultScreen(dpy);
117 root = RootWindow(dpy, screen);
118 XStoreName(dpy, root, statusstr[0]);
128 while(statusContinue)
143 void sighandler(int signum)
145 getsigcmds(signum-SIGRTMIN);
149 void termhandler(int signum)
155 int main(int argc, char** argv)
157 for(int i = 0; i < argc; i++)
159 if (!strcmp("-d",argv[i]))
160 delim = argv[++i][0];
162 signal(SIGTERM, termhandler);
163 signal(SIGINT, termhandler);