status

statusbar program for dwm
git clone anongit@rnpnr.xyz:status.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 4f40a9da065ed4530e34eae267193e936086e99e
Parent: 16a5531598f1ec0ffa745277dd5699fb79afab47
Author: Randy Palamar
Date:   Fri, 27 Dec 2024 14:48:31 -0700

move timer check outside block update function

This means that every block must store a timer even when it isn't
used but simplifies the rest of the code. No extra memory is used
since we don't really need 64 bits to store the block len.

Diffstat:
Mblocks/date.c | 21++++-----------------
Mblocks/linux/backlight.c | 7+------
Mblocks/linux/battery_info.c | 9++-------
Mconfig.def.h | 14++++++--------
Mstatus.c | 39++++++++++++++++++++++++---------------
5 files changed, 37 insertions(+), 53 deletions(-)

diff --git a/blocks/date.c b/blocks/date.c @@ -1,25 +1,12 @@ /* See LICENSE for license details. */ -struct date_arg { - char *fmt; /* Time Format String (ex: %R) */ - f32 interval; /* Update Interval [s] */ -}; - static BLOCK_UPDATE_FN(date_update) { - struct date_arg *da = b->arg; - - b32 result = timer_update(b->user_data, da->interval, dt); - if (result) { - time_t t = time(NULL); - strftime(buffer, sizeof(buffer), da->fmt, localtime(&t)); - b->len = snprintf(b->data, sizeof(b->data), b->fmt, buffer); - } - - return result; + time_t t = time(NULL); + strftime(buffer, sizeof(buffer), b->arg, localtime(&t)); + b->len = snprintf(b->data, sizeof(b->data), b->fmt, buffer); } static BLOCK_INIT_FN(date_init) { - b->user_data = alloc(a, f32, 1); - date_update(b, 1); + date_update(b); } diff --git a/blocks/linux/backlight.c b/blocks/linux/backlight.c @@ -3,17 +3,12 @@ struct linux_backlight_data {i64 max_brightness; char *brightness_path;}; static BLOCK_UPDATE_FN(backlight_update) { - if (dt > 0) - return 0; - struct linux_backlight_data *lbd = b->user_data; i64 current = read_i64(lbd->brightness_path); f32 percent = 100 * current / (f32)lbd->max_brightness + 0.5; i64 len = snprintf(buffer, sizeof(buffer), "%d%%", (i32)percent); buffer[len] = 0; b->len = snprintf(b->data, sizeof(b->data), b->fmt, buffer); - - return 1; } static BLOCK_INIT_FN(backlight_init) @@ -37,6 +32,6 @@ static BLOCK_INIT_FN(backlight_init) lbd->brightness_path = (char *)path.buffer; a->beg += path.write_index; - backlight_update(b, 0); + backlight_update(b); add_file_watch(a, lbd->brightness_path, block_index, backlight_update); } diff --git a/blocks/linux/battery_info.c b/blocks/linux/battery_info.c @@ -7,16 +7,13 @@ struct bat_arg { f32 interval; /* [s] */ }; -struct linux_battery_data { Stream path_base; i64 energy_full; f32 timer; }; +struct linux_battery_data { Stream path_base; i64 energy_full; }; static BLOCK_UPDATE_FN(battery_info_update) { struct bat_arg *ba = b->arg; struct linux_battery_data *lbd = b->user_data; - if (!timer_update(&lbd->timer, ba->interval, dt)) - return 0; - char *pre = ba->pre ? ba->pre : ""; char *suf = ba->suf ? ba->suf : ""; @@ -63,8 +60,6 @@ static BLOCK_UPDATE_FN(battery_info_update) } buffer[len] = 0; b->len = snprintf(b->data, sizeof(b->data), b->fmt, buffer); - - return 1; } #define LINUX_BAT_INFO_STRS \ @@ -96,5 +91,5 @@ static BLOCK_INIT_FN(battery_info_init) die("battery_info_init: failed to read battery capacity\n"); lbd->path_base.write_index = sidx; - battery_info_update(b, 1); + battery_info_update(b); } diff --git a/config.def.h b/config.def.h @@ -14,10 +14,7 @@ // static struct vol_arg va = { "default", "Speaker" }; /* check blocks/xxx/battery_info.c for info */ -static struct bat_arg ba = {.bat = s8("BAT0"), .interval = 30}; - -/* check blocks/date.c for info */ -static struct date_arg da = {.fmt = "%R", .interval = 30}; +static struct bat_arg ba = {.bat = s8("BAT0")}; /* backlight name (/sys/class/backlight/xxx) */ //static s8 linux_backlight = s8("xxx"); @@ -28,10 +25,11 @@ static struct date_arg da = {.fmt = "%R", .interval = 30}; * * battery_info battery percentage and status (struct bat_arg *) * backlight percentage (s8 *) backlight name - * date date and time (struct date_arg *) + * date date and time (char *) fmt ("%R") */ -/* NOTE: X(name, statusline_format, argument) */ +/* NOTE: X(name, statusline_format, interval, argument) * + * interval == 0 means never update from timer */ #define BLOCKS \ - X(battery_info, "[ %s ]", &ba) \ - X(date, "[ %s ]", &da) + X(battery_info, "[ %s ]", 30, &ba) \ + X(date, "[ %s ]", 30, "%R") diff --git a/status.c b/status.c @@ -80,12 +80,13 @@ typedef struct { void *user_data; void *arg; char *fmt; + f32 timer; + u32 len; char data[BLOCKLEN]; - size len; } Block; #define BLOCK_INIT_FN(name) void name(Block *b, i32 block_index, Arena *a) -#define BLOCK_UPDATE_FN(name) b32 name(Block *b, f32 dt) +#define BLOCK_UPDATE_FN(name) void name(Block *b) typedef BLOCK_UPDATE_FN(block_update_fn); typedef struct FileWatch { @@ -128,15 +129,6 @@ get_time(void) return result; } -static b32 -timer_update(f32 *timer, f32 interval, f32 dt) -{ - b32 result = dt <= 0; - *timer -= dt; - while (*timer < 0) { *timer += interval; result = 1; } - return result; -} - static s8 read_s8(char *path, s8 buffer) { @@ -248,7 +240,7 @@ add_file_watch(Arena *a, char *path, i32 block_index, block_update_fn *update_fn #include "config.h" -#define X(name, format, argument) {.fmt = format, .arg = argument}, +#define X(name, format, interval, argument) {.fmt = format, .arg = argument}, static Block blocks[] = { BLOCKS }; @@ -294,7 +286,8 @@ dispatch_file_watch_events(Arena a) b32 file_changed = (ie->mask & IN_CLOSE_WRITE) != 0; file_changed |= (ie->mask & IN_MODIFY) != 0; /* TODO(rnp): it seems like this hits multiple times per update */ - if (file_changed && fw->update_fn(blocks + fw->block_index, 0)) { + if (file_changed) { + fw->update_fn(blocks + fw->block_index); update_dirty_block_index(fw->block_index); } } @@ -325,11 +318,27 @@ update_status(void) } } +static b32 +timer_update(f32 *timer, f32 interval, f32 dt) +{ + b32 result = dt <= 0; + if (interval > 0) { + *timer -= dt; + while (*timer < 0) { *timer += interval; result = 1; } + } + return result; +} + static void update_blocks(f32 dt) { i32 count = 0; - #define X(name, fmt, args) if (name ##_update(blocks + count++, dt)) update_dirty_block_index(count - 1); + #define X(name, fmt, interval, args) \ + if (timer_update(&blocks[count].timer, interval, dt)) { \ + name ##_update(blocks + count); \ + update_dirty_block_index(count); \ + } \ + count++; BLOCKS #undef X } @@ -351,7 +360,7 @@ status_init(Arena *a) file_watches.wd = inotify_init1(O_NONBLOCK|O_CLOEXEC); i32 count = 0; - #define X(name, fmt, arg) name ##_init(blocks + count, count, a); count++; + #define X(name, fmt, interval, arg) name ##_init(blocks + count, count, a); count++; BLOCKS #undef X