Updated Haptic API for SDL 3.0 conventions

Also removed the XInput haptic support since using the haptic API for rumble is no longer supported.
This commit is contained in:
Sam Lantinga
2024-01-17 15:22:35 -08:00
parent 8ca9134115
commit f224af5ac5
35 changed files with 943 additions and 1320 deletions

View File

@@ -49,6 +49,7 @@ static void haptic_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class,
*/
typedef struct SDL_hapticlist_item
{
SDL_HapticID instance_id;
char *fname; /* Dev path name (like /dev/input/event1) */
SDL_Haptic *haptic; /* Associated haptic. */
dev_t dev_num;
@@ -196,6 +197,17 @@ static SDL_hapticlist_item *HapticByDevIndex(int device_index)
return item;
}
static SDL_hapticlist_item *HapticByInstanceID(SDL_HapticID instance_id)
{
SDL_hapticlist_item *item;
for (item = SDL_hapticlist; item; item = item->next) {
if (instance_id == item->instance_id) {
return item;
}
}
return NULL;
}
#ifdef SDL_USE_LIBUDEV
static void haptic_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath)
{
@@ -263,6 +275,7 @@ static int MaybeAddDevice(const char *path)
return -1;
}
item->instance_id = SDL_GetNextObjectID();
item->fname = SDL_strdup(path);
if (!item->fname) {
SDL_free(item);
@@ -299,7 +312,7 @@ static int MaybeRemoveDevice(const char *path)
for (item = SDL_hapticlist; item; item = item->next) {
/* found it, remove it. */
if (SDL_strcmp(path, item->fname) == 0) {
const int retval = item->haptic ? item->haptic->index : -1;
const int retval = item->haptic ? 0 : -1;
if (prev) {
prev->next = item->next;
@@ -326,6 +339,20 @@ static int MaybeRemoveDevice(const char *path)
}
#endif /* SDL_USE_LIBUDEV */
/*
* Return the instance ID of a haptic device, does not need to be opened.
*/
SDL_HapticID SDL_SYS_HapticInstanceID(int index)
{
SDL_hapticlist_item *item;
item = HapticByDevIndex(index);
if (item) {
return item->instance_id;
}
return 0;
}
/*
* Gets the name from a file descriptor.
*/
@@ -348,23 +375,23 @@ const char *SDL_SYS_HapticName(int index)
{
SDL_hapticlist_item *item;
int fd;
const char *name;
const char *name = NULL;
item = HapticByDevIndex(index);
/* Open the haptic device. */
name = NULL;
fd = open(item->fname, O_RDONLY | O_CLOEXEC, 0);
if (item) {
/* Open the haptic device. */
fd = open(item->fname, O_RDONLY | O_CLOEXEC, 0);
if (fd >= 0) {
if (fd >= 0) {
name = SDL_SYS_HapticNameFromFD(fd);
if (!name) {
/* No name found, return device character device */
name = item->fname;
name = SDL_SYS_HapticNameFromFD(fd);
if (!name) {
/* No name found, return device character device */
name = item->fname;
}
close(fd);
}
close(fd);
}
return name;
}
@@ -422,7 +449,7 @@ int SDL_SYS_HapticOpen(SDL_Haptic *haptic)
int ret;
SDL_hapticlist_item *item;
item = HapticByDevIndex(haptic->index);
item = HapticByInstanceID(haptic->instance_id);
/* Open the character device */
fd = open(item->fname, O_RDWR | O_CLOEXEC, 0);
if (fd < 0) {
@@ -516,10 +543,10 @@ int SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
int SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
{
#ifdef SDL_JOYSTICK_LINUX
int device_index = 0;
int fd;
int ret;
SDL_hapticlist_item *item;
const char *name;
SDL_AssertJoysticksLocked();
@@ -529,14 +556,9 @@ int SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
/* Find the joystick in the haptic list. */
for (item = SDL_hapticlist; item; item = item->next) {
if (SDL_strcmp(item->fname, joystick->hwdata->fname) == 0) {
haptic->instance_id = item->instance_id;
break;
}
++device_index;
}
haptic->index = device_index;
if (device_index >= MAX_HAPTICS) {
return SDL_SetError("Haptic: Joystick doesn't have Haptic capabilities");
}
fd = open(joystick->hwdata->fname, O_RDWR | O_CLOEXEC, 0);
@@ -551,6 +573,10 @@ int SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
haptic->hwdata->fname = SDL_strdup(joystick->hwdata->fname);
name = SDL_SYS_HapticNameFromFD(fd);
if (name) {
haptic->name = SDL_strdup(name);
}
return 0;
#else
return -1;