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:
@@ -20,6 +20,17 @@ add_executable(audio_ring_test audio_ring_test.cpp)
|
||||
target_link_libraries(audio_ring_test PRIVATE coop_common)
|
||||
add_test(NAME audio_ring_test COMMAND audio_ring_test)
|
||||
|
||||
# Unit test for the MKB event ring (SPSC push/pop, wrap-around, full/empty).
|
||||
add_executable(mkb_ring_test mkb_ring_test.cpp)
|
||||
target_link_libraries(mkb_ring_test PRIVATE coop_common)
|
||||
add_test(NAME mkb_ring_test COMMAND mkb_ring_test)
|
||||
|
||||
# Unit test for the host->game mouse coordinate mapping (letterbox inverse +
|
||||
# decorated-window client offset). Header-only, no device.
|
||||
add_executable(mkb_map_test mkb_map_test.cpp)
|
||||
target_include_directories(mkb_map_test PRIVATE ${CMAKE_SOURCE_DIR}/host/src)
|
||||
add_test(NAME mkb_map_test COMMAND mkb_map_test)
|
||||
|
||||
# Integration test for WASAPI process-loopback capture. Reuses the shipping
|
||||
# capture code and captures from coop_tone (a known sine-wave render process).
|
||||
add_executable(audio_loopback_test
|
||||
@@ -114,6 +125,8 @@ add_test(NAME opengl_hook_test COMMAND opengl_hook_test)
|
||||
coop_output_subdir(tests
|
||||
hook_selftest
|
||||
audio_ring_test
|
||||
mkb_ring_test
|
||||
mkb_map_test
|
||||
audio_loopback_test
|
||||
audio_hook_test
|
||||
srgb_format_test
|
||||
|
||||
93
tests/mkb_map_test.cpp
Normal file
93
tests/mkb_map_test.cpp
Normal 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;
|
||||
}
|
||||
82
tests/mkb_ring_test.cpp
Normal file
82
tests/mkb_ring_test.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
// Unit test for the MKB event ring (SPSC push/pop, wrap-around, full/empty).
|
||||
#include <cstdio>
|
||||
|
||||
#include "coop/protocol.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()
|
||||
{
|
||||
MkbRing ring{};
|
||||
|
||||
// Empty pop fails.
|
||||
MkbEvent out{};
|
||||
check(!pop_mkb_event(ring, out), "pop on empty ring returns false");
|
||||
|
||||
// Push then pop returns the same event, FIFO.
|
||||
for (std::uint32_t i = 0; i < 10; ++i)
|
||||
{
|
||||
MkbEvent ev{Mkb_KeyDown, i, static_cast<int>(i) * 2, static_cast<int>(i) * 3};
|
||||
check(push_mkb_event(ring, ev), "push succeeds with room");
|
||||
}
|
||||
for (std::uint32_t i = 0; i < 10; ++i)
|
||||
{
|
||||
check(pop_mkb_event(ring, out), "pop succeeds with data");
|
||||
check(out.code == i && out.x == static_cast<int>(i) * 2 && out.y == static_cast<int>(i) * 3,
|
||||
"popped event matches pushed (FIFO)");
|
||||
}
|
||||
check(!pop_mkb_event(ring, out), "ring empty again after draining");
|
||||
|
||||
// Fill to capacity, then one more push is dropped.
|
||||
for (std::uint32_t i = 0; i < kMkbQueueSize; ++i)
|
||||
{
|
||||
check(push_mkb_event(ring, MkbEvent{Mkb_Char, i, 0, 0}), "push fills to capacity");
|
||||
}
|
||||
check(!push_mkb_event(ring, MkbEvent{Mkb_Char, 999, 0, 0}), "push on full ring is dropped");
|
||||
|
||||
// Drain and verify order survived a full buffer.
|
||||
for (std::uint32_t i = 0; i < kMkbQueueSize; ++i)
|
||||
{
|
||||
check(pop_mkb_event(ring, out) && out.code == i, "full-buffer drain is in order");
|
||||
}
|
||||
|
||||
// Wrap-around: indices are free-running, so many cycles must keep working.
|
||||
std::uint32_t produced = 0, consumed = 0;
|
||||
for (int cycle = 0; cycle < 1000; ++cycle)
|
||||
{
|
||||
for (int k = 0; k < 50; ++k)
|
||||
{
|
||||
if (push_mkb_event(ring, MkbEvent{Mkb_MouseDown, produced, 0, 0}))
|
||||
{
|
||||
++produced;
|
||||
}
|
||||
}
|
||||
while (pop_mkb_event(ring, out))
|
||||
{
|
||||
check(out.code == consumed, "wrap-around preserves FIFO order");
|
||||
++consumed;
|
||||
}
|
||||
}
|
||||
check(produced == consumed, "all wrap-around events consumed");
|
||||
|
||||
if (g_failures == 0)
|
||||
{
|
||||
std::printf("PASS: mkb_ring_test\n");
|
||||
return 0;
|
||||
}
|
||||
std::printf("FAIL: %d checks failed\n", g_failures);
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user