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 FILE *cmdf = popen(block->command, "r");
56 int i = strlen(block->icon);
57 fgets(output+i, CMDLENGTH-i-delimLen, cmdf);
59 if (delim[0] != '\0') {
60 //only chop off newline if one is present at the end
61 i = output[i-1] == '\n' ? i-1 : i;
62 strncpy(output+i, delim, delimLen);
69 void getcmds(int time)
72 for (unsigned int i = 0; i < LENGTH(blocks); i++)
75 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
76 getcmd(current,statusbar[i]);
80 void getsigcmds(unsigned int signal)
83 for (unsigned int i = 0; i < LENGTH(blocks); i++)
86 if (current->signal == signal)
87 getcmd(current,statusbar[i]);
94 /* initialize all real time signals with dummy handler */
95 for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
96 signal(i, dummysighandler);
99 for (unsigned int i = 0; i < LENGTH(blocks); i++)
101 if (blocks[i].signal > 0)
102 signal(SIGMINUS+blocks[i].signal, sighandler);
107 int getstatus(char *str, char *last)
111 for (unsigned int i = 0; i < LENGTH(blocks); i++)
112 strcat(str, statusbar[i]);
113 str[strlen(str)-strlen(delim)] = '\0';
114 return strcmp(str, last);//0 if they are the same
119 if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
121 Display *d = XOpenDisplay(NULL);
125 screen = DefaultScreen(dpy);
126 root = RootWindow(dpy, screen);
127 XStoreName(dpy, root, statusstr[0]);
133 if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
135 printf("%s\n",statusstr[0]);
145 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++) //Handle command line arguments
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);