summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Wolff <janw@mailbox.org>2019-07-18 19:40:24 +0200
committerJan Wolff <janw@mailbox.org>2019-07-18 19:40:24 +0200
commit8dc2834964495ef2ec856d26e9fbddd32741301e (patch)
tree4d83f9303f67cbee036edd22e3723dd342a97939
parent9bcaa412056746c8a0e0ba88a769089e85cfc1d6 (diff)
uptime monitorHEADmaster
-rw-r--r--config.def.h8
-rw-r--r--dwmstatus.c26
2 files changed, 31 insertions, 3 deletions
diff --git a/config.def.h b/config.def.h
index 72a1c18..ed99d94 100644
--- a/config.def.h
+++ b/config.def.h
@@ -8,15 +8,17 @@ static const unsigned interval = 60;
// format of the clock string
static const char * clock_fmt = "%F %H:%M";
+// battery files
static const char * battery_now = "/sys/class/power_supply/BAT1/charge_now";
static const char * battery_full = "/sys/class/power_supply/BAT1/charge_full";
static const char * battery_status = "/sys/class/power_supply/BAT1/status";
-enum BlockType {B_TAG, B_CLOCK, B_BATTERY};
+// uptime file
+static const char * uptime_fname = "/proc/uptime";
// amount of different blocks
enum BlockType blockTypes[] = {
- B_TAG,
B_BATTERY,
- B_CLOCK
+ B_UPTIME,
+ B_CLOCK,
};
diff --git a/dwmstatus.c b/dwmstatus.c
index 828f49d..e4b4540 100644
--- a/dwmstatus.c
+++ b/dwmstatus.c
@@ -7,6 +7,8 @@
#include <signal.h>
#include <X11/Xlib.h>
+enum BlockType {B_TAG, B_CLOCK, B_BATTERY, B_UPTIME};
+
#include "config.h"
static unsigned block_count;
@@ -73,6 +75,27 @@ void update_clock(char * dst) {
strftime(dst, BLOCK_SIZE, clock_fmt, localtime(&t));
}
+void update_uptime(char * dst) {
+ char buf[32];
+ float uptime;
+ int uptime_m, uptime_h;
+ FILE * fp;
+
+ if((fp = fopen(uptime_fname, "r")) == NULL)
+ error("failed to read current uptime state");
+ fread(buf, 32, 1, fp);
+ fclose(fp);
+
+ uptime = atof(buf);
+ uptime_m = ((int)uptime / 60) % 60;
+ uptime_h = (int)uptime / (60 * 60);
+
+ if(uptime_h == 0)
+ snprintf(dst, BLOCK_SIZE, "UP %dm", uptime_m);
+ else
+ snprintf(dst, BLOCK_SIZE, "UP %dm %dh", uptime_m, uptime_h);
+}
+
void update() {
char block[BLOCK_SIZE];
status[0] = '\0';
@@ -88,6 +111,9 @@ void update() {
case B_BATTERY:
update_battery(block);
break;
+ case B_UPTIME:
+ update_uptime(block);
+ break;
}
if(i == 0) {