7 #define LENGTH(X) (sizeof(X) / sizeof (X[0]))
13 unsigned int interval;
16 void sighandler(int num);
17 void replace(char *str, char old, char new);
18 void getcmds(int time);
19 void getsigcmds(int signal);
21 int getstatus(char *str, char *last);
24 void sighandler(int signum);
25 void termhandler(int signum);
33 static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
34 static char statusstr[2][256];
35 static int statusContinue = 1;
36 static void (*writestatus) () = setroot;
38 void replace(char *str, char old, char new)
41 for(int i = 0; i < N; i++)
46 //opens process *cmd and stores output in *output
47 void getcmd(const Block *block, char *output)
49 strcpy(output, block->icon);
50 char *cmd = block->command;
51 FILE *cmdf = popen(cmd,"r");
55 int i = strlen(block->icon);
56 fgets(output+i, CMDLENGTH-i, cmdf);
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); i++)
101 strcat(str, statusbar[i]);
102 str[strlen(str)-1] = '\0';
103 return strcmp(str, last);//0 if they are the same
108 if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
110 Display *d = XOpenDisplay(NULL);
114 screen = DefaultScreen(dpy);
115 root = RootWindow(dpy, screen);
116 XStoreName(dpy, root, statusstr[0]);
122 if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
124 printf("%s\n",statusstr[0]);
134 while(statusContinue)
144 void sighandler(int signum)
146 getsigcmds(signum-SIGRTMIN);
150 void termhandler(int signum)
156 int main(int argc, char** argv)
158 for(int i = 0; i < argc; i++)
160 if (!strcmp("-d",argv[i]))
161 delim = argv[++i][0];
162 else if(!strcmp("-p",argv[i]))
163 writestatus = pstdout;
165 signal(SIGTERM, termhandler);
166 signal(SIGINT, termhandler);