c66dbc794b6d61f0054903077dbc63a9751088fa
[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         FILE *cmdf = popen(block->command, "r");
54         if (!cmdf)
55                 return;
56         int i = strlen(block->icon);
57         fgets(output+i, CMDLENGTH-i-delimLen, cmdf);
58         i = strlen(output);
59         if (i == 0)//return if block and command output are both empty
60                 return;
61         if (delim[0] != '\0') {
62                 //only chop off newline if one is present at the end
63                 i = output[i-1] == '\n' ? i-1 : i;
64                 strncpy(output+i, delim, delimLen); 
65         }
66         else
67                 output[i++] = '\0';
68         pclose(cmdf);
69 }
70
71 void getcmds(int time)
72 {
73         const Block* current;
74         for (unsigned int i = 0; i < LENGTH(blocks); i++)
75         {
76                 current = blocks + i;
77                 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
78                         getcmd(current,statusbar[i]);
79         }
80 }
81
82 void getsigcmds(unsigned int signal)
83 {
84         const Block *current;
85         for (unsigned int i = 0; i < LENGTH(blocks); i++)
86         {
87                 current = blocks + i;
88                 if (current->signal == signal)
89                         getcmd(current,statusbar[i]);
90         }
91 }
92
93 void setupsignals()
94 {
95 #ifndef __OpenBSD__
96             /* initialize all real time signals with dummy handler */
97     for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
98         signal(i, dummysighandler);
99 #endif
100
101         for (unsigned int i = 0; i < LENGTH(blocks); i++)
102         {
103                 if (blocks[i].signal > 0)
104                         signal(SIGMINUS+blocks[i].signal, sighandler);
105         }
106
107 }
108
109 int getstatus(char *str, char *last)
110 {
111         strcpy(last, str);
112         str[0] = '\0';
113         for (unsigned int i = 0; i < LENGTH(blocks); i++)
114                 strcat(str, statusbar[i]);
115         str[strlen(str)-strlen(delim)] = '\0';
116         return strcmp(str, last);//0 if they are the same
117 }
118
119 void setroot()
120 {
121         if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
122                 return;
123         Display *d = XOpenDisplay(NULL);
124         if (d) {
125                 dpy = d;
126         }
127         screen = DefaultScreen(dpy);
128         root = RootWindow(dpy, screen);
129         XStoreName(dpy, root, statusstr[0]);
130         XCloseDisplay(dpy);
131 }
132
133 void pstdout()
134 {
135         if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
136                 return;
137         printf("%s\n",statusstr[0]);
138         fflush(stdout);
139 }
140
141
142 void statusloop()
143 {
144         setupsignals();
145         int i = 0;
146         getcmds(-1);
147         while (statusContinue)
148         {
149                 getcmds(i++);
150                 writestatus();
151                 sleep(1.0);
152         }
153 }
154
155 #ifndef __OpenBSD__
156 /* this signal handler should do nothing */
157 void dummysighandler(int signum)
158 {
159     return;
160 }
161 #endif
162
163 void sighandler(int signum)
164 {
165         getsigcmds(signum-SIGPLUS);
166         writestatus();
167 }
168
169 void termhandler()
170 {
171         statusContinue = 0;
172 }
173
174 int main(int argc, char** argv)
175 {
176         for (int i = 0; i < argc; i++) //Handle command line arguments
177         {
178                 if (!strcmp("-d",argv[i]))
179                         strncpy(delim, argv[++i], delimLen);
180                 else if (!strcmp("-p",argv[i]))
181                         writestatus = pstdout;
182         }
183         delimLen = MIN(delimLen, strlen(delim));
184         delim[delimLen++] = '\0';
185         signal(SIGTERM, termhandler);
186         signal(SIGINT, termhandler);
187         statusloop();
188 }