src/haptic/linux/SDL_syshaptic.c
changeset 9743 5f2ac7cec7e9
parent 9740 55e88e3bc7cc
child 9883 48cf7d652118
equal deleted inserted replaced
9742:4189503733ac 9743:5f2ac7cec7e9
    57  */
    57  */
    58 typedef struct SDL_hapticlist_item
    58 typedef struct SDL_hapticlist_item
    59 {
    59 {
    60     char *fname;                /* Dev path name (like /dev/input/event1) */
    60     char *fname;                /* Dev path name (like /dev/input/event1) */
    61     SDL_Haptic *haptic;         /* Associated haptic. */
    61     SDL_Haptic *haptic;         /* Associated haptic. */
       
    62     dev_t dev_num;
    62     struct SDL_hapticlist_item *next;
    63     struct SDL_hapticlist_item *next;
    63 } SDL_hapticlist_item;
    64 } SDL_hapticlist_item;
    64 
    65 
    65 
    66 
    66 /*
    67 /*
   234 #endif /* SDL_USE_LIBUDEV */
   235 #endif /* SDL_USE_LIBUDEV */
   235 
   236 
   236 static int
   237 static int
   237 MaybeAddDevice(const char *path)
   238 MaybeAddDevice(const char *path)
   238 {
   239 {
   239     dev_t dev_nums[MAX_HAPTICS];
       
   240     struct stat sb;
   240     struct stat sb;
   241     int fd;
   241     int fd;
   242     int k;
       
   243     int duplicate;
       
   244     int success;
   242     int success;
   245     SDL_hapticlist_item *item;
   243     SDL_hapticlist_item *item;
   246 
       
   247 
   244 
   248     if (path == NULL) {
   245     if (path == NULL) {
   249         return -1;
   246         return -1;
   250     }
   247     }
   251 
   248 
   253     if (stat(path, &sb) != 0) {
   250     if (stat(path, &sb) != 0) {
   254         return -1;
   251         return -1;
   255     }
   252     }
   256 
   253 
   257     /* check for duplicates */
   254     /* check for duplicates */
   258     duplicate = 0;
   255     for (item = SDL_hapticlist; item != NULL; item = item->next) {
   259     for (k = 0; (k < numhaptics) && !duplicate; ++k) {
   256         if (item->dev_num == sb.st_rdev) {
   260         if (sb.st_rdev == dev_nums[k]) {
   257             return -1;  /* duplicate. */
   261             duplicate = 1;
       
   262         }
   258         }
   263     }
       
   264     if (duplicate) {
       
   265         return -1;
       
   266     }
   259     }
   267 
   260 
   268     /* try to open */
   261     /* try to open */
   269     fd = open(path, O_RDWR, 0);
   262     fd = open(path, O_RDWR, 0);
   270     if (fd < 0) {
   263     if (fd < 0) {
   290     item->fname = SDL_strdup(path);
   283     item->fname = SDL_strdup(path);
   291     if (item->fname == NULL) {
   284     if (item->fname == NULL) {
   292         SDL_free(item);
   285         SDL_free(item);
   293         return -1;
   286         return -1;
   294     }
   287     }
       
   288 
       
   289     item->dev_num = sb.st_rdev;
   295 
   290 
   296     /* TODO: should we add instance IDs? */
   291     /* TODO: should we add instance IDs? */
   297     if (SDL_hapticlist_tail == NULL) {
   292     if (SDL_hapticlist_tail == NULL) {
   298         SDL_hapticlist = SDL_hapticlist_tail = item;
   293         SDL_hapticlist = SDL_hapticlist_tail = item;
   299     } else {
   294     } else {
   300         SDL_hapticlist_tail->next = item;
   295         SDL_hapticlist_tail->next = item;
   301         SDL_hapticlist_tail = item;
   296         SDL_hapticlist_tail = item;
   302     }
   297     }
   303 
       
   304     dev_nums[numhaptics] = sb.st_rdev;
       
   305 
   298 
   306     ++numhaptics;
   299     ++numhaptics;
   307 
   300 
   308     /* !!! TODO: Send a haptic add event? */
   301     /* !!! TODO: Send a haptic add event? */
   309 
   302 
   543 {
   536 {
   544     int device_index = 0;
   537     int device_index = 0;
   545     int fd;
   538     int fd;
   546     int ret;
   539     int ret;
   547     SDL_hapticlist_item *item;
   540     SDL_hapticlist_item *item;
   548 
       
   549 
   541 
   550     /* Find the joystick in the haptic list. */
   542     /* Find the joystick in the haptic list. */
   551     for (item = SDL_hapticlist; item; item = item->next) {
   543     for (item = SDL_hapticlist; item; item = item->next) {
   552         if (SDL_strcmp(item->fname, joystick->hwdata->fname) == 0) {
   544         if (SDL_strcmp(item->fname, joystick->hwdata->fname) == 0) {
   553             haptic->index = device_index;
   545             haptic->index = device_index;