Skip to content

Commit

Permalink
Fix: Initializing dinput in xinput init causes race condition in Wine…
Browse files Browse the repository at this point in the history
… 3.0+. Fixes #13
  • Loading branch information
kozec committed Feb 10, 2018
1 parent 3758bc1 commit 9202547
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
20 changes: 12 additions & 8 deletions dumbxinputemu/dinput_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 =============================== */
Expand Down Expand Up @@ -463,6 +465,7 @@ void dumb_XInputEnable(BOOL enable)
TRACE("(%d)\n", enable);

DPRINT("XInputEnable: %d\n", enable);
dinput_start();

if ((dinput.enabled = enable))
{
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions xinput1_1/xinput1_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions xinput1_2/xinput1_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions xinput1_3/xinput1_3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions xinput1_4/xinput1_4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions xinput9_1_0/xinput9_1_0.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 9202547

Please sign in to comment.