2018-07-13 05:05:48 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
#include <limits.h>
|
|
|
|
#include <linux/wireless.h>
|
|
|
|
|
2024-03-18 22:55:03 +00:00
|
|
|
static const char col_blue[] = "#458588";
|
|
|
|
static const char col_dark[] = "#3c3836";
|
|
|
|
static const char col_gray[] = "#928374";
|
|
|
|
static const char col_green[] = "#b8bb26";
|
|
|
|
static const char col_orange[] = "#fe8019";
|
|
|
|
static const char col_red[] = "#fb4934";
|
|
|
|
static const char col_yellow[] = "#fabd2f";
|
|
|
|
|
2018-07-13 05:05:48 +00:00
|
|
|
const char *battery_state(const char *);
|
|
|
|
|
|
|
|
const char *
|
|
|
|
s2d_battery_perc(const char *bat)
|
|
|
|
{
|
|
|
|
int perc;
|
|
|
|
int c;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
const char *state;
|
|
|
|
const char *colors[] = {
|
2024-03-18 22:55:03 +00:00
|
|
|
col_red,
|
|
|
|
col_orange,
|
|
|
|
col_yellow,
|
|
|
|
col_green,
|
|
|
|
col_green, /* Just in case the battery report a charge over 100% */
|
|
|
|
col_blue, /* Full charge color */
|
|
|
|
col_gray
|
2018-07-13 05:05:48 +00:00
|
|
|
};
|
2024-03-18 22:55:03 +00:00
|
|
|
const char *elicon = "^c#3c3836^^r8,7,7,1^^r13,8,1,1^^r12,9,1,1^^r11,10,7,1^";
|
|
|
|
const char *bat_icon = "^c%s^^r0,0,36,19^^f6^^c%s^^r0,7,2,4^^r2,3,21,12^^c%s^^r4,5,17,8^^c%s^^r%d,6,%d,6^%s^d^^f30^";
|
2018-07-13 05:05:48 +00:00
|
|
|
|
|
|
|
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/capacity");
|
|
|
|
if (pscanf(path, "%i", &perc) != 1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
state = battery_state(bat);
|
2024-03-18 22:55:03 +00:00
|
|
|
if (perc > 100) {
|
|
|
|
perc = 100;
|
|
|
|
c = 6;
|
|
|
|
} else if (!strcmp(state, "?")) {
|
2018-07-13 05:05:48 +00:00
|
|
|
c = 5;
|
2024-03-18 22:55:03 +00:00
|
|
|
} else {
|
2018-07-13 05:05:48 +00:00
|
|
|
c = perc / 25;
|
2024-03-18 22:55:03 +00:00
|
|
|
}
|
2018-07-13 05:05:48 +00:00
|
|
|
|
2024-03-18 22:55:03 +00:00
|
|
|
return (bprintf(bat_icon, colors[c], col_dark, colors[c], col_dark,
|
|
|
|
4 + 16 - perc * 16 / 100, perc * 16 / 100, strcmp(state,"+") ? "" : elicon));
|
2018-07-13 05:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
s2d_datetime(const char *fmt)
|
|
|
|
{
|
|
|
|
time_t t;
|
|
|
|
int day, month, hour, min, sec;
|
|
|
|
const char *bin[2] = {"^c#000000^", "^c#ff0000^"};
|
|
|
|
char clock[] = "^c#888888^^r0,1,62,6^^r0,8,62,4^^r0,13,62,4^^c#ff0000^^r1,9,%d,2^^r1,14,%d,2^%s^r2,2,10,4^%s^r14,2,10,4^%s^r26,2,10,4^%s^r38,2,10,4^%s^r50,2,10,4^^f62^^d^";
|
|
|
|
|
|
|
|
fmt = "%d/%m %T";
|
|
|
|
|
|
|
|
t = time(NULL);
|
|
|
|
if (strftime(buf, sizeof(buf), fmt, localtime(&t)) == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (sscanf(buf, "%2d/%2d %2d:%2d:%2d", &day, &month, &hour, &min, &sec) != 5)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return bprintf(clock, min, sec, bin[hour >> 4 & 1], bin[hour >> 3 & 1], bin[hour >> 2 & 1], bin[hour >> 1 & 1], bin[hour & 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
s2d_wifi_perc(const char *iface)
|
|
|
|
{
|
|
|
|
int i, cur;
|
|
|
|
char *p, *datastart;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
char status[5];
|
|
|
|
FILE *fp;
|
|
|
|
const char *colors[5] = {
|
2024-03-18 22:55:03 +00:00
|
|
|
col_red,
|
|
|
|
col_orange,
|
|
|
|
col_yellow,
|
|
|
|
col_green,
|
|
|
|
col_green, /* Just in case the value is over 100% */
|
|
|
|
};
|
|
|
|
const char *bars[] = {
|
|
|
|
"^r0,12,2,3^",
|
|
|
|
"^r3,9,2,6^",
|
|
|
|
"^r6,6,2,9^",
|
|
|
|
"^r9,3,2,12^"
|
2018-07-13 05:05:48 +00:00
|
|
|
};
|
|
|
|
const char *defc = "^c#888888^";
|
2024-03-18 22:55:03 +00:00
|
|
|
const char *wifi_icon = "^c%s^^r0,0,36,19^^f12^^c%s^%s%s%s%s^f24^^d^";
|
2018-07-13 05:05:48 +00:00
|
|
|
|
|
|
|
if (esnprintf(path, sizeof(path), "/sys/class/net/%s/operstate", iface) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!(fp = fopen(path, "r"))) {
|
|
|
|
warn("fopen '%s':", path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = fgets(status, 5, fp);
|
|
|
|
fclose(fp);
|
|
|
|
if (!p || strcmp(status, "up\n") != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(fp = fopen("/proc/net/wireless", "r"))) {
|
|
|
|
warn("fopen '/proc/net/wireless':");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
if (i < 2 || !p) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(datastart = strstr(buf, iface))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
datastart = (datastart+(strlen(iface)+1));
|
|
|
|
sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t "
|
|
|
|
"%*d\t\t%*d\t\t %*d\t %*d\t\t %*d", &cur);
|
|
|
|
|
|
|
|
/* 70 is the max of /proc/net/wireless */
|
|
|
|
cur = (int)((float)cur / 70 * 100);
|
2024-03-18 22:55:03 +00:00
|
|
|
return bprintf(wifi_icon, colors[cur/25], col_dark, bars[0], cur > 25 ? bars[1] : "", cur > 50 ? bars[2] : "", cur > 75 ? bars[3] : "");
|
2018-07-13 05:05:48 +00:00
|
|
|
}
|
|
|
|
#endif
|