From 920254732c0378e31557700eedce795fd5c6de8d Mon Sep 17 00:00:00 2001 From: kozec Date: Sat, 10 Feb 2018 22:57:13 +0100 Subject: [PATCH] Fix: Initializing dinput in xinput init causes race condition in Wine 3.0+. Fixes #13 --- dumbxinputemu/dinput_input.c | 20 ++++++++++++-------- xinput1_1/xinput1_1.c | 5 +++-- xinput1_2/xinput1_2.c | 5 +++-- xinput1_3/xinput1_3.c | 5 +++-- xinput1_4/xinput1_4.c | 5 +++-- xinput9_1_0/xinput9_1_0.c | 5 +++-- 6 files changed, 27 insertions(+), 18 deletions(-) diff --git a/dumbxinputemu/dinput_input.c b/dumbxinputemu/dinput_input.c index 811b955..2905834 100644 --- a/dumbxinputemu/dinput_input.c +++ b/dumbxinputemu/dinput_input.c @@ -10,12 +10,13 @@ #ifndef TRACE // Available only in Wine - // #define TRACE(format, ...) printf("TRACE[%d] " format, __LINE__, ## __VA_ARGS__) + // #define TRACE(format, ...) do { printf("ERR[%d] " format, __LINE__, ## __VA_ARGS__); fflush(stdout); } while (0) + // #define DPRINT(format, ...) do { printf("ERR[%d] " format, __LINE__, ## __VA_ARGS__); fflush(stdout); } while (0) #define TRACE(...) do { } while(0) #define DPRINT(...) do { } while(0) #define FIXME(...) do { } while(0) #define WARN(...) do { } while(0) - #define ERR(format, ...) printf("ERR[%d] " format, __LINE__, ## __VA_ARGS__) + #define ERR(format, ...) do { printf("ERR[%d] " format, __LINE__, ## __VA_ARGS__); fflush(stdout); } while (0) #endif struct CapsFlags { @@ -44,7 +45,8 @@ static struct { /* ========================= Internal functions ============================= */ -bool initialized = FALSE; +static bool initialized = FALSE; +static void dinput_start(void); static BOOL dinput_is_good(const LPDIRECTINPUTDEVICE8A device, struct CapsFlags *caps) @@ -299,6 +301,9 @@ static BOOL CALLBACK dinput_enum_callback(const DIDEVICEINSTANCEA *instance, voi static void dinput_start(void) { HRESULT hr; + if (initialized) + return; + initialized = TRUE; hr = DirectInput8Create(GetModuleHandleA(NULL), 0x0800, &IID_IDirectInput8A, (void **)&dinput.iface, NULL); @@ -366,15 +371,12 @@ static void dinput_update(int index) void dumb_Init(DWORD version) { - if (initialized) - return; - dinput_start(); - initialized = TRUE; + // Does nothing } void dumb_Cleanup() { - // Does nothing + // Does nothing as well } /* ============================ Dll Functions =============================== */ @@ -463,6 +465,7 @@ void dumb_XInputEnable(BOOL enable) TRACE("(%d)\n", enable); DPRINT("XInputEnable: %d\n", enable); + dinput_start(); if ((dinput.enabled = enable)) { @@ -488,6 +491,7 @@ DWORD dumb_XInputGetCapabilities(DWORD index, DWORD flags, TRACE("(%u %d %p)\n", index, flags, capabilities); DPRINT("XInputGetCapabilities: %d\n", index); + dinput_start(); if (index >= XUSER_MAX_COUNT) return ERROR_BAD_ARGUMENTS; diff --git a/xinput1_1/xinput1_1.c b/xinput1_1/xinput1_1.c index 8f218e7..b22dd29 100644 --- a/xinput1_1/xinput1_1.c +++ b/xinput1_1/xinput1_1.c @@ -8,9 +8,10 @@ BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { - if (ul_reason_for_call == DLL_PROCESS_ATTACH) + if (ul_reason_for_call == DLL_PROCESS_ATTACH) { + DisableThreadLibraryCalls(hModule); dumb_Init(DUMBINPUT_V1_1); - else if (ul_reason_for_call == DLL_PROCESS_DETACH) + } else if (ul_reason_for_call == DLL_PROCESS_DETACH) dumb_Cleanup(); return TRUE; } diff --git a/xinput1_2/xinput1_2.c b/xinput1_2/xinput1_2.c index 5815e4a..d45b55b 100644 --- a/xinput1_2/xinput1_2.c +++ b/xinput1_2/xinput1_2.c @@ -8,9 +8,10 @@ BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { - if (ul_reason_for_call == DLL_PROCESS_ATTACH) + if (ul_reason_for_call == DLL_PROCESS_ATTACH) { + DisableThreadLibraryCalls(hModule); dumb_Init(DUMBINPUT_V1_2); - else if (ul_reason_for_call == DLL_PROCESS_DETACH) + } else if (ul_reason_for_call == DLL_PROCESS_DETACH) dumb_Cleanup(); return TRUE; } diff --git a/xinput1_3/xinput1_3.c b/xinput1_3/xinput1_3.c index b37e719..42f88dc 100644 --- a/xinput1_3/xinput1_3.c +++ b/xinput1_3/xinput1_3.c @@ -8,9 +8,10 @@ BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { - if (ul_reason_for_call == DLL_PROCESS_ATTACH) + if (ul_reason_for_call == DLL_PROCESS_ATTACH) { + DisableThreadLibraryCalls(hModule); dumb_Init(DUMBINPUT_V1_3); - else if (ul_reason_for_call == DLL_PROCESS_DETACH) + } else if (ul_reason_for_call == DLL_PROCESS_DETACH) dumb_Cleanup(); return TRUE; } diff --git a/xinput1_4/xinput1_4.c b/xinput1_4/xinput1_4.c index 4d3e7aa..4024ceb 100644 --- a/xinput1_4/xinput1_4.c +++ b/xinput1_4/xinput1_4.c @@ -8,9 +8,10 @@ BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { - if (ul_reason_for_call == DLL_PROCESS_ATTACH) + if (ul_reason_for_call == DLL_PROCESS_ATTACH) { + DisableThreadLibraryCalls(hModule); dumb_Init(DUMBINPUT_V1_4); - else if (ul_reason_for_call == DLL_PROCESS_DETACH) + } else if (ul_reason_for_call == DLL_PROCESS_DETACH) dumb_Cleanup(); return TRUE; } diff --git a/xinput9_1_0/xinput9_1_0.c b/xinput9_1_0/xinput9_1_0.c index 67cbf68..57a20d8 100644 --- a/xinput9_1_0/xinput9_1_0.c +++ b/xinput9_1_0/xinput9_1_0.c @@ -8,9 +8,10 @@ BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { - if (ul_reason_for_call == DLL_PROCESS_ATTACH) + if (ul_reason_for_call == DLL_PROCESS_ATTACH) { + DisableThreadLibraryCalls(hModule); dumb_Init(DUMBINPUT_V9_1_0); - else if (ul_reason_for_call == DLL_PROCESS_DETACH) + } else if (ul_reason_for_call == DLL_PROCESS_DETACH) dumb_Cleanup(); return TRUE; }