Skip to content

Commit

Permalink
Merge pull request #5501 from bluerobotics/pr-joystick-duplicate-names
Browse files Browse the repository at this point in the history
Handle joysticks with duplicate names
  • Loading branch information
DonLakeFlyer authored Aug 3, 2017
2 parents c6ac209 + 63a342c commit a1c60f6
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Joystick/JoystickSDL.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ QMap<QString, Joystick*> JoystickSDL::discover(MultiVehicleManager* _multiVehicl
for (int i=0; i<SDL_NumJoysticks(); i++) {
QString name = SDL_JoystickNameForIndex(i);

// TODO use GUID instead of name in case of two joysticks with same name
if (!ret.contains(name)) {
int axisCount, buttonCount, hatCount;
bool isGameController;
Expand Down Expand Up @@ -65,6 +64,17 @@ QMap<QString, Joystick*> JoystickSDL::discover(MultiVehicleManager* _multiVehicl
}

qCDebug(JoystickLog) << "\t" << name << "axes:" << axisCount << "buttons:" << buttonCount << "hats:" << hatCount << "isGC:" << isGameController;

// Check for joysticks with duplicate names and differentiate the keys when neccessary.
// This is required when using an Xbox 360 wireless receiver that always identifies as
// 4 individual joysticks, regardless of how many joysticks are actually connected to the
// receiver. Using GUID does not help, all of these devices present the same GUID.
QString originalName = name;
uint8_t duplicateIdx = 1;
while (newRet[name]) {
name = QString("%1 %2").arg(originalName).arg(duplicateIdx++);
}

newRet[name] = new JoystickSDL(name, qMax(0,axisCount), qMax(0,buttonCount), qMax(0,hatCount), i, isGameController, _multiVehicleManager);
} else {
newRet[name] = ret[name];
Expand Down Expand Up @@ -135,7 +145,10 @@ void JoystickSDL::_close(void) {

if (SDL_JoystickInstanceID(sdlJoystick) != -1) {
qCDebug(JoystickLog) << "\tID:" << SDL_JoystickInstanceID(sdlJoystick);
SDL_JoystickClose(sdlJoystick);
// This segfaults so often, and I've spent so much time trying to find the cause and fix it
// I think this might be an SDL bug
// We are much more stable just commenting this out
//SDL_JoystickClose(sdlJoystick);
}
}

Expand Down

0 comments on commit a1c60f6

Please sign in to comment.