From e9bbe8178ab45cb9880c9b6b0324e9b4467f26d5 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Sat, 25 Nov 2023 18:42:33 +0100 Subject: Recalculate layouts only when needed --- src/comet.c | 27 +++++++++++++++++++++------ src/connect.c | 12 ++---------- src/state.c | 13 ++++++++++--- src/state.h | 2 +- 4 files changed, 34 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/comet.c b/src/comet.c index fd393bc..45de1f6 100644 --- a/src/comet.c +++ b/src/comet.c @@ -43,14 +43,17 @@ static gboolean disk_update(gpointer data) struct statvfs buffer; g_assert(statvfs("/", &buffer) == 0); - const double used = 1.0 - ((double)buffer.f_bavail / (double)buffer.f_blocks); + size_t len1 = g_utf8_strlen(btn->text, -1); + g_free(btn->text); btn->text = g_strdup_printf(" %d%%", (int)round(used * 100.0)); + size_t len2 = g_utf8_strlen(btn->text, -1); + log_debug("Updated disk percentage"); - state_redraw(btn->action_data); + state_redraw(btn->action_data, len1 != len2); return G_SOURCE_CONTINUE; } @@ -86,9 +89,13 @@ static gboolean cpu_update(gpointer data) char *perc = cpu_percentage(); // Don't update on error if (perc != NULL) { + size_t len1 = g_utf8_strlen(btn->text, -1); + g_free(btn->text); btn->text = perc; - state_redraw(btn->action_data); + + size_t len2 = g_utf8_strlen(btn->text, -1); + state_redraw(btn->action_data, len1 != len2); } log_debug("Updated cpu percentage"); @@ -111,11 +118,15 @@ static gboolean ram_update(gpointer data) &total, &unused, &buffers, &buffers, &cached)); fclose(meminfo); + size_t len1 = g_utf8_strlen(btn->text, -1); + g_free(btn->text); btn->text = g_strdup_printf(" %d%%", (int)(100 * (total - unused - buffers - cached) / total)); + size_t len2 = g_utf8_strlen(btn->text, -1); + log_debug("Updated ram percentage"); - state_redraw(btn->action_data); + state_redraw(btn->action_data, len1 != len2); return G_SOURCE_CONTINUE; } @@ -123,11 +134,15 @@ static gboolean date_update(gpointer data) { Button *btn = data; + size_t len1 = g_utf8_strlen(btn->text, -1); g_free(btn->text); + GDateTime *time = g_date_time_new_now_local(); char *date = g_date_time_format(time, "%A %d %H:%M"); g_assert(date != NULL); + btn->text = date; + size_t len2 = g_utf8_strlen(btn->text, -1); struct { State *state; @@ -135,7 +150,7 @@ static gboolean date_update(gpointer data) } *date_ctx = btn->action_data; log_debug("Updated date and time"); - state_redraw(date_ctx->state); + state_redraw(date_ctx->state, len1 != len2); struct timespec current; clock_gettime(CLOCK_REALTIME, ¤t); @@ -251,7 +266,7 @@ int main(int argc, char **argv) guint source_term = g_unix_signal_add(SIGTERM, mainloop_quit, mainloop); guint source_int = g_unix_signal_add(SIGINT, mainloop_quit, mainloop); - state_redraw(state); + state_redraw(state, true); log_debug("Starting main loop"); g_main_loop_run(mainloop); diff --git a/src/connect.c b/src/connect.c index 083d5a7..fa19ffb 100644 --- a/src/connect.c +++ b/src/connect.c @@ -126,12 +126,6 @@ static gboolean source_check(GSource *source) return xsource->event != NULL; } -static void redraw(State *state) -{ - draw_compute_layout(state->draw, state->win, state->btns); - draw_paint(state->draw, state->win); -} - static gboolean source_dispatch(GSource *source, GSourceFunc callback, gpointer data) { EventSource *xsource = (EventSource *)source; @@ -162,8 +156,7 @@ static gboolean source_dispatch(GSource *source, GSourceFunc callback, gpointer xcb_expose_event_t *expose = (xcb_expose_event_t *)xsource->event; log_debug("Processing event 'Expose' [type=%d]", XCB_EXPOSE); - // Redraw - redraw(xsource->con->state); + state_redraw(xsource->con->state, true); break; } @@ -203,8 +196,7 @@ static gboolean source_dispatch(GSource *source, GSourceFunc callback, gpointer update_xrm(xsource->con); update_scale(xsource->con); - // Redraw - redraw(xsource->con->state); + state_redraw(xsource->con->state, true); } break; } diff --git a/src/state.c b/src/state.c index 64c479b..11ecd77 100644 --- a/src/state.c +++ b/src/state.c @@ -19,7 +19,7 @@ void state_add_button(State *state, Button *btn) state->btns = g_list_append(state->btns, btn); } -static gboolean redraw(gpointer data) +static gboolean redraw_and_layout(gpointer data) { State *state = data; draw_compute_layout(state->draw, state->win, state->btns); @@ -27,9 +27,16 @@ static gboolean redraw(gpointer data) return G_SOURCE_REMOVE; } -void state_redraw(State *state) +static gboolean redraw(gpointer data) +{ + State *state = data; + draw_paint(state->draw, state->win); + return G_SOURCE_REMOVE; +} + +void state_redraw(State *state, bool changed_layout) { - g_idle_add_full(G_PRIORITY_HIGH_IDLE, redraw, state, NULL); + g_idle_add_full(G_PRIORITY_HIGH_IDLE, changed_layout ? redraw_and_layout : redraw, state, NULL); } void state_destroy(State *state) diff --git a/src/state.h b/src/state.h index 329b6a3..a8bc0eb 100644 --- a/src/state.h +++ b/src/state.h @@ -19,7 +19,7 @@ State *state_create(Window *win, Drawable *draw); void state_add_button(State *state, Button *btn); -void state_redraw(State *state); +void state_redraw(State *state, bool changed_layout); void state_destroy(State *state); -- cgit v1.2.3