src/haptic/SDL_haptic.c
changeset 8170 3b7bb407fcf7
parent 8149 681eb46b8ac4
child 8974 f813b8b10b55
     1.1 --- a/src/haptic/SDL_haptic.c	Tue Feb 04 15:40:51 2014 -0500
     1.2 +++ b/src/haptic/SDL_haptic.c	Tue Feb 04 15:44:09 2014 -0500
     1.3 @@ -25,8 +25,7 @@
     1.4  #include "../joystick/SDL_joystick_c.h" /* For SDL_PrivateJoystickValid */
     1.5  #include "SDL_assert.h"
     1.6  
     1.7 -Uint8 SDL_numhaptics = 0;
     1.8 -SDL_Haptic **SDL_haptics = NULL;
     1.9 +SDL_Haptic *SDL_haptics = NULL;
    1.10  
    1.11  
    1.12  /*
    1.13 @@ -35,20 +34,10 @@
    1.14  int
    1.15  SDL_HapticInit(void)
    1.16  {
    1.17 -    int arraylen;
    1.18      int status;
    1.19  
    1.20 -    SDL_numhaptics = 0;
    1.21      status = SDL_SYS_HapticInit();
    1.22      if (status >= 0) {
    1.23 -        arraylen = (status + 1) * sizeof(*SDL_haptics);
    1.24 -        SDL_haptics = (SDL_Haptic **) SDL_malloc(arraylen);
    1.25 -        if (SDL_haptics == NULL) {      /* Out of memory. */
    1.26 -            SDL_numhaptics = 0;
    1.27 -        } else {
    1.28 -            SDL_memset(SDL_haptics, 0, arraylen);
    1.29 -            SDL_numhaptics = status;
    1.30 -        }
    1.31          status = 0;
    1.32      }
    1.33  
    1.34 @@ -62,16 +51,19 @@
    1.35  static int
    1.36  ValidHaptic(SDL_Haptic * haptic)
    1.37  {
    1.38 -    int i;
    1.39      int valid;
    1.40 +    SDL_Haptic *hapticlist;
    1.41  
    1.42      valid = 0;
    1.43      if (haptic != NULL) {
    1.44 -        for (i = 0; i < SDL_numhaptics; i++) {
    1.45 -            if (SDL_haptics[i] == haptic) {
    1.46 +        hapticlist = SDL_haptics;
    1.47 +        while ( hapticlist )
    1.48 +        {
    1.49 +            if (hapticlist == haptic) {
    1.50                  valid = 1;
    1.51                  break;
    1.52              }
    1.53 +            hapticlist = hapticlist->next;
    1.54          }
    1.55      }
    1.56  
    1.57 @@ -90,7 +82,7 @@
    1.58  int
    1.59  SDL_NumHaptics(void)
    1.60  {
    1.61 -    return SDL_numhaptics;
    1.62 +    return SDL_SYS_NumHaptics();
    1.63  }
    1.64  
    1.65  
    1.66 @@ -100,9 +92,9 @@
    1.67  const char *
    1.68  SDL_HapticName(int device_index)
    1.69  {
    1.70 -    if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
    1.71 +    if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
    1.72          SDL_SetError("Haptic: There are %d haptic devices available",
    1.73 -                     SDL_numhaptics);
    1.74 +                     SDL_NumHaptics());
    1.75          return NULL;
    1.76      }
    1.77      return SDL_SYS_HapticName(device_index);
    1.78 @@ -115,22 +107,27 @@
    1.79  SDL_Haptic *
    1.80  SDL_HapticOpen(int device_index)
    1.81  {
    1.82 -    int i;
    1.83      SDL_Haptic *haptic;
    1.84 +    SDL_Haptic *hapticlist;
    1.85  
    1.86 -    if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
    1.87 +    if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
    1.88          SDL_SetError("Haptic: There are %d haptic devices available",
    1.89 -                     SDL_numhaptics);
    1.90 +                     SDL_NumHaptics());
    1.91          return NULL;
    1.92      }
    1.93  
    1.94 -    /* If the haptic is already open, return it */
    1.95 -    for (i = 0; SDL_haptics[i]; i++) {
    1.96 -        if (device_index == SDL_haptics[i]->index) {
    1.97 -            haptic = SDL_haptics[i];
    1.98 +    hapticlist = SDL_haptics;
    1.99 +    /* If the haptic is already open, return it
   1.100 +    * TODO: Should we create haptic instance IDs like the Joystick API?
   1.101 +    */
   1.102 +    while ( hapticlist )
   1.103 +    {
   1.104 +        if (device_index == hapticlist->index) {
   1.105 +            haptic = hapticlist;
   1.106              ++haptic->ref_count;
   1.107              return haptic;
   1.108          }
   1.109 +        hapticlist = hapticlist->next;
   1.110      }
   1.111  
   1.112      /* Create the haptic device */
   1.113 @@ -150,15 +147,10 @@
   1.114      }
   1.115  
   1.116      /* Add haptic to list */
   1.117 -    for (i = 0; SDL_haptics[i]; i++)
   1.118 -        /* Skip to next haptic */ ;
   1.119 -    if (i >= SDL_numhaptics) {
   1.120 -        SDL_free(haptic);
   1.121 -        SDL_SetError("Haptic: Trying to add device past the number originally detected");
   1.122 -        return NULL;
   1.123 -    }
   1.124 -    SDL_haptics[i] = haptic;
   1.125      ++haptic->ref_count;
   1.126 +    /* Link the haptic in the list */
   1.127 +    haptic->next = SDL_haptics;
   1.128 +    SDL_haptics = haptic;
   1.129  
   1.130      /* Disable autocenter and set gain to max. */
   1.131      if (haptic->supported & SDL_HAPTIC_GAIN)
   1.132 @@ -176,21 +168,26 @@
   1.133  int
   1.134  SDL_HapticOpened(int device_index)
   1.135  {
   1.136 -    int i, opened;
   1.137 +    int opened;
   1.138 +    SDL_Haptic *hapticlist;
   1.139  
   1.140      /* Make sure it's valid. */
   1.141 -    if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
   1.142 +    if ((device_index < 0) || (device_index >= SDL_NumHaptics())) {
   1.143          SDL_SetError("Haptic: There are %d haptic devices available",
   1.144 -                     SDL_numhaptics);
   1.145 +                     SDL_NumHaptics());
   1.146          return 0;
   1.147      }
   1.148  
   1.149      opened = 0;
   1.150 -    for (i = 0; SDL_haptics[i]; i++) {
   1.151 -        if (SDL_haptics[i]->index == (Uint8) device_index) {
   1.152 +    hapticlist = SDL_haptics;
   1.153 +    /* TODO Should this use an instance ID? */
   1.154 +    while ( hapticlist )
   1.155 +    {
   1.156 +        if (hapticlist->index == (Uint8) device_index) {
   1.157              opened = 1;
   1.158              break;
   1.159          }
   1.160 +        hapticlist = hapticlist->next;
   1.161      }
   1.162      return opened;
   1.163  }
   1.164 @@ -271,13 +268,13 @@
   1.165  SDL_Haptic *
   1.166  SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
   1.167  {
   1.168 -    int i;
   1.169      SDL_Haptic *haptic;
   1.170 +    SDL_Haptic *hapticlist;
   1.171  
   1.172      /* Make sure there is room. */
   1.173 -    if (SDL_numhaptics <= 0) {
   1.174 +    if (SDL_NumHaptics() <= 0) {
   1.175          SDL_SetError("Haptic: There are %d haptic devices available",
   1.176 -                     SDL_numhaptics);
   1.177 +                     SDL_NumHaptics());
   1.178          return NULL;
   1.179      }
   1.180  
   1.181 @@ -293,13 +290,16 @@
   1.182          return NULL;
   1.183      }
   1.184  
   1.185 +    hapticlist = SDL_haptics;
   1.186      /* Check to see if joystick's haptic is already open */
   1.187 -    for (i = 0; SDL_haptics[i]; i++) {
   1.188 -        if (SDL_SYS_JoystickSameHaptic(SDL_haptics[i], joystick)) {
   1.189 -            haptic = SDL_haptics[i];
   1.190 +    while ( hapticlist )
   1.191 +    {
   1.192 +        if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick)) {
   1.193 +            haptic = hapticlist;
   1.194              ++haptic->ref_count;
   1.195              return haptic;
   1.196          }
   1.197 +        hapticlist = hapticlist->next;
   1.198      }
   1.199  
   1.200      /* Create the haptic device */
   1.201 @@ -318,15 +318,10 @@
   1.202      }
   1.203  
   1.204      /* Add haptic to list */
   1.205 -    for (i = 0; SDL_haptics[i]; i++)
   1.206 -        /* Skip to next haptic */ ;
   1.207 -    if (i >= SDL_numhaptics) {
   1.208 -        SDL_free(haptic);
   1.209 -        SDL_SetError("Haptic: Trying to add device past the number originally detected");
   1.210 -        return NULL;
   1.211 -    }
   1.212 -    SDL_haptics[i] = haptic;
   1.213      ++haptic->ref_count;
   1.214 +    /* Link the haptic in the list */
   1.215 +    haptic->next = SDL_haptics;
   1.216 +    SDL_haptics = haptic;
   1.217  
   1.218      return haptic;
   1.219  }
   1.220 @@ -339,6 +334,8 @@
   1.221  SDL_HapticClose(SDL_Haptic * haptic)
   1.222  {
   1.223      int i;
   1.224 +    SDL_Haptic *hapticlist;
   1.225 +    SDL_Haptic *hapticlistprev;
   1.226  
   1.227      /* Must be valid */
   1.228      if (!ValidHaptic(haptic)) {
   1.229 @@ -359,13 +356,26 @@
   1.230      SDL_SYS_HapticClose(haptic);
   1.231  
   1.232      /* Remove from the list */
   1.233 -    for (i = 0; SDL_haptics[i]; ++i) {
   1.234 -        if (haptic == SDL_haptics[i]) {
   1.235 -            SDL_haptics[i] = NULL;
   1.236 -            SDL_memcpy(&SDL_haptics[i], &SDL_haptics[i + 1],
   1.237 -                       (SDL_numhaptics - i) * sizeof(haptic));
   1.238 +    hapticlist = SDL_haptics;
   1.239 +    hapticlistprev = NULL;
   1.240 +    while ( hapticlist )
   1.241 +    {
   1.242 +        if (haptic == hapticlist)
   1.243 +        {
   1.244 +            if ( hapticlistprev )
   1.245 +            {
   1.246 +                /* unlink this entry */
   1.247 +                hapticlistprev->next = hapticlist->next;
   1.248 +            }
   1.249 +            else
   1.250 +            {
   1.251 +                SDL_haptics = haptic->next;
   1.252 +            }
   1.253 +
   1.254              break;
   1.255          }
   1.256 +        hapticlistprev = hapticlist;
   1.257 +        hapticlist = hapticlist->next;
   1.258      }
   1.259  
   1.260      /* Free */
   1.261 @@ -379,9 +389,8 @@
   1.262  SDL_HapticQuit(void)
   1.263  {
   1.264      SDL_SYS_HapticQuit();
   1.265 -    SDL_free(SDL_haptics);
   1.266 +    SDL_assert(SDL_haptics == NULL);
   1.267      SDL_haptics = NULL;
   1.268 -    SDL_numhaptics = 0;
   1.269  }
   1.270  
   1.271  /*