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