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