Skip to content

Commit

Permalink
Haptic/Linux: Keep track of device numbers properly to track duplicates.
Browse files Browse the repository at this point in the history
Fixes Bugzilla #3014.
  • Loading branch information
icculus committed Jun 16, 2015
1 parent d797582 commit 0c3830a
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/haptic/linux/SDL_syshaptic.c
Expand Up @@ -59,6 +59,7 @@ typedef struct SDL_hapticlist_item
{
char *fname; /* Dev path name (like /dev/input/event1) */
SDL_Haptic *haptic; /* Associated haptic. */
dev_t dev_num;
struct SDL_hapticlist_item *next;
} SDL_hapticlist_item;

Expand Down Expand Up @@ -236,15 +237,11 @@ void haptic_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const
static int
MaybeAddDevice(const char *path)
{
dev_t dev_nums[MAX_HAPTICS];
struct stat sb;
int fd;
int k;
int duplicate;
int success;
SDL_hapticlist_item *item;


if (path == NULL) {
return -1;
}
Expand All @@ -255,15 +252,11 @@ MaybeAddDevice(const char *path)
}

/* check for duplicates */
duplicate = 0;
for (k = 0; (k < numhaptics) && !duplicate; ++k) {
if (sb.st_rdev == dev_nums[k]) {
duplicate = 1;
for (item = SDL_hapticlist; item != NULL; item = item->next) {
if (item->dev_num == sb.st_rdev) {
return -1; /* duplicate. */
}
}
if (duplicate) {
return -1;
}

/* try to open */
fd = open(path, O_RDWR, 0);
Expand Down Expand Up @@ -293,6 +286,8 @@ MaybeAddDevice(const char *path)
return -1;
}

item->dev_num = sb.st_rdev;

/* TODO: should we add instance IDs? */
if (SDL_hapticlist_tail == NULL) {
SDL_hapticlist = SDL_hapticlist_tail = item;
Expand All @@ -301,8 +296,6 @@ MaybeAddDevice(const char *path)
SDL_hapticlist_tail = item;
}

dev_nums[numhaptics] = sb.st_rdev;

++numhaptics;

/* !!! TODO: Send a haptic add event? */
Expand Down Expand Up @@ -546,7 +539,6 @@ SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
int ret;
SDL_hapticlist_item *item;


/* Find the joystick in the haptic list. */
for (item = SDL_hapticlist; item; item = item->next) {
if (SDL_strcmp(item->fname, joystick->hwdata->fname) == 0) {
Expand Down

0 comments on commit 0c3830a

Please sign in to comment.