10 #define SIGPLUS SIGUSR1+1
11 #define SIGMINUS SIGUSR1-1
13 #define SIGPLUS SIGRTMIN
14 #define SIGMINUS SIGRTMIN
16 #define LENGTH(X) (sizeof(X) / sizeof (X[0]))
18 #define MIN( a, b ) ( ( a < b) ? a : b )
19 #define STATUSLENGTH (LENGTH(blocks) * CMDLENGTH + 1)
24 unsigned int interval;
28 void dummysighandler(int num);
30 void sighandler(int num);
31 void getcmds(int time);
32 void getsigcmds(unsigned int signal);
34 void sighandler(int signum);
35 int getstatus(char *str, char *last);
41 static void (*writestatus) () = setroot;
47 static void (*writestatus) () = pstdout;
53 static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
54 static char statusstr[2][STATUSLENGTH];
55 static int statusContinue = 1;
56 static int returnStatus = 0;
58 //opens process *cmd and stores output in *output
59 void getcmd(const Block *block, char *output)
61 strcpy(output, block->icon);
62 FILE *cmdf = popen(block->command, "r");
65 int i = strlen(block->icon);
66 fgets(output+i, CMDLENGTH-i-delimLen, cmdf);
68 if (i == 0)//return if block and command output are both empty
70 if (delim[0] != '\0') {
71 //only chop off newline if one is present at the end
72 i = output[i-1] == '\n' ? i-1 : i;
73 strncpy(output+i, delim, delimLen);
80 void getcmds(int time)
83 for (unsigned int i = 0; i < LENGTH(blocks); i++) {
85 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
86 getcmd(current,statusbar[i]);
90 void getsigcmds(unsigned int signal)
93 for (unsigned int i = 0; i < LENGTH(blocks); i++) {
95 if (current->signal == signal)
96 getcmd(current,statusbar[i]);
103 /* initialize all real time signals with dummy handler */
104 for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
105 signal(i, dummysighandler);
108 for (unsigned int i = 0; i < LENGTH(blocks); i++) {
109 if (blocks[i].signal > 0)
110 signal(SIGMINUS+blocks[i].signal, sighandler);
115 int getstatus(char *str, char *last)
119 for (unsigned int i = 0; i < LENGTH(blocks); i++)
120 strcat(str, statusbar[i]);
121 str[strlen(str)-strlen(delim)] = '\0';
122 return strcmp(str, last);//0 if they are the same
128 if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
130 XStoreName(dpy, root, statusstr[0]);
136 dpy = XOpenDisplay(NULL);
138 fprintf(stderr, "dwmblocks: Failed to open display\n");
141 screen = DefaultScreen(dpy);
142 root = RootWindow(dpy, screen);
149 if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
151 printf("%s\n",statusstr[0]);
171 /* this signal handler should do nothing */
172 void dummysighandler(int signum)
178 void sighandler(int signum)
180 getsigcmds(signum-SIGPLUS);
189 int main(int argc, char** argv)
191 for (int i = 0; i < argc; i++) {//Handle command line arguments
192 if (!strcmp("-d",argv[i]))
193 strncpy(delim, argv[++i], delimLen);
194 else if (!strcmp("-p",argv[i]))
195 writestatus = pstdout;
201 delimLen = MIN(delimLen, strlen(delim));
202 delim[delimLen++] = '\0';
203 signal(SIGTERM, termhandler);
204 signal(SIGINT, termhandler);