Skip to content

Commit

Permalink
Add Xbox Guide Button as a bindable hotkey
Browse files Browse the repository at this point in the history
  • Loading branch information
oneup03 committed Sep 11, 2024
1 parent 76e1be5 commit 082a53f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
53 changes: 52 additions & 1 deletion vrto3d/src/hmd_device_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,55 @@
// Link the XInput library
#pragma comment(lib, "XInput.lib")

//-----------------------------------------------------------------------------
// Purpose:
// Set a function pointer to the xinput get state call. By default, set it to
// XInputGetState() in whichever xinput we are linked to (xinput9_1_0.dll). If
// the d3dx.ini is using the guide button we will try to switch to either
// xinput 1.3 or 1.4 to get access to the undocumented XInputGetStateEx() call.
// We can't rely on these existing on Win7 though, so if we fail to load them
// don't treat it as fatal and continue using the original one.
//-----------------------------------------------------------------------------
static HMODULE xinput_lib;
typedef DWORD(WINAPI* tXInputGetState)(DWORD dwUserIndex, XINPUT_STATE* pState);
static tXInputGetState _XInputGetState = XInputGetState;
static void SwitchToXinpuGetStateEx()
{
tXInputGetState XInputGetStateEx;

if (xinput_lib)
return;

// 3DMigoto is linked against xinput9_1_0.dll, but that version does
// not export XInputGetStateEx to get the guide button. Try loading
// xinput 1.3 and 1.4, which both support this functionality.
xinput_lib = LoadLibrary(L"xinput1_3.dll");
if (xinput_lib) {
DriverLog("Loaded xinput1_3.dll for guide button support\n");
}
else {
xinput_lib = LoadLibrary(L"xinput1_4.dll");
if (xinput_lib) {
DriverLog("Loaded xinput1_4.dll for guide button support\n");
}
else {
DriverLog("ERROR: Unable to load xinput 1.3 or 1.4: Guide button will not be available\n");
return;
}
}

// Unnamed and undocumented exports FTW
LPCSTR XInputGetStateExOrdinal = (LPCSTR)100;
XInputGetStateEx = (tXInputGetState)GetProcAddress(xinput_lib, XInputGetStateExOrdinal);
if (!XInputGetStateEx) {
DriverLog("ERROR: Unable to get XInputGetStateEx: Guide button will not be available\n");
return;
}

_XInputGetState = XInputGetStateEx;
}


// Load settings from default.vrsettings
static const char *stereo_main_settings_section = "driver_vrto3d";
static const char *stereo_display_settings_section = "vrto3d_display";
Expand All @@ -50,6 +99,8 @@ MockControllerDeviceDriver::MockControllerDeviceDriver()
DriverLog( "VRto3D Model Number: %s", stereo_model_number_.c_str() );
DriverLog( "VRto3D Serial Number: %s", stereo_serial_number_.c_str() );

SwitchToXinpuGetStateEx();

// Display settings
StereoDisplayDriverConfiguration display_configuration{};
display_configuration.window_x = 0;
Expand Down Expand Up @@ -769,7 +820,7 @@ void StereoDisplayComponent::CheckUserSettings(uint32_t device_index)
XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));
// Get the state of the first controller (index 0)
DWORD dwResult = XInputGetState(0, &state);
DWORD dwResult = _XInputGetState(0, &state);

if (((config_.ctrl_xinput && dwResult == ERROR_SUCCESS &&
((config_.ctrl_toggle_key == XINPUT_GAMEPAD_LEFT_TRIGGER && state.Gamepad.bLeftTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD)
Expand Down
1 change: 1 addition & 0 deletions vrto3d/src/key_mappings.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ static std::unordered_map<std::string, int> XInputMappings = {
{"XINPUT_GAMEPAD_DPAD_RIGHT", XINPUT_GAMEPAD_DPAD_RIGHT},
{"XINPUT_GAMEPAD_START", XINPUT_GAMEPAD_START},
{"XINPUT_GAMEPAD_BACK", XINPUT_GAMEPAD_BACK},
{"XINPUT_GAMEPAD_GUIDE", 0x400},
{"XINPUT_GAMEPAD_LEFT_THUMB", XINPUT_GAMEPAD_LEFT_THUMB},
{"XINPUT_GAMEPAD_RIGHT_THUMB", XINPUT_GAMEPAD_RIGHT_THUMB}
};
Expand Down
2 changes: 1 addition & 1 deletion vrto3d/vrto3d/resources/settings/default.vrsettings
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"user_key_type1": "switch",
"user_depth1": 0.5,
"user_convergence1": 0.02,
"user_load_key2": "XINPUT_GAMEPAD_START",
"user_load_key2": "XINPUT_GAMEPAD_GUIDE",
"user_store_key2": "VK_NUMPAD5",
"user_key_type2": "toggle",
"user_depth2": 0.1,
Expand Down

0 comments on commit 082a53f

Please sign in to comment.