Files
CoopAllTheThings/hook/src/xinput_hook.cpp
BlackMark 0935dccfc4 Per-subsystem hook control: install/remove input, focus, audio at runtime
Add a host->hook control channel (protocol v5 -> v6: HookControl in SharedBlock,
per-subsystem "disabled" flags, 0 = install so the zero-filled default is
unchanged). The worker now reconciles each subsystem every tick: install what's
requested-and-missing, remove what's no longer wanted -- so the audio hooks
re-attach the ring and republish format on a reinstall, and XInput/focus clear
their stale status flags on removal.

Injection panel: a checkbox per subsystem (input forwarding / focus spoof /
audio render-hook) toggles it at runtime, showing the requested vs actual
installed state from the registry, plus DLL heartbeat liveness. The hook-status
section now keys off whether a DLL was injected (host-side) rather than the
input-hook "attached" flag, so it stays visible with input unhooked.

Guards for dependent features: the synthetic-input control is disabled when
input forwarding is off, and the Audio panel explains that mirroring uses
loopback (echo) when the render-hook is off.

Verified against Phantom Brave via coop_audio_probe: starting with audio
requested off installs only input+focus (8 hooks, no capture); re-enabling at
runtime installs the audio hooks (13) and capture starts immediately. All four
tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-19 20:17:48 +02:00

235 lines
6.2 KiB
C++

