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

@ -398,6 +398,19 @@ struct colors {
enum which_color_theme active_theme;
};
struct kitty_mime_data {
char *mime_type;
uint8_t *data;
size_t data_len;
};
typedef tll(struct kitty_mime_data) kitty_mime_data_list_t;
struct kitty_mime_alias {
char *target;
char *alias;
};
typedef tll(struct kitty_mime_alias) kitty_mime_alias_list_t;
struct terminal {
struct fdm *fdm;
struct reaper *reaper;
@ -542,6 +555,7 @@ struct terminal {
bool report_theme_changes:1;
bool size_notifications:1;
bool kitty_clipboard:1;
bool sixel_display_mode:1;
bool sixel_private_palette:1;
@ -822,6 +836,30 @@ struct terminal {
/* State, to handle chunked notifications */
struct notification kitty_notification;
/* OSC-5522 */
struct {
bool emit_events; /* Enabled/disabled via private mode 5522 */
/*
* State for setting the clipboard (OSC-5522;type=write|wdata|walias)
*/
bool for_primary;
bool has_error; /* When set, all subsequent wdata|walias packets are ignored */
const char *error; /* EIO|EINVAL|ENOSYS|EPERM|EBUSY */
/* Current mime-type being collected (via multiple wdata packets) */
char *active_mime_type;
uint8_t *data;
size_t data_len;
/* mime-type aliases, collected via one or more walias packets */
kitty_mime_alias_list_t mime_aliases;
/* Finished mime-types (will be written to the clipboard when
* the final wdata packet is received) */
kitty_mime_data_list_t committed_mime_data;
} kitty_clipboard;
/* Currently active notifications, from foot's perspective (their
notification helper processes are still running) */
tll(struct notification) active_notifications;
@ -990,6 +1028,12 @@ void term_theme_switch_to_light(struct terminal *term);
void term_theme_toggle(struct terminal *term);
const struct color_theme *term_theme_get(const struct terminal *term);
bool term_osc_paste_allowed(const struct terminal *term);
bool term_osc_copy_allowed(const struct terminal *term);
/* Get (first) seat that is focusing this terminal instance */
struct seat *term_first_focused_seat(struct terminal *term);
static inline void term_reset_grapheme_state(struct terminal *term)
{
#if defined(FOOT_GRAPHEME_CLUSTERING)