-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdwmstatus.c
More file actions
187 lines (160 loc) · 3.8 KB
/
dwmstatus.c
File metadata and controls
187 lines (160 loc) · 3.8 KB
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
/* See LICENSE file for copyright and license details. */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include <X11/Xlib.h>
#include "config.h"
#include "functions.h"
#include "alsa.h"
#include "pulse.h"
static Display *dpy;
static Interface *iface;
static int pa = 1;
static struct pulseaudio_t pulse;
static struct {
const char *iface;
const char *maildir;
const char *mailbox;
} cfg;
static void setstatus(char *str) {
XStoreName(dpy, DefaultRootWindow(dpy), str);
XSync(dpy, False);
}
static void parse_args(int *argc, char **argv[]) {
static const struct option opts[] = {
{ "interface", required_argument, 0, 'i' },
{ "maildir", required_argument, 0, 'm' },
{ 0, 0, 0, 0 }
};
while (1) {
int opt = getopt_long(*argc, *argv, "i:m:", opts, NULL);
if (opt == -1)
break;
switch(opt) {
case 'i':
cfg.iface = optarg;
break;
case 'm':
cfg.maildir = optarg;
break;
}
}
}
static void *maildir_init(void) {
if(!cfg.maildir || cfg.maildir[0] == '\0')
return NULL;
return smprintf("%s/%s/new", cfg.maildir, MAIL_BOX);
}
static int dwmstatus_init(void) {
/* initialize a bunch of things:
*
* - x display connection
* - network info struct
* - read initial rx/tx values
* - connection to the pulse server
* - mailbox location
*/
if(!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr, "cannot open display\n");
return EXIT_FAILURE;
}
iface = network_init(cfg.iface);
if(pulse_init(&pulse) != 0)
pa = 0;
cfg.mailbox = maildir_init();
return EXIT_SUCCESS;
}
static int runevery(time_t *ltime, int sec) {
time_t now = time(NULL);
if (difftime(now, *ltime) >= sec) {
*ltime = now;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
static void volume(buffer_t *buf) {
if(pa)
ponyprint(buf, pulse, &pa);
else
alsaprint(buf);
}
static void render_table(buffer_t **table, size_t table_len, buffer_t *status, const char *sep) {
size_t i, len = 0, slen = strlen(sep);
char *p;
unsigned int first;
for (first = 0; first < table_len && (table[first]->data == NULL || table[first]->data[0] == '\0'); ++first);
if (first == table_len) {
buffer_clear(status);
return;
}
len = strlen(table[first]->data);
for (i = first + 1; i < table_len; ++i)
if (table[i]->data && table[i]->data[0] != '\0')
len += strlen(table[i]->data) + slen;
if (len >= status->len) {
status->data = realloc(status->data, (size_t)len + 1);
status->len = len + 1;
}
p = stpcpy(status->data, table[first]->data);
for (i = first + 1; i < table_len; ++i)
if (table[i]->data && table[i]->data[0] != '\0') {
p = stpcpy(p, sep);
p = stpcpy(p, table[i]->data);
}
}
int main(int argc, char *argv[]) {
time_t count60 = 0;
time_t count180 = 0;
int tmsleep = 0;
cfg.maildir = getenv("MAILDIR");
parse_args(&argc, &argv);
/* init */
if (dwmstatus_init()) {
fprintf(stderr, "failed to initialize\n");
return EXIT_FAILURE;
}
buffer_t *status = buffer_new();
buffer_t *mail = buffer_new();
buffer_t *vol = buffer_new();
buffer_t *load = buffer_new();
buffer_t *temp = buffer_new();
buffer_t *mem = buffer_new();
buffer_t *net = buffer_new();
buffer_t *addr = buffer_new();
buffer_t *batt = buffer_new();
buffer_t *date = buffer_new();
buffer_t *table[] = {
mail,
vol,
load,
temp,
mem,
net,
addr,
batt,
date
};
size_t table_len = sizeof(table) / sizeof(buffer_t *);
for(;;sleep(INTERVAL)) {
if(runevery(&count60, tmsleep)) {
ipaddr(addr, cfg.iface);
mktimes(date, &tmsleep);
if(runevery(&count180, 180)) {
new_mail(mail, cfg.mailbox);
}
}
volume(vol);
loadavg(load);
coretemp(temp);
memory(mem);
network(net, iface);
battery(batt);
render_table(table, table_len, status, " \x09| ");
setstatus(status->data);
}
return EXIT_SUCCESS;
}