feat: implement opt-in OSC52 clipboard querying
Closes: #2050
Hi Wez! This is my first time contribution to the project. I've implemented the missing osc52 clipboard reading command. The feature is disabled by default and can be enabled via the config option config.enable_osc52_clipboard_reading = true . I've also added the documentation with security notes and some tests related to osc52.
The clipboard content is received asynchronously through a channel, which is passed as the second parameter in the Clipboard::get_contents function. For the TerminalState it is the ThreadedWriter directly, for the mux client it is a tx-rx channel that than sends the pdu responce with the clipboard data back to the server and than to the server's ThreadedWriter.
The waiting of the clipboard data on the mux server side is a bit clunky wezterm-mux-server-impl/src/dispatch.rs
because there is no promise handling mechanism similar to what the client has so I left a mutable promise variable before the message loop and handled the response with if Pdu::QueryClipboardResponce(..) = before passing a pdu to the actual server's message handler. I would like to hear any idea how to improve it.
I am open to any suggestions for naming, fixes and improvements.
Hi 39555,
This works nicely! Especially nice that you made it work with the mux-server as well. (It might be worth noting in the docs that the config option also needs to be enabled on the remote machine, for the mux-server).
Great work.
"@loops, what do you mean by the config on the remote server? I thought WezTerm only reads the config from the local machine. Could you write a small guide?
I’m experiencing a strange bug on windows both in the GUI and when using wezterm connect unix or connecting via SSH.... The OSC sequence isn’t being parsed at all. However, everything works perfectly on WSL (via default_domain), macOS, and when using SSH from WSL to macOS or from Windows to macOS via wezterm connect <ssh_domain> or wezterm ssh <addr>."
I've spend some time digging in. Unfortunately. This feature can't work on windows due to ConPTY. They implicitly filter this osc without any user notification.... Both Alacritty and Rio terminals implement this feature and it doesn't work on windows either.
https://github.com/alacritty/alacritty/issues/7962
Thank you for this! This feature was essential and previously missing in wezterm.
~~While using your code, I noticed something that might be useful. When I use a terminal without OSC52 query support (like wezterm without your PR) and attempt to paste with OSC 52 in neovim, I see the message:~~
Waiting for OSC 52 response from the terminal. Press Ctrl-C to interrupt...
~~This informs the user that OSC 52 paste is unavailable.~~
~~However, with your PR in wezterm, if I don’t set config.enable_osc52_clipboard_reading = true, the query fails silently and retrieves the last copied item in neovim. I'm not sure if this is expected behavior, but it could be confusing for users who might overlook the opt-in setting.~~
EDIT: I cannot reproduce it anymore. Please disregard the comment. I really appreciate your work on this feature — it has vastly improved my experience with wezterm!
Thanks a lot! Is there a blocker for getting a review/merge?
Thanks a lot! Is there a blocker for getting a review/merge?
Wez currently has no bandwidth to work on Wezterm. Maybe someone else could review it and suggest improvements before Wez is ready
I've spend some time digging in. Unfortunately. This feature can't work on windows due to ConPTY. They implicitly filter this osc without any user notification.... Both Alacritty and Rio terminals implement this feature and it doesn't work on windows either.
how come this works on mintty tho?
@Kreijstal Just discovered your issue at https://github.com/mintty/mintty/issues/1264 Thanks! Does mintty use conpty? Because neither Allacritty nor Rio can paste from the clipboard on Windows. We need to dig inside the https://github.com/microsoft/terminal/tree/aa5459df4a33c731b25be9f47fe481d8c73db14d/src/winconpty
@Kreijstal Just discovered your issue at mintty/mintty#1264 Thanks! Does
minttyuseconpty? Because neither Allacritty nor Rio can paste from the clipboard on Windows. We need to dig inside the https://github.com/microsoft/terminal/tree/aa5459df4a33c731b25be9f47fe481d8c73db14d/src/winconpty
I guess windows terminal vendors their own conpty? crazy
I researched this a little bit. winconpty starts the OpenConsole.exe process. OpenConsole.exe is built from the src/host folder https://github.com/microsoft/terminal/blob/aa5459df4a33c731b25be9f47fe481d8c73db14d/doc/ORGANIZATION.md?plain=1#L39C1-L39C58 than some magic happens inside that scary state machine https://github.com/microsoft/terminal/blob/aa5459df4a33c731b25be9f47fe481d8c73db14d/src/terminal/parser/stateMachine.cpp and seems like the sequence gets lost here but im not sure https://github.com/microsoft/terminal/blob/0961a77a5aec003e9f79d388f8e7bffb9a550cdb/src/terminal/parser/OutputStateMachineEngine.cpp#L803
Maybe there can be a third option ask for reading clipboard besides true and false, just like kitty's clipboard_control.
Is lack of support for windows a blocker for merging this PR?
Just wanted to note that I've seen this PR, but it's pretty big and I haven't had a chance to really review it yet.
re: conpty discussion above, I wanted to note that I updated to the most recent build of that for wezterm yesterday. It has some improvements around OSC handling that might help here.
I'm also looking forward to see this merged. I just want to mention that Ghostty also has a this implemented in the feature
clipboard-read = allow
maybe it could be worth checking their implementation details.
I really hope this change will be merged soon, it's the only thing missing right now.
@39555 Would you please be able to update this PR to resolve conflicts?
FYI, I have an branch with this PR updated to recent wezterm at https://github.com/Lenbok/wezterm/tree/osc52-read