#include "bar/config.h" #include "bar/event.h" #include "bar/modules.h" #include #include typedef struct { redraw_fn_t redraw; void *redraw_data; char time_str[32]; // "10:24 AM" char date_str[32]; // "Tuesday, 26/05" } clock_state_t; static void tick(clock_state_t *p) { time_t t = time(nullptr); struct tm tm = {0}; localtime_r(&t, &tm); const char *time_fmt = g_config.time_format_24h ? "%H:%M" : "%I:%M %p"; strftime(p->time_str, sizeof p->time_str, time_fmt, &tm); strftime(p->date_str, sizeof p->date_str, "%A, %d/%m", &tm); } static void clock_timer_fn(void *data) { module_t *mod = data; clock_state_t *c = mod->internal; tick(c); c->redraw(c->redraw_data); } static size_t clock_render(module_t *mod, segment_t *out) { clock_state_t *c = mod->internal; *out = (segment_t){.is_multi_line = true, .align_right = true, .top = c->time_str, .bottom = c->date_str, .top_fg = CLR_MUTED, .bottom_fg = CLR_FG, .bg = CLR_BG}; return 1; } static void clock_destroy(module_t *mod) { free(mod->internal); free(mod); } module_t *clock_module_new(redraw_fn_t fn, void *data) { module_t *mod = malloc(sizeof *mod); clock_state_t *c = malloc(sizeof *c); if (!mod || !c) { free(mod); free(c); return nullptr; } *c = (clock_state_t){.redraw = fn, .redraw_data = data}; *mod = (module_t){ .render = clock_render, .destroy = clock_destroy, .internal = c}; tick(c); event_loop_add_timer(g_config.interval_clock_ms, true, clock_timer_fn, mod); return mod; }