ef257859c3e0fa0d238f5c8d0eff26689ca6c9e6
[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 #else
43 static void (*writestatus) () = pstdout;
44 #endif
45
46
47 #include "blocks.h"
48
49 static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
50 static char statusstr[2][STATUSLENGTH];
51 static int statusContinue = 1;
52 static int returnStatus = 0;
53
54 //opens process *cmd and stores output in *output
55 void getcmd(const Block *block, char *output)
56 {
57         strcpy(output, block->icon);
58         FILE *cmdf = popen(block->command, "r");
59         if (!cmdf)
60                 return;
61         int i = strlen(block->icon);
62         fgets(output+i, CMDLENGTH-i-delimLen, cmdf);
63         i = strlen(output);
64         if (i == 0)//return if block and command output are both empty
65                 return;
66         if (delim[0] != '\0') {
67                 //only chop off newline if one is present at the end
68                 i = output[i-1] == '\n' ? i-1 : i;
69                 strncpy(output+i, delim, delimLen); 
70         }
71         else
72                 output[i++] = '\0';
73         pclose(cmdf);
74 }
75
76 void getcmds(int time)
77 {
78         const Block* current;
79         for (unsigned int i = 0; i < LENGTH(blocks); i++)
80         {
81                 current = blocks + i;
82                 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
83                         getcmd(current,statusbar[i]);
84         }
85 }
86
87 void getsigcmds(unsigned int signal)
88 {
89         const Block *current;
90         for (unsigned int i = 0; i < LENGTH(blocks); i++)
91         {
92                 current = blocks + i;
93                 if (current->signal == signal)
94                         getcmd(current,statusbar[i]);
95         }
96 }
97
98 void setupsignals()
99 {
100 #ifndef __OpenBSD__
101             /* initialize all real time signals with dummy handler */
102     for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
103         signal(i, dummysighandler);
104 #endif
105
106         for (unsigned int i = 0; i < LENGTH(blocks); i++)
107         {
108                 if (blocks[i].signal > 0)
109                         signal(SIGMINUS+blocks[i].signal, sighandler);
110         }
111
112 }
113
114 int getstatus(char *str, char *last)
115 {
116         strcpy(last, str);
117         str[0] = '\0';
118         for (unsigned int i = 0; i < LENGTH(blocks); i++)
119                 strcat(str, statusbar[i]);
120         str[strlen(str)-strlen(delim)] = '\0';
121         return strcmp(str, last);//0 if they are the same
122 }
123
124 #ifndef NO_X
125 void setroot()
126 {
127         static Display *dpy;
128         static int screen;
129         static Window root;
130         if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
131                 return;
132         dpy = XOpenDisplay(NULL);
133         if (!dpy) {
134                 fprintf(stderr, "Failed to open display\n");
135                 statusContinue = 0;
136                 returnStatus = 1;
137                 return;
138         }
139         screen = DefaultScreen(dpy);
140         root = RootWindow(dpy, screen);
141         XStoreName(dpy, root, statusstr[0]);
142         XCloseDisplay(dpy);
143 }
144 #endif
145
146 void pstdout()
147 {
148         if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
149                 return;
150         printf("%s\n",statusstr[0]);
151         fflush(stdout);
152 }
153
154
155 void statusloop()
156 {
157         setupsignals();
158         int i = 0;
159         getcmds(-1);
160         while (1)
161         {
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         {
193                 if (!strcmp("-d",argv[i]))
194                         strncpy(delim, argv[++i], delimLen);
195                 else if (!strcmp("-p",argv[i]))
196                         writestatus = pstdout;
197         }
198         delimLen = MIN(delimLen, strlen(delim));
199         delim[delimLen++] = '\0';
200         signal(SIGTERM, termhandler);
201         signal(SIGINT, termhandler);
202         statusloop();
203         return returnStatus;
204 }