commit d7a8fbd6d38a1cf9ff7d9d92ff0fe022546805b1
parent 51bc02f6ea93e54c976aa76f5a43dd032e637f51
Author: bsandro <email@bsandro.tech>
Date: Sun, 9 Jul 2023 22:24:50 +0300
Thinkpad T60 generic info
Diffstat:
M | dwmstatus.c | | | 170 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------ |
1 file changed, 144 insertions(+), 26 deletions(-)
diff --git a/dwmstatus.c b/dwmstatus.c
@@ -17,9 +17,7 @@
#include <X11/Xlib.h>
-char *tzargentina = "America/Buenos_Aires";
-char *tzutc = "UTC";
-char *tzberlin = "Europe/Berlin";
+char *tzcyprus = "Asia/Nicosia";
static Display *dpy;
@@ -113,6 +111,140 @@ readfile(char *base, char *file)
return smprintf("%s", line);
}
+char *get_cpu()
+{
+ static char status[128];
+ char buffer[256];
+ FILE *cpu_fp;
+ unsigned long total_new[4];
+ unsigned long total, active = 0;
+ unsigned long diff_total, diff_active = 0;
+ double cpu0 = 0, cpu1 = 0;
+ static unsigned long cpu0_total = 0, cpu0_active = 0;
+ static unsigned long cpu1_total = 0, cpu1_active = 0;
+
+ cpu_fp = fopen("/proc/stat", "r");
+
+ if (cpu_fp == NULL)
+ return 0;
+
+ while (!feof(cpu_fp))
+ {
+ if (fgets(buffer, 256, cpu_fp) == NULL)
+ break;
+
+ if (strncmp(buffer, "cpu0", 4) == 0)
+ {
+ sscanf(buffer, "%*s %lu %lu %lu %lu",
+ &total_new[0],
+ &total_new[1],
+ &total_new[2],
+ &total_new[3]);
+
+ total = total_new[0] + total_new[1] + total_new[2] + total_new[3];
+ active = total - total_new[3];
+
+ diff_total = total - cpu0_total;
+ diff_active = active - cpu0_active;
+
+ cpu0 = (double) diff_active / diff_total * 100;
+
+ cpu0_total = total;
+ cpu0_active = active;
+ }
+ else if (strncmp(buffer, "cpu1", 4) == 0)
+ {
+ sscanf(buffer, "%*s %lu %lu %lu %lu",
+ &total_new[0],
+ &total_new[1],
+ &total_new[2],
+ &total_new[3]);
+
+ total = total_new[0] + total_new[1] + total_new[2] + total_new[3];
+ active = total - total_new[3];
+
+ diff_total = total - cpu1_total;
+ diff_active = active - cpu1_active;
+
+ cpu1 = (double) diff_active / diff_total * 100;
+
+ cpu1_total = total;
+ cpu1_active = active;
+
+ break;
+ }
+ }
+
+ fclose(cpu_fp);
+
+ //snprintf(status, 128, "%.0f%% %.0f%%", cpu0, cpu1);
+ snprintf(status, 128, "%.0f%%", cpu0);
+ (void)cpu1;
+
+#ifdef DEBUG
+ printf("CPU: %.0f%% %.0f%%\n", cpu0, cpu1);
+#endif
+
+ return status;
+}
+
+char *get_mem()
+{
+ static char status[128];
+ char buffer[256];
+ FILE *mem_fp;
+ unsigned long memtotal, memfree, buffers, cached, meminuse = 0;
+ unsigned long swaptotal, swapfree, swapused = 0;
+
+ mem_fp = fopen("/proc/meminfo", "r");
+
+ if (mem_fp == NULL)
+ return 0;
+
+ while (!feof(mem_fp))
+ {
+ if (fgets(buffer, 255, mem_fp) == NULL)
+ break;
+
+ if (strncmp(buffer, "MemTotal:", 9) == 0)
+ sscanf(buffer, "%*s %lu", &memtotal);
+ else if (strncmp(buffer, "MemFree:", 8) == 0)
+ sscanf(buffer, "%*s %lu", &memfree);
+ else if (strncmp(buffer, "Buffers:", 8) == 0)
+ sscanf(buffer, "%*s %lu", &buffers);
+ else if (strncmp(buffer, "SwapTotal:", 10) == 0)
+ sscanf(buffer, "%*s %lu", &swaptotal);
+ else if (strncmp(buffer, "SwapFree:", 9) == 0)
+ sscanf(buffer, "%*s %lu", &swapfree);
+ else if (strncmp(buffer, "Cached:", 7) == 0)
+ sscanf(buffer, "%*s %lu", &cached);
+ }
+
+ fclose(mem_fp);
+
+ memfree = memfree + buffers + cached;
+ meminuse = memtotal - memfree;
+ swapused = swaptotal - swapfree;
+
+ //snprintf(status, 128, "%.0f%%(%dM/%dM) S:%.0f%%(%dM/%dM)",
+ snprintf(status, 128, "%.0f%% S:%.0f%%",
+ (float)meminuse/memtotal*100,
+ //(int)meminuse/1024,
+ //(int)memtotal/1024,
+ (float)swapused/swaptotal*100);
+ //(int)swapused/1024,
+ //(int)swaptotal/1024);
+
+#ifdef DEBUG
+ printf("MEM: %.0f%% (%dM/%dM)\n",
+ (float)meminuse/memtotal*100,
+ (int)meminuse/1024,
+ (int)memtotal/1024);
+#endif
+
+ return status;
+}
+
char *
getbattery(char *base)
{
@@ -181,42 +313,28 @@ main(void)
char *status;
char *avgs;
char *bat;
- char *bat1;
- char *tmar;
- char *tmutc;
- char *tmbln;
- char *t0, *t1, *t2;
+ char *tmcy;
+ char *t1;
if (!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr, "dwmstatus: cannot open display.\n");
return 1;
}
- for (;;sleep(60)) {
+ for (;;sleep(20)) {
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);
+ tmcy = mktimes("%F %H:%M", tzcyprus);
+ t1 = gettemperature("/sys/class/thermal/thermal_zone0/hwmon0", "temp1_input");
+
+ status = smprintf("T:%s M:%s C:%s B:%s | %s",
+ t1, get_mem(), get_cpu(), bat, tmcy);
setstatus(status);
- free(t0);
free(t1);
- free(t2);
free(avgs);
free(bat);
- free(bat1);
- free(tmar);
- free(tmutc);
- free(tmbln);
+ free(tmcy);
free(status);
}