#include "xinput_hook.hpp"
#include <array>
#include <atomic>
#include <vector>
#include <windows.h>
#include <xinput.h>
#include <safetyhook.hpp>
#include "hook_registry.hpp"
namespace coop::hook
{
namespace
{
// XInput guide-button bit, reported only by the undocumented ordinal-100
// XInputGetStateEx that many games use. Mirrors how Steam/x360ce expose it.
constexpr std::uint16_t kGuideButton = 0x0400;
IpcClient* g_ipc = nullptr;
std::vector<safetyhook::InlineHook> g_hooks;
// Registry ids for the hook list (one per logical export; shared across the
// xinput*.dll variants that may each export it).
int g_id_getstate = -1;
int g_id_getstateex = -1;
int g_id_getcaps = -1;
int g_id_setstate = -1;
// Last good snapshot, so a momentary failed IPC read (host mid-write) doesn't
// flicker the controller as disconnected inside the game.
std::array<CoopPadState, kMaxPads> g_cache;
void refresh_cache()
{
if (g_ipc == nullptr)
{
return;
}
CoopPadState pads[kMaxPads];
std::uint32_t count = 0;
if (g_ipc->snapshot(pads, count))
{
for (std::uint32_t i = 0; i < kMaxPads; ++i)
{
g_cache[i] = pads[i];
}
}
}
void fill_gamepad(const CoopPadState& pad, XINPUT_GAMEPAD& out)
{
out.wButtons = pad.buttons;
out.bLeftTrigger = pad.left_trigger;
out.bRightTrigger = pad.right_trigger;
out.sThumbLX = pad.thumb_lx;
out.sThumbLY = pad.thumb_ly;
out.sThumbRX = pad.thumb_rx;
out.sThumbRY = pad.thumb_ry;
}
// Core of every state query. `keep_guide` drops the guide bit for the plain
// (documented) XInputGetState, which must not report it.
DWORD query_state(DWORD user_index, XINPUT_STATE* state, bool keep_guide)
{
if (state == nullptr || user_index >= kMaxPads)
{
return ERROR_DEVICE_NOT_CONNECTED;
}
if (g_ipc != nullptr)
{
g_ipc->note_state_query(user_index); // proves to the host the game is polling us
}
refresh_cache();
const CoopPadState& pad = g_cache[user_index];
if (!pad.connected)
{
return ERROR_DEVICE_NOT_CONNECTED;
}
XINPUT_STATE result = {};
result.dwPacketNumber = pad.packet;
fill_gamepad(pad, result.Gamepad);
if (!keep_guide)
{
result.Gamepad.wButtons &= ~kGuideButton;
}
*state = result;
return ERROR_SUCCESS;
}
DWORD WINAPI hk_XInputGetState(DWORD user_index, XINPUT_STATE* state)
{
hook_note_call(g_id_getstate);
return query_state(user_index, state, /*keep_guide=*/false);
}
DWORD WINAPI hk_XInputGetStateEx(DWORD user_index, XINPUT_STATE* state)
{
hook_note_call(g_id_getstateex);
return query_state(user_index, state, /*keep_guide=*/true);
}
DWORD WINAPI hk_XInputGetCapabilities(DWORD user_index, DWORD /*flags*/, XINPUT_CAPABILITIES* caps)
{
hook_note_call(g_id_getcaps);
if (caps == nullptr || user_index >= kMaxPads)
{
return ERROR_DEVICE_NOT_CONNECTED;
}
if (g_ipc != nullptr)
{
g_ipc->note_caps_query(user_index);
}
refresh_cache();
if (!g_cache[user_index].connected)
{
return ERROR_DEVICE_NOT_CONNECTED;
}
// Advertise a standard wired Xbox-style gamepad with all controls present.
XINPUT_CAPABILITIES result = {};
result.Type = XINPUT_DEVTYPE_GAMEPAD;
result.SubType = XINPUT_DEVSUBTYPE_GAMEPAD;
result.Flags = 0;
result.Gamepad.wButtons = 0xF3FF; // all standard buttons reachable
result.Gamepad.bLeftTrigger = 0xFF;
result.Gamepad.bRightTrigger = 0xFF;
result.Gamepad.sThumbLX = static_cast<SHORT>(0x7FFF);
result.Gamepad.sThumbLY = static_cast<SHORT>(0x7FFF);
result.Gamepad.sThumbRX = static_cast<SHORT>(0x7FFF);
result.Gamepad.sThumbRY = static_cast<SHORT>(0x7FFF);
*caps = result;
return ERROR_SUCCESS;
}
// Swallow rumble: it would otherwise be sent to whatever physical device sits at
// this index on the host machine. Forwarding it back to the guest is a later
// phase; for now report success so the game's logic is happy.
DWORD WINAPI hk_XInputSetState(DWORD user_index, XINPUT_VIBRATION* /*vibration*/)
{
hook_note_call(g_id_setstate);
if (user_index >= kMaxPads || !g_cache[user_index].connected)
{
return ERROR_DEVICE_NOT_CONNECTED;
}
return ERROR_SUCCESS;
}
void hook_export(HMODULE module, const char* name, void* detour, int registry_id)
{
if (module == nullptr)
{
return;
}
if (void* target = reinterpret_cast<void*>(GetProcAddress(module, name)))
{
g_hooks.emplace_back(safetyhook::create_inline(target, detour));
hook_set_installed(registry_id, true);
}
}
void hook_ordinal(HMODULE module, WORD ordinal, void* detour, int registry_id)
{
if (module == nullptr)
{
return;
}
if (void* target = reinterpret_cast<void*>(GetProcAddress(module, MAKEINTRESOURCEA(ordinal))))
{
g_hooks.emplace_back(safetyhook::create_inline(target, detour));
hook_set_installed(registry_id, true);
}
}
} // namespace
bool install_xinput_hooks(IpcClient& ipc)
{
if (!g_hooks.empty())
{
return true; // already installed
}
g_ipc = &ipc;
refresh_cache();
g_id_getstate = hook_register("XInputGetState", HookSubsys_Input);
g_id_getstateex = hook_register("XInputGetStateEx (ord 100)", HookSubsys_Input);
g_id_getcaps = hook_register("XInputGetCapabilities", HookSubsys_Input);
g_id_setstate = hook_register("XInputSetState", HookSubsys_Input);
// A process generally loads exactly one of these, but hook every one that is
// present so we don't miss the one the game actually calls.
const wchar_t* modules[] = {L"xinput1_4.dll", L"xinput1_3.dll", L"xinput9_1_0.dll", L"xinputuap.dll"};
for (const wchar_t* name : modules)
{
HMODULE module = GetModuleHandleW(name);
if (module == nullptr)
{
continue;
}
hook_export(module, "XInputGetState", reinterpret_cast<void*>(&hk_XInputGetState), g_id_getstate);
hook_ordinal(module, 100, reinterpret_cast<void*>(&hk_XInputGetStateEx), g_id_getstateex);
hook_export(module, "XInputGetCapabilities", reinterpret_cast<void*>(&hk_XInputGetCapabilities),
g_id_getcaps);
hook_export(module, "XInputSetState", reinterpret_cast<void*>(&hk_XInputSetState), g_id_setstate);
}
if (!g_hooks.empty())
{
g_ipc->mark_attached();
return true;
}
return false;
}
void remove_xinput_hooks()
{
g_hooks.clear(); // InlineHook destructor restores the original bytes
hook_set_installed(g_id_getstate, false);
hook_set_installed(g_id_getstateex, false);
hook_set_installed(g_id_getcaps, false);
hook_set_installed(g_id_setstate, false);
if (g_ipc != nullptr)
{
g_ipc->mark_detached();
}
g_ipc = nullptr;
}
} // namespace coop::hook