wezterm icon indicating copy to clipboard operation
wezterm copied to clipboard

feat: implement opt-in OSC52 clipboard querying

Open 39555 opened this issue 1 year ago • 16 comments

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.

39555 avatar Oct 06 '24 21:10 39555

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 avatar Oct 06 '24 23:10 loops

"@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?

39555 avatar Oct 07 '24 13:10 39555

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>."

39555 avatar Oct 07 '24 14:10 39555

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

39555 avatar Oct 09 '24 08:10 39555

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!

jborbik avatar Oct 09 '24 14:10 jborbik

Thanks a lot! Is there a blocker for getting a review/merge?

melMass avatar Nov 04 '24 17:11 melMass

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

39555 avatar Nov 04 '24 17:11 39555

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.

alacritty/alacritty#7962

how come this works on mintty tho?

Kreijstal avatar Dec 05 '24 20:12 Kreijstal

@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

39555 avatar Dec 05 '24 21:12 39555

@Kreijstal Just discovered your issue at mintty/mintty#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

I guess windows terminal vendors their own conpty? crazy

Kreijstal avatar Dec 05 '24 21:12 Kreijstal

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

39555 avatar Dec 05 '24 21:12 39555

Maybe there can be a third option ask for reading clipboard besides true and false, just like kitty's clipboard_control.

adoal avatar Dec 06 '24 17:12 adoal

Is lack of support for windows a blocker for merging this PR?

Lenbok avatar Jan 24 '25 02:01 Lenbok

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.

wez avatar Feb 09 '25 20:02 wez

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.

tuxflo avatar May 26 '25 09:05 tuxflo

I really hope this change will be merged soon, it's the only thing missing right now.

Alby407 avatar Jun 05 '25 11:06 Alby407

@39555 Would you please be able to update this PR to resolve conflicts?

Lenbok avatar Jul 24 '25 21:07 Lenbok

FYI, I have an branch with this PR updated to recent wezterm at https://github.com/Lenbok/wezterm/tree/osc52-read

Lenbok avatar Sep 30 '25 20:09 Lenbok