Add mouse + keyboard forwarding (MKB subsystem, opt-in)

Forward the host window's clicks and keystrokes into the injected game so guests
can drive menus / "Press Start" / text entry that a pad can't.

Protocol (v7->v8): new HookSubsys_Mkb and an SPSC MkbRing of MkbEvents in
SharedBlock (host produces, hook consumes); push/pop helpers.

Hook (hook/src/mkb_hook.cpp, new subsystem): a worker-loop pump drains the ring at
~5 ms and PostMessageW's the matching window messages (WM_KEY*/WM_CHAR, mouse
buttons, WM_MOUSEWHEEL) to the game's main window; it also inline-hooks user32
GetAsyncKeyState / GetKeyboardState / GetCursorPos (stdcall trampolines per the x86
rule) to report a synthesized state so polling games react too. Removing the
subsystem clears all synthesized keys (no stuck input).

Host: the Injection panel gets a "Mouse + keyboard forwarding" subsystem toggle
(opt-in, default off -- the toggle is the hook). host/src/inject/mkb_forward.cpp
reads ImGui IO each frame and forwards only when the host window is focused and
ImGui isn't capturing the event; keyboard always, mouse only while mirroring (clicks
+ wheel, not movement). Mouse coords are mapped through the letterbox to game-client
space (host/src/inject/mkb_map.hpp), accounting for WGC-of-decorated-window vs
hooked/borderless. RawInput/DirectInput games are out of scope for this version.

Verified: new mkb_ring_test + mkb_map_test pass; full build x64 + x86 clean; ctest
x64 9/9 and x86 3/3 green (no regression from the protocol bump). The subsystem is
opt-in, so it can't affect existing behavior unless enabled; the end-to-end
click-into-game path needs live Remote Play + a real game to confirm.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-21 05:06:38 +02:00
parent a32e78ffef
commit 7673f186db
21 changed files with 1003 additions and 27 deletions

93
tests/mkb_map_test.cpp Normal file
View File

@@ -0,0 +1,93 @@
// Unit test for the host->game mouse coordinate mapping (letterbox inverse +
// client-offset handling for WGC-of-decorated-window vs hooked/borderless).
#include <cstdio>
#include "inject/mkb_map.hpp"
using namespace coop;
namespace
{
int g_failures = 0;
void check(bool cond, const char* what)
{
if (!cond)
{
std::printf("FAIL: %s\n", what);
++g_failures;
}
}
} // namespace
int main()
{
// --- Hooked / borderless: frame == client, no offset. dst exactly matches src
// (no letterbox), so the mapping is the identity.
{
MkbMapInput in;
in.dst_w = in.src_w = in.client_w = 1920;
in.dst_h = in.src_h = in.client_h = 1080;
int gx = 0, gy = 0;
check(map_host_to_game_client(in, gx, gy) && gx == 0 && gy == 0, "identity: top-left");
in.host_x = 960;
in.host_y = 540;
check(map_host_to_game_client(in, gx, gy) && gx == 960 && gy == 540, "identity: center");
}
// --- Letterbox: a 1600x900 (16:9) frame fit into a 1000x1000 host window. Scale
// = 1000/1600 = 0.625, vertical bars of (1000 - 562.5)/2 = 218.75 px.
{
MkbMapInput in;
in.dst_w = 1000;
in.dst_h = 1000;
in.src_w = in.client_w = 1600;
in.src_h = in.client_h = 900;
int gx = 0, gy = 0;
// A click in the top letterbox bar is rejected.
in.host_x = 500;
in.host_y = 10;
check(!map_host_to_game_client(in, gx, gy), "letterbox: top bar rejected");
// Center of the host window maps to center of the game frame.
in.host_x = 500;
in.host_y = 500;
check(map_host_to_game_client(in, gx, gy) && gx == 800 && gy == 450, "letterbox: center maps to game center");
// Just inside the image at the top edge (y = 218.75 -> 219) maps near client top.
in.host_x = 500;
in.host_y = 219;
check(map_host_to_game_client(in, gx, gy) && gy >= 0 && gy <= 2, "letterbox: top edge maps near 0");
}
// --- WGC of a decorated window: frame is the whole window (1608x939) with the
// client area (1600x900) offset by an 8 px left border / 31 px title bar. No
// host letterbox here (dst matches the window frame).
{
MkbMapInput in;
in.dst_w = in.src_w = 1608;
in.dst_h = in.src_h = 939;
in.client_off_x = 8;
in.client_off_y = 31;
in.client_w = 1600;
in.client_h = 900;
int gx = 0, gy = 0;
// A click on the title bar (above the client area) is rejected.
in.host_x = 800;
in.host_y = 10;
check(!map_host_to_game_client(in, gx, gy), "decorated: title bar rejected");
// Client top-left corner.
in.host_x = 8;
in.host_y = 31;
check(map_host_to_game_client(in, gx, gy) && gx == 0 && gy == 0, "decorated: client origin");
// A point inside the client area subtracts the decoration offset.
in.host_x = 108;
in.host_y = 131;
check(map_host_to_game_client(in, gx, gy) && gx == 100 && gy == 100, "decorated: client interior");
}
if (g_failures == 0)
{
std::printf("PASS: mkb_map_test\n");
return 0;
}
std::printf("FAIL: %d checks failed\n", g_failures);
return 1;
}