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