summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Wolff <janw@mailbox.org>2019-06-17 20:07:30 +0200
committerJan Wolff <janw@mailbox.org>2019-06-17 20:07:30 +0200
commitb2084f87893ee4753baafca0bd635385b837ab30 (patch)
tree10f7dfd4086352bb269968506725d03116459bff
parent2be36d330a7116892bbd1d7f537b0e38b2807bad (diff)
config.h
-rw-r--r--Makefile9
-rw-r--r--config.def.h25
-rw-r--r--dwmstatus.c60
3 files changed, 78 insertions, 16 deletions
diff --git a/Makefile b/Makefile
index 86b0ef0..6ea2a1f 100644
--- a/Makefile
+++ b/Makefile
@@ -7,12 +7,17 @@ PREFIX = /usr/local
prog: $(OBJ)
$(CC) $(CFLAGS) $(OBJ) -o $(BIN)
-debug: CFLAGS += -DDEBUG -g -fsanitize=address -fsanitize=leak
+debug: CFLAGS += -DDEBUG
debug: prog
%.o: %.c
$(CC) $(INCLUDE) $(CFLAGS) -c $<
+${OBJ}: config.h
+
+config.h:
+ cp config.def.h $@
+
clean:
rm *.o
cd ..
@@ -27,4 +32,4 @@ uninstall:
rm -f ${PREFIX}/bin/dwmstatus
-.PHONY: clean
+.PHONY: clean install uninstall
diff --git a/config.def.h b/config.def.h
new file mode 100644
index 0000000..2f9ecca
--- /dev/null
+++ b/config.def.h
@@ -0,0 +1,25 @@
+// watermark your desktop here
+#define TAG "dwm rulez"
+// update interval (in seconds)
+#define INTERVAL 60
+// maximum length of an individual block
+#define BLOCK_SIZE 128
+
+// format of the clock string
+#define CLOCK_FORMAT "%F %H:%M"
+
+#define BATTERY_NOW "/sys/class/power_supply/BAT1/charge_now"
+#define BATTERY_FULL "/sys/class/power_supply/BAT1/charge_full"
+#define BATTERY_STATUS "/sys/class/power_supply/BAT1/status"
+
+enum BlockType {B_TAG, B_CLOCK, B_BATTERY};
+
+// amount of different blocks
+#define BLOCK_COUNT 2
+enum BlockType blockTypes[] = {
+ B_TAG,
+ B_CLOCK
+};
+
+#define STATUS_SIZE (BLOCK_COUNT + 1) * BLOCK_SIZE
+#define STATUS_TMP_SIZE (BLOCK_COUNT - 1) * BLOCK_SIZE
diff --git a/dwmstatus.c b/dwmstatus.c
index 3cc6be0..5abf380 100644
--- a/dwmstatus.c
+++ b/dwmstatus.c
@@ -1,13 +1,17 @@
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sys/types.h>
#include <unistd.h>
+#include <signal.h>
+
+#include "config.h"
+
+const size_t blockc = sizeof(blockTypes) / sizeof(enum BlockType);
-#define STATUS_SIZE 512
-#define BLOCK_SIZE 128
char status[STATUS_SIZE];
-char blocks[2][BLOCK_SIZE];
+char status_tmp[STATUS_TMP_SIZE];
void print() {
#ifdef DEBUG
@@ -17,41 +21,69 @@ void print() {
#endif
}
-void update_battery() {
+void update_tag(char * dst) {
+ strncpy(dst, TAG, BLOCK_SIZE);
+}
+
+void update_battery(char * dst) {
char buf_cur[32], buf_max[32], buf_status[1];
int cur, max;
FILE * fp;
- fp = fopen("/sys/class/power_supply/BAT1/charge_now", "r");
+ fp = fopen(BATTERY_NOW, "r");
fread(buf_cur, 32, 1, fp);
fclose(fp);
- fp = fopen("/sys/class/power_supply/BAT1/charge_full", "r");
+ fp = fopen(BATTERY_FULL, "r");
fread(buf_max, 32, 1, fp);
fclose(fp);
- fp = fopen("/sys/class/power_supply/BAT1/status", "r");
+ fp = fopen(BATTERY_STATUS, "r");
fread(buf_status, 1, 1, fp);
fclose(fp);
cur = atoi(buf_cur);
max = atoi(buf_max);
- sprintf(blocks[1], "%i%% %c", cur * 100 / max, buf_status[0]);
+ snprintf(dst, BLOCK_SIZE, "%i%% %c", cur * 100 / max, buf_status[0]);
}
-void update_time() {
+void update_clock(char * dst) {
time_t t = time(NULL);
struct tm * tm = localtime(&t);
- strftime(blocks[0], BLOCK_SIZE, "%F %H:%M", tm);
+ strftime(dst, BLOCK_SIZE, CLOCK_FORMAT, tm);
+}
+
+void update() {
+ char block[BLOCK_SIZE];
+ status[0] = '\0';
+
+ for(size_t i = 0; i < BLOCK_COUNT; i++) {
+ switch(blockTypes[i]) {
+ case B_TAG:
+ update_tag(block);
+ break;
+ case B_CLOCK:
+ update_clock(block);
+ break;
+ case B_BATTERY:
+ update_battery(block);
+ break;
+ }
+
+ if(i == 0) {
+ strncpy(status, block, BLOCK_SIZE);
+ } else {
+ strncpy(status_tmp, status, STATUS_SIZE);
+ snprintf(status, STATUS_SIZE, "%s | %s", status_tmp, block);
+ }
+ }
}
int main() {
while(1) {
- update_time();
- update_battery();
- snprintf(status, STATUS_SIZE, "%s | %s", blocks[1], blocks[0]);
+ update();
print();
- sleep(60);
+ sleep(INTERVAL);
}
}