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);
69 //return if block and command output are both empty
73 if (delim[0] != '\0') {
74 //only chop off newline if one is present at the end
75 i = output[i-1] == '\n' ? i-1 : i;
76 strncpy(output+i, delim, delimLen);
83 void getcmds(int time)
86 for (unsigned int i = 0; i < LENGTH(blocks); i++) {
88 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
89 getcmd(current,statusbar[i]);
93 void getsigcmds(unsigned int signal)
96 for (unsigned int i = 0; i < LENGTH(blocks); i++) {
98 if (current->signal == signal)
99 getcmd(current,statusbar[i]);
106 /* initialize all real time signals with dummy handler */
107 for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
108 signal(i, dummysighandler);
111 for (unsigned int i = 0; i < LENGTH(blocks); i++) {
112 if (blocks[i].signal > 0)
113 signal(SIGMINUS+blocks[i].signal, sighandler);
118 int getstatus(char *str, char *last)
122 for (unsigned int i = 0; i < LENGTH(blocks); i++)
123 strcat(str, statusbar[i]);
124 str[strlen(str)-strlen(delim)] = '\0';
125 return strcmp(str, last);//0 if they are the same
131 if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
133 XStoreName(dpy, root, statusstr[0]);
139 dpy = XOpenDisplay(NULL);
141 fprintf(stderr, "dwmblocks: Failed to open display\n");
144 screen = DefaultScreen(dpy);
145 root = RootWindow(dpy, screen);
152 if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
154 printf("%s\n",statusstr[0]);
174 /* this signal handler should do nothing */
175 void dummysighandler(int signum)
181 void sighandler(int signum)
183 getsigcmds(signum-SIGPLUS);
192 int main(int argc, char** argv)
194 for (int i = 0; i < argc; i++) {//Handle command line arguments
195 if (!strcmp("-d",argv[i]))
196 strncpy(delim, argv[++i], delimLen);
197 else if (!strcmp("-p",argv[i]))
198 writestatus = pstdout;
204 delimLen = MIN(delimLen, strlen(delim));
205 delim[delimLen++] = '\0';
206 signal(SIGTERM, termhandler);
207 signal(SIGINT, termhandler);