8 #define SIGPLUS SIGUSR1+1
9 #define SIGMINUS SIGUSR1-1
11 #define SIGPLUS SIGRTMIN
12 #define SIGMINUS SIGRTMIN
14 #define LENGTH(X) (sizeof(X) / sizeof (X[0]))
16 #define STATUSLENGTH (LENGTH(blocks) * CMDLENGTH + 1)
21 unsigned int interval;
25 void dummysighandler(int num);
27 void sighandler(int num);
28 void getcmds(int time);
29 void getsigcmds(int signal);
31 void sighandler(int signum);
32 int getstatus(char *str, char *last);
35 void termhandler(int signum);
43 static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
44 static char statusstr[2][STATUSLENGTH];
45 static int statusContinue = 1;
46 static void (*writestatus) () = setroot;
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]);
77 void getsigcmds(int signal)
80 for (int i = 0; i < LENGTH(blocks); i++)
83 if (current->signal == signal)
84 getcmd(current,statusbar[i]);
91 /* initialize all real time signals with dummy handler */
92 for(int i = SIGRTMIN; i <= SIGRTMAX; i++)
93 signal(i, dummysighandler);
96 for(int i = 0; i < LENGTH(blocks); i++)
98 if (blocks[i].signal > 0)
99 signal(SIGMINUS+blocks[i].signal, sighandler);
104 int getstatus(char *str, char *last)
108 for(int i = 0; i < LENGTH(blocks); i++)
109 strcat(str, statusbar[i]);
110 str[strlen(str)-1] = '\0';
111 return strcmp(str, last);//0 if they are the same
116 if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
118 Display *d = XOpenDisplay(NULL);
122 screen = DefaultScreen(dpy);
123 root = RootWindow(dpy, screen);
124 XStoreName(dpy, root, statusstr[0]);
130 if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
132 printf("%s\n",statusstr[0]);
142 while(statusContinue)
152 /* this signal handler should do nothing */
153 void dummysighandler(int signum)
159 void sighandler(int signum)
161 getsigcmds(signum-SIGPLUS);
165 void termhandler(int signum)
171 int main(int argc, char** argv)
173 for(int i = 0; i < argc; i++)
175 if (!strcmp("-d",argv[i]))
176 delim = argv[++i][0];
177 else if(!strcmp("-p",argv[i]))
178 writestatus = pstdout;
180 signal(SIGTERM, termhandler);
181 signal(SIGINT, termhandler);