-
Notifications
You must be signed in to change notification settings - Fork 0
/
statusline.c
411 lines (358 loc) · 11.7 KB
/
statusline.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include <ctype.h>
#include <errno.h>
#include <linux/wireless.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <time.h>
#define AC_STATUS_FILE "/sys/class/power_supply/AC/online"
#define BATTERY_LEVEL_FILE "/sys/class/power_supply/BAT0/capacity"
#define CPU_TEMP_FILE "/sys/class/hwmon/hwmon6/temp1_input"
#define CPU_TEMP_FACTOR 1000
#define FAN_STATUS_FILE "/proc/acpi/ibm/fan"
#define WIREGUARD_INTERFACE_FILE "/proc/net/dev_snmp6/wg0"
#define ETHERNET_INTERFACE "enp1s0f0"
#define WIRELESS_INTERFACE "wlp2s0"
// Not actually my location, but close enough
#define LAT 55.116889
#define LNG -4.436861
// Trigonometry for sun position calculations
#define PI 3.1415926535897932384
#define RADEG (180.0 / PI)
#define DEGRAD (PI / 180.0)
#define sind(x) sin((x) * DEGRAD)
#define cosd(x) cos((x) * DEGRAD)
#define tand(x) tan((x) * DEGRAD)
#define atand(x) (RADEG * atan(x))
#define asind(x) (RADEG * asin(x))
#define acosd(x) (RADEG * acos(x))
#define atan2d(y, x) (RADEG * atan2(y, x))
// COLOURS https://jonasjacek.github.io/colors/
#define RED "colour196" // #FF0000
#define ORANGE "colour214" // #FFAF00
#define YELLOW "colour226" // #FFFF00
#define GREEN "colour118" // #87FF00
#define WHITE "colour231" // #FFFFFF
#define LIGHT_BG "colour237" // #303030
#define DARK_BG "colour236" // #3A3A3A
#define TEXT WHITE
int8_t interfaceStatus(const char *interface);
void stripNonDigits(char *input, int length);
void resetStyles();
int fileToNumber(char *filename);
int8_t interfaceStatus(const char *interface) {
int8_t length = 15 // path start
+ strlen(interface) // interface
+ 8 // path end
+ 1; // null character
char *interfaceStatusFilePath = calloc(length, sizeof(char));
strcat(interfaceStatusFilePath, "/sys/class/net/");
strcat(interfaceStatusFilePath, interface);
strcat(interfaceStatusFilePath, "/carrier");
// Implicitly ends with \0 after strcat
return fileToNumber(interfaceStatusFilePath);
}
// Modify input string and only keep [0-9]
void stripNonDigits(char *input, int length) {
char *output = calloc(length, sizeof(char));
int index = 0;
char *sPtr = input;
for (; *sPtr != '\0'; sPtr++) {
if (isdigit(*sPtr)) {
output[index++] = *sPtr;
}
}
strcpy(input, output);
free(output);
}
void resetStyles() { printf("#[default]"); }
int fileToNumber(char *filename) {
FILE *file = fopen(filename, "r");
int buflen = 64;
char string[buflen];
fgets(string, buflen, file);
fclose(file);
return atoi(string);
}
int main() {
/*
* Power
*/
printf("#[bg=" LIGHT_BG "]");
int acStatus = fileToNumber(AC_STATUS_FILE);
int batteryLevel = fileToNumber(BATTERY_LEVEL_FILE);
if (acStatus == 1) {
printf("#[fg=" GREEN ",bold] AC ⌁ ");
if (batteryLevel < 75) {
if (batteryLevel > 50) {
printf("#[fg=" ORANGE "]");
} else {
printf("#[fg=" RED "]");
}
printf("(%2d %%) ", batteryLevel);
}
} else {
if (batteryLevel > 75) {
printf("#[fg=" GREEN "]");
} else if (batteryLevel > 50) {
printf("#[fg=" ORANGE "]");
} else {
printf("#[fg=" RED ",reverse]");
}
printf(" %2d %% ", batteryLevel);
}
resetStyles();
/*
* CPU Temp
*/
int cpuTemp = fileToNumber(CPU_TEMP_FILE) / CPU_TEMP_FACTOR;
printf("#[bg=" DARK_BG "]");
if (cpuTemp > 80) {
printf("#[fg=" RED ",reverse]");
} else if (cpuTemp > 50) {
printf("#[fg=" ORANGE "]");
} else {
printf("#[fg=" GREEN "]");
}
printf(" %d°C ", cpuTemp);
resetStyles();
/* sysinfo struct used for multiple things */
struct sysinfo sysInfo;
int err = sysinfo(&sysInfo);
if (err == -1) {
fprintf(stderr, "FAILED 1");
exit(1);
}
/*
* Load Avg
*/
float loadAvg = sysInfo.loads[0] / (float)(1 << SI_LOAD_SHIFT);
printf("#[fg=" WHITE ",bg=" LIGHT_BG "] %2.2f ", loadAvg);
/*
* Memory Usage
*/
u_int64_t memoryTotal = (sysInfo.totalram * sysInfo.mem_unit);
u_int64_t memoryAvailable =
((sysInfo.freeram + sysInfo.bufferram) * sysInfo.mem_unit);
double freeMemoryPercentage = ((double)memoryAvailable / memoryTotal) * 100;
double usedMemoryPercentage = 100 - freeMemoryPercentage;
printf("#[bg=" DARK_BG "]");
if (usedMemoryPercentage > 75) {
printf("#[fg=" RED ",reverse]");
} else if (usedMemoryPercentage > 50) {
printf("#[fg=" ORANGE "]");
} else {
printf("#[fg=" GREEN "]");
}
printf(" %2.f%% ", usedMemoryPercentage);
resetStyles();
/*
* Fan Speed
*/
FILE *fanFile = fopen(FAN_STATUS_FILE, "r");
char *fanStatusLine = NULL;
char *fanSpeedLine = NULL;
size_t size = 0;
/* We don't actually care about this one, but it's first */
getline(&fanStatusLine, &size, fanFile);
getline(&fanSpeedLine, &size, fanFile);
fclose(fanFile);
stripNonDigits(fanSpeedLine, strlen(fanSpeedLine));
int fanSpeed = atoi(fanSpeedLine);
free(fanStatusLine);
free(fanSpeedLine);
printf("#[fg=" WHITE ",bg=" LIGHT_BG "] %4d ",
// after wake from sleep this seems to be the value
fanSpeed == 65535 ? 0 : fanSpeed);
/*
* Network Status
* TODO broken on 5.15.0
* https://unix.stackexchange.com/q/724019/548295
* Tried with nl80211, same result
* nl example https://github.com/bmegli/wifi-scan
*/
printf("#[fg=" WHITE ",bg=" DARK_BG "]");
int8_t ethernetStatus = interfaceStatus(ETHERNET_INTERFACE);
int8_t wirelessStatus = interfaceStatus(WIRELESS_INTERFACE);
if (ethernetStatus) {
// TODO any neat stats we can get?
printf("#[fg=" GREEN "] WIRED ↑ ");
} else if (wirelessStatus) {
// Communicate using ioctl to get information
struct iwreq ssidReq, signalReq;
int sockfd;
char *ssid[32];
// Allocate memory for the request
memset(&ssidReq, 0, sizeof(struct iwreq));
memset(&signalReq, 0, sizeof(struct iwreq));
// Assign our interface name to the request
sprintf(ssidReq.ifr_name, WIRELESS_INTERFACE);
sprintf(signalReq.ifr_name, WIRELESS_INTERFACE);
// Open socket for ioctl
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
fprintf(stderr, "FAILED 2");
exit(2);
}
// Get SSID
ssidReq.u.essid.pointer = ssid;
ssidReq.u.essid.length = 32;
if (ioctl(sockfd, SIOCGIWESSID, &ssidReq) == -1) {
fprintf(stderr, "FAILED 3");
exit(3);
}
struct iw_statistics *stats;
int8_t signal = 0;
signalReq.u.data.pointer = (struct iw_statistics *)malloc(sizeof(*stats));
signalReq.u.data.length = sizeof(*stats);
signalReq.u.data.flags = 1;
if (ioctl(sockfd, SIOCGIWSTATS, &signalReq) == -1) {
fprintf(stderr, "FAILED 4");
exit(4);
}
if (((struct iw_statistics *)signalReq.u.data.pointer)->qual.updated &
IW_QUAL_DBM) {
// signal is measured in dBm and is valid for us to use
signal =
((struct iw_statistics *)signalReq.u.data.pointer)->qual.level - 256;
free(signalReq.u.data.pointer);
}
if (signal > 70) {
printf("#[fg=" RED "]");
} else if (signal > 60) {
printf("#[fg=" ORANGE "]");
} else if (signal > 50) {
printf("#[fg=" YELLOW "]");
} else {
printf("#[fg=" GREEN "]");
}
printf(" ");
fwrite(ssidReq.u.essid.pointer, 1, ssidReq.u.essid.length, stdout);
printf(" %d", signal);
printf("#[fg=" GREEN "] ↑ ");
} else {
printf("#[fg=" RED ",reverse] OFFLINE ↓ ");
}
resetStyles();
/*
* VPN Status
* TODO support multiple vpns
* - AWS
* - Mullvad
*/
printf("#[bg=" LIGHT_BG ",bold]");
struct stat buffer;
int exists = stat(WIREGUARD_INTERFACE_FILE, &buffer);
if (exists == 0) {
printf("#[fg=" GREEN "] VPN ↑ ");
} else {
printf("#[fg=" RED "] VPN ↓ ");
}
resetStyles();
/*
* Day Month, Year
*/
char day[3];
char month[10];
char year[5];
time_t rawtime;
struct tm *timeInfo;
time(&rawtime);
timeInfo = localtime(&rawtime);
strftime(day, sizeof(day), "%d", timeInfo);
strftime(month, sizeof(month), "%B", timeInfo);
strftime(year, sizeof(year), "%Y", timeInfo);
printf("#[fg=colour146,bold,bg=" DARK_BG "] %s "
"#[fg=colour176,bold,bg=" DARK_BG "]%s, "
"#[fg=colour173,bold,bg=" DARK_BG "]%s#[fg=default] ",
day, month, year);
/*
* Time 24HR
*/
char time[6];
strftime(time, sizeof(time), "%R", timeInfo);
// B REDY 4 SUM QUIK MAFFS
// https://stjarnhimlen.se/comp/ppcomp.html#3
// Compute day number by converting calendar date to Julian Day Number
// tm_year is indexed from 1900
// tm_mon are indexed from 0
// Needs to be INTEGER division hence the floor
double d = (367 * (timeInfo->tm_year + 1900) -
floor((7 * ((timeInfo->tm_year + 1900) +
floor(((timeInfo->tm_mon + 1) + 9) / 12.0))) /
4.0) +
floor((275 * (timeInfo->tm_mon + 1)) / 9.0) +
(timeInfo->tm_mday) - 730530);
// Sun rise/set - https://stjarnhimlen.se/comp/riset.html
// Sun's orbital elements from https://stjarnhimlen.se/comp/ppcomp.html#4.1
double w = 282.9404 + 4.70935E-5 * d; // argument of perihelion
double e = 0.016709 - 1.151E-9 * d; // eccentricity
double M = 356.0470 + 0.9856002585 * d; // mean anomaly
double ecl = 23.4393 - 3.563E-7 * d; // obliquity of the ecliptic
// https://stjarnhimlen.se/comp/ppcomp.html#5
double E = M + e * RADEG * sind(M) * (1.0 + e * cosd(M)); // eccentric anomaly
double x = cosd(E) - e;
double y = sqrt(1.0 - e * e) * sind(E);
double r = sqrt(x * x + y * y); // distance
double v = atan2d(y, x); // true anomaly
double sunTrueLongitude = v + w;
if (sunTrueLongitude >= 360.0) {
sunTrueLongitude -= 360.0;
}
/* Convert sunTrueLongitude,r to ecliptic rectangular geocentric coordinates
* xs,ys: */
double xs = r * cosd(sunTrueLongitude);
double ys = r * sind(sunTrueLongitude);
/* To convert this to equatorial, rectangular, geocentric coordinates,
* compute: */
double xe = xs;
double ye = ys * cosd(ecl);
double ze = ys * sind(ecl);
/* Finally, compute the Sun's Right Ascension (RA) and Declination (Dec): */
double rightAscension = atan2d(ye, xe);
double declination = atan2d(ze, sqrt(xe * xe + ye * ye));
double sunMeanLongitude = M + w;
// Sidereal time at Greenwich at 00:00 Universal Time
double GMST0 = sunMeanLongitude + 180.0;
double universalTime =
(timeInfo->tm_hour -
timeInfo->tm_gmtoff / 3600.0) // remove offset (seconds) from hours
+ (timeInfo->tm_min / 60.0);
double localSiderealTime = GMST0 + (universalTime * 15.04107) + LNG;
double localHourAngle = localSiderealTime - rightAscension;
double sunAngle = asind(sind(LAT) * sind(declination) +
cosd(LAT) * cosd(declination) * cosd(localHourAngle));
char *textColour;
char *bgColour;
if (sunAngle < -18) {
// night
textColour = WHITE;
bgColour = "colour16"; // #000000
} else if (sunAngle >= -18 && sunAngle < -12) {
// astronomical twilight
textColour = WHITE;
bgColour = "colour19"; // #0000AF
} else if (sunAngle >= -18 && sunAngle < -6) {
// nautical twilight
textColour = WHITE;
bgColour = "colour21"; // #0000FF
} else if (sunAngle >= -6 && sunAngle < -0.833) {
// civil twilight
textColour = WHITE;
bgColour = "colour27"; // #005FFF
} else if (sunAngle >= -0.833 && sunAngle < 0) {
// sunrise/sunset
textColour = WHITE;
bgColour = "colour167"; // #d75f5f
} else if (sunAngle > 0) {
// day
textColour = DARK_BG;
bgColour = "colour4"; // #00AFFF
}
printf("#[fg=%s,bold,bg=%s] %s ", textColour, bgColour, time);
return 0;
}