From f87c9bb9f70f1a6c8c7081c762c15ba7eaaf971e Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Sat, 3 Aug 2024 08:12:13 +0100 Subject: xmalloc: remove delim param from xstrjoin() and add separate xstrjoin3() This avoids the need for an unused third argument for most xstrjoin() calls and replaces the cases where it's needed with a more flexible function. Code generation is the same in both cases, when there are 2 string params and a compile-time known delimiter. This commit also converts 4 uses of xasprintf() to use xstrjoin*(). See also: https://godbolt.org/z/xsjrhv9b6 --- osc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'osc.c') diff --git a/osc.c b/osc.c index eb5e9718..92e3cad6 100644 --- a/osc.c +++ b/osc.c @@ -767,7 +767,7 @@ kitty_notification(struct terminal *term, char *string) else { /* Append, comma separated */ char *old_category = category; - category = xstrjoin(old_category, decoded, ','); + category = xstrjoin3(old_category, ",", decoded); free(decoded); free(old_category); } @@ -910,7 +910,7 @@ kitty_notification(struct terminal *term, char *string) category = NULL; /* Prevent double free */ } else { /* Append, comma separated */ - char *new_category = xstrjoin(notif->category, category, ','); + char *new_category = xstrjoin3(notif->category, ",", category); free(notif->category); notif->category = new_category; } @@ -929,7 +929,7 @@ kitty_notification(struct terminal *term, char *string) payload = NULL; } else { char *old = *ptr; - *ptr = xstrjoin(old, payload, 0); + *ptr = xstrjoin(old, payload); free(old); } break; @@ -1002,7 +1002,7 @@ kitty_notification(struct terminal *term, char *string) alive_ids = xstrdup(item_id); else { char *old_alive_ids = alive_ids; - alive_ids = xstrjoin(old_alive_ids, item_id, ','); + alive_ids = xstrjoin3(old_alive_ids, ",", item_id); free(old_alive_ids); } } -- cgit v1.3 From 6b72108ee220dd64a3ff5a43a6c84d99078da62e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 3 Aug 2024 11:05:58 +0200 Subject: osc: kitty notifications: ignore invalid IDs Notification IDs must only use characters from [a-zA-Z0-9_-+.] Terminals **must** sanitize ids received from client programs before sending them back in responses, to mitigate input injection based attacks. That is, they must either reject ids containing characters not from the above set, or remove bad characters when reading ids sent to them. Foot implements the first: reject IDs containing characters not from the above set. --- osc.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) (limited to 'osc.c') diff --git a/osc.c b/osc.c index eb5e9718..3ae8a051 100644 --- a/osc.c +++ b/osc.c @@ -564,6 +564,33 @@ osc_notify(struct terminal *term, char *string) }); } +IGNORE_WARNING("-Wpedantic") +static bool +verify_kitty_id_is_valid(const char *id) +{ + const size_t len = strlen(id); + + for (size_t i = 0; i < len; i++) { + switch (id[i]) { + case 'a' ... 'z': + case 'A' ... 'Z': + case '0' ... '9': + case '_': + case '-': + case '+': + case '.': + break; + + default: + return false; + } + } + + return true; +} +UNIGNORE_WARNINGS + + static void kitty_notification(struct terminal *term, char *string) { @@ -672,8 +699,11 @@ kitty_notification(struct terminal *term, char *string) case 'i': /* id */ - free(id); - id = xstrdup(value); + if (verify_kitty_id_is_valid(value)) { + free(id); + id = xstrdup(value); + } else + LOG_WARN("OSC-99: ignoring invalid 'i' identifier"); break; case 'p': @@ -963,7 +993,7 @@ kitty_notification(struct terminal *term, char *string) tll_push_back(notif->actions, xstrdup(button)); } } - + break; } } -- cgit v1.3 From 63492624918cd23a5efdd9b8a8b4f057da66d961 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 4 Aug 2024 14:16:56 +0200 Subject: osc: kitty notifications: implement s=silent This implements part of the new 's' (sound) parameter; the 'silent' value. When s=silent, we set the ${muted} template argument to "true". It is intended to set the 'suppress-sound' hint: notify-send --hint BOOLEAN:suppress-sound:${muted} --- config.c | 2 +- doc/foot.ini.5.scd | 6 ++++++ foot.ini | 2 +- notify.c | 13 ++++++++----- notify.h | 2 ++ osc.c | 29 +++++++++++++++++++++++++---- 6 files changed, 43 insertions(+), 11 deletions(-) (limited to 'osc.c') diff --git a/config.c b/config.c index bf3c4175..5dbbacb7 100644 --- a/config.c +++ b/config.c @@ -3242,7 +3242,7 @@ config_load(struct config *conf, const char *conf_path, parse_modifiers(XKB_MOD_NAME_SHIFT, 5, &conf->mouse.selection_override_modifiers); tokenize_cmdline( - "notify-send --wait --app-name ${app-id} --icon ${app-id} --category ${category} --urgency ${urgency} --expire-time ${expire-time} --hint STRING:image-path:${icon} --replace-id ${replace-id} ${action-argument} --print-id -- ${title} ${body}", + "notify-send --wait --app-name ${app-id} --icon ${app-id} --category ${category} --urgency ${urgency} --expire-time ${expire-time} --hint STRING:image-path:${icon} --hint BOOLEAN:suppress-sound:${muted} --replace-id ${replace-id} ${action-argument} --print-id -- ${title} ${body}", &conf->desktop_notifications.command.argv.args); tokenize_cmdline("--action ${action-name}=${action-label}", &conf->desktop_notifications.command_action_arg.argv.args); tokenize_cmdline("xdg-open ${url}", &conf->url.launch.argv.args); diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index 8f9ffe57..6db7ffae 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -459,6 +459,11 @@ Note: do not set *TERM* here; use the *term* option in the main below. Can be used together with e.g. notify-send's *--replace-id* option. + _${muted}_ is replaced by either *true* or *false*, depending + on whether the notification has requested all notification + sounds be muted. It is intended to set the *suppress-sound* + hint (with e.g. notify-send's *--hint* option). + _${action-argument}_ will be expanded to the *command-action-argument* option, for each notification action. There will always be at least one action, the @@ -568,6 +573,7 @@ xdgtoken=95ebdfe56e4f47ddb5bba9d7dc3a2c35 --urgency ${urgency}++ --expire-time ${expire-time}++ --hint STRING:image-path:${icon}++ + --hint BOOLEAN:suppress-sound:${muted}++ --replace-id ${replace-id}++ ${action-argument}++ --print-id++ diff --git a/foot.ini b/foot.ini index 029daa9b..fba9305e 100644 --- a/foot.ini +++ b/foot.ini @@ -47,7 +47,7 @@ # command-focused=no [desktop-notifications] -# command=notify-send --wait --app-name ${app-id} --icon ${app-id} --category ${category} --urgency ${urgency} --expire-time ${expire-time} --hint STRING:image-path:${icon} --replace-id ${replace-id} ${action-argument} --print-id -- ${title} ${body} +# command=notify-send --wait --app-name ${app-id} --icon ${app-id} --category ${category} --urgency ${urgency} --expire-time ${expire-time} --hint STRING:image-path:${icon} --hint BOOLEAN:suppress-sound:${muted} --replace-id ${replace-id} ${action-argument} --print-id -- ${title} ${body} # command-action-argument=--action ${action-name}=${action-label} # close="" # inhibit-when-focused=yes diff --git a/notify.c b/notify.c index 15139c40..0b37f40a 100644 --- a/notify.c +++ b/notify.c @@ -421,9 +421,12 @@ notify_notify(struct terminal *term, struct notification *notif) ? "normal" : "critical"; LOG_DBG("notify: title=\"%s\", body=\"%s\", app-id=\"%s\", category=\"%s\", " - "urgency=\"%s\", icon=\"%s\", expires=%d, replaces=%u (tracking: %s)", + "urgency=\"%s\", icon=\"%s\", expires=%d, replaces=%u, muted=%s " + "(tracking: %s)", title, body, app_id, notif->category, urgency_str, icon_name_or_path, - notif->expire_time, replaces_id, track_notification ? "yes" : "no"); + notif->expire_time, replaces_id, + notif->muted ? "yes" : "no", + track_notification ? "yes" : "no"); xassert(title != NULL); if (title == NULL) @@ -463,14 +466,14 @@ notify_notify(struct terminal *term, struct notification *notif) } if (!spawn_expand_template( - &term->conf->desktop_notifications.command, 10, + &term->conf->desktop_notifications.command, 11, (const char *[]){ "app-id", "window-title", "icon", "title", "body", "category", - "urgency", "expire-time", "replace-id", "action-argument"}, + "urgency", "muted", "expire-time", "replace-id", "action-argument"}, (const char *[]){ app_id, term->window_title, icon_name_or_path, title, body, notif->category != NULL ? notif->category : "", urgency_str, - expire_time, replaces_id_str, + notif->muted ? "true" : "false", expire_time, replaces_id_str, /* Custom expansion below, since we need to expand to multiple arguments */ "${action-argument}"}, diff --git a/notify.h b/notify.h index 592d2c84..ccdfdea8 100644 --- a/notify.h +++ b/notify.h @@ -52,6 +52,8 @@ struct notification { bool report_activated; /* OSC-99: report notification activation to client */ bool report_closed; /* OSC-99: report notification closed to client */ + bool muted; /* Explicitly mute the notification */ + /* * Used internally by notify */ diff --git a/osc.c b/osc.c index 28d8b7b8..50415570 100644 --- a/osc.c +++ b/osc.c @@ -609,6 +609,7 @@ kitty_notification(struct terminal *term, char *string) char *icon_cache_id = NULL; /* The 'g' parameter */ char *symbolic_icon = NULL; /* The 'n' parameter */ char *category = NULL; /* The 't' parameter */ + char *sound_name = NULL; /* The 's' parameter */ char *payload = NULL; bool focus = true; /* The 'a' parameter */ @@ -739,7 +740,7 @@ kitty_notification(struct terminal *term, char *string) char reply[128]; int n = xsnprintf( reply, sizeof(reply), - "\033]99;i=%s:p=?;p=%s:a=%s:o=%s:u=%s:c=1:w=1%s", + "\033]99;i=%s:p=?;p=%s:a=%s:o=%s:u=%s:c=1:w=1:s=silent%s", reply_id, p_caps, a_caps, when_caps, u_caps, terminator); xassert(n < sizeof(reply)); @@ -783,10 +784,15 @@ kitty_notification(struct terminal *term, char *string) break; } - case 'f': - free(app_id); - app_id = base64_decode(value, NULL); + case 'f': { + /* App-name */ + char *decoded = base64_decode(value, NULL); + if (decoded != NULL) { + free(app_id); + app_id = decoded; + } break; + } case 't': { /* Type (category) */ @@ -805,6 +811,16 @@ kitty_notification(struct terminal *term, char *string) break; } + case 's': { + /* Sound */ + char *decoded = base64_decode(value, NULL); + if (decoded != NULL) { + free(sound_name); + sound_name = decoded; + } + break; + } + case 'g': /* graphical ID (see 'n' and 'p=icon') */ free(icon_cache_id); @@ -946,6 +962,10 @@ kitty_notification(struct terminal *term, char *string) } } + if (sound_name != NULL) { + notif->muted = streq(sound_name, "silent"); + } + /* Handled chunked payload - append to existing metadata */ switch (payload_type) { case PAYLOAD_TITLE: @@ -1067,6 +1087,7 @@ out: free(symbolic_icon); free(payload); free(category); + free(sound_name); } void -- cgit v1.3 From 84d36606cbd3e35aae6a68e69101b22da60dbe99 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 4 Aug 2024 15:21:06 +0200 Subject: osc: kitty notifications: add support for XDG sound names --- config.c | 2 +- doc/foot.ini.5.scd | 7 +++++++ foot.ini | 2 +- notify.c | 17 +++++++++++------ notify.h | 1 + osc.c | 11 ++++++++++- 6 files changed, 31 insertions(+), 9 deletions(-) (limited to 'osc.c') diff --git a/config.c b/config.c index 5dbbacb7..1ec73da0 100644 --- a/config.c +++ b/config.c @@ -3242,7 +3242,7 @@ config_load(struct config *conf, const char *conf_path, parse_modifiers(XKB_MOD_NAME_SHIFT, 5, &conf->mouse.selection_override_modifiers); tokenize_cmdline( - "notify-send --wait --app-name ${app-id} --icon ${app-id} --category ${category} --urgency ${urgency} --expire-time ${expire-time} --hint STRING:image-path:${icon} --hint BOOLEAN:suppress-sound:${muted} --replace-id ${replace-id} ${action-argument} --print-id -- ${title} ${body}", + "notify-send --wait --app-name ${app-id} --icon ${app-id} --category ${category} --urgency ${urgency} --expire-time ${expire-time} --hint STRING:image-path:${icon} --hint BOOLEAN:suppress-sound:${muted} --hint STRING:sound-name:${sound-name} --replace-id ${replace-id} ${action-argument} --print-id -- ${title} ${body}", &conf->desktop_notifications.command.argv.args); tokenize_cmdline("--action ${action-name}=${action-label}", &conf->desktop_notifications.command_action_arg.argv.args); tokenize_cmdline("xdg-open ${url}", &conf->url.launch.argv.args); diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index 6db7ffae..71992c8f 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -464,6 +464,12 @@ Note: do not set *TERM* here; use the *term* option in the main sounds be muted. It is intended to set the *suppress-sound* hint (with e.g. notify-send's *--hint* option). + _${sound-name}_ is replaced by sound-name requested by the + notification. This should be a name from the freedesktop sound + naming specification, but this is not something that foot + enforces. It is intended to set the *sound-name* hint (with + e.g. notify-send's *--hint* option). + _${action-argument}_ will be expanded to the *command-action-argument* option, for each notification action. There will always be at least one action, the @@ -574,6 +580,7 @@ xdgtoken=95ebdfe56e4f47ddb5bba9d7dc3a2c35 --expire-time ${expire-time}++ --hint STRING:image-path:${icon}++ --hint BOOLEAN:suppress-sound:${muted}++ + --hint STRING:sound-name:${sound-name}++ --replace-id ${replace-id}++ ${action-argument}++ --print-id++ diff --git a/foot.ini b/foot.ini index fba9305e..9e2f5f29 100644 --- a/foot.ini +++ b/foot.ini @@ -47,7 +47,7 @@ # command-focused=no [desktop-notifications] -# command=notify-send --wait --app-name ${app-id} --icon ${app-id} --category ${category} --urgency ${urgency} --expire-time ${expire-time} --hint STRING:image-path:${icon} --hint BOOLEAN:suppress-sound:${muted} --replace-id ${replace-id} ${action-argument} --print-id -- ${title} ${body} +# command=notify-send --wait --app-name ${app-id} --icon ${app-id} --category ${category} --urgency ${urgency} --expire-time ${expire-time} --hint STRING:image-path:${icon} --hint BOOLEAN:suppress-sound:${muted} --hint STRING:sound-name:${sound-name} --replace-id ${replace-id} ${action-argument} --print-id -- ${title} ${body} # command-action-argument=--action ${action-name}=${action-label} # close="" # inhibit-when-focused=yes diff --git a/notify.c b/notify.c index 0b37f40a..d1c06fb1 100644 --- a/notify.c +++ b/notify.c @@ -34,6 +34,7 @@ notify_free(struct terminal *term, struct notification *notif) free(notif->icon_cache_id); free(notif->icon_symbolic_name); free(notif->icon_data); + free(notif->sound_name); free(notif->xdg_token); free(notif->stdout_data); @@ -421,11 +422,11 @@ notify_notify(struct terminal *term, struct notification *notif) ? "normal" : "critical"; LOG_DBG("notify: title=\"%s\", body=\"%s\", app-id=\"%s\", category=\"%s\", " - "urgency=\"%s\", icon=\"%s\", expires=%d, replaces=%u, muted=%s " - "(tracking: %s)", + "urgency=\"%s\", icon=\"%s\", expires=%d, replaces=%u, muted=%s, " + "sound-name=%s (tracking: %s)", title, body, app_id, notif->category, urgency_str, icon_name_or_path, notif->expire_time, replaces_id, - notif->muted ? "yes" : "no", + notif->muted ? "yes" : "no", notif->sound_name, track_notification ? "yes" : "no"); xassert(title != NULL); @@ -466,14 +467,17 @@ notify_notify(struct terminal *term, struct notification *notif) } if (!spawn_expand_template( - &term->conf->desktop_notifications.command, 11, + &term->conf->desktop_notifications.command, 12, (const char *[]){ "app-id", "window-title", "icon", "title", "body", "category", - "urgency", "muted", "expire-time", "replace-id", "action-argument"}, + "urgency", "muted", "sound-name", "expire-time", "replace-id", + "action-argument"}, (const char *[]){ app_id, term->window_title, icon_name_or_path, title, body, notif->category != NULL ? notif->category : "", urgency_str, - notif->muted ? "true" : "false", expire_time, replaces_id_str, + notif->muted ? "true" : "false", + notif->sound_name != NULL ? notif->sound_name : "", + expire_time, replaces_id_str, /* Custom expansion below, since we need to expand to multiple arguments */ "${action-argument}"}, @@ -545,6 +549,7 @@ notify_notify(struct terminal *term, struct notification *notif) notif->icon_data = NULL; notif->icon_data_sz = 0; notif->icon_path = NULL; + notif->sound_name = NULL; notif->icon_fd = -1; notif->stdout_fd = -1; struct notification *new_notif = &tll_back(term->active_notifications); diff --git a/notify.h b/notify.h index ccdfdea8..89b51238 100644 --- a/notify.h +++ b/notify.h @@ -53,6 +53,7 @@ struct notification { bool report_closed; /* OSC-99: report notification closed to client */ bool muted; /* Explicitly mute the notification */ + char *sound_name; /* Should be set to NULL if muted == true */ /* * Used internally by notify diff --git a/osc.c b/osc.c index 50415570..eba1850f 100644 --- a/osc.c +++ b/osc.c @@ -740,7 +740,7 @@ kitty_notification(struct terminal *term, char *string) char reply[128]; int n = xsnprintf( reply, sizeof(reply), - "\033]99;i=%s:p=?;p=%s:a=%s:o=%s:u=%s:c=1:w=1:s=silent%s", + "\033]99;i=%s:p=?;p=%s:a=%s:o=%s:u=%s:c=1:w=1:s=silent,xdg-names%s", reply_id, p_caps, a_caps, when_caps, u_caps, terminator); xassert(n < sizeof(reply)); @@ -964,6 +964,15 @@ kitty_notification(struct terminal *term, char *string) if (sound_name != NULL) { notif->muted = streq(sound_name, "silent"); + + if (notif->muted || streq(sound_name, "system")) { + free(notif->sound_name); + notif->sound_name = NULL; + } else { + free(notif->sound_name); + notif->sound_name = sound_name; + sound_name = NULL; /* Prevent double free */ + } } /* Handled chunked payload - append to existing metadata */ -- cgit v1.3 From ee9c76ded00de9beac698d2bd4771e8c1d966c6a Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 9 Aug 2024 08:25:36 +0200 Subject: osc: kitty: update 's' to the latest spec * The spec now defines a couple of "standard" names. Translate these to the freedesktop compliant names. * The query response no longer contains 'xdg-names', but instead list the supported standard names. --- osc.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'osc.c') diff --git a/osc.c b/osc.c index eba1850f..53597d0f 100644 --- a/osc.c +++ b/osc.c @@ -740,7 +740,7 @@ kitty_notification(struct terminal *term, char *string) char reply[128]; int n = xsnprintf( reply, sizeof(reply), - "\033]99;i=%s:p=?;p=%s:a=%s:o=%s:u=%s:c=1:w=1:s=silent,xdg-names%s", + "\033]99;i=%s:p=?;p=%s:a=%s:o=%s:u=%s:c=1:w=1:s=system,silent,error,warn,warning,info,question%s", reply_id, p_caps, a_caps, when_caps, u_caps, terminator); xassert(n < sizeof(reply)); @@ -817,6 +817,22 @@ kitty_notification(struct terminal *term, char *string) if (decoded != NULL) { free(sound_name); sound_name = decoded; + + const char *translated_name = NULL; + + if (streq(decoded, "error")) + translated_name = "dialog-error"; + else if (streq(decoded, "warn") || streq(decoded, "warning")) + translated_name = "dialog-warning"; + else if (streq(decoded, "info")) + translated_name = "dialog-information"; + else if (streq(decoded, "question")) + translated_name = "dialog-question"; + + if (translated_name != NULL) { + free(sound_name); + sound_name = xstrdup(translated_name); + } } break; } -- cgit v1.3 From 2896c1898127256193d49c80a56ba7188fad0e33 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 14 Aug 2024 08:50:44 +0200 Subject: osc: regression: fix OSC-111 handling of alpha changes When the background alpha changes from fully opaque, to transparent, or vice versa, we need to do more than just repaint the affected cells. For example, we need to create new surfaces with the correct pixel format. OSC-11 (set background color) already does this, but the same alpha checking logic was missing in OSC-111 (reset background color). Fixes #1801 --- CHANGELOG.md | 7 +++++++ osc.c | 11 ++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) (limited to 'osc.c') diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d7d6614..fe82d2ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,6 +72,13 @@ ### Deprecated ### Removed ### Fixed + +* Regression: OSC-111 not handling alpha changes correctly, causing + visual glitches ([#1801][1801]). + +[1801]: https://codeberg.org/dnkl/foot/issues/1801 + + ### Security ### Contributors diff --git a/osc.c b/osc.c index 53597d0f..3c5f7616 100644 --- a/osc.c +++ b/osc.c @@ -1381,13 +1381,22 @@ osc_dispatch(struct terminal *term) term_damage_color(term, COLOR_DEFAULT, 0); break; - case 111: /* Reset default text background color */ + case 111: { /* Reset default text background color */ LOG_DBG("resetting background color"); + bool alpha_changed = term->colors.alpha != term->conf->colors.alpha; + term->colors.bg = term->conf->colors.bg; term->colors.alpha = term->conf->colors.alpha; + + if (alpha_changed) { + wayl_win_alpha_changed(term->window); + term_font_subpixel_changed(term); + } + term_damage_color(term, COLOR_DEFAULT, 0); term_damage_margins(term); break; + } case 112: LOG_DBG("resetting cursor color"); -- cgit v1.3