Only open X server once rather than open and close after every writeout.
[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)//return if block and command output are both empty
69                 return;
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); 
74         }
75         else
76                 output[i++] = '\0';
77         pclose(cmdf);
78 }
79
80 void getcmds(int time)
81 {
82         const Block* current;
83         for (unsigned int i = 0; i < LENGTH(blocks); i++) {
84                 current = blocks + i;
85                 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
86                         getcmd(current,statusbar[i]);
87         }
88 }
89
90 void getsigcmds(unsigned int signal)
91 {
92         const Block *current;
93         for (unsigned int i = 0; i < LENGTH(blocks); i++) {
94                 current = blocks + i;
95                 if (current->signal == signal)
96                         getcmd(current,statusbar[i]);
97         }
98 }
99
100 void setupsignals()
101 {
102 #ifndef __OpenBSD__
103             /* initialize all real time signals with dummy handler */
104     for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
105         signal(i, dummysighandler);
106 #endif
107
108         for (unsigned int i = 0; i < LENGTH(blocks); i++) {
109                 if (blocks[i].signal > 0)
110                         signal(SIGMINUS+blocks[i].signal, sighandler);
111         }
112
113 }
114
115 int getstatus(char *str, char *last)
116 {
117         strcpy(last, str);
118         str[0] = '\0';
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
123 }
124
125 #ifndef NO_X
126 void setroot()
127 {
128         if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
129                 return;
130         XStoreName(dpy, root, statusstr[0]);
131         XFlush(dpy);
132 }
133
134 int setupX()
135 {
136         dpy = XOpenDisplay(NULL);
137         if (!dpy) {
138                 fprintf(stderr, "dwmblocks: Failed to open display\n");
139                 return 0;
140         }
141         screen = DefaultScreen(dpy);
142         root = RootWindow(dpy, screen);
143         return 1;
144 }
145 #endif
146
147 void pstdout()
148 {
149         if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
150                 return;
151         printf("%s\n",statusstr[0]);
152         fflush(stdout);
153 }
154
155
156 void statusloop()
157 {
158         setupsignals();
159         int i = 0;
160         getcmds(-1);
161         while (1) {
162                 getcmds(i++);
163                 writestatus();
164                 if (!statusContinue)
165                         break;
166                 sleep(1.0);
167         }
168 }
169
170 #ifndef __OpenBSD__
171 /* this signal handler should do nothing */
172 void dummysighandler(int signum)
173 {
174     return;
175 }
176 #endif
177
178 void sighandler(int signum)
179 {
180         getsigcmds(signum-SIGPLUS);
181         writestatus();
182 }
183
184 void termhandler()
185 {
186         statusContinue = 0;
187 }
188
189 int main(int argc, char** argv)
190 {
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;
196         }
197 #ifndef NO_X
198         if (!setupX())
199                 return 1;
200 #endif
201         delimLen = MIN(delimLen, strlen(delim));
202         delim[delimLen++] = '\0';
203         signal(SIGTERM, termhandler);
204         signal(SIGINT, termhandler);
205         statusloop();
206 #ifndef NO_X
207         XCloseDisplay(dpy);
208 #endif
209         return 0;
210 }