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 MIN( a, b ) ( ( a < b) ? a : b )
17 #define STATUSLENGTH (LENGTH(blocks) * CMDLENGTH + 1)
22 unsigned int interval;
26 void dummysighandler(int num);
28 void sighandler(int num);
29 void getcmds(int time);
30 void getsigcmds(unsigned int signal);
32 void sighandler(int signum);
33 int getstatus(char *str, char *last);
44 static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
45 static char statusstr[2][STATUSLENGTH];
46 static int statusContinue = 1;
47 static void (*writestatus) () = setroot;
49 //opens process *cmd and stores output in *output
50 void getcmd(const Block *block, char *output)
52 strcpy(output, block->icon);
53 char *cmd = block->command;
54 FILE *cmdf = popen(cmd,"r");
58 int i = strlen(block->icon);
59 fgets(output+i, CMDLENGTH-i-delimLen, cmdf);
61 if (delim[0] != '\0' && --i)
62 strncpy(output+i, delim, delimLen);
68 void getcmds(int time)
71 for(unsigned int i = 0; i < LENGTH(blocks); i++)
74 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
75 getcmd(current,statusbar[i]);
79 void getsigcmds(unsigned int signal)
82 for (unsigned int i = 0; i < LENGTH(blocks); i++)
85 if (current->signal == signal)
86 getcmd(current,statusbar[i]);
93 /* initialize all real time signals with dummy handler */
94 for(int i = SIGRTMIN; i <= SIGRTMAX; i++)
95 signal(i, dummysighandler);
98 for(unsigned int i = 0; i < LENGTH(blocks); i++)
100 if (blocks[i].signal > 0)
101 signal(SIGMINUS+blocks[i].signal, sighandler);
106 int getstatus(char *str, char *last)
110 for(unsigned int i = 0; i < LENGTH(blocks); i++)
111 strcat(str, statusbar[i]);
112 str[strlen(str)-strlen(delim)] = '\0';
113 return strcmp(str, last);//0 if they are the same
118 if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
120 Display *d = XOpenDisplay(NULL);
124 screen = DefaultScreen(dpy);
125 root = RootWindow(dpy, screen);
126 XStoreName(dpy, root, statusstr[0]);
132 if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
134 printf("%s\n",statusstr[0]);
144 while(statusContinue)
154 /* this signal handler should do nothing */
155 void dummysighandler(int signum)
161 void sighandler(int signum)
163 getsigcmds(signum-SIGPLUS);
172 int main(int argc, char** argv)
174 for(int i = 0; i < argc; i++)
176 if (!strcmp("-d",argv[i]))
177 strncpy(delim, argv[++i], delimLen);
178 else if(!strcmp("-p",argv[i]))
179 writestatus = pstdout;
181 delim[MIN(delimLen++, strlen(delim))] = '\0';
182 signal(SIGTERM, termhandler);
183 signal(SIGINT, termhandler);