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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
#include <arpa/inet.h>
#include <errno.h>
#include <ifaddrs.h>
#include <linux/if.h>
#include <linux/net.h>
#include <linux/wireless.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
/* Required for timercmp */
#define __USE_MISC
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
/* Constants */
#define ELEMENT_SEPERATOR " "
#define STATUS_STRBUF_SZ 512
#define ELEMENT_STRBUF_SZ 64
/* Type definitions */
typedef void (*element_function)(char*);
/* Data structures */
struct element {
const element_function f;
const struct timeval fire_interval;
struct timeval fire_previous;
char buf[ELEMENT_STRBUF_SZ];
};
enum battery_status_charge {
bat_unknown,
bat_not_charging,
bat_charging,
bat_discharging
};
struct battery_status {
enum battery_status_charge status;
float charge;
};
/* Prototypes */
void date(char* buf);
struct battery_status get_battery_status(const char* buf);
void get_battery1_status(char* buf);
void get_battery0_status(char* buf);
void get_all_bat_status(char* buf);
void get_net_link_status(char* buf);
/* Data */
static char* battery_level_icon[] = {
"", /* "\uf579" */
"", /* "\uf57a" */
"", /* "\uf57b" */
"", /* "\uf57c" */
"", /* "\uf57d" */
"", /* "\uf57e" */
"", /* "\uf57f" */
"", /* "\uf580" */
"", /* "\uf581" */
"", /* "\uf578" */
"", /* "\uf578" */
};
static struct element statusbar[] = {
/* Add status elements here */
{get_net_link_status, {5, 0}, {0}, {0}},
{ get_all_bat_status, {60, 0}, {0}, {0}},
{ date, {2, 500000}, {0}, {0}},
};
/* Functions */
void
date(char* buf) {
time_t now = time(NULL);
struct tm tm;
tm = *localtime(&now);
strftime(buf, ELEMENT_STRBUF_SZ, "%Y-%m-%d %H:%M", &tm);
}
struct battery_status
get_battery_status(const char* buf) {
const char path_prefix[] = "/sys/class/power_supply/";
char charge_path[128];
char capacity_path[128];
char charge_str[512];
char capacity_str[512];
int charge = 0;
int capacity = 0;
FILE* bat_charge;
FILE* bat_capacity;
memset(charge_path, 0, 128);
memset(capacity_path, 0, 128);
strcat(charge_path, path_prefix);
strcat(charge_path, buf);
strcat(charge_path, "/energy_now");
strcat(capacity_path, path_prefix);
strcat(capacity_path, buf);
strcat(capacity_path, "/energy_full");
bat_charge = fopen(charge_path, "r");
bat_capacity = fopen(capacity_path, "r");
if (!bat_charge) {
printf("%d: \"%s\" %s\n", errno, charge_path, strerror(errno));
return (struct battery_status){bat_unknown, -1};
}
if (!bat_capacity) {
printf("%d: \"%s\" %s\n", errno, capacity_path, strerror(errno));
return (struct battery_status){bat_unknown, -1};
}
fread(charge_str, sizeof(char), 512, bat_charge);
fread(capacity_str, sizeof(char), 512, bat_capacity);
fclose(bat_charge);
fclose(bat_capacity);
charge = atoi(charge_str);
capacity = atoi(capacity_str);
return (struct battery_status){bat_unknown, (float)charge / capacity};
}
void
get_battery1_status(char* buf) {
struct battery_status s = get_battery_status("BAT1");
int batlvl = (int)(s.charge * 100.f) / 10;
char* batlvl_icon = battery_level_icon[batlvl];
snprintf(buf, ELEMENT_STRBUF_SZ, "%s %.1f%%", batlvl_icon, 100.f * s.charge);
}
void
get_battery0_status(char* buf) {
struct battery_status s = get_battery_status("BAT0");
int batlvl = (int)(s.charge * 100.f) / 10;
char* batlvl_icon = battery_level_icon[batlvl];
snprintf(buf, ELEMENT_STRBUF_SZ, "%s %.1f%%", batlvl_icon, 100.f * s.charge);
}
/* todo, remake this to enumerate all possible batteries */
void
get_all_bat_status(char* buf) {
struct battery_status s = get_battery_status("BAT0");
struct battery_status t = get_battery_status("BAT1");
s.charge = (s.charge + t.charge) / 2.f;
int batlvl = (int)(s.charge * 100.f) / 10;
char* batlvl_icon = battery_level_icon[batlvl];
/*snprintf(buf, ELEMENT_STRBUF_SZ, "%.1f%%", 100.f * s.charge);*/
snprintf(buf, ELEMENT_STRBUF_SZ, "%s %.1f%%", batlvl_icon, 100.f * s.charge);
}
void
get_net_link_status(char* buf) {
struct ifaddrs* if_addr;
size_t n = 0;
getifaddrs(&if_addr);
for (struct ifaddrs* ifa = if_addr; ifa != NULL; ifa = ifa->ifa_next) {
/* Ignore loopback interface flag */
if (ifa->ifa_flags & IFF_LOOPBACK)
continue;
/* Ignore interfaces without an address */
if (ifa->ifa_addr == NULL)
continue;
/* Ignore everythings not an IPv4 link */
/* (AF_PACKET might be usefull if you want to display tx) */
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
if (ifa->ifa_flags & IFF_UP) {
size_t l = strlen(ifa->ifa_name);
struct iwreq wreq;
int sock;
const size_t ipv6_charlen = 8 * 4 + 7;
char address[ipv6_charlen + 1]; // in case of ipv6, plus null-byte
char id[IW_ESSID_MAX_SIZE + 1];
/* Get the IP address */
memset(address, 0, ipv6_charlen + 1);
/* Use sa_family instead of hardcoding AF_INET, just in case
* we get ip6'ed */
/* Does inet_ntop add a null-byte? */
inet_ntop(ifa->ifa_addr->sa_family,
(void*)&((struct sockaddr_in*)ifa->ifa_addr)->sin_addr, address,
ipv6_charlen);
/* Get the SSID */
memset(id, 0, IW_ESSID_MAX_SIZE + 1);
memset(&wreq, 0, sizeof(struct iwreq));
sock = socket(AF_INET, SOCK_DGRAM, 0);
setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifa->ifa_name, l);
strncpy(wreq.ifr_name, ifa->ifa_name, l);
wreq.u.essid.pointer = id;
wreq.u.essid.length = IW_ESSID_MAX_SIZE;
ioctl(sock, SIOCGIWESSID, &wreq);
strncpy(buf + n, ifa->ifa_name, l);
size_t ssid_len = strlen(id);
if (ssid_len > 0) {
buf[n + l] = ' ';
buf[n + l + 1] = '(';
strncpy(buf + n + l + 2, id, ssid_len);
l += ssid_len;
buf[n + l + 2] = ')';
}
n += l;
strcat(buf + n, address);
n += strlen(address);
strcat(buf + n, " ");
n += 2;
}
}
}
int
main(void) {
const int num_elems = sizeof(statusbar) / sizeof(statusbar[0]);
struct timeval now;
const struct timeval one_minute = {60, 0};
while (true) {
gettimeofday(&now, NULL);
unsigned i;
/* Stall updating for at most 1 minute */
struct timeval next_update;
timeradd(&now, &one_minute, &next_update);
for (i = 0; i < num_elems; i++) {
struct timeval next_fire;
timeradd(&statusbar[i].fire_previous, &statusbar[i].fire_interval,
&next_fire);
if (timercmp(&next_fire, &now, >)) {
/* Check if this is the next to-be-updated element */
if (timercmp(&next_fire, &next_update, <)) {
next_update = next_fire;
}
continue;
}
memset(statusbar[i].buf, 0, ELEMENT_STRBUF_SZ);
statusbar[i].f(statusbar[i].buf);
statusbar[i].fire_previous = now;
/* Check if this element needs to be refreshed next, again */
timeradd(&statusbar[i].fire_previous, &statusbar[i].fire_interval,
&next_fire);
if (timercmp(&next_fire, &next_update, <)) {
next_update = next_fire;
}
/* printf("[%ld.%ld] %s\n",
* statusbar[i].fire_interval.tv_sec,
* statusbar[i].fire_interval.tv_usec,
* (char*)statusbar[i].buf);
*/
}
char buf[STATUS_STRBUF_SZ];
memset(buf, 0, STATUS_STRBUF_SZ);
for (i = 0; i < num_elems; i++) {
strcat(buf, statusbar[i].buf);
if (i != num_elems - 1)
strcat(buf, ELEMENT_SEPERATOR);
}
/* strcat(buf, "\0"); */
puts(buf);
fflush(stdout);
timersub(&next_update, &now, &next_update);
usleep(next_update.tv_sec * 1000 * 1000 + next_update.tv_usec);
}
return 0;
}
|