Updated BSD joystick code for the new API
authorSam Lantinga <slouken@libsdl.org>
Mon, 26 Nov 2012 22:09:58 -0800
changeset 6697dbda91031456
parent 6696 73e2ad636f6e
child 6698 28ab2ef7bfc9
Updated BSD joystick code for the new API
src/joystick/bsd/SDL_sysjoystick.c
     1.1 --- a/src/joystick/bsd/SDL_sysjoystick.c	Mon Nov 26 22:09:34 2012 -0800
     1.2 +++ b/src/joystick/bsd/SDL_sysjoystick.c	Mon Nov 26 22:09:58 2012 -0800
     1.3 @@ -157,13 +157,15 @@
     1.4  #define REP_BUF_DATA(rep) ((rep)->buf->data)
     1.5  #endif
     1.6  
     1.7 +int SDL_SYS_numjoysticks = 0;
     1.8 +
     1.9  int
    1.10  SDL_SYS_JoystickInit(void)
    1.11  {
    1.12      char s[16];
    1.13      int i, fd;
    1.14  
    1.15 -    SDL_numjoysticks = 0;
    1.16 +    SDL_SYS_numjoysticks = 0;
    1.17  
    1.18      SDL_memset(joynames, 0, sizeof(joynames));
    1.19      SDL_memset(joydevnames, 0, sizeof(joydevnames));
    1.20 @@ -173,12 +175,12 @@
    1.21  
    1.22          SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i);
    1.23  
    1.24 -        nj.index = SDL_numjoysticks;
    1.25 +        nj.index = SDL_SYS_numjoysticks;
    1.26          joynames[nj.index] = strdup(s);
    1.27  
    1.28 -        if (SDL_SYS_JoystickOpen(&nj) == 0) {
    1.29 +        if (SDL_SYS_JoystickOpen(&nj, nj.index) == 0) {
    1.30              SDL_SYS_JoystickClose(&nj);
    1.31 -            SDL_numjoysticks++;
    1.32 +            SDL_SYS_numjoysticks++;
    1.33          } else {
    1.34              SDL_free(joynames[nj.index]);
    1.35              joynames[nj.index] = NULL;
    1.36 @@ -188,7 +190,7 @@
    1.37          SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i);
    1.38          fd = open(s, O_RDONLY);
    1.39          if (fd != -1) {
    1.40 -            joynames[SDL_numjoysticks++] = strdup(s);
    1.41 +            joynames[SDL_SYS_numjoysticks++] = strdup(s);
    1.42              close(fd);
    1.43          }
    1.44      }
    1.45 @@ -196,11 +198,11 @@
    1.46      /* Read the default USB HID usage table. */
    1.47      hid_init(NULL);
    1.48  
    1.49 -    return (SDL_numjoysticks);
    1.50 +    return (SDL_SYS_numjoysticks);
    1.51  }
    1.52  
    1.53  const char *
    1.54 -SDL_SYS_JoystickName(int index)
    1.55 +SDL_SYS_JoystickNameForIndex(int index)
    1.56  {
    1.57      if (joydevnames[index] != NULL) {
    1.58          return (joydevnames[index]);
    1.59 @@ -260,9 +262,9 @@
    1.60  
    1.61  
    1.62  int
    1.63 -SDL_SYS_JoystickOpen(SDL_Joystick * joy)
    1.64 +SDL_SYS_JoystickOpen(SDL_Joystick * joy, int device_index)
    1.65  {
    1.66 -    char *path = joynames[joy->index];
    1.67 +    char *path = joynames[device_index];
    1.68      struct joystick_hwdata *hw;
    1.69      struct hid_item hitem;
    1.70      struct hid_data *hdata;
    1.71 @@ -556,6 +558,52 @@
    1.72      return;
    1.73  }
    1.74  
    1.75 +/* Function to perform the mapping from device index to the instance id for this index */
    1.76 +SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int index)
    1.77 +{
    1.78 +    return index;
    1.79 +}
    1.80 +
    1.81 +/* Function to determine is this joystick is attached to the system right now */
    1.82 +int SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
    1.83 +{
    1.84 +    return 1;
    1.85 +}
    1.86 +
    1.87 +int SDL_SYS_NumJoysticks()
    1.88 +{
    1.89 +    return SDL_SYS_numjoysticks;
    1.90 +}
    1.91 +
    1.92 +int SDL_SYS_JoystickNeedsPolling()
    1.93 +{
    1.94 +    return 0;
    1.95 +}
    1.96 +
    1.97 +void SDL_SYS_JoystickDetect()
    1.98 +{
    1.99 +}
   1.100 +
   1.101 +JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
   1.102 +{
   1.103 +    JoystickGUID guid;
   1.104 +    // the GUID is just the first 16 chars of the name for now
   1.105 +    const char *name = SDL_SYS_JoystickNameForIndex( device_index );
   1.106 +    SDL_zero( guid );
   1.107 +    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   1.108 +    return guid;
   1.109 +}
   1.110 +
   1.111 +JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
   1.112 +{
   1.113 +    JoystickGUID guid;
   1.114 +    // the GUID is just the first 16 chars of the name for now
   1.115 +    const char *name = joystick->name;
   1.116 +    SDL_zero( guid );
   1.117 +    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   1.118 +    return guid;
   1.119 +}
   1.120 +
   1.121  static int
   1.122  report_alloc(struct report *r, struct report_desc *rd, int repind)
   1.123  {
   1.124 @@ -612,4 +660,5 @@
   1.125  }
   1.126  
   1.127  #endif /* SDL_JOYSTICK_USBHID */
   1.128 +
   1.129  /* vi: set ts=4 sw=4 expandtab: */