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, cmdf);
69 //return if block and command output are both empty
72 } else if (output[i-1] == '\n') {
78 void getcmds(int time)
81 for (unsigned int i = 0; i < LENGTH(blocks); i++) {
83 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
84 getcmd(current,statusbar[i]);
88 void getsigcmds(unsigned int signal)
91 for (unsigned int i = 0; i < LENGTH(blocks); i++) {
93 if (current->signal == signal)
94 getcmd(current,statusbar[i]);
101 /* initialize all real time signals with dummy handler */
102 for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
103 signal(i, dummysighandler);
106 for (unsigned int i = 0; i < LENGTH(blocks); i++) {
107 if (blocks[i].signal > 0)
108 signal(SIGMINUS+blocks[i].signal, sighandler);
113 int getstatus(char *str, char *last)
117 for (unsigned int i = 0; i < LENGTH(blocks); i++)
118 strcat(str, statusbar[i]);
119 str[strlen(str)] = '\0';
120 return strcmp(str, last);//0 if they are the same
126 if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
128 XStoreName(dpy, root, statusstr[0]);
134 dpy = XOpenDisplay(NULL);
136 fprintf(stderr, "dwmblocks: Failed to open display\n");
139 screen = DefaultScreen(dpy);
140 root = RootWindow(dpy, screen);
147 if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
149 printf("%s\n",statusstr[0]);
169 /* this signal handler should do nothing */
170 void dummysighandler(int signum)
176 void sighandler(int signum)
178 getsigcmds(signum-SIGPLUS);
187 int main(int argc, char** argv)
189 for (int i = 0; i < argc; i++) {//Handle command line arguments
190 if (!strcmp("-p",argv[i]))
191 writestatus = pstdout;
197 signal(SIGTERM, termhandler);
198 signal(SIGINT, termhandler);