-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmekstatus.c
308 lines (288 loc) · 8.39 KB
/
mekstatus.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
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
/* mekstatus, a status provider thing for i3bar.
* copyleft (CC-0) 2013-2015 Melker "meklu" Narikka
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
/* battery id
* if this doesn't work, take a look in /sys/class/power_supply/
* desktop users need not worry, it won't show up on your rigs */
#define _mekstatus_battery_id "BAT0"
/* the number of load elements, see getloadavg(3)
* the maximum value is usually 3 */
#define _mekstatus_load_nelem 3
/* some color constants */
const char *color_red = "#ff0000";
const char *color_cyan = "#00ffff";
const char *color_green = "#00ff00";
const char *color_yellow = "#ffff00";
const char *color_grey = "#696969";
static inline double mstatclamp(double val, double min, double max) { return (val >= min) ? ((val <= max) ? val : max) : min; }
const char *battstatuscolor(const char *status) {
if (strncmp(status, "-", 2) == 0) {
return color_red;
} else if (strncmp(status, "+", 2) == 0) {
return color_green;
} else if (strncmp(status, "~", 2) == 0) {
return color_cyan;
}
return color_yellow;
}
typedef struct {
double r;
double g;
double b;
} color;
double getdeltapos(double abspos, double rangestart, double rangeend) {
double len = rangeend - rangestart;
double relpos = abspos - rangestart;
return mstatclamp(relpos / len, 0.0, 1.0);
}
color blendcolor(color start, color end, double pos) {
color retcol = { 0.0, 0.0, 0.0 };
retcol.r = ((1.0 - pos) * start.r) + (pos * end.r);
retcol.g = ((1.0 - pos) * start.g) + (pos * end.g);
retcol.b = ((1.0 - pos) * start.b) + (pos * end.b);
return retcol;
}
char *percentagecolor(double percentage) {
char *buf = (char *) malloc(8); /* 6 hex chars + hash + \0 */
color red = { 1.0, 0.0, 0.0 };
color yellow = { 1.0, 1.0, 0.0 };
color green = { 0.0, 1.0, 0.0 };
color cyan = { 0.0, 1.0, 1.0 };
color retcol = { 0.0, 0.0, 0.0 };
if (percentage <= 50.0) {
retcol = blendcolor(red, yellow, getdeltapos(percentage, 0.0, 50.0));
} else if (percentage <= 75.0) {
retcol = blendcolor(yellow, green, getdeltapos(percentage, 50.0, 75.0));
} else { /* if (percentage <= 100.0) { */
retcol = blendcolor(green, cyan, getdeltapos(percentage, 75.0, 100.0));
}
snprintf(buf, 8, "#%.2x%.2x%.2x",
(int) (retcol.r * 255.0),
(int) (retcol.g * 255.0),
(int) (retcol.b * 255.0)
);
return buf;
}
void colorprint(const char *str, const char *color) {
printf(",{\"full_text\":\"%s\"", str);
if (strlen(color) > 0) {
printf(",\"color\":\"%s\"", color);
}
printf("}");
}
/* sets up a file handle for a battery property
* features a convoluted filter system */
int checkbatteryproperty(const char *identifier, const char *property, int (*filter)(FILE *, void *), void *userbuf) {
FILE *batt_f;
const char *batt_prefix = "/sys/class/power_supply/";
char *buf = (char *) malloc(strlen(batt_prefix) + strlen(identifier) + 1 + strlen(property) + 1);
int filtret;
sprintf(buf, "%s%s/%s", batt_prefix, identifier, property);
batt_f = fopen(buf, "r");
if (batt_f == NULL) {
return 0;
}
filtret = filter(batt_f, userbuf);
fclose(batt_f);
free(buf);
return filtret;
}
/* some of the aforementioned filters */
int getstrfilt(FILE *f, void *buf) {
int ret = 0;
/* 32 bytes ought to be enough for anybody */
if (fscanf(f, "%31s", (char *) buf) > 0) {
return ret;
}
return 0;
}
/* this and the filter below it return their value as well, making the buffer
* indesirable in most cases; hence 'ignorebuf' */
int getnumfilt(FILE *f, void *ignorebuf) {
int ret = 0;
if (fscanf(f, "%d", &ret) > 0) {
if (ignorebuf != NULL) {
*(int *) ignorebuf = ret;
}
return ret;
}
if (ignorebuf != NULL) {
*(int *) ignorebuf = 0;
}
return 0;
}
int isbatteryfilt(FILE *f, void *ignorebuf) {
const char *bat_s = "Battery";
char *buf = (char *) malloc(64);
if (fscanf(f, "%s", buf) > 0) {
if (strncmp(bat_s, buf, sizeof(bat_s) * sizeof(char)) == 0) {
free(buf);
if (ignorebuf != NULL) {
*(int *) ignorebuf = 1;
}
return 1;
}
}
free(buf);
if (ignorebuf != NULL) {
*(int *) ignorebuf = 0;
}
return 0;
}
/* gets the full charge value */
int getbatteryfull(const char *identifier) {
return checkbatteryproperty(identifier, "charge_full", getnumfilt, NULL);
}
/* gets the current charge */
int getbatterycharge(const char *identifier) {
return checkbatteryproperty(identifier, "charge_now", getnumfilt, NULL);
}
/* gets a nice looking character representing battery status */
int getbatterystatus(const char *identifier, char *buf) {
char fbuf[32];
int ret;
strncpy(fbuf, buf, sizeof(fbuf) - sizeof(fbuf[0]));
fbuf[31] = '\0';
ret = checkbatteryproperty(identifier, "status", getstrfilt, (void *) buf);
/* gotta get dem funroll oops, hence only comparing 4 bytes */
if (strncmp(buf, "Discharging", 4) == 0) {
strcpy(buf, "-");
} else if (strncmp(buf, "Charging", 4) == 0) {
strcpy(buf, "+");
} else if (strncmp(buf, "Full", 4) == 0) {
strcpy(buf, "~");
} else {
/* if we only had a single character in the previous buffer,
* use it instead of '?' because it might look nicer */
if (fbuf[1] == '\0') {
strcpy(buf, fbuf);
} else {
strcpy(buf, "?");
}
}
return ret;
}
/* checks whether the battery exists or not */
int hasbattery(const char *identifier) {
return checkbatteryproperty(identifier, "type", isbatteryfilt, NULL);
}
void catchalarm(int sig) {
(void) sig;
return;
}
int main() {
/* battery stack */
const char *batt_name = _mekstatus_battery_id;
int batt_use = hasbattery(batt_name);
int batt_sent = 0;
int batt_charge_full;
int batt_charge_curr;
char batt_charge_status[32];
char *batt_color;
double batt_charge_prc;
/* date/time stack */
char buf[64];
size_t buflen = 64;
char *format_d = "%F";
char *format_t = "%T";
char *format_z = "%z";
struct timespec ts_time;
struct tm *s_time;
/* load stack */
double load[_mekstatus_load_nelem];
/* iterator */
int i = 0;
/* catch SIGALRM */
signal(SIGALRM, catchalarm);
printf("{\"version\":1}\n");
fflush(stdout);
printf("[\n");
fflush(stdout);
printf("[]\n");
fflush(stdout);
do {
printf(",[{\"full_text\":\"\"}");
/* date, time */
clock_gettime(CLOCK_REALTIME, &ts_time);
s_time = localtime(&ts_time.tv_sec);
/* date */
strftime(buf, buflen, format_d, s_time);
colorprint(buf, color_yellow);
/* time */
strftime(buf, buflen, format_t, s_time);
colorprint(buf, color_cyan);
/* timezone */
strftime(buf, buflen, format_z, s_time);
colorprint(buf, color_grey);
/* load */
getloadavg(load, _mekstatus_load_nelem);
for (i = 0; i < _mekstatus_load_nelem - 1; i += 1) {
snprintf(buf, sizeof(buf), "%.2f", load[i]);
colorprint(buf, percentagecolor(100.0 - 20.0 * mstatclamp(load[i], 0.0, 5.0)));
}
snprintf(buf, sizeof(buf), "%.2f", load[_mekstatus_load_nelem - 1]);
colorprint(buf, percentagecolor(100.0 - 20.0 * mstatclamp(load[i], 0.0, 5.0)));
/* battery, if any exists */
if (batt_use) {
/* update values */
batt_charge_full = getbatteryfull(batt_name);
batt_charge_curr = getbatterycharge(batt_name);
getbatterystatus(batt_name, batt_charge_status);
batt_charge_prc = (double) 100 * (double) batt_charge_curr / (double) batt_charge_full;
batt_color = percentagecolor(batt_charge_prc);
/* print it */
snprintf(buf, sizeof(buf), "%.2f%%", batt_charge_prc);
colorprint(buf, batt_color);
colorprint(batt_charge_status, battstatuscolor(batt_charge_status));
free(batt_color);
/* notify if we haven't already */
if (batt_sent == 0) {
static char *args[] = {
"notify-send",
"--app-name=mekstatus",
"--urgency=critical",
"--expire-time=0",
"--category=warning",
"Low battery",
"Your battery is running low.",
NULL
};
if (
batt_charge_status[0] != '+' &&
batt_charge_status[0] != '~' &&
batt_charge_prc < 10.0
) {
pid_t child = fork();
if (child == 0) {
exit(execvp(args[0], args));
} else if (child > 0) {
batt_sent = 1;
wait(NULL);
}
}
} else if (
batt_charge_prc > 15.0 ||
batt_charge_status[0] == '+' ||
batt_charge_status[0] == '~'
) {
batt_sent = 0;
}
}
printf("]\n");
fflush(stdout);
/* aligning to second */
ts_time.tv_sec += 1;
ts_time.tv_nsec = 0;
/* if the time is changed radically when we're sleeping, fire an alarm so as to not sleep for
* three days straight */
alarm(2);
clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts_time, NULL);
} while (1);
}