Skip to content

Commit

Permalink
swiggity swooty i'm comin for that FBGoost
Browse files Browse the repository at this point in the history
  • Loading branch information
DHowett committed Jun 23, 2023
1 parent f0291c6 commit d881eae
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
39 changes: 33 additions & 6 deletions src/cascadia/TerminalConnection/ConptyConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation

THROW_IF_FAILED(_CreatePseudoConsoleAndPipes(til::unwrap_coord_size(dimensions), flags, &_inPipe, &_outPipe, &_hPC));

if (_initialParentHwnd != 0)
if (_parentWindow != nullptr)
{
THROW_IF_FAILED(ConptyReparentPseudoConsole(_hPC.get(), reinterpret_cast<HWND>(_initialParentHwnd)));
THROW_IF_FAILED(ConptyReparentPseudoConsole(_hPC.get(), _parentWindow));
}

// GH#12515: The conpty assumes it's hidden at the start. If we're visible, let it know now.
Expand All @@ -359,6 +359,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
}

THROW_IF_FAILED(_LaunchAttachedClient());
Focused(_focused);
}
// But if it was an inbound handoff... attempt to synchronize the size of it with what our connection
// window is expecting it to be on the first layout.
Expand All @@ -375,12 +376,14 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
TelemetryPrivacyDataTag(PDT_ProductAndServiceUsage));

THROW_IF_FAILED(ConptyResizePseudoConsole(_hPC.get(), til::unwrap_coord_size(dimensions)));
THROW_IF_FAILED(ConptyReparentPseudoConsole(_hPC.get(), reinterpret_cast<HWND>(_initialParentHwnd)));
THROW_IF_FAILED(ConptyReparentPseudoConsole(_hPC.get(), _parentWindow));

if (_initialVisibility)
{
THROW_IF_FAILED(ConptyShowHidePseudoConsole(_hPC.get(), _initialVisibility));
}

Focused(_focused);
}

THROW_IF_FAILED(ConptyReleasePseudoConsole(_hPC.get()));
Expand Down Expand Up @@ -523,16 +526,40 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
void ConptyConnection::ReparentWindow(const uint64_t newParent)
{
// If we haven't started connecting at all, stash this HWND to use once we have started.
_parentWindow = reinterpret_cast<HWND>(newParent);
if (!_isStateAtOrBeyond(ConnectionState::Connecting))
{
_initialParentHwnd = newParent;
return;
}

// Otherwise, just inform the conpty of the new owner window handle.
// This shouldn't be hittable until GH#5000 / GH#1256, when it's
// possible to reparent terminals to different windows.
else if (_isConnected())
if (_isConnected())
{
THROW_IF_FAILED(ConptyReparentPseudoConsole(_hPC.get(), _parentWindow));
}
}

void ConptyConnection::Focused(bool f)
{
static auto s = []() {
auto m = LoadLibraryExW(L"user32.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
return GetProcAddressByFunctionDeclaration(m, SetAdditionalForegroundBoostProcesses);
}();

_focused = f;

if (!_parentWindow || !s)
return;

if (f)
{
LOG_LAST_ERROR_IF(!s(_parentWindow, 1, &_piClient.hProcess));
}
else
{
THROW_IF_FAILED(ConptyReparentPseudoConsole(_hPC.get(), reinterpret_cast<HWND>(newParent)));
LOG_LAST_ERROR_IF(!s(_parentWindow, 0, &_piClient.hProcess));
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/cascadia/TerminalConnection/ConptyConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
void ShowHide(const bool show);

void ReparentWindow(const uint64_t newParent);
void Focused(bool f);

winrt::guid Guid() const noexcept;
winrt::hstring Commandline() const;
Expand Down Expand Up @@ -68,7 +69,8 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation

til::CoordType _rows{};
til::CoordType _cols{};
uint64_t _initialParentHwnd{ 0 };
HWND _parentWindow{};
bool _focused{};
hstring _commandline{};
hstring _startingDirectory{};
hstring _startingTitle{};
Expand Down
4 changes: 1 addition & 3 deletions src/cascadia/TerminalConnection/ConptyConnection.idl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Terminal.TerminalConnection
{
delegate void NewConnectionHandler(ConptyConnection connection);

[default_interface] runtimeclass ConptyConnection : ITerminalConnection
[default_interface] runtimeclass ConptyConnection : ITerminalConnection, ITerminalConnectionWithWindowAffinity
{
ConptyConnection();
Guid Guid { get; };
Expand All @@ -19,8 +19,6 @@ namespace Microsoft.Terminal.TerminalConnection

void ShowHide(Boolean show);

void ReparentWindow(UInt64 newParent);

static event NewConnectionHandler NewConnection;
static void StartInboundListener();
static void StopInboundListener();
Expand Down
6 changes: 6 additions & 0 deletions src/cascadia/TerminalConnection/ITerminalConnection.idl
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ namespace Microsoft.Terminal.TerminalConnection
event Windows.Foundation.TypedEventHandler<ITerminalConnection, Object> StateChanged;
ConnectionState State { get; };
};

interface ITerminalConnectionWithWindowAffinity
{
void ReparentWindow(UInt64 newParent);
void Focused(Boolean f);
};
}
4 changes: 4 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2053,6 +2053,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation
const auto previous = std::exchange(_isReadOnly, false);
const auto restore = wil::scope_exit([&]() { _isReadOnly = previous; });
_terminal->FocusChanged(focused);
if (auto c{ _connection.try_as<TerminalConnection::ITerminalConnectionWithWindowAffinity>() })
{
c.Focused(focused);
}
}

bool ControlCore::_isBackgroundTransparent()
Expand Down

0 comments on commit d881eae

Please sign in to comment.