Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure a terminal requesting FG rights actually has them #12899

Merged
merged 27 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9dd1649
Doesn't build. Want to merge in the ConGetSet change first, cause _ob…
zadjii-msft Mar 29, 2022
1a1caf9
cherry-pick 479c6c9f08cf273768e64f9f7fbc74a2c231b815
zadjii-msft Mar 31, 2022
5be7f76
comments comments comments
zadjii-msft Mar 31, 2022
37adb94
remove a todo
zadjii-msft Mar 31, 2022
172acd2
you knew there'd be typos
zadjii-msft Mar 31, 2022
0d17cd7
Merge branch 'main' into dev/migrie/b/2988-focus-foreground
zadjii-msft Apr 6, 2022
3e0c15a
Merge branch 'dev/migrie/f/z-order-owner' into dev/migrie/b/2988-focu…
zadjii-msft Apr 6, 2022
72e3ed4
check the ownership for FG rights. NOT TESTED
zadjii-msft Apr 6, 2022
6ab1fb2
Both these fixes should be in this branch
zadjii-msft Apr 7, 2022
f12b8c1
Merge remote-tracking branch 'origin/main' into dev/migrie/b/2988-foc…
zadjii-msft Apr 13, 2022
ac53c8a
Merge remote-tracking branch 'origin/main' into dev/migrie/b/2988-foc…
zadjii-msft Apr 13, 2022
9688573
Merge branch 'dev/migrie/b/2988-focus-foreground' into dev/migrie/b/2…
zadjii-msft Apr 13, 2022
e7c95d2
it builds again
zadjii-msft Apr 13, 2022
2fc0b45
sure, spel
zadjii-msft Apr 13, 2022
e6e5669
what? I always run the tests locally i dunno what you're talking abou…
zadjii-msft Apr 13, 2022
440323d
this should have always been in this branch
zadjii-msft Apr 13, 2022
456429f
Merge branch 'dev/migrie/b/2988-focus-foreground' into dev/migrie/b/2…
zadjii-msft Apr 13, 2022
8ae577e
oh my god I've done it again
zadjii-msft Apr 14, 2022
434b6c3
GA_ROOT is probably better
zadjii-msft Apr 14, 2022
3b4675c
oh my god I've done it again
zadjii-msft Apr 14, 2022
9706b59
Merge remote-tracking branch 'origin/main' into dev/migrie/b/2988-foc…
zadjii-msft Apr 15, 2022
f71af78
lucky day, these can get moved around now
zadjii-msft Apr 15, 2022
6afc170
Merge branch 'dev/migrie/b/2988-focus-foreground' into dev/migrie/b/2…
zadjii-msft Apr 15, 2022
5a81184
Merge remote-tracking branch 'origin/main' into dev/migrie/b/2988-foc…
zadjii-msft Apr 19, 2022
2883cb8
minor typos
zadjii-msft Apr 19, 2022
a371498
that's what I get for adding comments
zadjii-msft Apr 19, 2022
54c59fa
thanks I guess
zadjii-msft Apr 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/host/outputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ void ConhostInternalGetSet::ReparentWindow(const uint64_t handle)
// If the window hasn't been created yet, by some other call to
// LocatePseudoWindow, then this will also initialize the owner of the
// window.
if (const auto psuedoHwnd{ ServiceLocator::LocatePseudoWindow(reinterpret_cast<HWND>(handle)) })
if (const auto pseudoHwnd{ ServiceLocator::LocatePseudoWindow(reinterpret_cast<HWND>(handle)) })
{
LOG_LAST_ERROR_IF_NULL(::SetParent(psuedoHwnd, reinterpret_cast<HWND>(handle)));
LOG_LAST_ERROR_IF_NULL(::SetParent(pseudoHwnd, reinterpret_cast<HWND>(handle)));
}
}
2 changes: 1 addition & 1 deletion src/interactivity/inc/ServiceLocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace Microsoft::Console::Interactivity

static Globals& LocateGlobals();

static HWND LocatePseudoWindow(const HWND owner = 0 /*HWND_DESKTOP*/);
static HWND LocatePseudoWindow(const HWND owner = nullptr /*HWND_DESKTOP = 0*/);

protected:
ServiceLocator(ServiceLocator const&) = delete;
Expand Down
59 changes: 52 additions & 7 deletions src/terminal/adapter/InteractDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,60 @@ bool InteractDispatch::IsVtInputEnabled() const
// - true always.
bool InteractDispatch::FocusChanged(const bool focused) const
{
// When we do GH#11682, we should make sure that ConPTY requests this mode
// from the terminal when it starts up, and ConPTY never unsets that flag.
// It should only ever internally disable the events from flowing to the
// client application.

auto& g = ServiceLocator::LocateGlobals();
auto& gci = g.getConsoleInformation();
WI_UpdateFlag(gci.Flags, CONSOLE_HAS_FOCUS, focused);
gci.ProcessHandleList.ModifyConsoleProcessFocus(focused);

// This should likely always be true - we shouldn't ever have an
// InteractDispatch outside ConPTY mode, but just in case...
if (gci.IsInVtIoMode())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we even HAVE an InteractDispatch if we're not in VtIo mode?

{
bool shouldActuallyFocus = false;

// From https://github.com/microsoft/terminal/pull/12799#issuecomment-1086289552
// Make sure that the process that's telling us it's focused, actually
// _is_ in the FG. We don't want to allow malicious.exe to say "yep I'm
// in the foreground, also, here's a popup" if it isn't actually in the
// FG.
if (focused)
{
if (const auto pseudoHwnd{ ServiceLocator::LocatePseudoWindow() })
{
// They want focus, we found a pseudo hwnd.

// Note: ::GetParent(pseudoHwnd) will return 0. GetAncestor works though.
// GA_PARENT and GA_ROOT seemingly return the same thing for
// Terminal. We're going with GA_ROOT since it seems
// semantically more correct here.
if (const auto ownerHwnd{ ::GetAncestor(pseudoHwnd, GA_ROOT) })
{
// We have an owner from a previous call to ReparentWindow

if (const auto currentFgWindow{ ::GetForegroundWindow() })
{
// There is a window in the foreground (it's possible there
// isn't one)

// Get the PID of the current FG window, and compare with our owner's PID.
DWORD currentFgPid{ 0 };
DWORD ownerPid{ 0 };
GetWindowThreadProcessId(currentFgWindow, &currentFgPid);
GetWindowThreadProcessId(ownerHwnd, &ownerPid);

if (ownerPid == currentFgPid)
{
// Huzzah, the app that owns us is actually the FG
// process. They're allowed to grand FG rights.
shouldActuallyFocus = true;
}
}
}
}
}

WI_UpdateFlag(gci.Flags, CONSOLE_HAS_FOCUS, shouldActuallyFocus);
gci.ProcessHandleList.ModifyConsoleProcessFocus(shouldActuallyFocus);
}
// Does nothing outside of ConPTY. If there's a real HWND, then the HWND is solely in charge.

// Theoretically, this could be propagated as a focus event as well, to the
// input buffer. That should be considered when implementing GH#11682.
Expand Down