src/joystick/bsd/SDL_sysjoystick.c
changeset 12105 997ec56425a8
parent 12090 c3209fca27b2
child 12106 94f9bf5b9c36
equal deleted inserted replaced
12104:fc8ab202b5a8 12105:997ec56425a8
   159 #define REP_BUF_DATA(rep) ((rep)->buf->ugd_data)
   159 #define REP_BUF_DATA(rep) ((rep)->buf->ugd_data)
   160 #else
   160 #else
   161 #define REP_BUF_DATA(rep) ((rep)->buf->data)
   161 #define REP_BUF_DATA(rep) ((rep)->buf->data)
   162 #endif
   162 #endif
   163 
   163 
   164 static int SDL_SYS_numjoysticks = 0;
   164 static int numjoysticks = 0;
   165 
   165 
   166 int
   166 static int
   167 SDL_SYS_JoystickInit(void)
   167 BSD_JoystickInit(void)
   168 {
   168 {
   169     char s[16];
   169     char s[16];
   170     int i, fd;
   170     int i, fd;
   171 
   171 
   172     SDL_SYS_numjoysticks = 0;
   172     numjoysticks = 0;
   173 
   173 
   174     SDL_memset(joynames, 0, sizeof(joynames));
   174     SDL_memset(joynames, 0, sizeof(joynames));
   175     SDL_memset(joydevnames, 0, sizeof(joydevnames));
   175     SDL_memset(joydevnames, 0, sizeof(joydevnames));
   176 
   176 
   177     for (i = 0; i < MAX_UHID_JOYS; i++) {
   177     for (i = 0; i < MAX_UHID_JOYS; i++) {
   178         SDL_Joystick nj;
   178         SDL_Joystick nj;
   179 
   179 
   180         SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i);
   180         SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i);
   181 
   181 
   182         joynames[SDL_SYS_numjoysticks] = SDL_strdup(s);
   182         joynames[numjoysticks] = SDL_strdup(s);
   183 
   183 
   184         if (SDL_SYS_JoystickOpen(&nj, SDL_SYS_numjoysticks) == 0) {
   184         if (BSD_JoystickOpen(&nj, numjoysticks) == 0) {
   185             SDL_SYS_JoystickClose(&nj);
   185             BSD_JoystickClose(&nj);
   186             SDL_SYS_numjoysticks++;
   186             numjoysticks++;
   187         } else {
   187         } else {
   188             SDL_free(joynames[SDL_SYS_numjoysticks]);
   188             SDL_free(joynames[numjoysticks]);
   189             joynames[SDL_SYS_numjoysticks] = NULL;
   189             joynames[numjoysticks] = NULL;
   190         }
   190         }
   191     }
   191     }
   192     for (i = 0; i < MAX_JOY_JOYS; i++) {
   192     for (i = 0; i < MAX_JOY_JOYS; i++) {
   193         SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i);
   193         SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i);
   194         fd = open(s, O_RDONLY);
   194         fd = open(s, O_RDONLY);
   195         if (fd != -1) {
   195         if (fd != -1) {
   196             joynames[SDL_SYS_numjoysticks++] = SDL_strdup(s);
   196             joynames[numjoysticks++] = SDL_strdup(s);
   197             close(fd);
   197             close(fd);
   198         }
   198         }
   199     }
   199     }
   200 
   200 
   201     /* Read the default USB HID usage table. */
   201     /* Read the default USB HID usage table. */
   202     hid_init(NULL);
   202     hid_init(NULL);
   203 
   203 
   204     return (SDL_SYS_numjoysticks);
   204     return (numjoysticks);
   205 }
   205 }
   206 
   206 
   207 int
   207 static int
   208 SDL_SYS_NumJoysticks(void)
   208 BSD_JoystickGetCount(void)
   209 {
   209 {
   210     return SDL_SYS_numjoysticks;
   210     return numjoysticks;
   211 }
   211 }
   212 
   212 
   213 void
   213 static void
   214 SDL_SYS_JoystickDetect(void)
   214 BSD_JoystickDetect(void)
   215 {
   215 {
   216 }
   216 }
   217 
   217 
   218 const char *
   218 static const char *
   219 SDL_SYS_JoystickNameForDeviceIndex(int device_index)
   219 BSD_JoystickGetDeviceName(int device_index)
   220 {
   220 {
   221     if (joydevnames[device_index] != NULL) {
   221     if (joydevnames[device_index] != NULL) {
   222         return (joydevnames[device_index]);
   222         return (joydevnames[device_index]);
   223     }
   223     }
   224     return (joynames[device_index]);
   224     return (joynames[device_index]);
   225 }
   225 }
   226 
   226 
   227 /* Function to perform the mapping from device index to the instance id for this index */
   227 /* Function to perform the mapping from device index to the instance id for this index */
   228 SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
   228 static SDL_JoystickID
       
   229 BSD_JoystickGetDeviceInstanceID(int device_index)
   229 {
   230 {
   230     return device_index;
   231     return device_index;
   231 }
   232 }
   232 
   233 
   233 static int
   234 static int
   279         result = SDL_HAT_CENTERED;
   280         result = SDL_HAT_CENTERED;
   280     return result;
   281     return result;
   281 }
   282 }
   282 
   283 
   283 
   284 
   284 int
   285 static int
   285 SDL_SYS_JoystickOpen(SDL_Joystick * joy, int device_index)
   286 BSD_JoystickOpen(SDL_Joystick * joy, int device_index)
   286 {
   287 {
   287     char *path = joynames[device_index];
   288     char *path = joynames[device_index];
   288     struct joystick_hwdata *hw;
   289     struct joystick_hwdata *hw;
   289     struct hid_item hitem;
   290     struct hid_item hitem;
   290     struct hid_data *hdata;
   291     struct hid_data *hdata;
   363             str[i] = UGETW(usd.usd_desc.bString[i]);
   364             str[i] = UGETW(usd.usd_desc.bString[i]);
   364         }
   365         }
   365         str[i] = '\0';
   366         str[i] = '\0';
   366         asprintf(&new_name, "%s @ %s", str, path);
   367         asprintf(&new_name, "%s @ %s", str, path);
   367         if (new_name != NULL) {
   368         if (new_name != NULL) {
   368             SDL_free(joydevnames[SDL_SYS_numjoysticks]);
   369             SDL_free(joydevnames[numjoysticks]);
   369             joydevnames[SDL_SYS_numjoysticks] = new_name;
   370             joydevnames[numjoysticks] = new_name;
   370         }
   371         }
   371     }
   372     }
   372 desc_failed:
   373 desc_failed:
   373 #endif
   374 #endif
   374     if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
   375     if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
   465     SDL_free(hw->path);
   466     SDL_free(hw->path);
   466     SDL_free(hw);
   467     SDL_free(hw);
   467     return (-1);
   468     return (-1);
   468 }
   469 }
   469 
   470 
   470 void
   471 static void
   471 SDL_SYS_JoystickUpdate(SDL_Joystick * joy)
   472 BSD_JoystickUpdate(SDL_Joystick * joy)
   472 {
   473 {
   473     struct hid_item hitem;
   474     struct hid_item hitem;
   474     struct hid_data *hdata;
   475     struct hid_data *hdata;
   475     struct report *rep;
   476     struct report *rep;
   476     int nbutton, naxe = -1;
   477     int nbutton, naxe = -1;
   578         hid_end_parse(hdata);
   579         hid_end_parse(hdata);
   579     }
   580     }
   580 }
   581 }
   581 
   582 
   582 /* Function to close a joystick after use */
   583 /* Function to close a joystick after use */
   583 void
   584 static void
   584 SDL_SYS_JoystickClose(SDL_Joystick * joy)
   585 BSD_JoystickClose(SDL_Joystick * joy)
   585 {
   586 {
   586     if (SDL_strncmp(joy->hwdata->path, "/dev/joy", 8)) {
   587     if (SDL_strncmp(joy->hwdata->path, "/dev/joy", 8)) {
   587         report_free(&joy->hwdata->inreport);
   588         report_free(&joy->hwdata->inreport);
   588         hid_dispose_report_desc(joy->hwdata->repdesc);
   589         hid_dispose_report_desc(joy->hwdata->repdesc);
   589     }
   590     }
   590     close(joy->hwdata->fd);
   591     close(joy->hwdata->fd);
   591     SDL_free(joy->hwdata->path);
   592     SDL_free(joy->hwdata->path);
   592     SDL_free(joy->hwdata);
   593     SDL_free(joy->hwdata);
   593 }
   594 }
   594 
   595 
   595 void
   596 static void
   596 SDL_SYS_JoystickQuit(void)
   597 BSD_JoystickQuit(void)
   597 {
   598 {
   598     int i;
   599     int i;
   599 
   600 
   600     for (i = 0; i < MAX_JOYS; i++) {
   601     for (i = 0; i < MAX_JOYS; i++) {
   601         SDL_free(joynames[i]);
   602         SDL_free(joynames[i]);
   603     }
   604     }
   604 
   605 
   605     return;
   606     return;
   606 }
   607 }
   607 
   608 
   608 SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
   609 static SDL_JoystickGUID
       
   610 BSD_JoystickGetDeviceGUID( int device_index )
   609 {
   611 {
   610     SDL_JoystickGUID guid;
   612     SDL_JoystickGUID guid;
   611     /* the GUID is just the first 16 chars of the name for now */
   613     /* the GUID is just the first 16 chars of the name for now */
   612     const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
   614     const char *name = BSD_JoystickNameForDeviceIndex( device_index );
   613     SDL_zero( guid );
       
   614     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
       
   615     return guid;
       
   616 }
       
   617 
       
   618 SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
       
   619 {
       
   620     SDL_JoystickGUID guid;
       
   621     /* the GUID is just the first 16 chars of the name for now */
       
   622     const char *name = joystick->name;
       
   623     SDL_zero( guid );
   615     SDL_zero( guid );
   624     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   616     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   625     return guid;
   617     return guid;
   626 }
   618 }
   627 
   619 
   678 {
   670 {
   679     SDL_free(r->buf);
   671     SDL_free(r->buf);
   680     r->status = SREPORT_UNINIT;
   672     r->status = SREPORT_UNINIT;
   681 }
   673 }
   682 
   674 
       
   675 static int
       
   676 BSD_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
       
   677 {
       
   678     return SDL_Unsupported();
       
   679 }
       
   680 
       
   681 SDL_JoystickDriver SDL_BSD_JoystickDriver =
       
   682 {
       
   683     BSD_JoystickInit,
       
   684     BSD_JoystickGetCount,
       
   685     BSD_JoystickDetect,
       
   686     BSD_JoystickGetDeviceName,
       
   687     BSD_JoystickGetDeviceGUID,
       
   688     BSD_JoystickGetDeviceInstanceID,
       
   689     BSD_JoystickOpen,
       
   690     BSD_JoystickRumble,
       
   691     BSD_JoystickUpdate,
       
   692     BSD_JoystickClose,
       
   693     BSD_JoystickQuit,
       
   694 };
       
   695 
   683 #endif /* SDL_JOYSTICK_USBHID */
   696 #endif /* SDL_JOYSTICK_USBHID */
   684 
   697 
   685 /* vi: set ts=4 sw=4 expandtab: */
   698 /* vi: set ts=4 sw=4 expandtab: */