shithub: cstory

Download patch

ref: c00262bcd83657b1f8b06a47d4777ab2145ef3a9
parent: fd855ee732a3a9d24264931272191e19447260a5
author: Clownacy <Clownacy@users.noreply.github.com>
date: Mon Sep 2 19:09:26 EDT 2019

Restore the SDL2 joystick code

Modified to fit the original code better

--- a/src/Input.cpp
+++ b/src/Input.cpp
@@ -4,64 +4,33 @@
 #include <stdio.h>
 #include <string.h>
 
-#define DIRECTINPUT_VERSION 0x500
-#include <dinput.h>
+#include "SDL.h"
 
 #include "WindowsWrapper.h"
 
-typedef struct DirectInputPair
-{
-	LPDIRECTINPUTA lpDI;
-	LPDIRECTINPUTDEVICE2A device;
-} DirectInputPair;
+static SDL_Joystick *joystick;
 
-static LPDIRECTINPUTDEVICE2A joystick;
-static LPDIRECTINPUTA lpDI;
-
 static int joystick_neutral_x;
 static int joystick_neutral_y;
 
 void ReleaseDirectInput(void)
 {
+	// Close opened joystick (if exists)
 	if (joystick != NULL)
 	{
-		joystick->Release();
+		SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
+		SDL_JoystickClose(joystick);
 		joystick = NULL;
 	}
-
-	if (lpDI != NULL)
-	{
-		lpDI->Release();
-		lpDI = NULL;
-	}
 }
 
-// The original name for this function is unknown
-BOOL SetDeviceAquire(BOOL aquire)
-{
-	if (aquire == TRUE)
-	{
-		if (joystick != NULL)
-			joystick->Acquire();
-	}
-	else
-	{
-		if (joystick != NULL)
-			joystick->Unacquire();
-	}
+BOOL HookAllDirectInputDevices(void);
 
-	return TRUE;
-}
-
-BOOL HookAllDirectInputDevices(HWND hWnd);
-BOOL __stdcall EnumDevices_Callback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef);
-
-BOOL InitDirectInput(HINSTANCE hinst, HWND hWnd)
+BOOL InitDirectInput(void)
 {
-	if (DirectInputCreateA(hinst, DIRECTINPUT_VERSION, &lpDI, NULL) != DI_OK)
-		return FALSE;
+	SDL_InitSubSystem(SDL_INIT_JOYSTICK);
 
-	if (!HookAllDirectInputDevices(hWnd))
+	if (!HookAllDirectInputDevices())
 		return FALSE;
 
 	return TRUE;
@@ -68,103 +37,33 @@
 }
 
 // The original name for this function is unknown
