Commit ab07a4d3 authored by Richard Gobeille's avatar Richard Gobeille
Browse files

engine: force feedback support for SDL backend

This is most of the work to support force feedback (rumble) for controllers, but it isn't hooked up to anything yet. Your controller will rumble once during game startup.
parent 0578f900
......@@ -148,6 +148,11 @@ vec2_t CONSTEXPR const g_defaultVideoModes []
extern char inputdevices;
#define DEV_KEYBOARD 0x1
#define DEV_MOUSE 0x2
#define DEV_JOYSTICK 0x4
#define DEV_HAPTIC 0x8
// keys
#define NUMKEYS 256
#define KEYFIFOSIZ 64
......
......@@ -858,6 +858,7 @@ int debugprintf(const char *f, ...)
static SDL_Joystick *joydev = NULL;
#if SDL_MAJOR_VERSION >= 2
static SDL_GameController *controller = NULL;
static SDL_Haptic *haptic = NULL;
static void LoadSDLControllerDB()
{
......@@ -911,9 +912,14 @@ static int numjoysticks;
void joyScanDevices()
{
inputdevices &= ~4;
inputdevices &= ~(DEV_JOYSTICK | DEV_HAPTIC);
#if SDL_MAJOR_VERSION >= 2
if (haptic)
{
SDL_HapticClose(haptic);
haptic = nullptr;
}
if (controller)
{
SDL_GameControllerClose(controller);
......@@ -930,11 +936,11 @@ void joyScanDevices()
if (numjoysticks < 1)
{
buildputs("No game controllers found\n");
buildprintf("No game controllers found\n");
}
else
{
buildputs("Game controllers:\n");
buildprintf("Game controllers:\n");
char name[128];
......@@ -949,6 +955,15 @@ void joyScanDevices()
buildprintf(" %d. %s\n", i + 1, name);
}
#if SDL_MAJOR_VERSION >= 2
int const numhaptics = SDL_NumHaptics();
if (numhaptics > 0)
{
buildprintf("Haptic devices:\n");
for (int i = 0; i < numhaptics; i++)
buildprintf(" %d. %s\n", i+1, SDL_HapticName(i));
}
#endif
#if SDL_MAJOR_VERSION >= 2
for (int i = 0; i < numjoysticks; i++)
{
......@@ -993,7 +1008,12 @@ void joyScanDevices()
joystick.pAxis = (int32_t *)Xcalloc(joystick.numAxes, sizeof(int32_t));
DO_FREE_AND_NULL(joystick.pHat);
inputdevices |= 4;
inputdevices |= DEV_JOYSTICK;
auto joy = SDL_GameControllerGetJoystick(controller);
if ((haptic = SDL_HapticOpenFromJoystick(joy)) || !SDL_GameControllerRumble(controller, 0xffff, 0xffff, 200))
inputdevices |= DEV_HAPTIC;
else buildprintf("%s\n", SDL_GetError());
return;
}
......@@ -1004,7 +1024,7 @@ void joyScanDevices()
{
if ((joydev = SDL_JoystickOpen(i)))
{
buildprintf("Using joystick %s\n", SDL_JoystickNameForIndex(i));
buildprintf("Using joystick: %s\n", SDL_JoystickNameForIndex(i));
// KEEPINSYNC duke3d/src/gamedefs.h, mact/include/_control.h
joystick.numAxes = min(9, SDL_JoystickNumAxes(joydev));
......@@ -1034,13 +1054,18 @@ void joyScanDevices()
joystick.pHat[j] = -1; // center
SDL_JoystickEventState(SDL_ENABLE);
inputdevices |= 4;
inputdevices |= DEV_JOYSTICK;
#if SDL_MAJOR_VERSION >= 2
if ((haptic = SDL_HapticOpenFromJoystick(joydev)) || !SDL_JoystickRumble(joydev, 0xffff, 0xffff, 200))
inputdevices |= DEV_HAPTIC;
else buildprintf("%s\n", SDL_GetError());
#endif
return;
}
}
buildputs("No controllers are usable\n");
buildprintf("No controllers are usable\n");
}
}
......@@ -1067,8 +1092,8 @@ int32_t initinput(void(*hotplugCallback)(void) /*= nullptr*/)
}
#endif
inputdevices = 1 | 2; // keyboard (1) and mouse (2)
g_mouseGrabbed = 0;
inputdevices = DEV_KEYBOARD | DEV_MOUSE;
g_mouseGrabbed = false;
memset(g_keyNameTable, 0, sizeof(g_keyNameTable));
......@@ -1089,7 +1114,7 @@ int32_t initinput(void(*hotplugCallback)(void) /*= nullptr*/)
}
#if SDL_MAJOR_VERSION >= 2
if (!SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER))
if (!SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC))
#else
if (!SDL_InitSubSystem(SDL_INIT_JOYSTICK))
#endif
......@@ -1100,6 +1125,9 @@ int32_t initinput(void(*hotplugCallback)(void) /*= nullptr*/)
joyScanDevices();
}
if (inputdevices & DEV_HAPTIC)
buildprintf("Controller rumble enabled\n");
return 0;
}
......@@ -1111,17 +1139,23 @@ void uninitinput(void)
mouseUninit();
#if SDL_MAJOR_VERSION >= 2
if (haptic)
{
SDL_HapticClose(haptic);
haptic = nullptr;
}
if (controller)
{
SDL_GameControllerClose(controller);
controller = NULL;
controller = nullptr;
}
#endif
if (joydev)
{
SDL_JoystickClose(joydev);
joydev = NULL;
joydev = nullptr;
}
}
......
......@@ -686,7 +686,7 @@ int32_t initinput(void(*hotplugCallback)(void) /*= NULL*/)
g_keyFIFOend = g_keyAsciiPos = g_keyAsciiEnd = 0;
inputdevices = 1|2;
inputdevices = DEV_KEYBOARD | DEV_MOUSE;
joystick.numAxes = joystick.numButtons = joystick.numHats=0;
GetKeyNames();
......@@ -878,7 +878,7 @@ static BOOL CALLBACK InitDirectInput_enum(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRe
if ((lpddi->dwDevType&0xff) != DIDEVTYPE_JOYSTICK)
return DIENUM_CONTINUE;
inputdevices |= 4;
inputdevices |= DEV_JOYSTICK;
d = "CONTROLLER";
Bmemcpy(&guidDevs, &lpddi->guidInstance, sizeof(GUID));
......@@ -959,12 +959,12 @@ static BOOL InitDirectInput(void)
else if (result != DI_OK) initprintf(" Created DirectInput object with warning: %s\n",GetDInputError(result));
initprintf(" - Enumerating attached game controllers\n");
inputdevices = 1|2;
inputdevices = DEV_KEYBOARD | DEV_MOUSE;
result = IDirectInput7_EnumDevices(lpDI, DIDEVTYPE_JOYSTICK, InitDirectInput_enum, NULL, DIEDFL_ATTACHEDONLY);
if (FAILED(result)) { HorribleDInputDeath("Failed enumerating attached game controllers", result); }
else if (result != DI_OK) initprintf(" Enumerated game controllers with warning: %s\n",GetDInputError(result));
if (inputdevices == (1|2))
if (inputdevices == (DEV_KEYBOARD | DEV_MOUSE))
{
initprintf(" - No game controllers found\n");
UninitDirectInput();
......
......@@ -49,7 +49,7 @@ extern float CONTROL_MouseSensitivity;
static inline bool MOUSE_Startup(void)
{
mouseInit();
return ((inputdevices & 2) == 2);
return ((inputdevices & DEV_MOUSE) == DEV_MOUSE);
}
static inline void MOUSE_Shutdown(void) { mouseUninit(); }
......
......@@ -845,7 +845,7 @@ static void CONTROL_ResetJoystickValues()
{
CONTROL_NumJoyAxes = min(MAXJOYAXES, joystick.numAxes);
CONTROL_NumJoyButtons = min(MAXJOYBUTTONS, joystick.numButtons + 4 * (joystick.numHats > 0));
CONTROL_JoystickEnabled = CONTROL_JoyPresent = !!((inputdevices & 4) >> 2);
CONTROL_JoystickEnabled = CONTROL_JoyPresent = !!((inputdevices & DEV_JOYSTICK) >> 2);
}
void CONTROL_ScanForControllers()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment