From 0568c7d0ad05b7fc06775834e2a96089103b5d5e Mon Sep 17 00:00:00 2001 From: delthas Date: Wed, 6 Sep 2023 18:33:29 +0200 Subject: [PATCH] introduce rate-limiting timer for setting the app ID --- osc.c | 2 +- terminal.c | 29 +++++++++++++++++++++++++++++ terminal.h | 3 +++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/osc.c b/osc.c index 7b0e38a7..8d554baa 100644 --- a/osc.c +++ b/osc.c @@ -911,7 +911,7 @@ osc_dispatch(struct terminal *term) break; case 176: - xdg_toplevel_set_app_id(term->window->xdg_toplevel, *string ? string : term->conf->app_id); + term_set_app_id(term, string); break; case 555: diff --git a/terminal.c b/terminal.c index f19da873..8259850e 100644 --- a/terminal.c +++ b/terminal.c @@ -1662,6 +1662,7 @@ term_destroy(struct terminal *term) composed_free(term->composed); + free(term->app_id); free(term->window_title); tll_free_and_free(term->window_title_stack, free); @@ -3246,6 +3247,34 @@ term_set_window_title(struct terminal *term, const char *title) term->window_title_has_been_set = true; } +void +term_set_app_id(struct terminal *term, const char *app_id) +{ + if (app_id != NULL && *app_id == '\0') + app_id = NULL; + if (term->app_id == NULL && app_id == NULL) + return; + if (term->app_id != NULL && app_id != NULL && strcmp(term->app_id, app_id) == 0) + return; + + struct timespec now; + if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) + return; + struct timespec diff; + timespec_sub(&now, &term->app_id_last_update, &diff); + if (diff.tv_sec == 0 && diff.tv_nsec < 8333 * 1000) + return; + term->app_id_last_update = now; + + free(term->app_id); + if (app_id != NULL) { + term->app_id = xstrdup(app_id); + } else { + term->app_id = NULL; + } + xdg_toplevel_set_app_id(term->window->xdg_toplevel, term->app_id ? term->app_id : term->conf->app_id); +} + void term_flash(struct terminal *term, unsigned duration_ms) { diff --git a/terminal.h b/terminal.h index 0bba6945..a34968f2 100644 --- a/terminal.h +++ b/terminal.h @@ -477,6 +477,8 @@ struct terminal { bool window_title_has_been_set; char *window_title; tll(char *) window_title_stack; + char *app_id; + struct timespec app_id_last_update; struct { bool active; @@ -824,6 +826,7 @@ void term_xcursor_update_for_seat(struct terminal *term, struct seat *seat); void term_set_user_mouse_cursor(struct terminal *term, const char *cursor); void term_set_window_title(struct terminal *term, const char *title); +void term_set_app_id(struct terminal *term, const char *app_id); void term_flash(struct terminal *term, unsigned duration_ms); void term_bell(struct terminal *term); bool term_spawn_new(const struct terminal *term);