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 */
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 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
FreeElementList(recElement *pElement)
@ -143,7 +153,22 @@ JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender)
#if SDL_HAPTIC_IOKIT
MacHaptic_MaybeRemoveDevice(device->ffservice);
#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)
{
recDevice *device;
int device_index = 0;
if (res != kIOReturnSuccess) {
return;
@ -420,9 +446,6 @@ JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDevic
#endif
}
device->send_open_event = 1;
s_bDeviceAdded = SDL_TRUE;
/* Add device to the end of the list */
if ( !gpDeviceList ) {
gpDeviceList = device;
@ -431,10 +454,27 @@ JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDevic
curdevice = gpDeviceList;
while ( curdevice->pNext ) {
++device_index;
curdevice = curdevice->pNext;
}
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
@ -560,53 +600,12 @@ SDL_SYS_NumJoysticks()
void
SDL_SYS_JoystickDetect()
{
if (s_bDeviceAdded || s_bDeviceRemoved) {
recDevice *device = gpDeviceList;
s_bDeviceAdded = SDL_FALSE;
s_bDeviceRemoved = SDL_FALSE;
int device_index = 0;
/* send notifications */
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 *
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{
recDevice *device = gpDeviceList;
while (device_index-- > 0) {
device = device->pNext;
}
return device->product;
recDevice *device = GetDeviceForIndex(device_index);
return device ? device->product : "UNKNOWN";
}
/* 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_SYS_GetInstanceIdOfDeviceIndex(int device_index)
{
recDevice *device = gpDeviceList;
int index;
for (index = device_index; index > 0; index--) {
device = device->pNext;
}
return device->instance_id;
recDevice *device = GetDeviceForIndex(device_index);
return device ? device->instance_id : 0;
}
/* Function to open a joystick for use.
@ -653,12 +641,7 @@ SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
int
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
{
recDevice *device = gpDeviceList;
int index;
for (index = device_index; index > 0; index--) {
device = device->pNext;
}
recDevice *device = GetDeviceForIndex(device_index);
joystick->instance_id = device->instance_id;
joystick->hwdata = device;
@ -805,21 +788,19 @@ SDL_SYS_JoystickQuit(void)
CFRelease(hidman);
hidman = NULL;
}
s_bDeviceAdded = s_bDeviceRemoved = SDL_FALSE;
}
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
{
recDevice *device = gpDeviceList;
int index;
for (index = device_index; index > 0; index--) {
device = device->pNext;
recDevice *device = GetDeviceForIndex(device_index);
SDL_JoystickGUID guid;
if (device) {
guid = device->guid;
} else {
SDL_zero(guid);
}
return device->guid;
return guid;
}
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)

View File

@ -62,7 +62,6 @@ struct joystick_hwdata
int instance_id;
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 */
};