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