Skip to content

Commit a6a9933

Browse files
authored
Merge pull request #1 from apple1417/master
add `xinput1_3.dll` proxy
2 parents f3adbe5 + 85bec35 commit a6a9933

File tree

5 files changed

+127
-5
lines changed

5 files changed

+127
-5
lines changed

CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.24)
22

3-
project(pluginloader VERSION 1.0.0)
3+
project(pluginloader VERSION 1.0.1)
44

55
add_library(_pluginloader_base INTERFACE)
66
set(CMAKE_EXPORT_COMPILE_COMMANDS True)
@@ -9,7 +9,6 @@ target_compile_features(_pluginloader_base INTERFACE cxx_std_20)
99
set_target_properties(_pluginloader_base PROPERTIES
1010
COMPILE_WARNING_AS_ERROR True
1111
INTERPROCEDURAL_OPTIMIZATION True
12-
PREFIX ""
1312
)
1413
if(MSVC)
1514
target_compile_options(_pluginloader_base INTERFACE /W4)
@@ -41,7 +40,11 @@ target_precompile_headers(_pluginloader_base INTERFACE "src/pch.h")
4140

4241
function(pluginloader_add_impl name)
4342
add_library(pluginloader_${name} SHARED ${ARGN})
44-
set_target_properties(pluginloader_${name} PROPERTIES OUTPUT_NAME ${name})
43+
set_target_properties(pluginloader_${name} PROPERTIES
44+
PREFIX ""
45+
OUTPUT_NAME ${name}
46+
SUFFIX ".dll"
47+
)
4548
target_link_libraries(pluginloader_${name} PUBLIC _pluginloader_base)
4649
endfunction()
4750

@@ -50,10 +53,12 @@ endfunction()
5053

5154
pluginloader_add_impl(no_proxy "src/proxy/none.cpp")
5255
pluginloader_add_impl(d3d11 "src/proxy/d3d11.cpp")
56+
pluginloader_add_impl(xinput1_3 "src/proxy/xinput1_3.cpp" "src/proxy/xinput1_3.def")
5357

5458
install(
5559
TARGETS
5660
pluginloader_no_proxy
5761
pluginloader_d3d11
62+
pluginloader_xinput1_3
5863
RUNTIME DESTINATION .
5964
)

common_cmake

src/proxy/d3d11.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void init(HMODULE /*this_dll*/) {
6666

6767
wchar_t buf[MAX_PATH];
6868
if (GetSystemDirectoryW(&buf[0], ARRAYSIZE(buf)) == 0) {
69-
std::cerr << "[dhf] Unable to find system dll directory! We're probably about to crash.\n";
69+
std::cerr << "Unable to find system dll directory! We're probably about to crash.\n";
7070
return;
7171
}
7272

src/proxy/xinput1_3.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

src/proxy/xinput1_3.def

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
LIBRARY XINPUT1_3
2+
EXPORTS
3+
DllMain @1
4+
XInputGetState @2
5+
XInputSetState @3
6+
XInputGetCapabilities @4
7+
XInputEnable @5
8+
XInputGetDSoundAudioDeviceGuids @6
9+
XInputGetBatteryInformation @7
10+
XInputGetKeystroke @8
11+
XInputGetStateEx @100

0 commit comments

Comments
 (0)