From 8ca212579a6fa82bba03d29d0ebc12ecea45e0fe Mon Sep 17 00:00:00 2001
From: holio0 <3541353+holio0@users.noreply.github.com>
Date: Thu, 17 Oct 2024 10:34:45 +0800
Subject: [PATCH] tty: Add option to forward unrequested escape sequence

Adds an option "forward-unrequested-escseq" for tmux to forward
unrequested escape sequences to the client. For example, when an OSC 52
escape sequence is received outside a designated TTY_OSC52QUERY context,
it will be passed on to the client application if the option is on,
otherwise it's discarded.
---
 options-table.c | 8 ++++++++
 tmux.1          | 5 +++++
 tty-keys.c      | 6 ++++--
 3 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/options-table.c b/options-table.c
index bc7b12e5..2523fa1f 100644
--- a/options-table.c
+++ b/options-table.c
@@ -448,6 +448,14 @@ const struct options_table_entry options_table[] = {
 		  "'User0', 'User1' and so on."
 	},
 
+	{ .name = "forward-unrequested-escseq",
+	  .type = OPTIONS_TABLE_FLAG,
+	  .scope = OPTIONS_TABLE_SERVER,
+	  .default_num = 0,
+	  .text = "Whether unrequested escape sequences are forwarded to "
+		  "clients. Can be 'off' (disallowed) or 'on' (allowed)."
+	},
+
 	/* Session options. */
 	{ .name = "activity-action",
 	  .type = OPTIONS_TABLE_CHOICE,
diff --git a/tmux.1 b/tmux.1
index 285f7651..7f85c5be 100644
--- a/tmux.1
+++ b/tmux.1
@@ -4565,6 +4565,11 @@ This respects the
 .Ic base-index
 option if it has been set.
 If off, do not renumber the windows.
+.It Xo Ic forward-unrequested-escseq
+.Op Ic on | off
+.Xc
+If on, when an unrequested escape sequence is received, tmux will forward it to client.
+If off, tmux will ignore it.
 .It Ic repeat-time Ar time
 Allow multiple commands to be entered without pressing the prefix key again
 in the specified
diff --git a/tty-keys.c b/tty-keys.c
index 8d703fc0..aab7af96 100644
--- a/tty-keys.c
+++ b/tty-keys.c
@@ -1339,8 +1339,10 @@ tty_keys_clipboard(struct tty *tty, const char *buf, size_t len, size_t *size)
 	end--;
 
 	/* If we did not request this, ignore it. */
-	if (~tty->flags & TTY_OSC52QUERY)
-		return (0);
+	if (~tty->flags & TTY_OSC52QUERY) {
+		return options_get_number(global_options,
+		    "forward-unrequested-escseq") ? (-1) : (0);
+	}
 	tty->flags &= ~TTY_OSC52QUERY;
 	evtimer_del(&tty->clipboard_timer);