osc: implement OSC-5522, kitty's extended version of OSC-52

OSC-5522[^1] gives the terminal application access to all offered
mime-types when reading the clipboard, and allows setting multiple
mime-types when writing to the clipboard (with different contents, if
it so wishes).

In addition to the base protocol, we also implement the _event
extension_[^2], where a paste action (e.g. ctrl+shift+v) results in an
unsolicited mime-type listing being sent to the terminal
application (as if it had issued a mime-type query itself), instead of
the clipboard content being pasted directly. The application follows
up with an explicit read request (or chooses to ignore the event).

The protocol supports "passwords", as a way of bypassing terminal
popups asking the user for approval, after the first popup has been
approved by the user. Foot doesn't implement this kind of user
approval, but all read and write requests are denied with EPERM if the
user has disabled OSC copy/pasting with the security.osc52
configuration option. In addition, if disabled, event reporting cannot
be enabled at all (i.e. 'CSI ? 5522 h' is ignored).

[^1]: https://sw.kovidgoyal.net/kitty/clipboard/
[^2]: https://rockorager.dev/misc/bracketed-paste-mime/
This commit is contained in:
Daniel Eklöf 2026-05-15 18:35:51 +02:00
parent 4bc8a39d6c
commit c366e322eb
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
13 changed files with 839 additions and 126 deletions

View file

@ -33,7 +33,7 @@
#include "ime.h"
#include "input.h"
#include "notify.h"
#include "quirks.h"
#include "osc.h"
#include "reaper.h"
#include "render.h"
#include "selection.h"
@ -1939,6 +1939,8 @@ term_destroy(struct terminal *term)
for (size_t i = 0; i < ALEN(term->notification_icons); i++)
notify_icon_free(&term->notification_icons[i]);
kitty_clipboard_reset(term);
sixel_fini(term);
term_ime_reset(term);
@ -2167,6 +2169,8 @@ term_reset(struct terminal *term, bool hard)
for (size_t i = 0; i < ALEN(term->notification_icons); i++)
notify_icon_free(&term->notification_icons[i]);
kitty_clipboard_reset(term);
term->grapheme_shaping = term->conf->tweak.grapheme_shaping;
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
@ -4830,3 +4834,28 @@ term_theme_get(const struct terminal *term)
? &term->conf->colors_dark
: &term->conf->colors_light;
}
bool
term_osc_paste_allowed(const struct terminal *term)
{
return term->conf->security.osc52 == OSC52_ENABLED
|| term->conf->security.osc52 == OSC52_PASTE_ENABLED;
}
bool
term_osc_copy_allowed(const struct terminal *term)
{
return term->conf->security.osc52 == OSC52_ENABLED
|| term->conf->security.osc52 == OSC52_COPY_ENABLED;
}
struct seat *
term_first_focused_seat(struct terminal *term)
{
tll_foreach(term->wl->seats, it) {
if (it->item.kbd_focus == term)
return &it->item;
}
return NULL;
}