-BOOL HookAllDirectInputDevices(HWND hWnd)
+BOOL HookAllDirectInputDevices(void)
 {
-	DirectInputPair directinput_objects;
-
-	directinput_objects.device = NULL;
-	directinput_objects.lpDI = lpDI;
-
-	lpDI->AddRef();
-	lpDI->EnumDevices(4, EnumDevices_Callback, &directinput_objects, 1);
-
-	if (directinput_objects.lpDI != NULL)
+	// Open first available joystick
+	for (int i = 0; i < SDL_NumJoysticks(); i++)
 	{
-		directinput_objects.lpDI->Release();
-		directinput_objects.lpDI = NULL;
-	}
+		joystick = SDL_JoystickOpen(i);
 
-	if (directinput_objects.device == NULL)
-		return FALSE;
-
-	joystick = directinput_objects.device;
-
-	if (joystick->SetDataFormat(&c_dfDIJoystick) != DI_OK)	// c_dfDIJoystick might be incorrect
-		return FALSE;
-
-	if (joystick->SetCooperativeLevel(hWnd, DISCL_EXCLUSIVE | DISCL_BACKGROUND) != DI_OK)
-		return FALSE;
-
-	joystick->Acquire();
-
-	return TRUE;
-}
-
-// The original name for this function is unknown
-BOOL __stdcall EnumDevices_Callback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
-{
-	static int already_ran;
-	static DirectInputPair *directinput_objects;
-
-	if ((already_ran & 1) == 0)
-	{
-		already_ran |= 1;
-		directinput_objects = (DirectInputPair*)pvRef;
+		// Break as soon as a joystick is properly opened
+		if (joystick != NULL)
+			return TRUE;
 	}
 
-	static LPDIRECTINPUTDEVICEA device;
-	if (directinput_objects->lpDI->CreateDevice(lpddi->guidInstance, &device, NULL))
-	{
-		directinput_objects->device = NULL;
-		return TRUE;
-	}
-
-	static LPDIRECTINPUTDEVICE2A _joystick;
-	HRESULT res = device->QueryInterface(IID_IDirectInputDevice2A, (LPVOID*)&_joystick);
-
-	if (res < 0)
-	{
-		joystick = NULL;
-		return TRUE;
-	}
-
-	if (device != NULL)
-	{
-		device->Release();
-		device = NULL;
-	}
-
-	directinput_objects->device = _joystick;
-
-	char string[0x100];
-	sprintf(string, "DeviceGUID = %x\n", lpddi->guidInstance);
-	OutputDebugStringA(string);
-
 	return FALSE;
 }
 
 BOOL GetJoystickStatus(JOYSTICK_STATUS *status)
 {
-	DIJOYSTATE joystate;
-
 	if (joystick == NULL)
 		return FALSE;
 
-	if (joystick->Poll())
-		return FALSE;
+	int numButtons = SDL_JoystickNumButtons(joystick);
+	if (numButtons > 32)
+		numButtons = 32;
 
-	HRESULT res = joystick->GetDeviceState(sizeof(DIJOYSTATE), &joystate);
-	if (res)
+	for (int i = 0; i < numButtons; ++i)
 	{
-		if (res == DIERR_INPUTLOST)
-			SetDeviceAquire(0);
-		else
-			return FALSE;
-	}
-
-	for (int i = 0; i < 32; ++i)
-	{
-		if (joystate.rgbButtons[i] & 0x80)
+		if (SDL_JoystickGetButton(joystick, i) != 0)
 			status->bButton[i] = TRUE;
 		else
 			status->bButton[i] = FALSE;
@@ -175,14 +74,16 @@
 	status->bUp = FALSE;
 	status->bLeft = FALSE;
 
-	if (joystate.lX < joystick_neutral_x - 10000)
+	const Sint16 joystick_x = SDL_JoystickGetAxis(joystick, 0);
+	if (joystick_x < joystick_neutral_x - 10000)
 		status->bLeft = TRUE;
-	else if (joystate.lX > joystick_neutral_x + 10000)
+	else if (joystick_x > joystick_neutral_x + 10000)
 		status->bRight = TRUE;
 
-	if (joystate.lY < joystick_neutral_y - 10000)
+	const Sint16 joystick_y = SDL_JoystickGetAxis(joystick, 0);
+	if (joystick_y < joystick_neutral_y - 10000)
 		status->bUp = TRUE;
-	else if (joystate.lY > joystick_neutral_y + 10000)
+	else if (joystick_y > joystick_neutral_y + 10000)
 		status->bDown = TRUE;
 
 	return TRUE;
@@ -190,25 +91,11 @@
 
 BOOL ResetJoystickStatus(void)
 {
-	DIJOYSTATE joystate;
-
 	if (joystick == NULL)
 		return FALSE;
 
-	if (joystick->Poll())
-		return FALSE;
-
-	HRESULT res = joystick->GetDeviceState(sizeof(DIJOYSTATE), &joystate);
-	if (res)
-	{
-		if (res == DIERR_INPUTLOST)
-			SetDeviceAquire(0);
-		else
-			return FALSE;
-	}
-
-	joystick_neutral_x = joystate.lX;
-	joystick_neutral_y = joystate.lY;
+	joystick_neutral_x = SDL_JoystickGetAxis(joystick, 0);
+	joystick_neutral_y = SDL_JoystickGetAxis(joystick, 1);
 
 	return TRUE;
 }
--- a/src/Input.h
+++ b/src/Input.h
@@ -15,6 +15,6 @@
 };
 
 void ReleaseDirectInput(void);
-BOOL InitDirectInput(HINSTANCE hinst, HWND hWnd);
+BOOL InitDirectInput(void);
 BOOL GetJoystickStatus(JOYSTICK_STATUS *status);
 BOOL ResetJoystickStatus(void);
--- a/src/Main.cpp
+++ b/src/Main.cpp
@@ -326,7 +326,7 @@
 		InitDirectSound(hWnd);
 
 		// Initialize joystick
-		if (conf.bJoystick && InitDirectInput(info.info.win.hinstance, hWnd))
+		if (conf.bJoystick && InitDirectInput())
 		{
 			ResetJoystickStatus();
 			gbUseJoystick = TRUE;
--