terminal/src/host/PtySignalInputThread.hpp
Leonard Hecker 4386bf07fd
Avoid focus loops in ConPTY (#17829)
This fixes a lot of subtle issues:
* Avoid emitting another de-/iconify VT sequence when
  we encounter a (de)iconify VT sequence during parsing.
* Avoid emitting a de-/iconify VT sequence when
  a focus event is received on the signal pipe.
* Avoid emitting such sequences on startup.
* Avoid emitting multiple such sequences
  when rapidly un-/focusing the window.

It's also a minor cleanup, because the `GA_ROOTOWNER` is not security
relevant. It was added because there was concern that someone can just
spawn a ConPTY session, tell it that it's focused, and spawn a child
which is now focused. But why would someone do that, when the console
IOCTLs to do so are not just publicly available but also documented?

I also disabled the IME window.

## Validation Steps Performed
* First:
  ```cpp
  int main() {
      for (bool show = false;; show = !show) {
          printf(show ? "Show in 3s...\n" : "Hide in 3s...\n");
          Sleep(3000);
          ShowWindow(GetConsoleWindow(), show ? SW_SHOW : SW_HIDE);
      }
  }
  ```
* PowerShell 5's `Get-Credential` gains focus 
* `sleep 5; Get-Credential` and focus another app. WT should start
  blinking in the taskbar. Restore it. The popup has focus 
* Run `:hardcopy` in vim: Window is shown centered at (0,0) ✖️
  But that's okay because it does that already anyway 
* `Connect-AzAccount` doesn't crash PowerShell 
2024-10-08 11:37:33 -05:00

80 lines
2.2 KiB
C++

/*++
Copyright (c) Microsoft Corporation
Licensed under the MIT license.
Module Name:
- PtySignalInputThread.hpp
Abstract:
- Defines methods that wrap the thread that will wait for Pty Signals
if a Pty server (VT server) is running.
Author(s):
- Mike Griese (migrie) 15 Aug 2017
- Michael Niksa (miniksa) 19 Jan 2018
--*/
#pragma once
#include "outputStream.hpp"
namespace Microsoft::Console
{
class PtySignalInputThread final
{
public:
PtySignalInputThread(_In_ wil::unique_hfile hPipe);
~PtySignalInputThread();
[[nodiscard]] HRESULT Start() noexcept;
static DWORD WINAPI StaticThreadProc(_In_ LPVOID lpParameter);
// Prevent copying and assignment.
PtySignalInputThread(const PtySignalInputThread&) = delete;
PtySignalInputThread& operator=(const PtySignalInputThread&) = delete;
void ConnectConsole() noexcept;
void CreatePseudoWindow();
private:
enum class PtySignal : unsigned short
{
ShowHideWindow = 1,
ClearBuffer = 2,
SetParent = 3,
ResizeWindow = 8
};
struct ResizeWindowData
{
unsigned short sx;
unsigned short sy;
};
struct ShowHideData
{
unsigned short show; // used as a bool, but passed as a ushort
};
struct SetParentData
{
uint64_t handle;
};
[[nodiscard]] HRESULT _InputThread() noexcept;
[[nodiscard]] bool _GetData(_Out_writes_bytes_(cbBuffer) void* const pBuffer, const DWORD cbBuffer);
void _DoResizeWindow(const ResizeWindowData& data);
void _DoSetWindowParent(const SetParentData& data);
void _DoClearBuffer() const;
void _DoShowHide(const ShowHideData& data);
void _Shutdown();
wil::unique_hfile _hFile;
wil::unique_handle _hThread;
DWORD _dwThreadId;
bool _consoleConnected;
std::optional<ResizeWindowData> _earlyResize;
std::optional<ShowHideData> _initialShowHide;
ConhostInternalGetSet _api;
};
}