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.
This commit is contained in:
Sam Lantinga 2015-05-26 08:52:02 -07:00
parent 80916e01f3
commit 523064592a
2 changed files with 66 additions and 86 deletions

View File

@ -46,13 +46,23 @@ static IOHIDManagerRef hidman = NULL;
/* Linked list of all available devices */ /* Linked list of all available devices */
static recDevice *gpDeviceList = NULL; static recDevice *gpDeviceList = NULL;
/* if SDL_TRUE then a device was added since the last update call */
static SDL_bool s_bDeviceAdded = SDL_FALSE;
static SDL_bool s_bDeviceRemoved = SDL_FALSE;
/* static incrementing counter for new joystick devices seen on the system. Devices should start with index 0 */ /* static incrementing counter for new joystick devices seen on the system. Devices should start with index 0 */
static int s_joystick_instance_id = -1; static int s_joystick_instance_id = -1;
static recDevice *GetDeviceForIndex(int device_index)
{
recDevice *device = gpDeviceList;
while (device) {
if (!device->removed) {
if (device_index == 0)
break;
--device_index;
}
device = device->pNext;
}
return device;
}
static void static void
FreeElementList(recElement *pElement) FreeElementList(recElement *pElement)
@ -143,7 +153,22 @@ JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender)
#if SDL_HAPTIC_IOKIT #if SDL_HAPTIC_IOKIT
MacHaptic_MaybeRemoveDevice(device->ffservice); MacHaptic_MaybeRemoveDevice(device->ffservice);
#endif #endif
s_bDeviceRemoved = SDL_TRUE;
/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceRemoved()? */
#if !SDL_EVENTS_DISABLED
{
SDL_Event event;
event.type = SDL_JOYDEVICEREMOVED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device->instance_id;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
}
#endif /* !SDL_EVENTS_DISABLED */
} }
@ -381,6 +406,7 @@ static void
JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject) JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject)
{ {
recDevice *device; recDevice *device;
int device_index = 0;
if (res != kIOReturnSuccess) { if (res != kIOReturnSuccess) {
return; return;
@ -420,9 +446,6 @@ JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDevic
#endif #endif
} }
device->send_open_event = 1;
s_bDeviceAdded = SDL_TRUE;
/* Add device to the end of the list */ /* Add device to the end of the list */
if ( !gpDeviceList ) { if ( !gpDeviceList ) {
gpDeviceList = device; gpDeviceList = device;
@ -431,10 +454,27 @@ JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDevic
curdevice = gpDeviceList; curdevice = gpDeviceList;
while ( curdevice->pNext ) { while ( curdevice->pNext ) {
++device_index;
curdevice = curdevice->pNext; curdevice = curdevice->pNext;
} }
curdevice->pNext = device; curdevice->pNext = device;
} }
/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceAdded()? */
#if !SDL_EVENTS_DISABLED
{
SDL_Event event;
event.type = SDL_JOYDEVICEADDED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_index;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
}
#endif /* !SDL_EVENTS_DISABLED */
} }
static SDL_bool static SDL_bool
@ -560,53 +600,12 @@ SDL_SYS_NumJoysticks()
void void
SDL_SYS_JoystickDetect() SDL_SYS_JoystickDetect()
{ {
if (s_bDeviceAdded || s_bDeviceRemoved) { recDevice *device = gpDeviceList;
recDevice *device = gpDeviceList; while (device) {
s_bDeviceAdded = SDL_FALSE; if (device->removed) {
s_bDeviceRemoved = SDL_FALSE; device = FreeDevice(device);
int device_index = 0; } else {
/* send notifications */ device = device->pNext;
while (device) {
if (device->send_open_event) {
device->send_open_event = 0;
/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceAdded()? */
#if !SDL_EVENTS_DISABLED
SDL_Event event;
event.type = SDL_JOYDEVICEADDED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_index;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
}
if (device->removed) {
const int instance_id = device->instance_id;
device = FreeDevice(device);
/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceRemoved()? */
#if !SDL_EVENTS_DISABLED
SDL_Event event;
event.type = SDL_JOYDEVICEREMOVED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = instance_id;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
} else {
device = device->pNext;
device_index++;
}
} }
} }
@ -621,13 +620,8 @@ SDL_SYS_JoystickDetect()
const char * const char *
SDL_SYS_JoystickNameForDeviceIndex(int device_index) SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{ {
recDevice *device = gpDeviceList; recDevice *device = GetDeviceForIndex(device_index);
return device ? device->product : "UNKNOWN";
while (device_index-- > 0) {
device = device->pNext;
}
return device->product;
} }
/* Function to return the instance id of the joystick at device_index /* Function to return the instance id of the joystick at device_index
@ -635,14 +629,8 @@ SDL_SYS_JoystickNameForDeviceIndex(int device_index)
SDL_JoystickID SDL_JoystickID
SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index) SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
{ {
recDevice *device = gpDeviceList; recDevice *device = GetDeviceForIndex(device_index);
int index; return device ? device->instance_id : 0;
for (index = device_index; index > 0; index--) {
device = device->pNext;
}
return device->instance_id;
} }
/* Function to open a joystick for use. /* Function to open a joystick for use.
@ -653,12 +641,7 @@ SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
int int
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
{ {
recDevice *device = gpDeviceList; recDevice *device = GetDeviceForIndex(device_index);
int index;
for (index = device_index; index > 0; index--) {
device = device->pNext;
}
joystick->instance_id = device->instance_id; joystick->instance_id = device->instance_id;
joystick->hwdata = device; joystick->hwdata = device;
@ -805,21 +788,19 @@ SDL_SYS_JoystickQuit(void)
CFRelease(hidman); CFRelease(hidman);
hidman = NULL; hidman = NULL;
} }
s_bDeviceAdded = s_bDeviceRemoved = SDL_FALSE;
} }
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index ) SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
{ {
recDevice *device = gpDeviceList; recDevice *device = GetDeviceForIndex(device_index);
int index; SDL_JoystickGUID guid;
if (device) {
for (index = device_index; index > 0; index--) { guid = device->guid;
device = device->pNext; } else {
SDL_zero(guid);
} }
return guid;
return device->guid;
} }
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick) SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)

View File

@ -62,7 +62,6 @@ struct joystick_hwdata
int instance_id; int instance_id;
SDL_JoystickGUID guid; SDL_JoystickGUID guid;
Uint8 send_open_event; /* 1 if we need to send an Added event for this device */
struct joystick_hwdata *pNext; /* next device */ struct joystick_hwdata *pNext; /* next device */
}; };