diff --git a/dwmstatus.c b/dwmstatus.c index d2a4b03..2df2e0a 100644 --- a/dwmstatus.c +++ b/dwmstatus.c @@ -17,8 +17,6 @@ #include -char *tzargentina = "America/Buenos_Aires"; -char *tzutc = "UTC"; char *tzberlin = "Europe/Berlin"; static Display *dpy; @@ -89,134 +87,84 @@ loadavg(void) if (getloadavg(avgs, 3) < 0) return smprintf(""); - return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]); + return smprintf("%.2f", avgs[1]); } char * -readfile(char *base, char *file) -{ - char *path, line[513]; - FILE *fd; - - memset(line, 0, sizeof(line)); - - path = smprintf("%s/%s", base, file); - fd = fopen(path, "r"); - free(path); +getresult(char *command) { + FILE *fd = popen(command, "r"); + char ret[100]; if (fd == NULL) - return NULL; + strcpy(ret, "invalid"); - if (fgets(line, sizeof(line)-1, fd) == NULL) - return NULL; - fclose(fd); + if (fgets(ret, 100, fd) == NULL) + strcpy(ret, "invalid"); - return smprintf("%s", line); + if (pclose(fd) == -1) + strcpy(ret, "invalid"); + + return smprintf("%s", ret); } char * -getbattery(char *base) -{ - char *co, status; - int descap, remcap; - - 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)) { - status = '-'; - } else if(!strncmp(co, "Charging", 8)) { - status = '+'; - } else { - status = '?'; - } - - if (remcap < 0 || descap < 0) - return smprintf("invalid"); +getpartition(char *partition) { + char *co = smprintf("df -lh | grep %s | tr -s ' ' | cut -d ' ' -f 4 | tr -d '\n'", partition); + return getresult(co); +} - return smprintf("%.0f%%%c", ((float)remcap / (float)descap) * 100, status); +char * +gettemp() { + char *co = smprintf("sensors amdgpu-pci-0400 | grep edge | cut -c 16-22 | tr -d '\n'"); + return getresult(co); } char * -gettemperature(char *base, char *sensor) -{ - char *co; +gettrack() { + char *co = smprintf("mpc -h core-a.lan | head -n 1 | tr -d '\n'"); + return getresult(co); +} - co = readfile(base, sensor); - if (co == NULL) - return smprintf(""); - return smprintf("%02.0f°C", atof(co) / 1000); +char * +getips() { + char *co = smprintf("ip a | grep 'inet ' | grep -v 127.0.0.1 | tr -s ' ' | cut -f 3 -d ' ' | tr '\n' ' '"); + return getresult(co); } + int main(void) { char *status; char *avgs; - char *bat; - char *bat1; - char *tmar; - char *tmutc; char *tmbln; - char *t0, *t1, *t2; + char *partition; + char *temp; + char *track; + char *ips; if (!(dpy = XOpenDisplay(NULL))) { fprintf(stderr, "dwmstatus: cannot open display.\n"); return 1; } - for (;;sleep(60)) { + for (;;sleep(1)) { avgs = loadavg(); - bat = getbattery("/sys/class/power_supply/BAT0"); - bat1 = getbattery("/sys/class/power_supply/BAT1"); - tmar = mktimes("%H:%M", tzargentina); - tmutc = mktimes("%H:%M", tzutc); - tmbln = mktimes("KW %W %a %d %b %H:%M %Z %Y", tzberlin); - t0 = gettemperature("/sys/devices/virtual/hwmon/hwmon0", "temp1_input"); - t1 = gettemperature("/sys/devices/virtual/hwmon/hwmon2", "temp1_input"); - t2 = gettemperature("/sys/devices/virtual/hwmon/hwmon4", "temp1_input"); - - status = smprintf("T:%s|%s|%s L:%s B:%s|%s A:%s U:%s %s", - t0, t1, t2, avgs, bat, bat1, tmar, tmutc, - tmbln); + tmbln = mktimes("%a %d %b %H:%M", tzberlin); + partition = getpartition("/$"); + temp = gettemp(); + track = gettrack(); + ips = getips(); + + status = smprintf(" %s | %s | %s | %s | %s", + track, avgs, temp, partition, tmbln); setstatus(status); - free(t0); - free(t1); - free(t2); - free(avgs); - free(bat); - free(bat1); - free(tmar); - free(tmutc); + free(ips); + free(track); + free(temp); + free(partition); free(tmbln); + free(avgs); free(status); } @@ -224,4 +172,3 @@ main(void) return 0; } -