|
| 1 | +#include "pch.h" |
| 2 | +#include <libloaderapi.h> |
| 3 | + |
| 4 | +#include "util.h" |
| 5 | + |
| 6 | +namespace pluginloader::proxy { |
| 7 | + |
| 8 | +namespace { |
| 9 | + |
| 10 | +HMODULE xinput_dll_handle = nullptr; |
| 11 | + |
| 12 | +FARPROC xinput_enable_ptr = nullptr; |
| 13 | +FARPROC xinput_get_battery_information_ptr = nullptr; |
| 14 | +FARPROC xinput_get_capabilities_ptr = nullptr; |
| 15 | +FARPROC xinput_get_dsound_audio_device_guids_ptr = nullptr; |
| 16 | +FARPROC xinput_get_keystroke_ptr = nullptr; |
| 17 | +FARPROC xinput_get_state_ptr = nullptr; |
| 18 | +FARPROC xinput_set_state_ptr = nullptr; |
| 19 | + |
| 20 | +const constexpr auto XINPUT_GET_STATE_EX_ORDINAL = 100; |
| 21 | +FARPROC xinput_get_state_ex_ptr = nullptr; |
| 22 | + |
| 23 | +// NOLINTBEGIN(readability-identifier-naming, readability-identifier-length) |
| 24 | + |
| 25 | +DLL_EXPORT void XInputEnable(BOOL enable) { |
| 26 | + return reinterpret_cast<decltype(&XInputEnable)>(xinput_enable_ptr)(enable); |
| 27 | +} |
| 28 | + |
| 29 | +DLL_EXPORT DWORD XInputGetBatteryInformation(DWORD dwUserIndex, |
| 30 | + BYTE devType, |
| 31 | + void* pBatteryInformation) { |
| 32 | + return reinterpret_cast<decltype(&XInputGetBatteryInformation)>( |
| 33 | + xinput_get_battery_information_ptr)(dwUserIndex, devType, pBatteryInformation); |
| 34 | +} |
| 35 | + |
| 36 | +DLL_EXPORT DWORD XInputGetCapabilities(DWORD dwUserIndex, DWORD dwFlags, void* pCapabilities) { |
| 37 | + return reinterpret_cast<decltype(&XInputGetCapabilities)>(xinput_get_capabilities_ptr)( |
| 38 | + dwUserIndex, dwFlags, pCapabilities); |
| 39 | +} |
| 40 | + |
| 41 | +DLL_EXPORT DWORD XInputGetDSoundAudioDeviceGuids(DWORD dwUserIndex, |
| 42 | + GUID* pDSoundRenderGuid, |
| 43 | + GUID* pDSoundCaptureGuid) { |
| 44 | + return reinterpret_cast<decltype(&XInputGetDSoundAudioDeviceGuids)>( |
| 45 | + xinput_get_dsound_audio_device_guids_ptr)(dwUserIndex, pDSoundRenderGuid, |
| 46 | + pDSoundCaptureGuid); |
| 47 | +} |
| 48 | + |
| 49 | +DLL_EXPORT DWORD XInputGetKeystroke(DWORD dwUserIndex, DWORD dwReserved, void* pKeystroke) { |
| 50 | + return reinterpret_cast<decltype(&XInputGetKeystroke)>(xinput_get_keystroke_ptr)( |
| 51 | + dwUserIndex, dwReserved, pKeystroke); |
| 52 | +} |
| 53 | + |
| 54 | +DLL_EXPORT DWORD XInputGetState(DWORD dwUserIndex, void* pState) { |
| 55 | + return reinterpret_cast<decltype(&XInputGetState)>(xinput_get_state_ptr)(dwUserIndex, pState); |
| 56 | +} |
| 57 | + |
| 58 | +DLL_EXPORT DWORD XInputSetState(DWORD dwUserIndex, void* pVibration) { |
| 59 | + return reinterpret_cast<decltype(&XInputSetState)>(xinput_set_state_ptr)(dwUserIndex, |
| 60 | + pVibration); |
| 61 | +} |
| 62 | + |
| 63 | +DLL_EXPORT DWORD XInputGetStateEx(DWORD dwUserIndex, void* pState) { |
| 64 | + return reinterpret_cast<decltype(&XInputGetStateEx)>(xinput_get_state_ex_ptr)(dwUserIndex, |
| 65 | + pState); |
| 66 | +} |
| 67 | + |
| 68 | +// NOLINTEND(readability-identifier-naming, readability-identifier-length) |
| 69 | + |
| 70 | +} // namespace |
| 71 | + |
| 72 | +void init(HMODULE /*this_dll*/) { |
| 73 | + // Suspend all other threads to prevent a giant race condition |
| 74 | + const util::ThreadSuspender suspender{}; |
| 75 | + |
| 76 | + wchar_t buf[MAX_PATH]; |
| 77 | + if (GetSystemDirectoryW(&buf[0], ARRAYSIZE(buf)) == 0) { |
| 78 | + std::cerr << "Unable to find system dll directory! We're probably about to crash.\n"; |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + auto system_xinput = std::filesystem::path{static_cast<wchar_t*>(buf)} / "xinput1_3.dll"; |
| 83 | + xinput_dll_handle = LoadLibraryA(system_xinput.generic_string().c_str()); |
| 84 | + |
| 85 | + xinput_enable_ptr = GetProcAddress(xinput_dll_handle, "XInputEnable"); |
| 86 | + xinput_get_battery_information_ptr = |
| 87 | + GetProcAddress(xinput_dll_handle, "XInputGetBatteryInformation"); |
| 88 | + xinput_get_capabilities_ptr = GetProcAddress(xinput_dll_handle, "XInputGetCapabilities"); |
| 89 | + xinput_get_dsound_audio_device_guids_ptr = |
| 90 | + GetProcAddress(xinput_dll_handle, "XInputGetDSoundAudioDeviceGuids"); |
| 91 | + xinput_get_keystroke_ptr = GetProcAddress(xinput_dll_handle, "XInputGetKeystroke"); |
| 92 | + xinput_get_state_ptr = GetProcAddress(xinput_dll_handle, "XInputGetState"); |
| 93 | + xinput_set_state_ptr = GetProcAddress(xinput_dll_handle, "XInputSetState"); |
| 94 | + |
| 95 | + xinput_get_state_ex_ptr = |
| 96 | + GetProcAddress(xinput_dll_handle, reinterpret_cast<LPCSTR>(XINPUT_GET_STATE_EX_ORDINAL)); |
| 97 | +} |
| 98 | + |
| 99 | +void free(void) { |
| 100 | + if (xinput_dll_handle != nullptr) { |
| 101 | + FreeLibrary(xinput_dll_handle); |
| 102 | + xinput_dll_handle = nullptr; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +} // namespace pluginloader::proxy |
0 commit comments