Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions native/common/hook_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
namespace inputsplit::proto {

constexpr uint32_t kMagic = 0x4C505349; // "ISPL" in little-endian
constexpr uint16_t kProtocolVersion = 5; // 2: DirectInput / Raw Input counters, 3: frame stats,
// 4: tuning (dead zones, button remapping),
// 5: player order in tuning
constexpr uint16_t kProtocolVersion = 6; // 2: DirectInput / Raw Input counters, 3: frame stats,
// 4: tuning, 5: player order, 6: pad bridge

// Pipe name prefix; the game PID (decimal) is appended.
constexpr wchar_t kPipePrefix[] = L"\\\\.\\pipe\\InputSplit.Hook.";
Expand Down Expand Up @@ -159,13 +158,48 @@ struct LogMessage {
char text[480]; // UTF-8, zero-terminated
};

// Pad bridge: PlayStation and Nintendo controllers shown to isolated games as XInput
// controllers. The helper reads the controller (HID) and writes its state into a shared
// memory section; the hook answers XInputGetState for that slot from it and writes
// vibration requests back. One entry per XInput slot.
constexpr wchar_t kPadBridgeName[] = L"Local\\InputSplit.PadBridge";
constexpr uint32_t kPadBridgeMagic = 0x44415042; // "BPAD"

struct BridgedPad {
uint32_t sequence; // odd while the helper writes (read again if it changed or is odd)
uint32_t connected; // 1 while a bridged controller holds this slot
uint32_t packet; // XINPUT_STATE::dwPacketNumber
uint16_t buttons; // XINPUT_GAMEPAD layout (plus 0x0400 Guide)
uint8_t leftTrigger;
uint8_t rightTrigger;
int16_t thumbLX;
int16_t thumbLY;
int16_t thumbRX;
int16_t thumbRY;
uint16_t vendorId;
uint16_t productId;
// Written by the hook (XInputSetState), read by the helper.
uint32_t rumbleSequence;
uint16_t rumbleLeft; // low-frequency (strong) motor
uint16_t rumbleRight; // high-frequency (weak) motor
uint32_t reserved;
};

struct PadBridgeMemory {
uint32_t magic;
uint32_t version; // kProtocolVersion
BridgedPad slots[4];
};

#pragma pack(pop)

static_assert(sizeof(Header) == 16, "Header layout changed");
static_assert(sizeof(HelloMessage) == 40, "HelloMessage layout changed");
static_assert(sizeof(ConfigureMessage) == 32, "ConfigureMessage layout changed");
static_assert(sizeof(FrameStatsMessage) == 24, "FrameStatsMessage layout changed");
static_assert(sizeof(TuningMessage) == 48, "TuningMessage layout changed");
static_assert(sizeof(BridgedPad) == 40, "BridgedPad layout changed");
static_assert(sizeof(PadBridgeMemory) == 168, "PadBridgeMemory layout changed");
static_assert(sizeof(StatusMessage) == 160, "StatusMessage layout changed");
static_assert(sizeof(LogMessage) == 504, "LogMessage layout changed");

Expand Down
1 change: 1 addition & 0 deletions native/helper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_executable(inputsplit-helper
windows_enum.cpp
safety.cpp
controllers.cpp
pad_bridge.cpp
displays.cpp
audio.cpp
injector.cpp
Expand Down
24 changes: 20 additions & 4 deletions native/helper/controllers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ extern "C" {
#include <thread>
#include <vector>

#include "pad_bridge.h"

namespace inputsplit::helper {
namespace {

Expand Down Expand Up @@ -246,13 +248,16 @@ Json SnapshotLocked() {
for (const HidPad& pad : g_hidPads) {
pads.push_back(Json{{"vendorId", pad.vendorId}, {"productId", pad.productId}});
}
return Json{{"controllers", list}, {"hidGamepads", pads}};
return Json{{"controllers", list},
{"hidGamepads", pads},
{"bridged", PadBridge::Instance().List()}};
}

void EmitTestState(int slot, DWORD& lastPacket, bool& wasConnected) {
XINPUT_STATE state{};
GetStateFn read = g_api.getStateEx != nullptr ? g_api.getStateEx : g_api.getState;
bool connected = read(static_cast<DWORD>(slot), &state) == ERROR_SUCCESS;
bool connected = read(static_cast<DWORD>(slot), &state) == ERROR_SUCCESS ||
PadBridge::Instance().Read(slot, state);
if (!connected) {
if (wasConnected) SendEvent("controllerState", Json{{"slot", slot}, {"connected", false}});
wasConnected = false;
Expand Down Expand Up @@ -284,8 +289,9 @@ void PollHotkey(const bool connected[XUSER_MAX_COUNT], const bool enabled[kShort
const auto now = std::chrono::steady_clock::now();
for (int slot = 0; slot < XUSER_MAX_COUNT; ++slot) {
XINPUT_STATE state{};
bool read = connected[slot] &&
g_api.getState(static_cast<DWORD>(slot), &state) == ERROR_SUCCESS;
bool read = connected[slot]
? g_api.getState(static_cast<DWORD>(slot), &state) == ERROR_SUCCESS
: PadBridge::Instance().Read(slot, state);
for (size_t i = 0; i < kShortcutCount; ++i) {
HotkeyTracker& tracker = trackers[slot][i];
bool pressed = enabled[i] && read &&
Expand Down Expand Up @@ -447,6 +453,16 @@ void ControllerMonitor::StartTest(int slot) {
g_wake.notify_all();
}

bool ControllerMonitor::SlotConnected(int slot) {
std::lock_guard<std::mutex> lock(g_mutex);
return slot >= 0 && slot < XUSER_MAX_COUNT && g_slots[slot].connected;
}

void ControllerMonitor::MarkChanged() {
std::lock_guard<std::mutex> lock(g_mutex);
SendEvent("controllersChanged", SnapshotLocked());
}

void ControllerMonitor::MeasurePollingRate(int slot) {
static std::atomic<bool> busy{false};
if (slot < 0 || slot >= XUSER_MAX_COUNT || g_api.getState == nullptr || busy.exchange(true)) {
Expand Down
4 changes: 4 additions & 0 deletions native/helper/controllers.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class ControllerMonitor {
// Short vibration as feedback ("on" one pulse, "off" two, "error" three). Returns false
// when XInput cannot vibrate.
bool Rumble(int slot, const std::string& pattern);
// A real XInput controller is in `slot` (as of the last presence check).
bool SlotConnected(int slot);
// Sends a fresh "controllersChanged" (the pad bridge took or released a slot).
void MarkChanged();

private:
ControllerMonitor() = default;
Expand Down
6 changes: 6 additions & 0 deletions native/helper/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "../common/hook_protocol.h"
#include "../common/version.h"
#include "controllers.h"
#include "pad_bridge.h"
#include "displays.h"
#include "process_info.h"
#include "rpc.h"
Expand Down Expand Up @@ -114,6 +115,10 @@ Json Handle(const std::string& method, const Json& params) {
ControllerMonitor::Instance().StartTest(params.at("slot").get<int>());
return Json::object();
}
if (method == "setPadBridge") {
PadBridge::Instance().SetEnabled(params.at("enabled").get<bool>());
return Json{{"enabled", PadBridge::Instance().enabled()}};
}
if (method == "measurePollingRate") {
ControllerMonitor::Instance().MeasurePollingRate(params.at("slot").get<int>());
return Json::object();
Expand Down Expand Up @@ -304,6 +309,7 @@ int wmain(int argc, wchar_t** argv) {
// stdin closed or shutdown requested: leave every game in its normal state.
SessionManager::Instance().StopAll("helperShutdown", 2000);
StopSystemWindow();
PadBridge::Instance().Shutdown();
ControllerMonitor::Instance().Stop();
fflush(stdout);
// Detached request threads may still be running; ExitProcess ends them cleanly.
Expand Down
Loading
Loading