[PR #13247] [MERGED] Make sure foreground access works for DefTerm #29441

Open
opened 2026-01-31 09:34:56 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/microsoft/terminal/pull/13247
Author: @zadjii-msft
Created: 6/8/2022
Status: ✅ Merged
Merged: 6/10/2022
Merged by: @undefined

Base: main ← Head: dev/migrie/b/13211-defterm-focus-foreground


📝 Commits (10+)

  • 4de1ef7 This makes sure we re-own the window appropriately, but defterm doesn't request focus mode. Weird.
  • 5ab0799 make sure to enable win32-input-mode, to make sure we request focus events, but that doesn't fix it either
  • da55ab7 This was totally wrong
  • f117fb8 Revert "This was totally wrong"
  • e970f7c this worked
  • 7d82c81 lemme tell you, this is NOT a HANDLE
  • a4cd034 Oh man only one spelling mistake
  • 9295f95 Merge remote-tracking branch 'origin/main' into dev/migrie/b/13211-defterm-focus-foreground
  • b1721f6 Merge remote-tracking branch 'origin/main' into dev/migrie/b/13211-defterm-focus-foreground
  • 55a7331 This is far simpler

📊 Changes

16 files changed (+86 additions, -32 deletions)

View changed files

📝 src/cascadia/TerminalApp/TerminalPage.cpp (+23 -0)
📝 src/cascadia/TerminalControl/ControlCore.cpp (+19 -2)
📝 src/cascadia/TerminalControl/ControlCore.h (+4 -4)
📝 src/host/ConsoleArguments.cpp (+6 -0)
📝 src/host/input.cpp (+1 -1)
📝 src/host/srvinit.cpp (+6 -1)
📝 src/inc/HostSignals.hpp (+4 -4)
📝 src/interactivity/base/HostSignalInputThread.cpp (+5 -3)
📝 src/interactivity/base/RemoteConsoleControl.cpp (+6 -8)
📝 src/interactivity/base/RemoteConsoleControl.hpp (+4 -1)
📝 src/interactivity/inc/IConsoleControl.hpp (+1 -1)
📝 src/interactivity/onecore/ConsoleControl.cpp (+2 -2)
📝 src/interactivity/onecore/ConsoleControl.hpp (+1 -1)
📝 src/interactivity/win32/ConsoleControl.cpp (+2 -2)
📝 src/interactivity/win32/ConsoleControl.hpp (+1 -1)
📝 src/server/ProcessList.cpp (+1 -1)

📄 Description

See also: #12799, the origin of much of this.

This change evolved over multiple phases.

Part the first

When we create a defterm connection in TerminalPage::_OnNewConnection,
we don't have the hosting HWND yet, so the tab gets created without one.
We'll later get called with the owner, in Initialize.

To remedy this, we need to:

  • In Initialize, make sure to update any existing controls with the
    new owner.
  • In ControlCore, actually propogate the new owner down to the
    connection

Part the second

DefTerm launches don't actually request focus mode, so the Terminal
never sends them focus events. We need those focus events so that the
console can request foreground rights.

To remedy this, we need to:

  • pass --win32input to the commandline used to initialize OpenConsole
    in ConPTY mode. We request focus events at the same time we request
    win32-input-mode.
  • I also added --resizeQuirk, because by all accounts that should be
    there
    . Resizing in defterm windows should be wacky without it, and
    I'm a little surprised we haven't seen any bugs due to this yet.

Part the third

ConsoleSetForeground expects a HANDLE to the process we want to give
foreground rights to. The problem is, the wire format we used also
decided that a HANDLE value was a good idea. It's not. If we pass the
literal value of the HANDLE to the process from OpenConsole to conhost,
so conhost can call that API, the value that conhost uses there will
most likely be an invalid handle. The HANDLE's value is its value in
OpenConsole, not in conhost.

To remedy this, we need to:

  • Just not forward ConsoleSetForeground. Turns out, we can just call
    that in OpenConsole safely. There's no validation. So just instantiate
    a static version of the Win32 version of ConsoleControl, just to use
    for SetForeground. (thanks Dustin)

  • Tested manually - Win+R powershell, notepad spawns on top.

Closes #13211


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/microsoft/terminal/pull/13247 **Author:** [@zadjii-msft](https://github.com/zadjii-msft) **Created:** 6/8/2022 **Status:** ✅ Merged **Merged:** 6/10/2022 **Merged by:** [@undefined](undefined) **Base:** `main` ← **Head:** `dev/migrie/b/13211-defterm-focus-foreground` --- ### 📝 Commits (10+) - [`4de1ef7`](https://github.com/microsoft/terminal/commit/4de1ef73e9a8e0fb7582d60c6e1152fb9aeb1a77) This makes sure we re-own the window appropriately, but defterm doesn't request focus mode. Weird. - [`5ab0799`](https://github.com/microsoft/terminal/commit/5ab079915b27438850d516e521960d71504b78d2) make sure to enable win32-input-mode, to make sure we request focus events, but that doesn't fix it either - [`da55ab7`](https://github.com/microsoft/terminal/commit/da55ab7c4045fab366f751be80c391c7dd3e5b9b) This was totally wrong - [`f117fb8`](https://github.com/microsoft/terminal/commit/f117fb85038d66ce4f16532c7935050de8524fad) Revert "This was totally wrong" - [`e970f7c`](https://github.com/microsoft/terminal/commit/e970f7c93c4005d8a138d4f2e6acab965fe43831) this worked - [`7d82c81`](https://github.com/microsoft/terminal/commit/7d82c81de53e92a87bb89fc05388224af1c2682f) lemme tell you, this is NOT a HANDLE - [`a4cd034`](https://github.com/microsoft/terminal/commit/a4cd0344d717e873018b9d60c9db1d3d73017e39) Oh man only one spelling mistake - [`9295f95`](https://github.com/microsoft/terminal/commit/9295f95904a2b042a17a828f74a7ae302f08a5bb) Merge remote-tracking branch 'origin/main' into dev/migrie/b/13211-defterm-focus-foreground - [`b1721f6`](https://github.com/microsoft/terminal/commit/b1721f6185ed7473407ddc8ca7a919bb4517ea92) Merge remote-tracking branch 'origin/main' into dev/migrie/b/13211-defterm-focus-foreground - [`55a7331`](https://github.com/microsoft/terminal/commit/55a73310a075a9c63b90fb562f36fda78adf6494) This is far simpler ### 📊 Changes **16 files changed** (+86 additions, -32 deletions) <details> <summary>View changed files</summary> 📝 `src/cascadia/TerminalApp/TerminalPage.cpp` (+23 -0) 📝 `src/cascadia/TerminalControl/ControlCore.cpp` (+19 -2) 📝 `src/cascadia/TerminalControl/ControlCore.h` (+4 -4) 📝 `src/host/ConsoleArguments.cpp` (+6 -0) 📝 `src/host/input.cpp` (+1 -1) 📝 `src/host/srvinit.cpp` (+6 -1) 📝 `src/inc/HostSignals.hpp` (+4 -4) 📝 `src/interactivity/base/HostSignalInputThread.cpp` (+5 -3) 📝 `src/interactivity/base/RemoteConsoleControl.cpp` (+6 -8) 📝 `src/interactivity/base/RemoteConsoleControl.hpp` (+4 -1) 📝 `src/interactivity/inc/IConsoleControl.hpp` (+1 -1) 📝 `src/interactivity/onecore/ConsoleControl.cpp` (+2 -2) 📝 `src/interactivity/onecore/ConsoleControl.hpp` (+1 -1) 📝 `src/interactivity/win32/ConsoleControl.cpp` (+2 -2) 📝 `src/interactivity/win32/ConsoleControl.hpp` (+1 -1) 📝 `src/server/ProcessList.cpp` (+1 -1) </details> ### 📄 Description See also: #12799, the origin of much of this. This change evolved over multiple phases. ### Part the first When we create a defterm connection in `TerminalPage::_OnNewConnection`, we don't have the hosting HWND yet, so the tab gets created without one. We'll later get called with the owner, in `Initialize`. To remedy this, we need to: * In `Initialize`, make sure to update any existing controls with the new owner. * In `ControlCore`, actually propogate the new owner down to the connection ### Part the second DefTerm launches don't actually request focus mode, so the Terminal never sends them focus events. We need those focus events so that the console can request foreground rights. To remedy this, we need to: * pass `--win32input` to the commandline used to initialize OpenConsole in ConPTY mode. We request focus events at the same time we request win32-input-mode. * I also added `--resizeQuirk`, because _by all accounts that should be there_. Resizing in defterm windows should be _wacky_ without it, and I'm a little surprised we haven't seen any bugs due to this yet. ### Part the third `ConsoleSetForeground` expects a `HANDLE` to the process we want to give foreground rights to. The problem is, the wire format we used _also_ decided that a HANDLE value was a good idea. It's not. If we pass the literal value of the HANDLE to the process from OpenConsole to conhost, so conhost can call that API, the value that conhost uses there will most likely be an invalid handle. The HANDLE's value is its value in _OpenConsole_, not in conhost. To remedy this, we need to: * Just not forward `ConsoleSetForeground`. Turns out, we _can_ just call that in OpenConsole safely. There's no validation. So just instantiate a static version of the Win32 version of ConsoleControl, just to use for SetForeground. (thanks Dustin) * [x] Tested manually - Win+R `powershell`, `notepad` spawns on top. Closes #13211 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-31 09:34:56 +00:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#29441