Skip to content

Commit

Permalink
Add xinput dll dynamic loading
Browse files Browse the repository at this point in the history
Signed-off-by: Marek Maškarinec <[email protected]>
  • Loading branch information
marekmaskarinec committed Nov 30, 2024
1 parent 713fd9a commit d6b5bba
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ifeq ($(PLATFORM), Linux)
UMKA_BIN = ./lib/umka/build/umka
else
ifeq ($(SHORT_PLATFORM), MINGW64_NT)
LDLIBS += -lm -lopengl32 -lgdi32 -Wl,-Bstatic -lpthread -lxinput
LDLIBS += -lm -lopengl32 -lgdi32 -Wl,-Bstatic -lpthread
TARGET=tophat.exe
DEFS += -DNO_OPENGL_HEADERS
UMKA_BIN = ./lib/umka/build/umka.exe
Expand Down
33 changes: 31 additions & 2 deletions src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,31 @@ i_update_button(th_gamepad_button *btn, float value)
btn->pressure = value;
}

#ifdef _WIN32
typedef DWORD(WINAPI *XInputGetState_t)(DWORD, XINPUT_STATE *);
XInputGetState_t _th_XInputGetState = NULL;
typedef DWORD(WINAPI *XInputSetState_t)(DWORD, XINPUT_VIBRATION *);
XInputSetState_t _th_XInputSetState = NULL;
#endif

void
th_input_update_gamepads()
{
#ifdef _WIN32
if (!_th_XInputGetState)
return;

XINPUT_STATE state;
for (int i = 0; i < 4; i++) {
state = (XINPUT_STATE){0};

th_generic_gamepad *gp = &thg->gamepad[i];

if (XInputGetState(i, &state) == ERROR_SUCCESS) {
if (_th_XInputGetState(i, &state) == ERROR_SUCCESS) {
XINPUT_VIBRATION vib = {0};
vib.wLeftMotorSpeed = (gp->rumble_left * 65535);
vib.wRightMotorSpeed = (gp->rumble_right * 65535);
XInputSetState(i, &vib);
_th_XInputSetState(i, &vib);

gp->rumble_left = 0;
gp->rumble_right = 0;
Expand Down Expand Up @@ -170,3 +180,22 @@ th_input_update_gamepads()
}
#endif
}

void
th_input_init(void)
{
#ifdef _WIN32
HMODULE xinput = LoadLibraryA("xinput1_4.dll");
if (!xinput) {
printf("Failed to load xinput1_4.dll, falling back to xinput1_3.dll\n");
xinput = LoadLibraryA("xinput1_3.dll");
if (!xinput) {
printf("Failed to load xinput1_3.dll, gamepad support will be disabled\n");
return;
}
}

_th_XInputGetState = (XInputGetState_t)GetProcAddress(xinput, "XInputGetState");
_th_XInputSetState = (XInputSetState_t)GetProcAddress(xinput, "XInputSetState");
#endif
}
2 changes: 2 additions & 0 deletions src/tophat.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ void
th_input_reset();
void
th_input_update_gamepads();
void
th_input_init(void);

// misc
void
Expand Down
1 change: 1 addition & 0 deletions src/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ init(void)
th_nav_init();
th_canvas_init();
th_image_init();
th_input_init();

if (umkaAlive(thg->umka)) {
int code = umkaCall(thg->umka, &thg->umka_init);
Expand Down

0 comments on commit d6b5bba

Please sign in to comment.