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>
This commit is contained in:
2026-06-19 20:17:48 +02:00
parent 1f905940ef
commit 0935dccfc4
12 changed files with 246 additions and 19 deletions

View File

@@ -88,7 +88,25 @@ void AudioPanel::draw_ui(const HookStatusView& status, bool debug_details)
// hooked path silences it, so don't warn there.
if (mirror_.source() == AudioMirror::Source::Loopback)
{
ImGui::TextDisabled("Game audio also plays locally (echo). Inject the hook to remove it.");
bool audio_hook_on = false;
const std::uint32_t hn =
status.hook_entry_count < kMaxHookEntries ? status.hook_entry_count : kMaxHookEntries;
for (std::uint32_t i = 0; i < hn; ++i)
{
if (status.hook_entries[i].subsystem == HookSubsys_Audio && status.hook_entries[i].installed)
{
audio_hook_on = true;
break;
}
}
if (audio_hook_on)
{
ImGui::TextDisabled("Game audio also plays locally (echo).");
}
else
{
ImGui::TextDisabled("Audio render-hook is off -> loopback (echo). Enable it in the Injection panel.");
}
}
// --- Render-stream view -----------------------------------------------

View File

@@ -92,9 +92,15 @@ void InjectionPanel::inject_selected()
return;
}
// Publish the desired subsystem state before the hook's first reconcile tick.
server_.set_subsystem_enabled(HookSubsys_Input, want_input_);
server_.set_subsystem_enabled(HookSubsys_Focus, want_focus_);
server_.set_subsystem_enabled(HookSubsys_Audio, want_audio_);
const InjectResult result = inject_dll(selected_pid_, hook_dll_path());
if (result.status == InjectStatus::Ok)
{
injected_ = true;
status_ = "Injected into " + narrow(selected_name_) + " (pid " + std::to_string(selected_pid_) + ").";
status_color_ = kGreen;
}
@@ -200,6 +206,62 @@ void InjectionPanel::draw_hook_list(const HookStatusView& status)
}
}
// True if any hook of `subsystem` is currently installed in the game.
static bool subsystem_installed(const HookStatusView& status, std::uint32_t subsystem)
{
const std::uint32_t n = status.hook_entry_count < kMaxHookEntries ? status.hook_entry_count : kMaxHookEntries;
for (std::uint32_t i = 0; i < n; ++i)
{
if (status.hook_entries[i].subsystem == subsystem && status.hook_entries[i].installed)
{
return true;
}
}
return false;
}
void InjectionPanel::draw_subsystem_controls(const HookStatusView& status)
{
ImGui::SeparatorText("Subsystems (hook / unhook)");
struct Row
{
const char* label;
std::uint32_t subsystem;
bool* want;
const char* depends;
};
const Row rows[] = {
{"Input forwarding (XInput)", HookSubsys_Input, &want_input_, "controller input reaches the game"},
{"Focus spoof", HookSubsys_Focus, &want_focus_, "the game keeps running unfocused"},
{"Audio render-hook", HookSubsys_Audio, &want_audio_, "audio mirror without echo"},
};
for (const Row& r : rows)
{
ImGui::PushID(r.label);
if (ImGui::Checkbox(r.label, r.want))
{
server_.set_subsystem_enabled(r.subsystem, *r.want);
}
ImGui::SameLine();
const bool on = subsystem_installed(status, r.subsystem);
if (*r.want != on)
{
ImGui::TextColored(kGrey, "(%s...)", *r.want ? "installing" : "removing");
}
else
{
ImGui::TextColored(on ? kGreen : kGrey, on ? "installed" : "off");
}
if (!*r.want)
{
ImGui::TextDisabled(" off: %s won't work", r.depends);
}
ImGui::PopID();
}
}
void InjectionPanel::draw_hook_status(bool debug_details)
{
if (!server_.running())
@@ -209,16 +271,37 @@ void InjectionPanel::draw_hook_status(bool debug_details)
const HookStatusView status = server_.hook_status();
ImGui::SeparatorText("Hook status");
if (!status.attached)
// DLL liveness from the heartbeat (advances ~4x/s while the worker runs).
const double now = ImGui::GetTime();
if (status.heartbeat != last_heartbeat_)
{
ImGui::TextColored(kGrey, "Waiting for hook to attach in the game...");
last_heartbeat_ = status.heartbeat;
last_heartbeat_time_ = now;
dll_alive_ = true;
}
else if (now - last_heartbeat_time_ > 1.5)
{
dll_alive_ = false;
}
ImGui::SeparatorText("Hook status");
if (!injected_)
{
ImGui::TextColored(kGrey, "Not injected.");
return;
}
ImGui::TextColored(kGreen, "Attached (game pid %u)", status.game_pid);
ImGui::TextColored(status.focus_spoof ? kGreen : kGrey, "Focus spoof: %s",
status.focus_spoof ? "active" : "inactive");
if (dll_alive_)
{
ImGui::TextColored(kGreen, "Hook DLL loaded in pid %lu (heartbeat %u)", server_.target_pid(),
status.heartbeat);
}
else
{
ImGui::TextColored(kRed, "Hook DLL not responding (no heartbeat).");
}
draw_subsystem_controls(status);
ImGui::TextDisabled("Controller poll rates are in the Controllers panel.");
draw_hook_list(status);
@@ -263,10 +346,11 @@ void InjectionPanel::draw(bool debug_details)
if (server_.running())
{
ImGui::TextColored(kGreen, "Forwarding input to pid %lu", server_.target_pid());
if (ImGui::Button("Stop forwarding"))
ImGui::TextColored(kGreen, "Connected to pid %lu", server_.target_pid());
if (ImGui::Button("Disconnect"))
{
server_.stop();
injected_ = false;
status_ = "Stopped.";
status_color_ = kGrey;
}
@@ -315,7 +399,10 @@ void InjectionPanel::draw(bool debug_details)
ImGui::TextColored(status_color_, "%s", status_.c_str());
}
// Synthetic input only reaches the game if the XInput hook is installed.
ImGui::BeginDisabled(injected_ && !want_input_);
ImGui::Checkbox("Forward synthetic test input", &test_input_);
ImGui::EndDisabled();
if (test_input_)
{
ImGui::SameLine();

View File

@@ -45,6 +45,7 @@ public:
private:
void refresh_processes();
void inject_selected();
void draw_subsystem_controls(const HookStatusView& status);
void draw_hook_list(const HookStatusView& status);
void draw_hook_status(bool debug_details);
@@ -58,6 +59,18 @@ private:
ImVec4 status_color_;
bool test_input_ = false;
// Host-requested per-subsystem install state (default on). Written to the hook
// over the control channel; the hook reconciles each tick.
bool want_input_ = true;
bool want_focus_ = true;
bool want_audio_ = true;
bool injected_ = false; // a hook DLL is loaded in the target
// Heartbeat liveness tracking (is the injected DLL responding?).
std::uint32_t last_heartbeat_ = 0;
double last_heartbeat_time_ = 0.0;
bool dll_alive_ = false;
};
} // namespace coop

View File

@@ -79,6 +79,15 @@ HookStatusView IpcServer::hook_status() const
return view;
}
void IpcServer::set_subsystem_enabled(std::uint32_t subsystem, bool enabled)
{
if (block_ != nullptr && subsystem < HookSubsys_Count)
{
// 0 = install, 1 = remove.
block_->control.subsystem_disabled[subsystem].store(enabled ? 0u : 1u, std::memory_order_release);
}
}
void IpcServer::stop()
{
if (block_ != nullptr)

View File

@@ -52,6 +52,10 @@ public:
// Reads the hook's diagnostics back-channel (zeroed if not started).
[[nodiscard]] HookStatusView hook_status() const;
// Request a hook subsystem be installed (true) or removed (false). The hook
// reconciles on its next tick. No-op if not started.
void set_subsystem_enabled(std::uint32_t subsystem, bool enabled);
[[nodiscard]] bool running() const
{
return block_ != nullptr;