]> git.armaanb.net Git - dwmblocks.git/blob - dwmblocks.c
Reduce refresh time
[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 "config.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, 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         } else if (output[i-1] == '\n') {
73                 output[i-1] = '\0';
74         }
75         pclose(cmdf);
76 }
77
78 void getcmds(int time)
79 {
80         const Block* current;
81         for (unsigned int i = 0; i < LENGTH(blocks); i++) {
82                 current = blocks + i;
83                 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
84                         getcmd(current,statusbar[i]);
85         }
86 }
87
88 void getsigcmds(unsigned int signal)
89 {
90         const Block *current;
91         for (unsigned int i = 0; i < LENGTH(blocks); i++) {
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                 if (blocks[i].signal > 0)
108                         signal(SIGMINUS+blocks[i].signal, sighandler);
109         }
110
111 }
112
113 int getstatus(char *str, char *last)
114 {
115         strcpy(last, str);
116         str[0] = '\0';
117         for (unsigned int i = 0; i < LENGTH(blocks); i++)
118                 strcat(str, statusbar[i]);
119         str[strlen(str)] = '\0';
120         return strcmp(str, last);//0 if they are the same
121 }
122
123 #ifndef NO_X
124 void setroot()
125 {
126         if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
127                 return;
128         XStoreName(dpy, root, statusstr[0]);
129         XFlush(dpy);
130 }
131
132 int setupX()
133 {
134         dpy = XOpenDisplay(NULL);
135         if (!dpy) {
136                 fprintf(stderr, "dwmblocks: Failed to open display\n");
137                 return 0;
138         }
139         screen = DefaultScreen(dpy);
140         root = RootWindow(dpy, screen);
141         return 1;
142 }
143 #endif
144
145 void pstdout()
146 {
147         if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
148                 return;
149         printf("%s\n",statusstr[0]);
150         fflush(stdout);
151 }
152
153
154 void statusloop()
155 {
156         setupsignals();
157         int i = 0;
158         getcmds(-1);
159         while (1) {
160                 getcmds(i++);
161                 writestatus();
162                 if (!statusContinue)
163                         break;
164                 sleep(1.0);
165         }
166 }
167
168 #ifndef __OpenBSD__
169 /* this signal handler should do nothing */
170 void dummysighandler(int signum)
171 {
172     return;
173 }
174 #endif
175
176 void sighandler(int signum)
177 {
178         getsigcmds(signum-SIGPLUS);
179         writestatus();
180 }
181
182 void termhandler()
183 {
184         statusContinue = 0;
185 }
186
187 int main(int argc, char** argv)
188 {
189         for (int i = 0; i < argc; i++) {//Handle command line arguments
190                 if (!strcmp("-p",argv[i]))
191                         writestatus = pstdout;
192         }
193 #ifndef NO_X
194         if (!setupX())
195                 return 1;
196 #endif
197         signal(SIGTERM, termhandler);
198         signal(SIGINT, termhandler);
199         statusloop();
200 #ifndef NO_X
201         XCloseDisplay(dpy);
202 #endif
203         return 0;
204 }