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

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

Original Pull Request: https://github.com/microsoft/terminal/pull/13247

State: closed
Merged: Yes


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

**Original Pull Request:** https://github.com/microsoft/terminal/pull/13247 **State:** closed **Merged:** Yes --- 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
claunia added the pull-request label 2026-01-31 09:34:57 +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#29445