-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwm-bar.c
217 lines (181 loc) · 4.56 KB
/
dwm-bar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "bar.h"
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
int fd;
time_t rtime;
char *bar[RELOAD + 1];
static void init_strings(void) {
for (int i = 0; i < RELOAD + 1; i++)
bar[i] = (char *)malloc(SIZE * sizeof(char));
}
static void close_handler(int sig) {
fprintf(stdout, "closing: received %d...\n", sig);
close(fd);
unlink(FIFO);
exit(0);
}
static void update_date(void) {
static struct tm *tm_time;
static char *const mon[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
memset(bar[DATE], '\0', SIZE);
tm_time = localtime(&rtime);
sprintf(bar[0], "^c#55B4D4^%02d-%s %02d:%02d", tm_time->tm_mday,
mon[tm_time->tm_mon], tm_time->tm_hour, tm_time->tm_min);
}
static void update_network(void) {
static char *const arg[] = {"bar-helper.sh", "network", NULL};
memset(bar[NETWORK], '\0', SIZE);
system_pipe("/usr/local/bin/bar-helper.sh", arg, bar[NETWORK]);
}
static void update_mem(void) {
static char *const arg[] = {"bar-helper.sh", "mem", NULL};
memset(bar[MEM], '\0', SIZE);
system_pipe("/usr/local/bin/bar-helper.sh", arg, bar[MEM]);
}
static void update_battery(void) {
static char *const arg[] = {"which", "acpi", NULL};
memset(bar[BATTERY], '\0', SIZE);
if (system_pipe("/usr/bin/which", arg, NULL) == 0) {
static char *const arg2[] = {"bar-helper.sh", "battery", NULL};
system_pipe("/usr/local/bin/bar-helper.sh", arg2, bar[BATTERY]);
}
}
static void update_volume(void) {
static char *const arg[] = {"bar-helper.sh", "volume", NULL};
memset(bar[VOLUME], '\0', SIZE);
system_pipe("/usr/local/bin/bar-helper.sh", arg, bar[VOLUME]);
}
static void update_temp(void) {
static char *const arg[] = {"bar-helper.sh", "temp", NULL};
memset(bar[TEMP], '\0', SIZE);
system_pipe("/usr/local/bin/bar-helper.sh", arg, bar[TEMP]);
}
static void update_disk(void) {
static char *const arg[] = {"bar-helper.sh", "disk", NULL};
memset(bar[DISK], '\0', SIZE);
system_pipe("/usr/local/bin/bar-helper.sh", arg, bar[DISK]);
}
static void update_mail(void) {
static char *const arg[] = {"bar-helper.sh", "mail", NULL};
memset(bar[MAIL], '\0', SIZE);
system_pipe("/usr/local/bin/bar-helper.sh", arg, bar[MAIL]);
}
static void update_music(void) {
static char *const arg2[] = {"cmus-remote", "-Q", NULL};
memset(bar[MUSIC], '\0', SIZE);
if (system_pipe("/usr/bin/cmus-remote", arg2, NULL) == 0) {
static char *const arg[] = {"bar-helper.sh", "music", NULL};
system_pipe("/usr/local/bin/bar-helper.sh", arg, bar[MUSIC]);
}
}
static void update_mic(void) {
static char *const arg[] = {"bar-helper.sh", "mic", NULL};
system_pipe("/usr/local/bin/bar-helper.sh", arg, bar[MIC]);
}
static void update_bar(void) {
update_date();
update_mic();
update_music();
update_mail();
update_disk();
update_temp();
update_volume();
update_battery();
update_network();
update_mem();
}
static void make_bar(char *buf) {
memset(buf, '\0', BAR_SIZE);
for (int i = RELOAD; i >= 0; i--) {
if (strlen(bar[i]) > 0) {
strcat(buf, bar[i]);
if (i != 0)
strcat(buf, " ");
}
}
}
static void display_bar(char *buf) {
static char final_buffer[BAR_SIZE];
sprintf(final_buffer, " %s ", buf);
xsetroot(final_buffer);
}
int main(void) {
struct timeval tval = {0, 100 * 1000000};
char bar_buf[BAR_SIZE];
int ss;
unsigned int reload;
rtime = time(NULL);
init_strings();
signal(SIGINT, close_handler);
unlink(FIFO);
fd_set fds;
if (mkfifo(FIFO, 0600) < 0) {
exit(1);
perror("mkfifo");
}
fd = open(FIFO, O_RDWR);
if (fd < 0)
perror("open");
update_bar();
while (1) {
rtime = time(NULL);
make_bar(bar_buf);
display_bar(bar_buf);
/* calls select */
ss = timeout(fd, &fds, &tval, &rtime);
if (ss < 0)
perror("select");
/* timeout */
else if (ss == 0) {
rtime = time(NULL);
update_bar();
}
/* reload specific item */
else if (FD_ISSET(fd, &fds) != 0) {
if (read(fd, &reload, sizeof(reload)) < 0)
perror("read");
switch (reload) {
case (DATE):
update_date();
break;
case (BATTERY):
update_battery();
break;
case (NETWORK):
update_network();
break;
case (TEMP):
update_temp();
break;
case (DISK):
update_disk();
break;
case (MAIL):
update_mail();
break;
case (MUSIC):
update_music();
break;
case (MIC):
update_mic();
break;
case (RELOAD):
update_bar();
break;
case (MEM):
update_mem();
break;
case (VOLUME):
update_volume();
break;
default:
printf("not found %d\n", reload);
break;
}
}
}
close(fd);
}