// SPDX-License-Identifier: LGPL-2.1-or-later #include "foreground_focus.h" #include "review_x11.h" namespace { struct Candidate { HWND window = nullptr; unsigned count = 0; }; BOOL CALLBACK find_candidate(HWND window, LPARAM data) { if (!IsWindowVisible(window) || !IsWindowEnabled(window) || IsIconic(window) || (GetWindowLongPtrW(window, GWL_EXSTYLE) & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE)) || !GetPropW(window, L"__wine_x11_whole_window")) return TRUE; auto &candidate = *reinterpret_cast(data); candidate.window = window; return ++candidate.count < 2; } } bool restore_virtual_desktop_focus() { if (GetForegroundWindow() || !wine_x11_desktop_focused()) return false; Candidate candidate; EnumWindows(find_candidate, reinterpret_cast(&candidate)); // Recheck focus after enumeration so a Linux/application transition cannot // turn a recovery into activation of a background Wine application. if (candidate.count != 1 || GetForegroundWindow() || !wine_x11_desktop_focused()) return false; return wine_x11_restore_focus(candidate.window); }