We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2704

Summary: Playstation 4 Controllers listed twice at the startup when a Playstation 3 controller is also connected
Product: SDL Reporter: Rodrigo Cardoso <rodrigo.alfenas>
Component: joystickAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: major    
Priority: P2 CC: amaranth72, computers57, icculus, rodrigo.alfenas, tomyun
Version: 2.0.3   
Hardware: x86   
OS: Mac OS X (All)   

Description Rodrigo Cardoso 2014-08-24 21:01:24 UTC
First of all I am using the latest SDL snapshot ( as of this writing, https://hg.libsdl.org/SDL/rev/da2bbecb7ad0 )

Description:

When I start my application with a PS4 controller AND a PS3 controller connected via bluetooth on Mac OS X (tested on 10.9 and 10.10 developer preview), the PS4 controller gets listed twice.

This does happen in these cases:
- opening all joysticks when starting the app
- restarting the SDL joystick subsystem and doing the same thing above.
- Letting the hotplug code handle the joystick openning, without opening all the joysticks from its index separatelly.


This does *NOT* happen in these cases:
- hotpluging in any order
- when I have another kind of controller connected togheter instead of a Dual Shock 3. (tested with a xbox 360 controller)



Not sure if this is related:
https://hg.libsdl.org/SDL/rev/f6a2b1a6932b

This guy appears to have the same problem i am reporting here:
http://steamcommunity.com/app/251470/discussions/0/540743212865042085/

This bug seems similar, but with other devices:
https://bugzilla.libsdl.org/show_bug.cgi?id=2511
Comment 1 Rodrigo Cardoso 2014-08-24 21:19:39 UTC
Some snaps of the code I used to test:

void loop() {
	SDL_Event e;
	while (SDL_PollEvent(&e)) {
		switch (e.type) {
			case SDL_JOYBUTTONDOWN:
			case SDL_JOYBUTTONUP:
			case SDL_JOYHATMOTION:
			case SDL_JOYAXISMOTION:	
			{
				printf("[%d] Joystick (%d) did something\n", e.common.timestamp, e.jbutton.which);
				break;
			}
			case SDL_JOYDEVICEADDED:
			{
				int success = openJoystick(e.jdevice.which);
				if(success)
					   printf("[%d] Joystick (%d) openned\n", e.common.timestamp, e.jdevice.which);
				break;
			}
			case SDL_JOYDEVICEREMOVED:
			{
				int success = closeJoystickWithInstanceId(e.jdevice.which);
				if(success)
					printf("[%d] Joystick (%d) closed\n", e.common.timestamp, e.jdevice.which);
				break;
			}
			default:
				break;
		}
	}
	SDL_Delay(delayTime);
}



int openJoystick(int which) {
	SDL_Joystick * j = SDL_JoystickOpen(device_index);
	if (!j) return 0;
	/* stored key SDL_JoystickInstanceID value j pair here */
	addJoystick(which, j);
	return 1;
}

int closeJoystickWithInstanceId(int instanceId) {
	SDL_Joystick *j = getJoystickWithId(instanceId);

	if (j != NULL) {
		SDL_JoystickClose(j);
		removeJoystickFromList(instanceId);
		return 1;
	}

	return 0;
}

void openAllJoysticks() {
	SDL_Event e;
	while (SDL_PollEvent(&e)) {}; // discard events
	SDL_JoystickUpdate();

	int numJoysticks = SDL_NumJoysticks();
	for (int i = 0; i < numJoysticks; ++i) {
		if (!openJoystick(i)) return NO;
	}
}
Comment 2 Rodrigo Cardoso 2014-08-26 05:53:09 UTC
Seems like ANY other controllers connected gets duplicated entries at the start up when a PS3 controller is also connected......

This apparently got cleaned when you disconnect and reconnect each controller.
Comment 3 Alex Szpakowski 2014-08-26 21:01:04 UTC
It seems that way to me as well: if I have an Xbox controller and a PS3 controller connected at startup, the Xbox controller is listed twice. If I have just one connected at startup and then connect the other during runtime everything is OK, and if I disconnect and reconnect one during runtime (if both were connected at startup) then it will also list the proper number of controllers.
Comment 4 Ryan C. Gordon 2014-08-31 15:25:44 UTC
This should be fixed in https://hg.libsdl.org/SDL/rev/b186c0df3c18 ... it looks like IOKit sends us one (random?) joystick when you first set up the "call this function when a device is added" callback and then it sends all the known devices when you start the event runloop (which is what we expected).

Not sure if this is a bug in IOKit, or something PS3 specific, or my misunderstanding the documentation, or what, but just making sure we don't already have the device in our list fixes the issue and makes the code more robust anyhow.

Please test and report back.

Thanks!

--ryan.
Comment 5 Rodrigo Cardoso 2014-09-04 21:50:56 UTC
It IS fixed. Thank you!
Comment 6 Rodrigo Cardoso 2014-09-23 08:32:35 UTC
*** Bug 2511 has been marked as a duplicate of this bug. ***
Comment 7 Kyungdahm Yun 2014-12-17 06:12:09 UTC
I just came across this ticket after reporting #2822, presumably the same issue with my own patch.

It seems HEAD branch already got fixed with a patch that rejects any duplicate joystick being reported. Meanwhile, my patch is supposed to eliminate redundant reporting from IOKit before running into the event loop.

I'm not sure if this would really fix all edge cases, but it's just one liner, so please take a look. In my opinion, having two patches altogether should not hurt anyways.