[SDL3] Adding input and FFB support for Logitech G29(PS3) on hidapi (#11598)
These changes enable the Logitech G29 wheel to run on hidapi with both SDL_Joystick and SDL_Haptic interfaces. While it is already possible to use the wheel on Linux in WINE + SDL2 thanks to the in-tree evdev driver as well as new-lg4ff, these set of changes allow the G29 to be used with WINE under MacOS and FreeBSD These wheels should also be supported, but I can only test them from G29's compat modes: G27, G25, DFGT, DFP, DFEX Haptic and led support are ported from https://github.com/berarma/new-lg4ff
This commit is contained in:
@@ -21,6 +21,9 @@
|
||||
#include "SDL_internal.h"
|
||||
|
||||
#include "SDL_syshaptic.h"
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
#include "SDL_hidapihaptic.h"
|
||||
#endif
|
||||
#include "SDL_haptic_c.h"
|
||||
#include "../joystick/SDL_joystick_c.h" // For SDL_IsJoystickValid
|
||||
#include "../SDL_hints_c.h"
|
||||
@@ -112,7 +115,17 @@ static SDL_Haptic *SDL_haptics = NULL;
|
||||
|
||||
bool SDL_InitHaptics(void)
|
||||
{
|
||||
return SDL_SYS_HapticInit();
|
||||
if (!SDL_SYS_HapticInit()) {
|
||||
return false;
|
||||
}
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (!SDL_HIDAPI_HapticInit()) {
|
||||
SDL_SYS_HapticQuit();
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool SDL_GetHapticIndex(SDL_HapticID instance_id, int *driver_index)
|
||||
@@ -205,7 +218,6 @@ SDL_Haptic *SDL_OpenHaptic(SDL_HapticID instance_id)
|
||||
}
|
||||
|
||||
// Initialize the haptic device
|
||||
SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, true);
|
||||
haptic->instance_id = instance_id;
|
||||
haptic->rumble_id = -1;
|
||||
if (!SDL_SYS_HapticOpen(haptic)) {
|
||||
@@ -227,6 +239,8 @@ SDL_Haptic *SDL_OpenHaptic(SDL_HapticID instance_id)
|
||||
haptic->next = SDL_haptics;
|
||||
SDL_haptics = haptic;
|
||||
|
||||
SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, true);
|
||||
|
||||
// Disable autocenter and set gain to max.
|
||||
if (haptic->supported & SDL_HAPTIC_GAIN) {
|
||||
SDL_SetHapticGain(haptic, 100);
|
||||
@@ -295,7 +309,11 @@ bool SDL_IsJoystickHaptic(SDL_Joystick *joystick)
|
||||
// Must be a valid joystick
|
||||
if (SDL_IsJoystickValid(joystick) &&
|
||||
!SDL_IsGamepad(SDL_GetJoystickID(joystick))) {
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
result = SDL_SYS_JoystickIsHaptic(joystick) || SDL_HIDAPI_JoystickIsHaptic(joystick);
|
||||
#else
|
||||
result = SDL_SYS_JoystickIsHaptic(joystick);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
SDL_UnlockJoysticks();
|
||||
@@ -310,16 +328,8 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
|
||||
|
||||
SDL_LockJoysticks();
|
||||
{
|
||||
// Must be a valid joystick
|
||||
if (!SDL_IsJoystickValid(joystick)) {
|
||||
SDL_SetError("Haptic: Joystick isn't valid.");
|
||||
SDL_UnlockJoysticks();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Joystick must be haptic
|
||||
if (SDL_IsGamepad(SDL_GetJoystickID(joystick)) ||
|
||||
!SDL_SYS_JoystickIsHaptic(joystick)) {
|
||||
// Joystick must be valid and haptic
|
||||
if (!SDL_IsJoystickHaptic(joystick)) {
|
||||
SDL_SetError("Haptic: Joystick isn't a haptic device.");
|
||||
SDL_UnlockJoysticks();
|
||||
return NULL;
|
||||
@@ -328,7 +338,11 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
|
||||
hapticlist = SDL_haptics;
|
||||
// Check to see if joystick's haptic is already open
|
||||
while (hapticlist) {
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick) || SDL_HIDAPI_JoystickSameHaptic(hapticlist, joystick)) {
|
||||
#else
|
||||
if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick)) {
|
||||
#endif
|
||||
haptic = hapticlist;
|
||||
++haptic->ref_count;
|
||||
SDL_UnlockJoysticks();
|
||||
@@ -349,6 +363,16 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
|
||||
*/
|
||||
SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, true);
|
||||
haptic->rumble_id = -1;
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (SDL_HIDAPI_JoystickIsHaptic(joystick)) {
|
||||
if (!SDL_HIDAPI_HapticOpenFromJoystick(haptic, joystick)) {
|
||||
SDL_SetError("Haptic: SDL_HIDAPI_HapticOpenFromJoystick failed.");
|
||||
SDL_free(haptic);
|
||||
SDL_UnlockJoysticks();
|
||||
return NULL;
|
||||
}
|
||||
} else
|
||||
#endif
|
||||
if (!SDL_SYS_HapticOpenFromJoystick(haptic, joystick)) {
|
||||
SDL_SetError("Haptic: SDL_SYS_HapticOpenFromJoystick failed.");
|
||||
SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, false);
|
||||
@@ -379,6 +403,16 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
|
||||
haptic->next = SDL_haptics;
|
||||
SDL_haptics = haptic;
|
||||
|
||||
SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, true);
|
||||
|
||||
// Disable autocenter and set gain to max.
|
||||
if (haptic->supported & SDL_HAPTIC_GAIN) {
|
||||
SDL_SetHapticGain(haptic, 100);
|
||||
}
|
||||
if (haptic->supported & SDL_HAPTIC_AUTOCENTER) {
|
||||
SDL_SetHapticAutocenter(haptic, 0);
|
||||
}
|
||||
|
||||
return haptic;
|
||||
}
|
||||
|
||||
@@ -395,13 +429,20 @@ void SDL_CloseHaptic(SDL_Haptic *haptic)
|
||||
return;
|
||||
}
|
||||
|
||||
// Close it, properly removing effects if needed
|
||||
for (i = 0; i < haptic->neffects; i++) {
|
||||
if (haptic->effects[i].hweffect != NULL) {
|
||||
SDL_DestroyHapticEffect(haptic, i);
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
|
||||
SDL_HIDAPI_HapticClose(haptic);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
// Close it, properly removing effects if needed
|
||||
for (i = 0; i < haptic->neffects; i++) {
|
||||
if (haptic->effects[i].hweffect != NULL) {
|
||||
SDL_DestroyHapticEffect(haptic, i);
|
||||
}
|
||||
}
|
||||
SDL_SYS_HapticClose(haptic);
|
||||
}
|
||||
SDL_SYS_HapticClose(haptic);
|
||||
SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, false);
|
||||
|
||||
// Remove from the list
|
||||
@@ -433,6 +474,9 @@ void SDL_QuitHaptics(void)
|
||||
SDL_CloseHaptic(SDL_haptics);
|
||||
}
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
SDL_HIDAPI_HapticQuit();
|
||||
#endif
|
||||
SDL_SYS_HapticQuit();
|
||||
}
|
||||
|
||||
@@ -495,6 +539,12 @@ int SDL_CreateHapticEffect(SDL_Haptic *haptic, const SDL_HapticEffect *effect)
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
|
||||
return SDL_HIDAPI_HapticNewEffect(haptic, effect);
|
||||
}
|
||||
#endif
|
||||
|
||||
// See if there's a free slot
|
||||
for (i = 0; i < haptic->neffects; i++) {
|
||||
if (haptic->effects[i].hweffect == NULL) {
|
||||
@@ -527,6 +577,12 @@ bool SDL_UpdateHapticEffect(SDL_Haptic *haptic, int effect, const SDL_HapticEffe
|
||||
{
|
||||
CHECK_HAPTIC_MAGIC(haptic, false);
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
|
||||
return SDL_HIDAPI_HapticUpdateEffect(haptic, effect, data);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!ValidEffect(haptic, effect)) {
|
||||
return false;
|
||||
}
|
||||
@@ -554,6 +610,12 @@ bool SDL_RunHapticEffect(SDL_Haptic *haptic, int effect, Uint32 iterations)
|
||||
{
|
||||
CHECK_HAPTIC_MAGIC(haptic, false);
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
|
||||
return SDL_HIDAPI_HapticRunEffect(haptic, effect, iterations);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!ValidEffect(haptic, effect)) {
|
||||
return false;
|
||||
}
|
||||
@@ -570,6 +632,12 @@ bool SDL_StopHapticEffect(SDL_Haptic *haptic, int effect)
|
||||
{
|
||||
CHECK_HAPTIC_MAGIC(haptic, false);
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
|
||||
return SDL_HIDAPI_HapticStopEffect(haptic, effect);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!ValidEffect(haptic, effect)) {
|
||||
return false;
|
||||
}
|
||||
@@ -586,6 +654,13 @@ void SDL_DestroyHapticEffect(SDL_Haptic *haptic, int effect)
|
||||
{
|
||||
CHECK_HAPTIC_MAGIC(haptic,);
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
|
||||
SDL_HIDAPI_HapticDestroyEffect(haptic, effect);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!ValidEffect(haptic, effect)) {
|
||||
return;
|
||||
}
|
||||
@@ -602,6 +677,12 @@ bool SDL_GetHapticEffectStatus(SDL_Haptic *haptic, int effect)
|
||||
{
|
||||
CHECK_HAPTIC_MAGIC(haptic, false);
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
|
||||
return SDL_HIDAPI_HapticGetEffectStatus(haptic, effect);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!ValidEffect(haptic, effect)) {
|
||||
return false;
|
||||
}
|
||||
@@ -648,6 +729,12 @@ bool SDL_SetHapticGain(SDL_Haptic *haptic, int gain)
|
||||
real_gain = gain;
|
||||
}
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
|
||||
return SDL_HIDAPI_HapticSetGain(haptic, real_gain);
|
||||
}
|
||||
#endif
|
||||
|
||||
return SDL_SYS_HapticSetGain(haptic, real_gain);
|
||||
}
|
||||
|
||||
@@ -663,6 +750,12 @@ bool SDL_SetHapticAutocenter(SDL_Haptic *haptic, int autocenter)
|
||||
return SDL_SetError("Haptic: Autocenter must be between 0 and 100.");
|
||||
}
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
|
||||
return SDL_HIDAPI_HapticSetAutocenter(haptic, autocenter);
|
||||
}
|
||||
#endif
|
||||
|
||||
return SDL_SYS_HapticSetAutocenter(haptic, autocenter);
|
||||
}
|
||||
|
||||
@@ -674,6 +767,12 @@ bool SDL_PauseHaptic(SDL_Haptic *haptic)
|
||||
return SDL_SetError("Haptic: Device does not support setting pausing.");
|
||||
}
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
|
||||
return SDL_HIDAPI_HapticPause(haptic);
|
||||
}
|
||||
#endif
|
||||
|
||||
return SDL_SYS_HapticPause(haptic);
|
||||
}
|
||||
|
||||
@@ -685,6 +784,12 @@ bool SDL_ResumeHaptic(SDL_Haptic *haptic)
|
||||
return true; // Not going to be paused, so we pretend it's unpaused.
|
||||
}
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
|
||||
return SDL_HIDAPI_HapticResume(haptic);
|
||||
}
|
||||
#endif
|
||||
|
||||
return SDL_SYS_HapticResume(haptic);
|
||||
}
|
||||
|
||||
@@ -692,6 +797,12 @@ bool SDL_StopHapticEffects(SDL_Haptic *haptic)
|
||||
{
|
||||
CHECK_HAPTIC_MAGIC(haptic, false);
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
|
||||
return SDL_HIDAPI_HapticStopAll(haptic);
|
||||
}
|
||||
#endif
|
||||
|
||||
return SDL_SYS_HapticStopAll(haptic);
|
||||
}
|
||||
|
||||
|
||||
48
src/haptic/SDL_hidapihaptic.h
Normal file
48
src/haptic/SDL_hidapihaptic.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 2025 Katharine Chui <katharine.chui@gmail.com>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
/*
|
||||
All hid command sent and effect rendering are ported from https://github.com/berarma/new-lg4ff
|
||||
*/
|
||||
|
||||
#ifndef SDL_hidapihaptic_h_
|
||||
#define SDL_hidapihaptic_h_
|
||||
|
||||
bool SDL_HIDAPI_HapticInit();
|
||||
bool SDL_HIDAPI_HapticIsHidapi(SDL_Haptic *haptic);
|
||||
bool SDL_HIDAPI_JoystickIsHaptic(SDL_Joystick *joystick);
|
||||
bool SDL_HIDAPI_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick);
|
||||
bool SDL_HIDAPI_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick);
|
||||
void SDL_HIDAPI_HapticClose(SDL_Haptic *haptic);
|
||||
void SDL_HIDAPI_HapticQuit(void);
|
||||
int SDL_HIDAPI_HapticNewEffect(SDL_Haptic *haptic, const SDL_HapticEffect *base);
|
||||
bool SDL_HIDAPI_HapticUpdateEffect(SDL_Haptic *haptic, int id, const SDL_HapticEffect *data);
|
||||
bool SDL_HIDAPI_HapticRunEffect(SDL_Haptic *haptic, int id, Uint32 iterations);
|
||||
bool SDL_HIDAPI_HapticStopEffect(SDL_Haptic *haptic, int id);
|
||||
void SDL_HIDAPI_HapticDestroyEffect(SDL_Haptic *haptic, int id);
|
||||
bool SDL_HIDAPI_HapticGetEffectStatus(SDL_Haptic *haptic, int id);
|
||||
bool SDL_HIDAPI_HapticSetGain(SDL_Haptic *haptic, int gain);
|
||||
bool SDL_HIDAPI_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter);
|
||||
bool SDL_HIDAPI_HapticPause(SDL_Haptic *haptic);
|
||||
bool SDL_HIDAPI_HapticResume(SDL_Haptic *haptic);
|
||||
bool SDL_HIDAPI_HapticStopAll(SDL_Haptic *haptic);
|
||||
|
||||
#endif //SDL_hidapihaptic_h_
|
||||
305
src/haptic/hidapi/SDL_hidapihaptic.c
Normal file
305
src/haptic/hidapi/SDL_hidapihaptic.c
Normal file
@@ -0,0 +1,305 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 2025 Katharine Chui <katharine.chui@gmail.com>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
#include "SDL_internal.h"
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
|
||||
#include "SDL_hidapihaptic_c.h"
|
||||
#include "SDL3/SDL_mutex.h"
|
||||
#include "SDL3/SDL_error.h"
|
||||
|
||||
extern struct SDL_JoystickDriver SDL_HIDAPI_JoystickDriver;
|
||||
|
||||
typedef struct haptic_list_node
|
||||
{
|
||||
SDL_Haptic *haptic;
|
||||
struct haptic_list_node *next;
|
||||
} haptic_list_node;
|
||||
|
||||
static haptic_list_node *haptic_list_head = NULL;
|
||||
static SDL_Mutex *haptic_list_mutex = NULL;
|
||||
|
||||
static SDL_HIDAPI_HapticDriver *drivers[] = {
|
||||
#ifdef SDL_HAPTIC_HIDAPI_LG4FF
|
||||
&SDL_HIDAPI_HapticDriverLg4ff,
|
||||
#endif
|
||||
NULL
|
||||
};
|
||||
|
||||
bool SDL_HIDAPI_HapticInit()
|
||||
{
|
||||
haptic_list_head = NULL;
|
||||
haptic_list_mutex = SDL_CreateMutex();
|
||||
if (haptic_list_mutex == NULL) {
|
||||
return SDL_OutOfMemory();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SDL_HIDAPI_HapticIsHidapi(SDL_Haptic *haptic)
|
||||
{
|
||||
haptic_list_node *cur;
|
||||
bool ret = false;
|
||||
|
||||
SDL_LockMutex(haptic_list_mutex);
|
||||
cur = haptic_list_head;
|
||||
while (cur != NULL) {
|
||||
if (cur->haptic == haptic) {
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
SDL_UnlockMutex(haptic_list_mutex);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
bool SDL_HIDAPI_JoystickIsHaptic(SDL_Joystick *joystick)
|
||||
{
|
||||
const int numdrivers = SDL_arraysize(drivers) - 1;
|
||||
int i;
|
||||
|
||||
SDL_AssertJoysticksLocked();
|
||||
|
||||
if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (i = 0; i < numdrivers; ++i) {
|
||||
if (drivers[i]->JoystickSupported(joystick)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SDL_HIDAPI_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
|
||||
{
|
||||
const int numdrivers = SDL_arraysize(drivers) - 1;
|
||||
int i;
|
||||
|
||||
SDL_AssertJoysticksLocked();
|
||||
|
||||
if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
|
||||
return SDL_SetError("Cannot open hidapi haptic from non hidapi joystick");
|
||||
}
|
||||
|
||||
for (i = 0; i < numdrivers; ++i) {
|
||||
if (drivers[i]->JoystickSupported(joystick)) {
|
||||
SDL_HIDAPI_HapticDevice *device;
|
||||
haptic_list_node *list_node;
|
||||
// the driver is responsible for calling SDL_SetError
|
||||
void *ctx = drivers[i]->Open(joystick);
|
||||
if (ctx == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
device = SDL_malloc(sizeof(SDL_HIDAPI_HapticDevice));
|
||||
if (device == NULL) {
|
||||
SDL_HIDAPI_HapticDevice temp;
|
||||
temp.ctx = ctx;
|
||||
temp.driver = drivers[i];
|
||||
temp.joystick = joystick;
|
||||
temp.driver->Close(&temp);
|
||||
return SDL_OutOfMemory();
|
||||
}
|
||||
|
||||
device->driver = drivers[i];
|
||||
device->haptic = haptic;
|
||||
device->joystick = joystick;
|
||||
device->ctx = ctx;
|
||||
|
||||
list_node = SDL_malloc(sizeof(haptic_list_node));
|
||||
if (list_node == NULL) {
|
||||
device->driver->Close(device);
|
||||
SDL_free(device);
|
||||
return SDL_OutOfMemory();
|
||||
}
|
||||
|
||||
haptic->hwdata = (struct haptic_hwdata *)device;
|
||||
|
||||
// this is outside of the syshaptic driver
|
||||
|
||||
haptic->neffects = device->driver->NumEffects(device);
|
||||
haptic->nplaying = device->driver->NumEffectsPlaying(device);
|
||||
haptic->supported = device->driver->GetFeatures(device);
|
||||
haptic->naxes = device->driver->NumAxes(device);
|
||||
|
||||
// outside of SYS_HAPTIC
|
||||
haptic->instance_id = 255;
|
||||
|
||||
list_node->haptic = haptic;
|
||||
list_node->next = NULL;
|
||||
|
||||
// grab a joystick ref so that it doesn't get fully destroyed before the haptic is closed
|
||||
SDL_OpenJoystick(SDL_GetJoystickID(joystick));
|
||||
|
||||
SDL_LockMutex(haptic_list_mutex);
|
||||
if (haptic_list_head == NULL) {
|
||||
haptic_list_head = list_node;
|
||||
} else {
|
||||
haptic_list_node *cur = haptic_list_head;
|
||||
while(cur->next != NULL) {
|
||||
cur = cur->next;
|
||||
}
|
||||
cur->next = list_node;
|
||||
}
|
||||
SDL_UnlockMutex(haptic_list_mutex);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return SDL_SetError("No supported HIDAPI haptic driver found for joystick");
|
||||
}
|
||||
|
||||
bool SDL_HIDAPI_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
|
||||
{
|
||||
SDL_HIDAPI_HapticDevice *device;
|
||||
|
||||
SDL_AssertJoysticksLocked();
|
||||
if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
|
||||
return false;
|
||||
}
|
||||
|
||||
device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
|
||||
|
||||
if (joystick == device->joystick) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void SDL_HIDAPI_HapticClose(SDL_Haptic *haptic)
|
||||
{
|
||||
haptic_list_node *cur, *last;
|
||||
|
||||
SDL_LockMutex(haptic_list_mutex);
|
||||
|
||||
cur = haptic_list_head;
|
||||
last = NULL;
|
||||
while (cur != NULL) {
|
||||
if (cur->haptic == haptic) {
|
||||
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
|
||||
|
||||
device->driver->Close(device);
|
||||
|
||||
// a reference was grabbed during open, now release it
|
||||
SDL_CloseJoystick(device->joystick);
|
||||
|
||||
if (cur == haptic_list_head) {
|
||||
haptic_list_head = cur->next;
|
||||
} else {
|
||||
last->next = cur->next;
|
||||
}
|
||||
|
||||
SDL_free(device->ctx);
|
||||
SDL_free(device);
|
||||
SDL_free(cur);
|
||||
SDL_UnlockMutex(haptic_list_mutex);
|
||||
return;
|
||||
}
|
||||
last = cur;
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
SDL_UnlockMutex(haptic_list_mutex);
|
||||
}
|
||||
|
||||
void SDL_HIDAPI_HapticQuit(void)
|
||||
{
|
||||
// the list is cleared in SDL_haptic.c
|
||||
if (haptic_list_mutex != NULL) {
|
||||
SDL_DestroyMutex(haptic_list_mutex);
|
||||
haptic_list_mutex = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int SDL_HIDAPI_HapticNewEffect(SDL_Haptic *haptic, const SDL_HapticEffect *base)
|
||||
{
|
||||
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
|
||||
return device->driver->CreateEffect(device, base);
|
||||
}
|
||||
|
||||
bool SDL_HIDAPI_HapticUpdateEffect(SDL_Haptic *haptic, int id, const SDL_HapticEffect *data)
|
||||
{
|
||||
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
|
||||
return device->driver->UpdateEffect(device, id, data);
|
||||
}
|
||||
|
||||
bool SDL_HIDAPI_HapticRunEffect(SDL_Haptic *haptic, int id, Uint32 iterations)
|
||||
{
|
||||
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
|
||||
return device->driver->RunEffect(device, id, iterations);
|
||||
}
|
||||
|
||||
bool SDL_HIDAPI_HapticStopEffect(SDL_Haptic *haptic, int id)
|
||||
{
|
||||
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
|
||||
return device->driver->StopEffect(device, id);
|
||||
}
|
||||
|
||||
void SDL_HIDAPI_HapticDestroyEffect(SDL_Haptic *haptic, int id)
|
||||
{
|
||||
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
|
||||
device->driver->DestroyEffect(device, id);
|
||||
}
|
||||
|
||||
bool SDL_HIDAPI_HapticGetEffectStatus(SDL_Haptic *haptic, int id)
|
||||
{
|
||||
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
|
||||
return device->driver->GetEffectStatus(device, id);
|
||||
}
|
||||
|
||||
bool SDL_HIDAPI_HapticSetGain(SDL_Haptic *haptic, int gain)
|
||||
{
|
||||
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
|
||||
return device->driver->SetGain(device, gain);
|
||||
}
|
||||
|
||||
bool SDL_HIDAPI_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter)
|
||||
{
|
||||
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
|
||||
return device->driver->SetAutocenter(device, autocenter);
|
||||
}
|
||||
|
||||
bool SDL_HIDAPI_HapticPause(SDL_Haptic *haptic)
|
||||
{
|
||||
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
|
||||
return device->driver->Pause(device);
|
||||
}
|
||||
|
||||
bool SDL_HIDAPI_HapticResume(SDL_Haptic *haptic)
|
||||
{
|
||||
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
|
||||
return device->driver->Resume(device);
|
||||
}
|
||||
|
||||
bool SDL_HIDAPI_HapticStopAll(SDL_Haptic *haptic)
|
||||
{
|
||||
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
|
||||
return device->driver->StopEffects(device);
|
||||
}
|
||||
|
||||
#endif //SDL_JOYSTICK_HIDAPI
|
||||
70
src/haptic/hidapi/SDL_hidapihaptic_c.h
Normal file
70
src/haptic/hidapi/SDL_hidapihaptic_c.h
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 2025 Katharine Chui <katharine.chui@gmail.com>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef SDL_hidapihaptic_c_h_
|
||||
#define SDL_hidapihaptic_c_h_
|
||||
|
||||
#include "SDL3/SDL_haptic.h"
|
||||
#include "SDL3/SDL_joystick.h"
|
||||
#include "../SDL_syshaptic.h"
|
||||
#include "../../joystick/SDL_joystick_c.h" // accessing _SDL_JoystickDriver
|
||||
#include "../../joystick/SDL_sysjoystick.h" // accessing _SDL_Joystick
|
||||
|
||||
#define SDL_HAPTIC_HIDAPI_LG4FF
|
||||
|
||||
typedef struct SDL_HIDAPI_HapticDriver SDL_HIDAPI_HapticDriver;
|
||||
typedef struct SDL_HIDAPI_HapticDevice
|
||||
{
|
||||
SDL_Haptic *haptic; /* related haptic ref */
|
||||
SDL_Joystick *joystick; /* related hidapi joystick */
|
||||
SDL_HIDAPI_HapticDriver *driver; /* driver to use */
|
||||
void *ctx; /* driver specific context */
|
||||
} SDL_HIDAPI_HapticDevice;
|
||||
|
||||
struct SDL_HIDAPI_HapticDriver
|
||||
{
|
||||
bool (*JoystickSupported)(SDL_Joystick *joystick); /* return SDL_TRUE if haptic can be opened from the joystick */
|
||||
void *(*Open)(SDL_Joystick *joystick); /* returns a driver context allocated with SDL_malloc, or null if it cannot be allocated */
|
||||
|
||||
/* functions below need to handle the possibility of a null joystick instance, indicating the absence of the joystick */
|
||||
void (*Close)(SDL_HIDAPI_HapticDevice *device); /* cleanup resources allocated during Open, do NOT free driver context created in Open */
|
||||
|
||||
/* below mirror SDL_haptic.h effect interfaces */
|
||||
int (*NumEffects)(SDL_HIDAPI_HapticDevice *device); /* returns supported number of effects the device can store */
|
||||
int (*NumEffectsPlaying)(SDL_HIDAPI_HapticDevice *device); /* returns supported number of effects the device can play concurrently */
|
||||
Uint32 (*GetFeatures)(SDL_HIDAPI_HapticDevice *device); /* returns supported effects in a bitmask */
|
||||
int (*NumAxes)(SDL_HIDAPI_HapticDevice *device); /* returns the number of haptic axes */
|
||||
int (*CreateEffect)(SDL_HIDAPI_HapticDevice *device, const SDL_HapticEffect *data); /* returns effect id if created correctly, negative number on error */
|
||||
bool (*UpdateEffect)(SDL_HIDAPI_HapticDevice *device, int id, const SDL_HapticEffect *data); /* returns 0 on success, negative number on error */
|
||||
bool (*RunEffect)(SDL_HIDAPI_HapticDevice *device, int id, Uint32 iterations); /* returns 0 on success, negative number on error */
|
||||
bool (*StopEffect)(SDL_HIDAPI_HapticDevice *device, int id); /* returns 0 on success, negative number on error */
|
||||
void (*DestroyEffect)(SDL_HIDAPI_HapticDevice *device, int id); /* returns 0 on success, negative number on error */
|
||||
bool (*GetEffectStatus)(SDL_HIDAPI_HapticDevice *device, int id); /* returns 0 if not playing, 1 if playing, negative number on error */
|
||||
bool (*SetGain)(SDL_HIDAPI_HapticDevice *device, int gain); /* gain 0 - 100, returns 0 on success, negative number on error */
|
||||
bool (*SetAutocenter)(SDL_HIDAPI_HapticDevice *device, int autocenter); /* gain 0 - 100, returns 0 on success, negative number on error */
|
||||
bool (*Pause)(SDL_HIDAPI_HapticDevice *device); /* returns 0 on success, negative number on error */
|
||||
bool (*Resume)(SDL_HIDAPI_HapticDevice *device); /* returns 0 on success, negative number on error */
|
||||
bool (*StopEffects)(SDL_HIDAPI_HapticDevice *device); /* returns 0 on success, negative number on error */
|
||||
};
|
||||
|
||||
extern SDL_HIDAPI_HapticDriver SDL_HIDAPI_HapticDriverLg4ff;
|
||||
|
||||
#endif //SDL_joystick_c_h_
|
||||
1265
src/haptic/hidapi/SDL_hidapihaptic_lg4ff.c
Normal file
1265
src/haptic/hidapi/SDL_hidapihaptic_lg4ff.c
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user