summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Wolff <janw@mailbox.org>2019-07-18 19:22:16 +0200
committerJan Wolff <janw@mailbox.org>2019-07-18 19:22:16 +0200
commit9bcaa412056746c8a0e0ba88a769089e85cfc1d6 (patch)
tree2d3d11f9ccb037b129569208415871a50b23690b
parent345e6e7d5495f67500c9ddb81d4bc789ee3e471e (diff)
easier config file (road to 0.2.0)
-rw-r--r--.gitignore3
-rw-r--r--Makefile4
-rw-r--r--config.def.h17
-rw-r--r--dwmstatus.c33
4 files changed, 33 insertions, 24 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a13b778
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+*.o
+config.h
+dwmstatus
diff --git a/Makefile b/Makefile
index 9ace516..15650ce 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-VERSION = 0.1.2
+VERSION = 0.2.0-pre
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -DVERSION=\"${VERSION}\"
@@ -13,7 +13,7 @@ PREFIX = /usr/local
prog: $(OBJ)
$(CC) $(CFLAGS) $(LIBS) $(INCLUDE) $(OBJ) -o $(BIN)
-debug: CFLAGS += -DDEBUG
+debug: CFLAGS += -DDEBUG -g
debug: prog
%.o: %.c
diff --git a/config.def.h b/config.def.h
index 2f9ecca..72a1c18 100644
--- a/config.def.h
+++ b/config.def.h
@@ -1,25 +1,22 @@
// watermark your desktop here
-#define TAG "dwm rulez"
+static const char * tag = "dwm rulez";
// update interval (in seconds)
-#define INTERVAL 60
+static const unsigned interval = 60;
// maximum length of an individual block
#define BLOCK_SIZE 128
// format of the clock string
-#define CLOCK_FORMAT "%F %H:%M"
+static const char * clock_fmt = "%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"
+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};
// amount of different blocks
-#define BLOCK_COUNT 2
enum BlockType blockTypes[] = {
B_TAG,
+ B_BATTERY,
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 b658e1b..828f49d 100644
--- a/dwmstatus.c
+++ b/dwmstatus.c
@@ -9,8 +9,10 @@
#include "config.h"
-static char status[STATUS_SIZE];
-static char status_tmp[STATUS_TMP_SIZE];
+static unsigned block_count;
+static unsigned status_size;
+static char * status;
+static char * status_tmp;
void error(const char * err) {
fputs(err, stderr);
@@ -37,7 +39,7 @@ void print() {
}
void update_tag(char * dst) {
- strncpy(dst, TAG, BLOCK_SIZE);
+ strncpy(dst, tag, BLOCK_SIZE);
}
void update_battery(char * dst) {
@@ -45,17 +47,17 @@ void update_battery(char * dst) {
int cur, max;
FILE * fp;
- if((fp = fopen(BATTERY_NOW, "r")) == NULL)
+ if((fp = fopen(battery_now, "r")) == NULL)
error("failed to read current battery state");
fread(buf_cur, 32, 1, fp);
fclose(fp);
- if((fp = fopen(BATTERY_FULL, "r")) == NULL)
+ if((fp = fopen(battery_full, "r")) == NULL)
error("failed to read max battery state");
fread(buf_max, 32, 1, fp);
fclose(fp);
- if((fp = fopen(BATTERY_STATUS, "r")) == NULL)
+ if((fp = fopen(battery_status, "r")) == NULL)
error("failed to read battery status");
fread(buf_status, 1, 1, fp);
fclose(fp);
@@ -68,15 +70,14 @@ void update_battery(char * dst) {
void update_clock(char * dst) {
time_t t = time(NULL);
- struct tm * tm = localtime(&t);
- strftime(dst, BLOCK_SIZE, CLOCK_FORMAT, tm);
+ strftime(dst, BLOCK_SIZE, clock_fmt, localtime(&t));
}
void update() {
char block[BLOCK_SIZE];
status[0] = '\0';
- for(size_t i = 0; i < BLOCK_COUNT; i++) {
+ for(size_t i = 0; i < block_count; i++) {
switch(blockTypes[i]) {
case B_TAG:
update_tag(block);
@@ -92,8 +93,8 @@ void update() {
if(i == 0) {
strncpy(status, block, BLOCK_SIZE);
} else {
- strncpy(status_tmp, status, STATUS_SIZE);
- snprintf(status, STATUS_SIZE, "%s | %s", status_tmp, block);
+ strncpy(status_tmp, status, status_size);
+ snprintf(status, status_size, "%s | %s", status_tmp, block);
}
}
}
@@ -111,9 +112,17 @@ int main(int argc, char * argv[]) {
else if(strcmp(argv[i], "--version") == 0) print_version();
}
+ block_count = sizeof(blockTypes) / sizeof(enum BlockType);
+ status_size = (block_count + 1) * BLOCK_SIZE;
+ status = malloc(sizeof(char) * status_size);
+ status_tmp = malloc(sizeof(char) * status_size);
+
while(1) {
update();
print();
- sleep(INTERVAL);
+ sleep(interval);
}
+
+ free(status);
+ free(status_tmp);
}