128 lines
3.2 KiB
C
128 lines
3.2 KiB
C
|
/* 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>
|
||
|
|
||
|
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[] = {
|
||
|
"^c#ff0000^",
|
||
|
"^c#ff8800^",
|
||
|
"^c#ddff00^",
|
||
|
"^c#00ff00^",
|
||
|
"^c#00ff00^", /* Just in case the battery report a charge over 100% */
|
||
|
"^c#0000ff^", /* Full charge color */
|
||
|
"^c#888888^"
|
||
|
};
|
||
|
const char *elicon = "^c#000000^^r7,6,9,3^^r10,9,9,3^^c#ffffff^^r8,7,7,1^^r13,8,1,1^^r12,9,1,1^^r11,10,7,1^";
|
||
|
const char *bat_icon = "^r0,7,2,4^^r2,4,22,10^^c#000000^^r3,5,20,8^%s^r%d,5,%d,8^%s^d^^f24^";
|
||
|
|
||
|
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);
|
||
|
if (!strcmp(state, "?"))
|
||
|
c = 5;
|
||
|
else
|
||
|
c = perc / 25;
|
||
|
|
||
|
return (bprintf(bat_icon, colors[c], 3 + 20 - perc/5, perc/5, strcmp(state,"+") ? "" : elicon));
|
||
|
}
|
||
|
|
||
|
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] = {
|
||
|
"^c#ff0000^",
|
||
|
"^c#ff8800^",
|
||
|
"^c#ddff00^",
|
||
|
"^c#00ff00^",
|
||
|
"^c#00ff00^", /* Just in case the value is over 100% */
|
||
|
};
|
||
|
const char *defc = "^c#888888^";
|
||
|
const char *wifi_icon = "%s^r0,11,2,3^%s^r3,8,2,6^%s^r6,5,2,9^%s^r9,2,2,12^^f12^^d^";
|
||
|
|
||
|
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);
|
||
|
return bprintf(wifi_icon, colors[cur/25], cur > 25 ? "" : defc, cur > 50 ? "" : defc, cur > 75 ? "" : defc);
|
||
|
}
|
||
|
#endif
|