]> git.armaanb.net Git - dwmblocks.git/blob - dwmblocks.c
Added the call to pclose() when the output from the command is empty
[dwmblocks.git] / dwmblocks.c
1 #include<stdlib.h>
2 #include<stdio.h>
3 #include<string.h>
4 #include<unistd.h>
5 #include<signal.h>
6 #ifndef NO_X
7 #include<X11/Xlib.h>
8 #endif
9 #ifdef __OpenBSD__
10 #define SIGPLUS                 SIGUSR1+1
11 #define SIGMINUS                SIGUSR1-1
12 #else
13 #define SIGPLUS                 SIGRTMIN
14 #define SIGMINUS                SIGRTMIN
15 #endif
16 #define LENGTH(X)               (sizeof(X) / sizeof (X[0]))
17 #define CMDLENGTH               50
18 #define MIN( a, b ) ( ( a < b) ? a : b )
19 #define STATUSLENGTH (LENGTH(blocks) * CMDLENGTH + 1)
20
21 typedef struct {
22         char* icon;
23         char* command;
24         unsigned int interval;
25         unsigned int signal;
26 } Block;
27 #ifndef __OpenBSD__
28 void dummysighandler(int num);
29 #endif
30 void sighandler(int num);
31 void getcmds(int time);
32 void getsigcmds(unsigned int signal);
33 void setupsignals();
34 void sighandler(int signum);
35 int getstatus(char *str, char *last);
36 void statusloop();
37 void termhandler();
38 void pstdout();
39 #ifndef NO_X
40 void setroot();
41 static void (*writestatus) () = setroot;
42 static int setupX();
43 static Display *dpy;
44 static int screen;
45 static Window root;
46 #else
47 static void (*writestatus) () = pstdout;
48 #endif
49
50
51 #include "blocks.h"
52
53 static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
54 static char statusstr[2][STATUSLENGTH];
55 static int statusContinue = 1;
56 static int returnStatus = 0;
57
58 //opens process *cmd and stores output in *output
59 void getcmd(const Block *block, char *output)
60 {
61         strcpy(output, block->icon);
62         FILE *cmdf = popen(block->command, "r");
63         if (!cmdf)
64                 return;
65         int i = strlen(block->icon);
66         fgets(output+i, CMDLENGTH-i-delimLen, cmdf);
67         i = strlen(output);
68         if (i == 0) {
69                 //return if block and command output are both empty
70                 pclose(cmdf);
71                 return;
72         }
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); 
77         }
78         else
79                 output[i++] = '\0';
80         pclose(cmdf);
81 }
82
83 void getcmds(int time)
84 {
85         const Block* current;
86         for (unsigned int i = 0; i < LENGTH(blocks); i++) {
87                 current = blocks + i;
88                 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
89                         getcmd(current,statusbar[i]);
90         }
91 }
92
93 void getsigcmds(unsigned int signal)
94 {
95         const Block *current;
96         for (unsigned int i = 0; i < LENGTH(blocks); i++) {
97                 current = blocks + i;
98                 if (current->signal == signal)
99                         getcmd(current,statusbar[i]);
100         }
101 }
102
103 void setupsignals()
104 {
105 #ifndef __OpenBSD__
106             /* initialize all real time signals with dummy handler */
107     for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
108         signal(i, dummysighandler);
109 #endif
110
111         for (unsigned int i = 0; i < LENGTH(blocks); i++) {
112                 if (blocks[i].signal > 0)
113                         signal(SIGMINUS+blocks[i].signal, sighandler);
114         }
115
116 }
117
118 int getstatus(char *str, char *last)
119 {
120         strcpy(last, str);
121         str[0] = '\0';
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
126 }
127
128 #ifndef NO_X
129 void setroot()
130 {
131         if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
132                 return;
133         XStoreName(dpy, root, statusstr[0]);
134         XFlush(dpy);
135 }
136
137 int setupX()
138 {
139         dpy = XOpenDisplay(NULL);
140         if (!dpy) {
141                 fprintf(stderr, "dwmblocks: Failed to open display\n");
142                 return 0;
143         }
144         screen = DefaultScreen(dpy);
145         root = RootWindow(dpy, screen);
146         return 1;
147 }
148 #endif
149
150 void pstdout()
151 {
152         if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
153                 return;
154         printf("%s\n",statusstr[0]);
155         fflush(stdout);
156 }
157
158
159 void statusloop()
160 {
161         setupsignals();
162         int i = 0;
163         getcmds(-1);
164         while (1) {
165                 getcmds(i++);
166                 writestatus();
167                 if (!statusContinue)
168                         break;
169                 sleep(1.0);
170         }
171 }
172
173 #ifndef __OpenBSD__
174 /* this signal handler should do nothing */
175 void dummysighandler(int signum)
176 {
177     return;
178 }
179 #endif
180
181 void sighandler(int signum)
182 {
183         getsigcmds(signum-SIGPLUS);
184         writestatus();
185 }
186
187 void termhandler()
188 {
189         statusContinue = 0;
190 }
191
192 int main(int argc, char** argv)
193 {
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;
199         }
200 #ifndef NO_X
201         if (!setupX())
202                 return 1;
203 #endif
204         delimLen = MIN(delimLen, strlen(delim));
205         delim[delimLen++] = '\0';
206         signal(SIGTERM, termhandler);
207         signal(SIGINT, termhandler);
208         statusloop();
209 #ifndef NO_X
210         XCloseDisplay(dpy);
211 #endif
212         return 0;
213 }