From c4d9f8a8ff4d65b43defb8d63e775e8b91f82bed Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 25 Jul 2024 19:24:28 +0200 Subject: osc: kitty notifications: implement the 'close' request Add a new config option, desktop-notifications.close, defining what to execute to close a notification. It has a single template parameter, ${id}, that is expanded to the external notification ID foot may have picked up from the notification helper. notify-send does not support closing notifications, and it appears impossible to pass an *unsigned* integer as argument to gdbus. Hence no default value for the new 'close' option. Example: printf '\e]99;i=123;this is a notification\e\\' printf '\e]99;i=123:p=close;\e\\' --- notify.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'notify.c') diff --git a/notify.c b/notify.c index 68ba83f3..fa547126 100644 --- a/notify.c +++ b/notify.c @@ -339,6 +339,51 @@ notify_notify(struct terminal *term, struct notification *notif) return true; } +void +notify_close(struct terminal *term, const char *id) +{ + LOG_DBG("close notification %s", id); + + if (term->conf->desktop_notifications.close.argv.args == NULL) + return; + + tll_foreach(term->active_notifications, it) { + const struct notification *notif = &it->item; + if (notif->id == 0 || !streq(notif->id, id)) + continue; + + if (notif->external_id == 0) + return; + + char **argv = NULL; + size_t argc = 0; + + char external_id[16]; + xsnprintf(external_id, sizeof(external_id), "%u", notif->external_id); + + if (!spawn_expand_template( + &term->conf->desktop_notifications.close, 1, + (const char *[]){"id"}, + (const char *[]){external_id}, + &argc, &argv)) + { + return; + } + + int devnull = open("/dev/null", O_RDONLY); + spawn( + term->reaper, NULL, argv, devnull, -1, -1, + NULL, (void *)term, NULL); + + if (devnull >= 0) + close(devnull); + + for (size_t i = 0; i < argc; i++) + free(argv[i]); + free(argv); + } +} + static void add_icon(struct notification_icon *icon, const char *id, const char *symbolic_name, const uint8_t *data, size_t data_sz) -- cgit v1.3