bsd: Update joystick code for new interfaces.
authorRyan C. Gordon <icculus@icculus.org>
Fri, 10 Aug 2018 14:42:40 -0400
changeset 12105997ec56425a8
parent 12104 fc8ab202b5a8
child 12106 94f9bf5b9c36
bsd: Update joystick code for new interfaces.

(this is an untested push to see if buildbot likes it.)
src/joystick/SDL_joystick.c
src/joystick/SDL_sysjoystick.h
src/joystick/bsd/SDL_sysjoystick.c
     1.1 --- a/src/joystick/SDL_joystick.c	Fri Aug 10 14:32:30 2018 -0400
     1.2 +++ b/src/joystick/SDL_joystick.c	Fri Aug 10 14:42:40 2018 -0400
     1.3 @@ -64,6 +64,9 @@
     1.4  #ifdef SDL_JOYSTICK_EMSCRIPTEN
     1.5      &SDL_EMSCRIPTEN_JoystickDriver,
     1.6  #endif
     1.7 +#ifdef SDL_JOYSTICK_USBHID  /* !!! FIXME: "USBHID" is a generic name, and doubly-confusing with HIDAPI next to it. This is the *BSD interface, rename this. */
     1.8 +    &SDL_BSD_JoystickDriver,
     1.9 +#endif
    1.10  #ifdef SDL_JOYSTICK_HIDAPI
    1.11      &SDL_HIDAPI_JoystickDriver,
    1.12  #endif
     2.1 --- a/src/joystick/SDL_sysjoystick.h	Fri Aug 10 14:32:30 2018 -0400
     2.2 +++ b/src/joystick/SDL_sysjoystick.h	Fri Aug 10 14:42:40 2018 -0400
     2.3 @@ -138,6 +138,7 @@
     2.4  
     2.5  /* The available joystick drivers */
     2.6  extern SDL_JoystickDriver SDL_ANDROID_JoystickDriver;
     2.7 +extern SDL_JoystickDriver SDL_BSD_JoystickDriver;
     2.8  extern SDL_JoystickDriver SDL_DARWIN_JoystickDriver;
     2.9  extern SDL_JoystickDriver SDL_DUMMY_JoystickDriver;
    2.10  extern SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver;
     3.1 --- a/src/joystick/bsd/SDL_sysjoystick.c	Fri Aug 10 14:32:30 2018 -0400
     3.2 +++ b/src/joystick/bsd/SDL_sysjoystick.c	Fri Aug 10 14:42:40 2018 -0400
     3.3 @@ -161,15 +161,15 @@
     3.4  #define REP_BUF_DATA(rep) ((rep)->buf->data)
     3.5  #endif
     3.6  
     3.7 -static int SDL_SYS_numjoysticks = 0;
     3.8 +static int numjoysticks = 0;
     3.9  
    3.10 -int
    3.11 -SDL_SYS_JoystickInit(void)
    3.12 +static int
    3.13 +BSD_JoystickInit(void)
    3.14  {
    3.15      char s[16];
    3.16      int i, fd;
    3.17  
    3.18 -    SDL_SYS_numjoysticks = 0;
    3.19 +    numjoysticks = 0;
    3.20  
    3.21      SDL_memset(joynames, 0, sizeof(joynames));
    3.22      SDL_memset(joydevnames, 0, sizeof(joydevnames));
    3.23 @@ -179,21 +179,21 @@
    3.24  
    3.25          SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i);
    3.26  
    3.27 -        joynames[SDL_SYS_numjoysticks] = SDL_strdup(s);
    3.28 +        joynames[numjoysticks] = SDL_strdup(s);
    3.29  
    3.30 -        if (SDL_SYS_JoystickOpen(&nj, SDL_SYS_numjoysticks) == 0) {
    3.31 -            SDL_SYS_JoystickClose(&nj);
    3.32 -            SDL_SYS_numjoysticks++;
    3.33 +        if (BSD_JoystickOpen(&nj, numjoysticks) == 0) {
    3.34 +            BSD_JoystickClose(&nj);
    3.35 +            numjoysticks++;
    3.36          } else {
    3.37 -            SDL_free(joynames[SDL_SYS_numjoysticks]);
    3.38 -            joynames[SDL_SYS_numjoysticks] = NULL;
    3.39 +            SDL_free(joynames[numjoysticks]);
    3.40 +            joynames[numjoysticks] = NULL;
    3.41          }
    3.42      }
    3.43      for (i = 0; i < MAX_JOY_JOYS; i++) {
    3.44          SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i);
    3.45          fd = open(s, O_RDONLY);
    3.46          if (fd != -1) {
    3.47 -            joynames[SDL_SYS_numjoysticks++] = SDL_strdup(s);
    3.48 +            joynames[numjoysticks++] = SDL_strdup(s);
    3.49              close(fd);
    3.50          }
    3.51      }
    3.52 @@ -201,22 +201,22 @@
    3.53      /* Read the default USB HID usage table. */
    3.54      hid_init(NULL);
    3.55  
    3.56 -    return (SDL_SYS_numjoysticks);
    3.57 +    return (numjoysticks);
    3.58  }
    3.59  
    3.60 -int
    3.61 -SDL_SYS_NumJoysticks(void)
    3.62 +static int
    3.63 +BSD_JoystickGetCount(void)
    3.64  {
    3.65 -    return SDL_SYS_numjoysticks;
    3.66 +    return numjoysticks;
    3.67  }
    3.68  
    3.69 -void
    3.70 -SDL_SYS_JoystickDetect(void)
    3.71 +static void
    3.72 +BSD_JoystickDetect(void)
    3.73  {
    3.74  }
    3.75  
    3.76 -const char *
    3.77 -SDL_SYS_JoystickNameForDeviceIndex(int device_index)
    3.78 +static const char *
    3.79 +BSD_JoystickGetDeviceName(int device_index)
    3.80  {
    3.81      if (joydevnames[device_index] != NULL) {
    3.82          return (joydevnames[device_index]);
    3.83 @@ -225,7 +225,8 @@
    3.84  }
    3.85  
    3.86  /* Function to perform the mapping from device index to the instance id for this index */
    3.87 -SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
    3.88 +static SDL_JoystickID
    3.89 +BSD_JoystickGetDeviceInstanceID(int device_index)
    3.90  {
    3.91      return device_index;
    3.92  }
    3.93 @@ -281,8 +282,8 @@
    3.94  }
    3.95  
    3.96  
    3.97 -int
    3.98 -SDL_SYS_JoystickOpen(SDL_Joystick * joy, int device_index)
    3.99 +static int
   3.100 +BSD_JoystickOpen(SDL_Joystick * joy, int device_index)
   3.101  {
   3.102      char *path = joynames[device_index];
   3.103      struct joystick_hwdata *hw;
   3.104 @@ -365,8 +366,8 @@
   3.105          str[i] = '\0';
   3.106          asprintf(&new_name, "%s @ %s", str, path);
   3.107          if (new_name != NULL) {
   3.108 -            SDL_free(joydevnames[SDL_SYS_numjoysticks]);
   3.109 -            joydevnames[SDL_SYS_numjoysticks] = new_name;
   3.110 +            SDL_free(joydevnames[numjoysticks]);
   3.111 +            joydevnames[numjoysticks] = new_name;
   3.112          }
   3.113      }
   3.114  desc_failed:
   3.115 @@ -467,8 +468,8 @@
   3.116      return (-1);
   3.117  }
   3.118  
   3.119 -void
   3.120 -SDL_SYS_JoystickUpdate(SDL_Joystick * joy)
   3.121 +static void
   3.122 +BSD_JoystickUpdate(SDL_Joystick * joy)
   3.123  {
   3.124      struct hid_item hitem;
   3.125      struct hid_data *hdata;
   3.126 @@ -580,8 +581,8 @@
   3.127  }
   3.128  
   3.129  /* Function to close a joystick after use */
   3.130 -void
   3.131 -SDL_SYS_JoystickClose(SDL_Joystick * joy)
   3.132 +static void
   3.133 +BSD_JoystickClose(SDL_Joystick * joy)
   3.134  {
   3.135      if (SDL_strncmp(joy->hwdata->path, "/dev/joy", 8)) {
   3.136          report_free(&joy->hwdata->inreport);
   3.137 @@ -592,8 +593,8 @@
   3.138      SDL_free(joy->hwdata);
   3.139  }
   3.140  
   3.141 -void
   3.142 -SDL_SYS_JoystickQuit(void)
   3.143 +static void
   3.144 +BSD_JoystickQuit(void)
   3.145  {
   3.146      int i;
   3.147  
   3.148 @@ -605,21 +606,12 @@
   3.149      return;
   3.150  }
   3.151  
   3.152 -SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
   3.153 +static SDL_JoystickGUID
   3.154 +BSD_JoystickGetDeviceGUID( int device_index )
   3.155  {
   3.156      SDL_JoystickGUID guid;
   3.157      /* the GUID is just the first 16 chars of the name for now */
   3.158 -    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
   3.159 -    SDL_zero( guid );
   3.160 -    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   3.161 -    return guid;
   3.162 -}
   3.163 -
   3.164 -SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
   3.165 -{
   3.166 -    SDL_JoystickGUID guid;
   3.167 -    /* the GUID is just the first 16 chars of the name for now */
   3.168 -    const char *name = joystick->name;
   3.169 +    const char *name = BSD_JoystickNameForDeviceIndex( device_index );
   3.170      SDL_zero( guid );
   3.171      SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   3.172      return guid;
   3.173 @@ -680,6 +672,27 @@
   3.174      r->status = SREPORT_UNINIT;
   3.175  }
   3.176  
   3.177 +static int
   3.178 +BSD_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
   3.179 +{
   3.180 +    return SDL_Unsupported();
   3.181 +}
   3.182 +
   3.183 +SDL_JoystickDriver SDL_BSD_JoystickDriver =
   3.184 +{
   3.185 +    BSD_JoystickInit,
   3.186 +    BSD_JoystickGetCount,
   3.187 +    BSD_JoystickDetect,
   3.188 +    BSD_JoystickGetDeviceName,
   3.189 +    BSD_JoystickGetDeviceGUID,
   3.190 +    BSD_JoystickGetDeviceInstanceID,
   3.191 +    BSD_JoystickOpen,
   3.192 +    BSD_JoystickRumble,
   3.193 +    BSD_JoystickUpdate,
   3.194 +    BSD_JoystickClose,
   3.195 +    BSD_JoystickQuit,
   3.196 +};
   3.197 +
   3.198  #endif /* SDL_JOYSTICK_USBHID */
   3.199  
   3.200  /* vi: set ts=4 sw=4 expandtab: */