57 lines
2.6 KiB
C
57 lines
2.6 KiB
C
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
#pragma once
|
|
#include <windows.h>
|
|
#include "bootstrapper_hook.h"
|
|
#include "hook_exclusions.h"
|
|
|
|
typedef struct WineAccessForegroundHooks {
|
|
HMODULE module;
|
|
HOOKPROC menu_callback, input_callback;
|
|
HHOOK menu, input;
|
|
DWORD thread;
|
|
const WineAccessHookExclusions *exclusions;
|
|
} WineAccessForegroundHooks;
|
|
|
|
static inline void wine_access_release_hooks(WineAccessForegroundHooks *hooks) {
|
|
if (hooks->menu) UnhookWindowsHookEx(hooks->menu);
|
|
if (hooks->input) UnhookWindowsHookEx(hooks->input);
|
|
hooks->menu = hooks->input = NULL;
|
|
hooks->thread = 0;
|
|
}
|
|
|
|
// Global hooks cross the 32/64-bit boundary through the installing thread.
|
|
// Avoid that synchronous traffic and hook only the matching foreground UI
|
|
// thread. Both hosts use the same detach/retry rules.
|
|
static inline void wine_access_update_hooks(WineAccessForegroundHooks *hooks, BOOL target_wow64) {
|
|
DWORD pid = 0;
|
|
DWORD thread = GetWindowThreadProcessId(GetForegroundWindow(), &pid);
|
|
HANDLE process = pid ? OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid) : NULL;
|
|
BOOL wow64 = FALSE;
|
|
BOOL known = process && IsWow64Process(process, &wow64);
|
|
// An excluded program's thread never receives the hooks; its controller
|
|
// speech does not need them and its anti-tamper code may object to them.
|
|
// With exclusions configured, an unidentifiable target is not hooked.
|
|
BOOL excluded = !wine_access_hooks_allowed(hooks->exclusions, process);
|
|
if (process) CloseHandle(process);
|
|
if (!known || wow64 != target_wow64 || excluded) thread = 0;
|
|
if (thread == hooks->thread) return;
|
|
wine_access_release_hooks(hooks);
|
|
if (!thread || !hooks->menu_callback || !hooks->input_callback) return;
|
|
hooks->menu = SetWindowsHookExW(WH_CALLWNDPROC, hooks->menu_callback, hooks->module, thread);
|
|
hooks->input = SetWindowsHookExW(WH_GETMESSAGE, hooks->input_callback, hooks->module, thread);
|
|
if (hooks->menu && hooks->input) {
|
|
hooks->thread = thread;
|
|
HWND foreground = GetForegroundWindow();
|
|
WCHAR className[128];
|
|
if (GetWindowThreadProcessId(foreground, NULL) == thread &&
|
|
GetClassNameW(foreground, className, ARRAYSIZE(className)) &&
|
|
!lstrcmpW(className, L"Blizzard Bootstrapper")) {
|
|
// A query may have arrived while detached. Reestablish readiness on
|
|
// every attachment, including return from another application.
|
|
RemovePropW(foreground, BOOTSTRAPPER_STATE_READY);
|
|
RemovePropW(foreground, BOOTSTRAPPER_STATE_PENDING);
|
|
PostMessageW(foreground, WM_NULL, 0, 0);
|
|
}
|
|
} else wine_access_release_hooks(hooks); // Retry both on the next foreground poll.
|
|
}
|