Apply clang-format across the whole tree
Run clang-format (the repo's .clang-format: LLVM base, 120 cols, tabs, Allman functions) over every source file so the tree is formatter-clean. Whitespace only -- no behavior change; full x64 + x86 suites pass. Also set SortIncludes: false in .clang-format. Windows include order is load-bearing (windows.h must precede tlhelp32.h / mmreg.h / xinput.h / dinput.h; winsock2.h must precede windows.h), and the default alphabetical sort reorders tlhelp32.h ahead of windows.h -- a build break. Leaving order alone keeps the manual, correct grouping.
This commit is contained in:
@@ -11,11 +11,9 @@
|
||||
#include "hook_install.hpp"
|
||||
#include "hook_registry.hpp"
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
namespace coop::hook {
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
DetourGate g_gate; // drains in-flight focus / WNDPROC detours before remove nulls their state
|
||||
|
||||
@@ -40,11 +38,9 @@ safetyhook::InlineHook g_hk_setcursorpos;
|
||||
LRESULT CALLBACK subclass_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
|
||||
{
|
||||
DetourGate::Guard guard(g_gate); // keep g_orig_proc / g_unicode valid for this whole dispatch
|
||||
switch (msg)
|
||||
{
|
||||
switch (msg) {
|
||||
case WM_ACTIVATE:
|
||||
if (LOWORD(wparam) == WA_INACTIVE)
|
||||
{
|
||||
if (LOWORD(wparam) == WA_INACTIVE) {
|
||||
wparam = MAKEWPARAM(WA_ACTIVE, HIWORD(wparam));
|
||||
hook_note_call(g_id_wndproc);
|
||||
}
|
||||
@@ -66,8 +62,7 @@ LRESULT CALLBACK subclass_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam
|
||||
// Read g_orig_proc once; if the subclass is live but the original isn't published yet (the tiny
|
||||
// install/remove window), fall back to DefWindowProc rather than call through a null pointer.
|
||||
const WNDPROC orig = g_orig_proc;
|
||||
if (orig == nullptr)
|
||||
{
|
||||
if (orig == nullptr) {
|
||||
return g_unicode ? DefWindowProcW(hwnd, msg, wparam, lparam) : DefWindowProcA(hwnd, msg, wparam, lparam);
|
||||
}
|
||||
return g_unicode ? CallWindowProcW(orig, hwnd, msg, wparam, lparam)
|
||||
@@ -78,8 +73,7 @@ HWND WINAPI hk_GetForegroundWindow()
|
||||
{
|
||||
DetourGate::Guard guard(g_gate);
|
||||
hook_note_call(g_id_foreground);
|
||||
if (g_focus_ipc != nullptr)
|
||||
{
|
||||
if (g_focus_ipc != nullptr) {
|
||||
g_focus_ipc->note_focus_query(FocusApi_Foreground);
|
||||
}
|
||||
return g_game_hwnd;
|
||||
@@ -89,8 +83,7 @@ HWND WINAPI hk_GetActiveWindow()
|
||||
{
|
||||
DetourGate::Guard guard(g_gate);
|
||||
hook_note_call(g_id_active);
|
||||
if (g_focus_ipc != nullptr)
|
||||
{
|
||||
if (g_focus_ipc != nullptr) {
|
||||
g_focus_ipc->note_focus_query(FocusApi_Active);
|
||||
}
|
||||
return g_game_hwnd;
|
||||
@@ -100,8 +93,7 @@ HWND WINAPI hk_GetFocus()
|
||||
{
|
||||
DetourGate::Guard guard(g_gate);
|
||||
hook_note_call(g_id_focus);
|
||||
if (g_focus_ipc != nullptr)
|
||||
{
|
||||
if (g_focus_ipc != nullptr) {
|
||||
g_focus_ipc->note_focus_query(FocusApi_Focus);
|
||||
}
|
||||
return g_game_hwnd;
|
||||
@@ -124,8 +116,7 @@ BOOL WINAPI hk_SetCursorPos(int x, int y)
|
||||
DetourGate::Guard guard(g_gate);
|
||||
hook_note_call(g_id_setcursorpos);
|
||||
const bool allow = g_focus_ipc != nullptr && g_focus_ipc->cursor_clip_allowed();
|
||||
if (!allow)
|
||||
{
|
||||
if (!allow) {
|
||||
return TRUE;
|
||||
}
|
||||
return g_hk_setcursorpos.stdcall<BOOL>(x, y);
|
||||
@@ -133,8 +124,7 @@ BOOL WINAPI hk_SetCursorPos(int x, int y)
|
||||
|
||||
void hook_export(HMODULE module, const char* name, void* detour, int registry_id)
|
||||
{
|
||||
if (void* target = reinterpret_cast<void*>(GetProcAddress(module, name)))
|
||||
{
|
||||
if (void* target = reinterpret_cast<void*>(GetProcAddress(module, name))) {
|
||||
g_focus_hooks.emplace_back();
|
||||
install_inline(g_focus_hooks.back(), target, detour); // assign-then-enable (no install race)
|
||||
hook_set_installed(registry_id, true);
|
||||
@@ -146,8 +136,7 @@ void hook_export(HMODULE module, const char* name, void* detour, int registry_id
|
||||
bool install_focus_spoof(IpcClient& ipc)
|
||||
{
|
||||
g_focus_ipc = &ipc;
|
||||
if (g_game_hwnd != nullptr)
|
||||
{
|
||||
if (g_game_hwnd != nullptr) {
|
||||
return true; // already active
|
||||
}
|
||||
|
||||
@@ -159,8 +148,7 @@ bool install_focus_spoof(IpcClient& ipc)
|
||||
g_id_setcursorpos = hook_register("SetCursorPos (cursor release)", HookSubsys_Focus);
|
||||
|
||||
HWND hwnd = find_main_window(GetCurrentProcessId());
|
||||
if (hwnd == nullptr)
|
||||
{
|
||||
if (hwnd == nullptr) {
|
||||
return false; // window not created yet; caller retries
|
||||
}
|
||||
|
||||
@@ -173,38 +161,30 @@ bool install_focus_spoof(IpcClient& ipc)
|
||||
// thread is safe (the new proc runs on the window's own thread); match A/W for CallWindowProc.
|
||||
g_orig_proc = g_unicode ? reinterpret_cast<WNDPROC>(GetWindowLongPtrW(hwnd, GWLP_WNDPROC))
|
||||
: reinterpret_cast<WNDPROC>(GetWindowLongPtrA(hwnd, GWLP_WNDPROC));
|
||||
if (g_unicode)
|
||||
{
|
||||
if (g_unicode) {
|
||||
SetWindowLongPtrW(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&subclass_proc));
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
SetWindowLongPtrA(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&subclass_proc));
|
||||
}
|
||||
hook_set_installed(g_id_wndproc, true);
|
||||
|
||||
if (HMODULE user32 = GetModuleHandleW(L"user32.dll"))
|
||||
{
|
||||
hook_export(user32, "GetForegroundWindow", reinterpret_cast<void*>(&hk_GetForegroundWindow),
|
||||
g_id_foreground);
|
||||
if (HMODULE user32 = GetModuleHandleW(L"user32.dll")) {
|
||||
hook_export(user32, "GetForegroundWindow", reinterpret_cast<void*>(&hk_GetForegroundWindow), g_id_foreground);
|
||||
hook_export(user32, "GetActiveWindow", reinterpret_cast<void*>(&hk_GetActiveWindow), g_id_active);
|
||||
hook_export(user32, "GetFocus", reinterpret_cast<void*>(&hk_GetFocus), g_id_focus);
|
||||
|
||||
if (void* clip = reinterpret_cast<void*>(GetProcAddress(user32, "ClipCursor")))
|
||||
{
|
||||
if (void* clip = reinterpret_cast<void*>(GetProcAddress(user32, "ClipCursor"))) {
|
||||
install_inline(g_hk_clipcursor, clip, &hk_ClipCursor);
|
||||
hook_set_installed(g_id_clipcursor, static_cast<bool>(g_hk_clipcursor));
|
||||
}
|
||||
if (void* setpos = reinterpret_cast<void*>(GetProcAddress(user32, "SetCursorPos")))
|
||||
{
|
||||
if (void* setpos = reinterpret_cast<void*>(GetProcAddress(user32, "SetCursorPos"))) {
|
||||
install_inline(g_hk_setcursorpos, setpos, &hk_SetCursorPos);
|
||||
hook_set_installed(g_id_setcursorpos, static_cast<bool>(g_hk_setcursorpos));
|
||||
}
|
||||
}
|
||||
|
||||
// Free any clip the game already set, so release takes effect immediately.
|
||||
if (!ipc.cursor_clip_allowed())
|
||||
{
|
||||
if (!ipc.cursor_clip_allowed()) {
|
||||
ClipCursor(nullptr);
|
||||
}
|
||||
|
||||
@@ -221,20 +201,16 @@ void update_input_diagnostics(IpcClient& ipc)
|
||||
bool raw_gamepad_sink = false;
|
||||
|
||||
UINT count = 0;
|
||||
if (GetRegisteredRawInputDevices(nullptr, &count, sizeof(RAWINPUTDEVICE)) == 0 && count > 0)
|
||||
{
|
||||
if (GetRegisteredRawInputDevices(nullptr, &count, sizeof(RAWINPUTDEVICE)) == 0 && count > 0) {
|
||||
std::vector<RAWINPUTDEVICE> devices(count);
|
||||
const UINT got = GetRegisteredRawInputDevices(devices.data(), &count, sizeof(RAWINPUTDEVICE));
|
||||
if (got != static_cast<UINT>(-1))
|
||||
{
|
||||
if (got != static_cast<UINT>(-1)) {
|
||||
raw_registered = got > 0;
|
||||
for (UINT i = 0; i < got; ++i)
|
||||
{
|
||||
for (UINT i = 0; i < got; ++i) {
|
||||
// Generic Desktop (0x01) joystick (0x04) / gamepad (0x05).
|
||||
const bool is_pad =
|
||||
devices[i].usUsagePage == 0x01 && (devices[i].usUsage == 0x04 || devices[i].usUsage == 0x05);
|
||||
if (is_pad)
|
||||
{
|
||||
if (is_pad) {
|
||||
raw_gamepad = true;
|
||||
raw_gamepad_sink = (devices[i].dwFlags & RIDEV_INPUTSINK) != 0;
|
||||
}
|
||||
@@ -248,22 +224,17 @@ void update_input_diagnostics(IpcClient& ipc)
|
||||
|
||||
void release_cursor_tick()
|
||||
{
|
||||
if (g_focus_ipc != nullptr && g_game_hwnd != nullptr && !g_focus_ipc->cursor_clip_allowed())
|
||||
{
|
||||
if (g_focus_ipc != nullptr && g_game_hwnd != nullptr && !g_focus_ipc->cursor_clip_allowed()) {
|
||||
ClipCursor(nullptr); // routes through hk_ClipCursor -> frees the cursor
|
||||
}
|
||||
}
|
||||
|
||||
void remove_focus_spoof()
|
||||
{
|
||||
if (g_game_hwnd != nullptr && g_orig_proc != nullptr)
|
||||
{
|
||||
if (g_unicode)
|
||||
{
|
||||
if (g_game_hwnd != nullptr && g_orig_proc != nullptr) {
|
||||
if (g_unicode) {
|
||||
SetWindowLongPtrW(g_game_hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(g_orig_proc));
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
SetWindowLongPtrA(g_game_hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(g_orig_proc));
|
||||
}
|
||||
}
|
||||
@@ -275,13 +246,12 @@ void remove_focus_spoof()
|
||||
// patched bytes. The reverse of the enable order (GFW first) keeps the invariant "GetActiveWindow
|
||||
// hooked => GetForegroundWindow hooked" across the whole install/remove cycle, so a call never
|
||||
// lands in a half-patched shared region.
|
||||
for (auto it = g_focus_hooks.rbegin(); it != g_focus_hooks.rend(); ++it)
|
||||
{
|
||||
for (auto it = g_focus_hooks.rbegin(); it != g_focus_hooks.rend(); ++it) {
|
||||
disable_for_removal(*it);
|
||||
}
|
||||
disable_for_removal(g_hk_clipcursor);
|
||||
disable_for_removal(g_hk_setcursorpos);
|
||||
ClipCursor(nullptr); // leave the cursor free when the spoof is removed
|
||||
ClipCursor(nullptr); // leave the cursor free when the spoof is removed
|
||||
hook_set_installed(g_id_foreground, false);
|
||||
hook_set_installed(g_id_active, false);
|
||||
hook_set_installed(g_id_focus, false);
|
||||
@@ -298,8 +268,7 @@ void remove_focus_spoof()
|
||||
// DO call the trampoline, so keep them ALIVE (disabled) -- persistent, re-enabled on re-install
|
||||
// (see hook_install.hpp) -- so a stale detour never hits a freed trampoline.
|
||||
g_focus_hooks.clear();
|
||||
if (g_focus_ipc != nullptr)
|
||||
{
|
||||
if (g_focus_ipc != nullptr) {
|
||||
g_focus_ipc->mark_focus_spoof(false, 0);
|
||||
}
|
||||
g_game_hwnd = nullptr;
|
||||
|
||||
Reference in New Issue
Block a user