Fixed bug 2869 - Controllers connected on launch are reported twice.
authorSam Lantinga
Tue, 26 May 2015 08:52:02 -0700
changeset 96299242a9b29c7d
parent 9628 065e4ddc8753
child 9630 6c86d3216f6a
Fixed bug 2869 - Controllers connected on launch are reported twice.

Since all device detection/removal happens on the main thread now, post events inline with when the status changes occur.
Also fixed rare cases when joystick API functions could return data about removed joysticks when called with a device index.
src/joystick/darwin/SDL_sysjoystick.c
src/joystick/darwin/SDL_sysjoystick_c.h
     1.1 --- a/src/joystick/darwin/SDL_sysjoystick.c	Tue May 26 11:38:04 2015 -0400
     1.2 +++ b/src/joystick/darwin/SDL_sysjoystick.c	Tue May 26 08:52:02 2015 -0700
     1.3 @@ -46,13 +46,23 @@
     1.4  /* Linked list of all available devices */
     1.5  static recDevice *gpDeviceList = NULL;
     1.6  
     1.7 -/* if SDL_TRUE then a device was added since the last update call */
     1.8 -static SDL_bool s_bDeviceAdded = SDL_FALSE;
     1.9 -static SDL_bool s_bDeviceRemoved = SDL_FALSE;
    1.10 -
    1.11  /* static incrementing counter for new joystick devices seen on the system. Devices should start with index 0 */
    1.12  static int s_joystick_instance_id = -1;
    1.13  
    1.14 +static recDevice *GetDeviceForIndex(int device_index)
    1.15 +{
    1.16 +    recDevice *device = gpDeviceList;
    1.17 +    while (device) {
    1.18 +        if (!device->removed) {
    1.19 +            if (device_index == 0)
    1.20 +                break;
    1.21 +
    1.22 +            --device_index;
    1.23 +        }
    1.24 +        device = device->pNext;
    1.25 +    }
    1.26 +    return device;
    1.27 +}
    1.28  
    1.29  static void
    1.30  FreeElementList(recElement *pElement)
    1.31 @@ -143,7 +153,22 @@
    1.32  #if SDL_HAPTIC_IOKIT
    1.33      MacHaptic_MaybeRemoveDevice(device->ffservice);
    1.34  #endif
    1.35 -    s_bDeviceRemoved = SDL_TRUE;
    1.36 +
    1.37 +/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceRemoved()? */
    1.38 +#if !SDL_EVENTS_DISABLED
    1.39 +    {
    1.40 +        SDL_Event event;
    1.41 +        event.type = SDL_JOYDEVICEREMOVED;
    1.42 +
    1.43 +        if (SDL_GetEventState(event.type) == SDL_ENABLE) {
    1.44 +            event.jdevice.which = device->instance_id;
    1.45 +            if ((SDL_EventOK == NULL)
    1.46 +                || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
    1.47 +                SDL_PushEvent(&event);
    1.48 +            }
    1.49 +        }
    1.50 +    }
    1.51 +#endif /* !SDL_EVENTS_DISABLED */
    1.52  }
    1.53  
    1.54  
    1.55 @@ -381,6 +406,7 @@
    1.56  JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject)
    1.57  {
    1.58      recDevice *device;
    1.59 +    int device_index = 0;
    1.60  
    1.61      if (res != kIOReturnSuccess) {
    1.62          return;
    1.63 @@ -420,9 +446,6 @@
    1.64  #endif
    1.65      }
    1.66  
    1.67 -    device->send_open_event = 1;
    1.68 -    s_bDeviceAdded = SDL_TRUE;
    1.69 -
    1.70      /* Add device to the end of the list */
    1.71      if ( !gpDeviceList ) {
    1.72          gpDeviceList = device;
    1.73 @@ -431,10 +454,27 @@
    1.74  
    1.75          curdevice = gpDeviceList;
    1.76          while ( curdevice->pNext ) {
    1.77 +            ++device_index;
    1.78              curdevice = curdevice->pNext;
    1.79          }
    1.80          curdevice->pNext = device;
    1.81      }
    1.82 +
    1.83 +/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceAdded()? */
    1.84 +#if !SDL_EVENTS_DISABLED
    1.85 +    {
    1.86 +        SDL_Event event;
    1.87 +        event.type = SDL_JOYDEVICEADDED;
    1.88 +
    1.89 +        if (SDL_GetEventState(event.type) == SDL_ENABLE) {
    1.90 +            event.jdevice.which = device_index;
    1.91 +            if ((SDL_EventOK == NULL)
    1.92 +                || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
    1.93 +                SDL_PushEvent(&event);
    1.94 +            }
    1.95 +        }
    1.96 +    }
    1.97 +#endif /* !SDL_EVENTS_DISABLED */
    1.98  }
    1.99  
   1.100  static SDL_bool
   1.101 @@ -560,53 +600,12 @@
   1.102  void
   1.103  SDL_SYS_JoystickDetect()
   1.104  {
   1.105 -    if (s_bDeviceAdded || s_bDeviceRemoved) {
   1.106 -        recDevice *device = gpDeviceList;
   1.107 -        s_bDeviceAdded = SDL_FALSE;
   1.108 -        s_bDeviceRemoved = SDL_FALSE;
   1.109 -        int device_index = 0;
   1.110 -        /* send notifications */
   1.111 -        while (device) {
   1.112 -            if (device->send_open_event) {
   1.113 -                device->send_open_event = 0;
   1.114 -/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceAdded()? */
   1.115 -#if !SDL_EVENTS_DISABLED
   1.116 -                SDL_Event event;
   1.117 -                event.type = SDL_JOYDEVICEADDED;
   1.118 -
   1.119 -                if (SDL_GetEventState(event.type) == SDL_ENABLE) {
   1.120 -                    event.jdevice.which = device_index;
   1.121 -                    if ((SDL_EventOK == NULL)
   1.122 -                        || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
   1.123 -                        SDL_PushEvent(&event);
   1.124 -                    }
   1.125 -                }
   1.126 -#endif /* !SDL_EVENTS_DISABLED */
   1.127 -
   1.128 -            }
   1.129 -
   1.130 -            if (device->removed) {
   1.131 -                const int instance_id = device->instance_id;
   1.132 -                device = FreeDevice(device);
   1.133 -
   1.134 -/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceRemoved()? */
   1.135 -#if !SDL_EVENTS_DISABLED
   1.136 -                SDL_Event event;
   1.137 -                event.type = SDL_JOYDEVICEREMOVED;
   1.138 -
   1.139 -                if (SDL_GetEventState(event.type) == SDL_ENABLE) {
   1.140 -                    event.jdevice.which = instance_id;
   1.141 -                    if ((SDL_EventOK == NULL)
   1.142 -                        || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
   1.143 -                        SDL_PushEvent(&event);
   1.144 -                    }
   1.145 -                }
   1.146 -#endif /* !SDL_EVENTS_DISABLED */
   1.147 -
   1.148 -            } else {
   1.149 -                device = device->pNext;
   1.150 -                device_index++;
   1.151 -            }
   1.152 +    recDevice *device = gpDeviceList;
   1.153 +    while (device) {
   1.154 +        if (device->removed) {
   1.155 +            device = FreeDevice(device);
   1.156 +        } else {
   1.157 +            device = device->pNext;
   1.158          }
   1.159      }
   1.160  
   1.161 @@ -621,13 +620,8 @@
   1.162  const char *
   1.163  SDL_SYS_JoystickNameForDeviceIndex(int device_index)
   1.164  {
   1.165 -    recDevice *device = gpDeviceList;
   1.166 -
   1.167 -    while (device_index-- > 0) {
   1.168 -        device = device->pNext;
   1.169 -    }
   1.170 -
   1.171 -    return device->product;
   1.172 +    recDevice *device = GetDeviceForIndex(device_index);
   1.173 +    return device ? device->product : "UNKNOWN";
   1.174  }
   1.175  
   1.176  /* Function to return the instance id of the joystick at device_index
   1.177 @@ -635,14 +629,8 @@
   1.178  SDL_JoystickID
   1.179  SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
   1.180  {
   1.181 -    recDevice *device = gpDeviceList;
   1.182 -    int index;
   1.183 -
   1.184 -    for (index = device_index; index > 0; index--) {
   1.185 -        device = device->pNext;
   1.186 -    }
   1.187 -
   1.188 -    return device->instance_id;
   1.189 +    recDevice *device = GetDeviceForIndex(device_index);
   1.190 +    return device ? device->instance_id : 0;
   1.191  }
   1.192  
   1.193  /* Function to open a joystick for use.
   1.194 @@ -653,12 +641,7 @@
   1.195  int
   1.196  SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
   1.197  {
   1.198 -    recDevice *device = gpDeviceList;
   1.199 -    int index;
   1.200 -
   1.201 -    for (index = device_index; index > 0; index--) {
   1.202 -        device = device->pNext;
   1.203 -    }
   1.204 +    recDevice *device = GetDeviceForIndex(device_index);
   1.205  
   1.206      joystick->instance_id = device->instance_id;
   1.207      joystick->hwdata = device;
   1.208 @@ -805,21 +788,19 @@
   1.209          CFRelease(hidman);
   1.210          hidman = NULL;
   1.211      }
   1.212 -
   1.213 -    s_bDeviceAdded = s_bDeviceRemoved = SDL_FALSE;
   1.214  }
   1.215  
   1.216  
   1.217  SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
   1.218  {
   1.219 -    recDevice *device = gpDeviceList;
   1.220 -    int index;
   1.221 -
   1.222 -    for (index = device_index; index > 0; index--) {
   1.223 -        device = device->pNext;
   1.224 +    recDevice *device = GetDeviceForIndex(device_index);
   1.225 +    SDL_JoystickGUID guid;
   1.226 +    if (device) {
   1.227 +        guid = device->guid;
   1.228 +    } else {
   1.229 +        SDL_zero(guid);
   1.230      }
   1.231 -
   1.232 -    return device->guid;
   1.233 +    return guid;
   1.234  }
   1.235  
   1.236  SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)
     2.1 --- a/src/joystick/darwin/SDL_sysjoystick_c.h	Tue May 26 11:38:04 2015 -0400
     2.2 +++ b/src/joystick/darwin/SDL_sysjoystick_c.h	Tue May 26 08:52:02 2015 -0700
     2.3 @@ -62,7 +62,6 @@
     2.4  
     2.5      int instance_id;
     2.6      SDL_JoystickGUID guid;
     2.7 -    Uint8 send_open_event;      /* 1 if we need to send an Added event for this device */
     2.8  
     2.9      struct joystick_hwdata *pNext;      /* next device */
    2.10  };