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