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