Restore wine access after program loses and regains focus.
This commit is contained in:
1 parent
f40e471938
commit
c3e981b4c2
12 files changed
+351
-2
No files matched your search
@@ -9,6 +9,7 @@
|
||||
#include <poll.h>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
|
||||
namespace {
|
||||
struct Connection {
|
||||
@@ -59,3 +60,62 @@ bool review_x11_geometry(HWND window, RECT &bounds, POINT &origin) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool wine_x11_desktop_focused() {
|
||||
USEROBJECTFLAGS flags{};
|
||||
if (!GetUserObjectInformationW(GetThreadDesktop(GetCurrentThreadId()), UOI_FLAGS,
|
||||
&flags, sizeof(flags), nullptr) || !(flags.dwFlags & 0x80000000))
|
||||
return false;
|
||||
const auto xid = reinterpret_cast<uintptr_t>(GetPropW(GetDesktopWindow(), L"__wine_x11_whole_window"));
|
||||
if (!xid || xid > UINT32_MAX || !connection.value || xcb_connection_has_error(connection.value))
|
||||
return false;
|
||||
const uint64_t deadline = GetTickCount64() + 20;
|
||||
auto focus = reply<xcb_get_input_focus_reply_t>(xcb_get_input_focus(connection.value).sequence, deadline);
|
||||
if (!focus || focus->focus != xid) return false;
|
||||
auto size = reply<xcb_get_geometry_reply_t>(xcb_get_geometry(connection.value, xid).sequence, deadline);
|
||||
if (!size) return false;
|
||||
constexpr char name[] = "_NET_ACTIVE_WINDOW";
|
||||
auto atom = reply<xcb_intern_atom_reply_t>(xcb_intern_atom(connection.value, 1, sizeof(name) - 1, name).sequence, deadline);
|
||||
if (!atom || !atom->atom) return false;
|
||||
auto active = reply<xcb_get_property_reply_t>(xcb_get_property(connection.value, 0, size->root,
|
||||
atom->atom, XCB_ATOM_WINDOW, 0, 1).sequence, deadline);
|
||||
uint32_t window = 0;
|
||||
if (!active || active->type != XCB_ATOM_WINDOW || active->format != 32 ||
|
||||
xcb_get_property_value_length(active.get()) != sizeof(window))
|
||||
return false;
|
||||
std::memcpy(&window, xcb_get_property_value(active.get()), sizeof(window));
|
||||
return window == xid;
|
||||
}
|
||||
|
||||
bool wine_x11_restore_focus(HWND window) {
|
||||
if (!wine_x11_desktop_focused()) return false;
|
||||
const auto xid = reinterpret_cast<uintptr_t>(GetPropW(window, L"__wine_x11_whole_window"));
|
||||
const auto desktop = reinterpret_cast<uintptr_t>(GetPropW(GetDesktopWindow(), L"__wine_x11_whole_window"));
|
||||
if (!xid || xid > UINT32_MAX) return false;
|
||||
const uint64_t deadline = GetTickCount64() + 20;
|
||||
auto tree = reply<xcb_query_tree_reply_t>(xcb_query_tree(connection.value, xid).sequence, deadline);
|
||||
if (!tree || tree->parent != desktop) return false;
|
||||
// Native focus informs Wine's application thread of activation. A helper's
|
||||
// SetForegroundWindow can be denied by the Win32 foreground-lock policy.
|
||||
constexpr char protocolsName[] = "WM_PROTOCOLS";
|
||||
constexpr char focusName[] = "WM_TAKE_FOCUS";
|
||||
auto protocols = reply<xcb_intern_atom_reply_t>(xcb_intern_atom(connection.value, 1,
|
||||
sizeof(protocolsName) - 1, protocolsName).sequence, deadline);
|
||||
auto takeFocus = reply<xcb_intern_atom_reply_t>(xcb_intern_atom(connection.value, 1,
|
||||
sizeof(focusName) - 1, focusName).sequence, deadline);
|
||||
if (!protocols || !protocols->atom || !takeFocus || !takeFocus->atom) return false;
|
||||
if (GetForegroundWindow() || !IsWindowVisible(window) || !IsWindowEnabled(window) ||
|
||||
IsIconic(window) || !wine_x11_desktop_focused())
|
||||
return false;
|
||||
xcb_client_message_event_t event{};
|
||||
event.response_type = XCB_CLIENT_MESSAGE;
|
||||
event.format = 32;
|
||||
event.window = xid;
|
||||
event.type = protocols->atom;
|
||||
event.data.data32[0] = takeFocus->atom;
|
||||
event.data.data32[1] = XCB_CURRENT_TIME;
|
||||
xcb_send_event(connection.value, 0, xid, XCB_EVENT_MASK_NO_EVENT, reinterpret_cast<const char *>(&event));
|
||||
xcb_set_input_focus(connection.value, XCB_INPUT_FOCUS_PARENT, xid, XCB_CURRENT_TIME);
|
||||
auto focus = reply<xcb_get_input_focus_reply_t>(xcb_get_input_focus(connection.value).sequence, deadline);
|
||||
return focus && focus->focus == xid;
|
||||
}
|
||||
Reference in new issue
Block a user