]> git.armaanb.net Git - dwmblocks.git/blob - dwmblocks.c
fixed output from commands without a newline end.
[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 (delim[0] != '\0') {
60                 //only chop off newline if one is present at the end
61                 i = output[i-1] == '\n' ? i-1 : i;
62                 strncpy(output+i, delim, delimLen); 
63         }
64         else
65                 output[i++] = '\0';
66         pclose(cmdf);
67 }
68
69 void getcmds(int time)
70 {
71         const Block* current;
72         for (unsigned int i = 0; i < LENGTH(blocks); i++)
73         {
74                 current = blocks + i;
75                 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
76                         getcmd(current,statusbar[i]);
77         }
78 }
79
80 void getsigcmds(unsigned int signal)
81 {
82         const Block *current;
83         for (unsigned int i = 0; i < LENGTH(blocks); i++)
84         {
85                 current = blocks + i;
86                 if (current->signal == signal)
87                         getcmd(current,statusbar[i]);
88         }
89 }
90
91 void setupsignals()
92 {
93 #ifndef __OpenBSD__
94             /* initialize all real time signals with dummy handler */
95     for (int i = SIGRTMIN; i <= SIGRTMAX; i++)
96         signal(i, dummysighandler);
97 #endif
98
99         for (unsigned int i = 0; i < LENGTH(blocks); i++)
100         {
101                 if (blocks[i].signal > 0)
102                         signal(SIGMINUS+blocks[i].signal, sighandler);
103         }
104
105 }
106
107 int getstatus(char *str, char *last)
108 {
109         strcpy(last, str);
110         str[0] = '\0';
111         for (unsigned int i = 0; i < LENGTH(blocks); i++)
112                 strcat(str, statusbar[i]);
113         str[strlen(str)-strlen(delim)] = '\0';
114         return strcmp(str, last);//0 if they are the same
115 }
116
117 void setroot()
118 {
119         if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
120                 return;
121         Display *d = XOpenDisplay(NULL);
122         if (d) {
123                 dpy = d;
124         }
125         screen = DefaultScreen(dpy);
126         root = RootWindow(dpy, screen);
127         XStoreName(dpy, root, statusstr[0]);
128         XCloseDisplay(dpy);
129 }
130
131 void pstdout()
132 {
133         if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
134                 return;
135         printf("%s\n",statusstr[0]);
136         fflush(stdout);
137 }
138
139
140 void statusloop()
141 {
142         setupsignals();
143         int i = 0;
144         getcmds(-1);
145         while (statusContinue)
146         {
147                 getcmds(i++);
148                 writestatus();
149                 sleep(1.0);
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++) //Handle command line arguments
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))] = '\0';
182         signal(SIGTERM, termhandler);
183         signal(SIGINT, termhandler);
184         statusloop();
185 }