2017-09-10 17:28:54 +00:00
|
|
|
/*
|
|
|
|
* Copy me if you can.
|
|
|
|
* by 20h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _BSD_SOURCE
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
|
|
|
|
char *tzargentina = "America/Buenos_Aires";
|
|
|
|
char *tzutc = "UTC";
|
2022-07-25 20:02:04 +00:00
|
|
|
char *tzwarsaw = "Europe/Warsaw";
|
2023-02-17 22:55:55 +00:00
|
|
|
char *tzlocal = "Australia/Sydney";
|
2017-09-10 17:28:54 +00:00
|
|
|
|
|
|
|
static Display *dpy;
|
|
|
|
|
|
|
|
char *
|
|
|
|
smprintf(char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list fmtargs;
|
|
|
|
char *ret;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
va_start(fmtargs, fmt);
|
|
|
|
len = vsnprintf(NULL, 0, fmt, fmtargs);
|
|
|
|
va_end(fmtargs);
|
|
|
|
|
|
|
|
ret = malloc(++len);
|
|
|
|
if (ret == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(fmtargs, fmt);
|
|
|
|
vsnprintf(ret, len, fmt, fmtargs);
|
|
|
|
va_end(fmtargs);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
settz(char *tzname)
|
|
|
|
{
|
|
|
|
setenv("TZ", tzname, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
mktimes(char *fmt, char *tzname)
|
|
|
|
{
|
|
|
|
char buf[129];
|
|
|
|
time_t tim;
|
|
|
|
struct tm *timtm;
|
|
|
|
|
|
|
|
settz(tzname);
|
|
|
|
tim = time(NULL);
|
|
|
|
timtm = localtime(&tim);
|
|
|
|
if (timtm == NULL)
|
|
|
|
return smprintf("");
|
|
|
|
|
|
|
|
if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) {
|
|
|
|
fprintf(stderr, "strftime == 0\n");
|
|
|
|
return smprintf("");
|
|
|
|
}
|
|
|
|
|
2023-02-17 22:55:55 +00:00
|
|
|
return smprintf("⏲%s", buf);
|
2017-09-10 17:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
setstatus(char *str)
|
|
|
|
{
|
|
|
|
XStoreName(dpy, DefaultRootWindow(dpy), str);
|
|
|
|
XSync(dpy, False);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
loadavg(void)
|
|
|
|
{
|
|
|
|
double avgs[3];
|
|
|
|
|
2022-07-25 20:11:24 +00:00
|
|
|
if (getloadavg(avgs, 1) < 0)
|
2017-09-10 17:28:54 +00:00
|
|
|
return smprintf("");
|
|
|
|
|
2022-07-25 22:18:42 +00:00
|
|
|
return smprintf("📈 %.2f ", avgs[0]);
|
2017-09-10 17:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
readfile(char *base, char *file)
|
|
|
|
{
|
|
|
|
char *path, line[513];
|
|
|
|
FILE *fd;
|
|
|
|
|
|
|
|
memset(line, 0, sizeof(line));
|
|
|
|
|
2022-10-13 22:05:19 +00:00
|
|
|
if (file != NULL)
|
|
|
|
path = smprintf("%s/%s", base, file);
|
|
|
|
else {
|
|
|
|
path = smprintf("%s", base);
|
|
|
|
}
|
2017-09-10 17:28:54 +00:00
|
|
|
fd = fopen(path, "r");
|
|
|
|
free(path);
|
|
|
|
if (fd == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (fgets(line, sizeof(line)-1, fd) == NULL)
|
|
|
|
return NULL;
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
return smprintf("%s", line);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
getbattery(char *base)
|
|
|
|
{
|
2022-07-25 20:39:33 +00:00
|
|
|
char *co;
|
|
|
|
char *status;
|
|
|
|
int descap, remcap;
|
2017-09-10 17:28:54 +00:00
|
|
|
|
|
|
|
descap = -1;
|
|
|
|
remcap = -1;
|
|
|
|
|
|
|
|
co = readfile(base, "present");
|
|
|
|
if (co == NULL)
|
|
|
|
return smprintf("");
|
|
|
|
if (co[0] != '1') {
|
|
|
|
free(co);
|
|
|
|
return smprintf("not present");
|
|
|
|
}
|
|
|
|
free(co);
|
|
|
|
|
|
|
|
co = readfile(base, "charge_full_design");
|
|
|
|
if (co == NULL) {
|
|
|
|
co = readfile(base, "energy_full_design");
|
|
|
|
if (co == NULL)
|
|
|
|
return smprintf("");
|
|
|
|
}
|
|
|
|
sscanf(co, "%d", &descap);
|
|
|
|
free(co);
|
|
|
|
|
|
|
|
co = readfile(base, "charge_now");
|
|
|
|
if (co == NULL) {
|
|
|
|
co = readfile(base, "energy_now");
|
|
|
|
if (co == NULL)
|
|
|
|
return smprintf("");
|
|
|
|
}
|
|
|
|
sscanf(co, "%d", &remcap);
|
|
|
|
free(co);
|
|
|
|
|
|
|
|
co = readfile(base, "status");
|
|
|
|
if (!strncmp(co, "Discharging", 11)) {
|
2022-07-25 20:39:33 +00:00
|
|
|
status = "🔋";
|
2017-09-10 17:28:54 +00:00
|
|
|
} else if(!strncmp(co, "Charging", 8)) {
|
2022-07-25 20:39:33 +00:00
|
|
|
status = "🔌";
|
2017-09-10 17:28:54 +00:00
|
|
|
} else {
|
2022-07-25 20:39:33 +00:00
|
|
|
status = "🔋?";
|
2017-09-10 17:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (remcap < 0 || descap < 0)
|
|
|
|
return smprintf("invalid");
|
|
|
|
|
2022-07-25 22:18:42 +00:00
|
|
|
return smprintf("%s %.0f%% ", status, ((float)remcap / (float)descap) * 100);
|
2017-09-10 17:28:54 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 21:05:07 +00:00
|
|
|
char *
|
|
|
|
getBrightness(char *base) {
|
|
|
|
char *co;
|
|
|
|
int brightness, max_brightness;
|
|
|
|
char *status;
|
|
|
|
|
|
|
|
co = readfile(base, "brightness");
|
|
|
|
if (co == NULL)
|
|
|
|
return smprintf("");
|
|
|
|
|
|
|
|
sscanf(co, "%d", &brightness);
|
|
|
|
free(co);
|
|
|
|
|
|
|
|
co = readfile(base, "max_brightness");
|
|
|
|
if (co == NULL)
|
|
|
|
return smprintf("");
|
|
|
|
sscanf(co, "%d", &max_brightness);
|
|
|
|
free(co);
|
|
|
|
|
|
|
|
int percentage = (((float)brightness / (float)max_brightness) * 100);
|
|
|
|
|
|
|
|
if(percentage < 50) {
|
|
|
|
status = "🔅";
|
|
|
|
} else {
|
|
|
|
status = "☀";
|
|
|
|
}
|
|
|
|
|
|
|
|
return smprintf("%s %d%% ", status, percentage);
|
|
|
|
}
|
2022-10-13 22:05:19 +00:00
|
|
|
char *
|
|
|
|
getPowerConsumption(char *base) {
|
|
|
|
char *co;
|
|
|
|
long usageRead;
|
|
|
|
float usage;
|
|
|
|
|
|
|
|
co = readfile(base, NULL);
|
|
|
|
if (co == NULL)
|
|
|
|
return smprintf("");
|
|
|
|
|
|
|
|
sscanf(co, "%ld", &usageRead);
|
|
|
|
free(co);
|
|
|
|
|
|
|
|
if (usage == 0)
|
|
|
|
return smprintf("");
|
|
|
|
|
|
|
|
usage = usageRead/ 1e6;
|
|
|
|
|
|
|
|
return smprintf("%.2lf W ", usage);
|
|
|
|
}
|
2022-07-25 21:05:07 +00:00
|
|
|
|
2017-09-10 17:28:54 +00:00
|
|
|
char *
|
|
|
|
gettemperature(char *base, char *sensor)
|
|
|
|
{
|
|
|
|
char *co;
|
|
|
|
|
|
|
|
co = readfile(base, sensor);
|
|
|
|
if (co == NULL)
|
|
|
|
return smprintf("");
|
2022-07-25 22:18:42 +00:00
|
|
|
return smprintf("🌡 %02.0f°C ", atof(co) / 1000);
|
2017-09-10 17:28:54 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 22:06:32 +00:00
|
|
|
void update() {
|
|
|
|
char *status;
|
|
|
|
char *avgs;
|
2023-02-17 22:55:55 +00:00
|
|
|
char *home_time;
|
|
|
|
char *local_time;
|
2022-07-25 22:06:32 +00:00
|
|
|
char *t0;
|
|
|
|
char *bat;
|
|
|
|
char *brightness;
|
2022-10-13 22:05:19 +00:00
|
|
|
char *powerConsumption;
|
2022-07-25 22:06:32 +00:00
|
|
|
|
|
|
|
avgs = loadavg();
|
2023-02-17 22:55:55 +00:00
|
|
|
home_time = mktimes("%H:%M", tzwarsaw);
|
|
|
|
local_time = mktimes("%H:%M %d.%m.%Y", tzlocal);
|
2022-07-25 22:06:32 +00:00
|
|
|
t0 = gettemperature("/sys/class//hwmon/hwmon1", "temp1_input");
|
|
|
|
bat = getbattery("/sys/class/power_supply/BAT0");
|
|
|
|
brightness = getBrightness("/sys/class/backlight/amdgpu_bl0");
|
2022-10-13 22:05:19 +00:00
|
|
|
powerConsumption = getPowerConsumption("/sys/class/power_supply/BAT0/power_now");
|
2022-07-25 22:06:32 +00:00
|
|
|
|
2023-02-17 22:55:55 +00:00
|
|
|
if (strcmp(tzwarsaw, tzlocal) == 0) {
|
|
|
|
status = smprintf("%s%s%s%s%s%s",
|
|
|
|
powerConsumption, bat, brightness, t0, avgs, local_time);
|
|
|
|
} else {
|
|
|
|
status = smprintf("%s%s%s%s%s%s %s",
|
|
|
|
powerConsumption, bat, brightness, t0, avgs, home_time, local_time);
|
|
|
|
}
|
2022-07-25 22:06:32 +00:00
|
|
|
setstatus(status);
|
|
|
|
|
|
|
|
free(t0);
|
|
|
|
free(bat);
|
|
|
|
free(avgs);
|
2023-02-17 22:55:55 +00:00
|
|
|
free(home_time);
|
|
|
|
free(local_time);
|
2022-07-25 22:06:32 +00:00
|
|
|
free(status);
|
|
|
|
free(brightness);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sleepUntilFullMinute() {
|
|
|
|
time_t tim;
|
|
|
|
struct tm *tm;
|
|
|
|
tim = time(NULL);
|
|
|
|
|
|
|
|
tm = localtime(&tim);
|
|
|
|
int seconds_left = 60 - tm->tm_sec;
|
|
|
|
sleep(seconds_left);
|
|
|
|
}
|
|
|
|
|
2017-09-10 17:28:54 +00:00
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
if (!(dpy = XOpenDisplay(NULL))) {
|
|
|
|
fprintf(stderr, "dwmstatus: cannot open display.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2022-07-25 22:06:32 +00:00
|
|
|
update();
|
|
|
|
sleepUntilFullMinute();
|
2022-07-25 21:57:07 +00:00
|
|
|
|
2017-09-10 17:28:54 +00:00
|
|
|
for (;;sleep(60)) {
|
2022-07-25 22:06:32 +00:00
|
|
|
update();
|
2017-09-10 17:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
XCloseDisplay(dpy);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|