[Conpty] Add support for mouse input
On: Microsoft Windows [Version 10.0.17763.134]
I have implemented conpty in my terminal emulator: https://github.com/wez/wezterm
I can successfully run target\debug\wezterm.exe to spawn console applications such as cmd.exe powershell.exe and bash.
The issue I'm seeing is that when I launch bash, either indirectly via cmd.exe or directly via the bash launcher, conpty seems to swallow the mouse reporting escape sequences; I don't see them being received by my terminal parser, and thus vim has no effective mouse support despite being configured with set mouse=a.
Running the same WSL install via wsl-terminal does have working mouse support, and wezterm has been my daily driver on Linux for about a year with working mouse support, so we can rule out an obvious misconfiguration with vim and the parser in wezterm.
I've also tried echo -e "\e[?1000h" to manually enable mouse reporting from the shell; normally (on linux and via wsl-terminal) this causes clicks in the terminal to send data to the shell (which appears as garbage input), but when running my terminal with conpty, these are also swallowed up somewhere.
Is there something special needed for the apps that I spawn into my pty to be able to work with the mouse?
In case you want to double check the key portion of the code, the relevant file is:
https://github.com/wez/wezterm/blob/master/src/winpty.rs
The flow is to CreatePipe a pair of pipes, CreatePseudoConsole, and then pass that to a spawned child via the threadproc attributes, as the samples in the MSDN docs and this repo also do.
Hey @wez, Unfortunately, ConPTY won't transit mouse reports (or, from a hosted application, requests for mouse reporting). We've got a backlog item tracking this that we're hoping to get to soon.
Unfortunately, we can't just pass encoded mouse events through: since ConPTY can host standard windows console applications, the likes of which would be expecting MOUSE_EVENTs to come in through ReadConsoleInput, we'll need to do some translation.
In all other regards, though, it sounds like you're setting up the pseudoconsole correctly.
Tracking: MSFT:20469462
@DHowett-MSFT thanks for the response! It's a bit of a bummer that the mouse reporting isn't there yet, but still good that the rest of the pty stuff is possible now!
I have nothing to add other than really wanting mouse support with ConPTY. With alacritty now having ConPTY support using alacritty + ssh + tmux is an incredible linux terminal on Windows, only mouse support is lacking now.
I am a heavy user of midnight commander in my Ubuntu bash shell where it works GREAT. Unfortunately, the Ubuntu shell tab in Windows Terminal 0.3.2171.0 does not appear to send ANY mouse events through to the mc application, making it extremely difficult for me to use. I was going to post a bug, but I this it would duplicate this.
For my use, sending mouse events is of utmost importance for a good experience with vim and tmux.
Just dropping my support for this here, plus a little bit of context. Over the past month I have stopped dual booting and started using the Windows 10 insider builds "fast" ring as my primary personal machine. On that I have wsl2 working perfectly, x410 working perfectly, microsoft terminal working great, visual studio code wsl2 interop working perfectly, and the explorer.exe interop working perfectly.
This mouse input ticket is pretty much the only thing stopping me from calling wsl2 / microsoft terminal a reasonable replacement for having a separate partition / development box. Since some spots here are open source, are there any pointers as to how to see the mouse device inputs from somewhere in /dev or should I just hang tight?
Thanks for all of this though! I really like it otherwise :)
One more vote to say/request that mouse support would be so sweet, and is sorely missed by anyone using tmux, though can sort of get by without it.
Please please please enable mouse support 😄
@DHowett-MSFT, sorry about the nag, is there any plan to fix this any time soon? Seeing labels/priorities assigned on other issues, but this is not one of them, so just checking. Thank you.
@damnskippy Priority is being assigned to things we need to fix in the in-box windows console host (due to internal bug deadlines). This is a huge feature (and requires an appropriately-scaled specification) and while we know we need it for v1.0 we're not actively working on it today.
If we could just "fix" it like a bug, I'd love that -- but it needs a lot more than a fix.
Thanks for the update. Your work is appreciated.
I love what you are doing here, I do, but until we get mouse support, we have to compliment this terminal with another terminal, somewhat defeating the purpose of this terminal.
Midnight Commander without this is a nightmare.
Mouse Input in general could be useful, say if someone in the community, were to create a DosBox add-in and profile which could run in Windows Terminal.
I'm a heavy vim user; even I miss mouse support 😢
I just read @cinnamon-msft blog update, seems the team is aiming for 1.0 by the end of this year? Does this mean we get mouse support by the end of the year? If so, is it actively being worked on right now?
Does this mean we get mouse support by the end of the year?
Maybe, but no hard commits. Estimating how long it takes software to get done is just about as hard as proving P==NP. I'd go so far as to say that the terminal wouldn't be 1.0-ready without this.
If so, is it actively being worked on right now?
Not currently. No one is assigned to the task, and usually when someone on the team bites off a task they'll assign themselves.
Out of curiosity, is everything needed to do this here? Could an ambitious (read: crazy) developer take it on and make a PR?
Sure, someone ambitious absolutely could try this on their own. Where to begin?
First, lets outline some scope. There's a lot of work to do with mouse input, so it'd be best to start small, and work on pieces to get to a complete solution. I think the first thing we should get working is just simple SGR-encoded mouse up/down sequences. We can work on mouse wheel events, then maybe hover, after that, but clicking I think will solve most use cases.
Start by taking a look at InputStateMachineEngine.cpp. The InputStateMachineEngine is responsible for parsing input sent over conpty, and translating it into INPUT_RECORDs. An enterprising young developer would want to modify that class to be able to also parse those mouse sequences, and translate them into INPUT_RECORDs. Once you have the INPUT_RECORDs, call InteractDispatch::WriteInput. This will add those INPUT_RECORDs to the input buffer. Once they are in the input buffer, they'll be delivered to the attached console client application normally.
Things I'd watch out for:
- conhost might not insert MouseEvents into the buffer at all unless we're in mouse input mode. If that's the case, then we should make sure to just ignore these sequences as well.
- Apps that want mouse input from VT will get not a stream of
INPUT_RECORDs, but a stream of characters. At some point in conhost, we attempt to translate those mouseINPUT_RECORDs into a stream of characters, if the attached application is in VT mouse mode. If we do that translation before the mouse events are in the buffer, then doing the above might not work for VT applications (read:wsl). If that's the case, then we'll need to make sure that the translation from mouseINPUT_RECORDto VT-encoded mouse events is done manually for the mouse events generated by theInputStateMachineEngine.- Looking into it more, that does look like the case. We only translate mouse input for events that were initiated by the window unfortunately. See this code:
https://github.com/microsoft/terminal/blob/2c8b3243dca0c48dd05ecd7b420a7a03b3e19c93/src/interactivity/win32/windowio.cpp#L113-L129
terminalMouseInput.HandleMousewill synthesize VT sequences for the client application, but it's only called from the window proc unfortunately. So, we'll need to somehow expose a way for theInputStateMachineEngineto call that (via a new method onInteractDispatch), and if that method fails, then generate the appropriateINPUT_RECORDs. - Technically, someone could move the
terminalMouseInput.HandleMousecall instead to the reading of the InputBuffer, and have it try to translateINPUT_RECORDs right as they're read, but that might be more complicated.
- Looking into it more, that does look like the case. We only translate mouse input for events that were initiated by the window unfortunately. See this code:
https://github.com/microsoft/terminal/blob/2c8b3243dca0c48dd05ecd7b420a7a03b3e19c93/src/interactivity/win32/windowio.cpp#L113-L129
What the deal with this issue? Mouse works perfectly in in the ancient Windows Console. Does it mean I have to stuck with console until this is fixed?
If you need VT mouse support, yes.