In my current project I allow the player to configure multiple controllers if they have them hooked up. I just tried to play my game with two joysticks on my Mac and noticed that the game froze during initialization.
I tracked the freeze down to a function in hidjoy.m, init_joystick(). In that function there is a do/while loop that never exits. After tinkering with the code I think there is a problem in this function:
The highlighted line is the root of the infinite do/while loop. I think because both of my joysticks are the same model, they both have the same ID. The find_joystick() function scans the vector of existing joysticks to see if there is an ID match before adding a new joystick to the vector. Since the IDs are the same, the second joystick is never added to the vector. That's why this code never exits, since the vector never expands to match the number of joysticks stored in the count variable:
do { al_rest(0.001); CFSetRef devices = IOHIDManagerCopyDevices(hidManagerRef); if (devices == nil) { break; } count = CFSetGetCount(devices); CFRelease(devices); al_lock_mutex(add_mutex); size = _al_vector_size(&joysticks); al_unlock_mutex(add_mutex); } while (size < count);
After a cursory look at the Apple HID CLass Device Interface Guide my guess is the IOHIDDeviceRef needs to be used to distinguish between joysticks since the string ID can end up being identical for different devices.
If IOHIDDeviceRef is unique it should work. Can you try it? I can try it out tomorrow too assuming my 360 controllers report the same product id (so I can see it fail before testing something that works.)
I probably won't be able to do it until Saturday. If you don't get to it before then I'll try it.
Do you have any short program that makes good use of dual joysticks? At least maybe you can confirm with your game if this patch works. What I've tested is:
1) Before patch, ex_joystick_events hangs at black screen on startup
2) After patch, ex_joystick_events loads
3) Both joysticks manipulate the knobs and doohickeys in ex_joystick_events
The patch to hidjoy.m is attached.
I applied your patch and the problem seems to be solved! ex_joystick_events works as you described. Both joysticks can now be used in my game. For good measure I hooked up a third (different) joystick and tried my game with all three and they all three were able to be used.
Just to double check, you're getting 3 different sets of input right? Like controlling 3 characters separately? Most likely the case but just want to make sure.
Yes. Everything is working perfectly now.
Alright. Thanks for reporting. BTW I did use your suggestion of using the IOHIDDeviceRef as the id.
Yeah, I saw it in the patch. Glad my idea worked Thanks for fixing this.
It's in SVN now.
This makes me feel pretty inside!