]> git.armaanb.net Git - dwmblocks.git/blob - dwmblocks.c
Added function declarations and renamed setrootcmd to statusstr.
[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* command;
11         unsigned int interval;
12         unsigned int signal;
13 } Block;
14 void sighandler(int num);
15 void replace(char *str, char old, char new);
16 void getcmds(int time);
17 void getsigcmds(int signal);
18 void setupsignals();
19 void getstatus(char *str);
20 void setroot();
21 void statusloop();
22 void statusinit();
23 void sighandler(int signum);
24 void termhandler(int signum);
25
26
27 #include "blocks.h"
28
29 static Display *dpy;
30 static int screen;
31 static Window root;
32 static char statusbar[LENGTH(blocks)][50] = {0};
33 static char statusstr[256];
34 static char *statuscat;
35 static const char *volupcmd[]  = { "volup", NULL };
36 static const char *voldowncmd[]  = { "voldown", NULL };
37 static const char *volmutecmd[]  = { "volmute", NULL };
38 static int statusContinue = 1,volmuted = 0;
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(char *cmd, char *output)
50 {
51         FILE *cmdf = popen(cmd,"r");
52         if (!cmdf)
53                 return;
54         int N = strlen(output);
55         char c;
56         int i = 0;
57         while((c = fgetc(cmdf)) != EOF)
58                 output[i++] = c;
59         output[i++] = '\0';
60         pclose(cmdf);
61 }
62
63 void getcmds(int time)
64 {
65         const Block* current;
66         for(int i = 0; i < LENGTH(blocks); i++)
67         {       
68                 current = blocks + i;
69                 if ((current->interval != 0 && time % current->interval == 0) || time == -1)
70                         getcmd(current->command,statusbar[i]);
71         }
72 }
73
74 void getsigcmds(int signal)
75 {
76         const Block *current;
77         for (int i = 0; i < LENGTH(blocks); i++)
78         {
79                 current = blocks + i;
80                 if (current->signal == signal)
81                         getcmd(current->command,statusbar[i]);
82         }
83 }
84
85 void setupsignals()
86 {
87         for(int i = 0; i < LENGTH(blocks); i++)
88         {         
89                 if (blocks[i].signal > 0)
90                         signal(SIGRTMIN+blocks[i].signal, sighandler);
91         }
92
93 }
94
95 void getstatus(char *str)
96 {
97         int j = 0;
98         for(int i = 0; i < 5; j+=strlen(statusbar[i++]))
99         {       
100                 strcpy(str + j, statusbar[i]);
101         }
102         str[j] = '\0';
103
104 }
105
106 void setroot()
107 {
108         Display *d = XOpenDisplay(NULL);
109         if (d) {
110                 dpy = d;
111         }
112         screen = DefaultScreen(dpy);
113         root = RootWindow(dpy, screen);
114         getstatus(statusstr);
115         replace(statusstr,'\n',' ');//gets rid of newlines from output
116         replace(statusstr,EOF,' ');//gets rid of EOF from output
117         XStoreName(dpy, root, statusstr);
118         XCloseDisplay(dpy);
119 }
120
121
122 void statusloop()
123 {
124         setupsignals();
125         int i = 0;
126         getcmds(-1);
127         while(statusContinue)
128         {
129                 getcmds(i);
130                 setroot();
131                 sleep(1.0);
132                 i++;
133         }
134 }
135
136 void statusinit()
137 {
138         statusloop();
139 }
140
141
142 void sighandler(int signum)
143 {
144         getsigcmds(signum-SIGRTMIN);
145         setroot();
146 }
147
148 void termhandler(int signum)
149 {
150         statusContinue = 0;
151         exit(0);
152 }
153
154 int main()
155 {
156         signal(SIGTERM, termhandler);
157         signal(SIGINT, termhandler);
158         statusinit();
159 }