79 lines
3.4 KiB
C
79 lines
3.4 KiB
C
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
#pragma once
|
|
#include <windows.h>
|
|
#include "bootstrapper_hook.h"
|
|
|
|
typedef struct WineAccessForegroundHooks {
|
|
HMODULE module;
|
|
HOOKPROC menu_callback, input_callback;
|
|
HHOOK menu, input;
|
|
DWORD thread;
|
|
} 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;
|
|
}
|
|
|
|
// Read the process image before injecting. A configured exclusion list also
|
|
// fails closed if the target image cannot be identified. Both architectures
|
|
// use this guard, independently of the reader's slower /proc discovery.
|
|
static inline BOOL wine_access_hooks_allowed(HANDLE process) {
|
|
WCHAR excluded[32768];
|
|
SetLastError(ERROR_SUCCESS);
|
|
DWORD count = GetEnvironmentVariableW(L"WINE_ACCESS_EXCLUDED_HOOK_PROGRAMS",
|
|
excluded, 32768);
|
|
if (!count) return GetLastError() == ERROR_ENVVAR_NOT_FOUND;
|
|
if (count >= 32768) return FALSE;
|
|
WCHAR image[32768];
|
|
DWORD length = 32768;
|
|
if (!process || !QueryFullProcessImageNameW(process, 0, image, &length)) return FALSE;
|
|
const WCHAR *name = image;
|
|
for (DWORD index = 0; index < length; ++index)
|
|
if (image[index] == L'\\' || image[index] == L'/') name = image + index + 1;
|
|
WCHAR *entry = excluded;
|
|
for (WCHAR *cursor = excluded; ; ++cursor) {
|
|
if (*cursor != L'\n' && *cursor) continue;
|
|
BOOL last = !*cursor;
|
|
*cursor = 0;
|
|
if (*entry && !lstrcmpiW(name, entry)) return FALSE;
|
|
if (last) break;
|
|
entry = cursor + 1;
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
// 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) && wine_access_hooks_allowed(process);
|
|
if (process) CloseHandle(process);
|
|
if (!known || wow64 != target_wow64) 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.
|
|
}
|