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 (i == 0)//return if block and command output are both empty
61 if (delim[0] != '\0') {
62 //only chop off newline if one is present at the end
63 i = output[i-1] == '\n' ? i-1 : i;
64 strncpy(output+i, delim, delimLen);
71 void getcmds(int time)
74 for (unsigned int i = 0; i < LENGTH(blocks); i++)
77 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
78 getcmd(current,statusbar[i]);
82 void getsigcmds(unsigned int signal)
85 for (unsigned int i = 0; i < LENGTH(blocks); i++)
88 if (current->signal == signal)
89 getcmd(current,statusbar[i]);
96 /* initialize all real time signals with dummy handler */
97 for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
98 signal(i, dummysighandler);
101 for (unsigned int i = 0; i < LENGTH(blocks); i++)
103 if (blocks[i].signal > 0)
104 signal(SIGMINUS+blocks[i].signal, sighandler);
109 int getstatus(char *str, char *last)
113 for (unsigned int i = 0; i < LENGTH(blocks); i++)
114 strcat(str, statusbar[i]);
115 str[strlen(str)-strlen(delim)] = '\0';
116 return strcmp(str, last);//0 if they are the same
121 if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
123 Display *d = XOpenDisplay(NULL);
127 screen = DefaultScreen(dpy);
128 root = RootWindow(dpy, screen);
129 XStoreName(dpy, root, statusstr[0]);
135 if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
137 printf("%s\n",statusstr[0]);
147 while (statusContinue)
156 /* this signal handler should do nothing */
157 void dummysighandler(int signum)
163 void sighandler(int signum)
165 getsigcmds(signum-SIGPLUS);
174 int main(int argc, char** argv)
176 for (int i = 0; i < argc; i++) //Handle command line arguments
178 if (!strcmp("-d",argv[i]))
179 strncpy(delim, argv[++i], delimLen);
180 else if (!strcmp("-p",argv[i]))
181 writestatus = pstdout;
183 delimLen = MIN(delimLen, strlen(delim));
184 delim[delimLen++] = '\0';
185 signal(SIGTERM, termhandler);
186 signal(SIGINT, termhandler);