From d6b5bbaeac1168d1aae9e8be00941d6e373bcc34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Ma=C5=A1karinec?= Date: Sat, 30 Nov 2024 19:24:27 +0100 Subject: [PATCH] Add xinput dll dynamic loading MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marek Maškarinec --- Makefile | 2 +- lib/umka | 2 +- src/input.c | 33 +++++++++++++++++++++++++++++++-- src/tophat.h | 2 ++ src/window.c | 1 + 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 1de0c48..b67e630 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/lib/umka b/lib/umka index 131802d..34e70df 160000 --- a/lib/umka +++ b/lib/umka @@ -1 +1 @@ -Subproject commit 131802d52887e6a6ed30e8fbdfb3fb922dd13ffb +Subproject commit 34e70df085b7dec3d483493d12804f7362c70ec5 diff --git a/src/input.c b/src/input.c index fcda166..4dadad7 100644 --- a/src/input.c +++ b/src/input.c @@ -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; @@ -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 +} diff --git a/src/tophat.h b/src/tophat.h index 93fdfdb..8e8e6c4 100644 --- a/src/tophat.h +++ b/src/tophat.h @@ -536,6 +536,8 @@ void th_input_reset(); void th_input_update_gamepads(); +void +th_input_init(void); // misc void diff --git a/src/window.c b/src/window.c index 9e8886e..52f991c 100644 --- a/src/window.c +++ b/src/window.c @@ -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);