]> git.armaanb.net Git - dwmblocks.git/blob - dwmblocks.c
30712432b175e3246463c0a527f7bb43c4099e33
[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 <<<<<<< HEAD
37 static void (*writestatus) () = setroot;
38 =======
39 >>>>>>> 5ff59d4e8ba9c64963d36c8ea51e7a1d644aef48
40
41 void replace(char *str, char old, char new)
42 {
43         int N = strlen(str);
44         for(int i = 0; i < N; i++)
45                 if(str[i] == old)
46                         str[i] = new;
47 }
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         char *cmd = block->command;
54         FILE *cmdf = popen(cmd,"r");
55         if (!cmdf)
56                 return;
57         char c;
58         int i = strlen(block->icon);
59         while((c = fgetc(cmdf)) != EOF)
60                 output[i++] = c;
61         if (delim != '\0' && --i)
62                 output[i++] = delim;
63         output[i++] = '\0';
64         pclose(cmdf);
65 }
66
67 void getcmds(int time)
68 {
69         const Block* current;
70         for(int i = 0; i < LENGTH(blocks); i++)
71         {       
72                 current = blocks + i;
73                 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
74                         getcmd(current,statusbar[i]);
75         }
76 }
77
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
99 int getstatus(char *str, char *last)
100 {
101         strcpy(last, str);
102 <<<<<<< HEAD
103         str[0] = '\0';
104         for(int i = 0; i < LENGTH(blocks); i++)
105 =======
106         int j = 0;
107         for(int i = 0; i < LENGTH(blocks); j+=strlen(statusbar[i++]))
108 >>>>>>> 5ff59d4e8ba9c64963d36c8ea51e7a1d644aef48
109         {       
110                 strcat(str, statusbar[i]);
111         }
112 <<<<<<< HEAD
113         str[strlen(str)-1] = '\0';
114 =======
115         str[--j] = '\0';
116 >>>>>>> 5ff59d4e8ba9c64963d36c8ea51e7a1d644aef48
117         return strcmp(str, last);//0 if they are the same
118 }
119
120 void setroot()
121 {
122         if (!getstatus(statusstr[0], statusstr[1]))//Only set root if text has changed.
123                 return;
124         Display *d = XOpenDisplay(NULL);
125         if (d) {
126                 dpy = d;
127         }
128         screen = DefaultScreen(dpy);
129         root = RootWindow(dpy, screen);
130         XStoreName(dpy, root, statusstr[0]);
131         XCloseDisplay(dpy);
132 }
133
134 void pstdout()
135 {
136         if (!getstatus(statusstr[0], statusstr[1]))//Only write out if text has changed.
137                 return;
138         printf("%s\n",statusstr[0]);
139         fflush(stdout);
140 }
141
142
143 void statusloop()
144 {
145         setupsignals();
146         int i = 0;
147         getcmds(-1);
148         while(statusContinue)
149         {
150                 getcmds(i);
151                 writestatus();
152                 sleep(1.0);
153                 i++;
154         }
155 }
156
157 void statusinit()
158 {
159         statusloop();
160 }
161
162
163 void sighandler(int signum)
164 {
165         getsigcmds(signum-SIGRTMIN);
166         writestatus();
167 }
168
169 void termhandler(int signum)
170 {
171         statusContinue = 0;
172         exit(0);
173 }
174
175 int main(int argc, char** argv)
176 {
177         for(int i = 0; i < argc; i++)
178         {       
179                 if (!strcmp("-d",argv[i]))
180                         delim = argv[++i][0];
181                 else if(!strcmp("-p",argv[i]))
182                         writestatus = pstdout;
183         }
184         signal(SIGTERM, termhandler);
185         signal(SIGINT, termhandler);
186         statusinit();
187 }