@@ -300,6 +300,30 @@ void ShipDeviceIndexMappingManager::HandlePhysicalDeviceConnect(int32_t sdlDevic
300
300
}
301
301
302
302
if (Context::GetInstance ()->GetControlDeck ()->IsSinglePlayerMappingMode ()) {
303
+ std::set<Ship::ShipDeviceIndex> alreadyConnectedDevices;
304
+ for (auto mapping : Context::GetInstance ()->GetControlDeck ()->GetControllerByPort (0 )->GetAllMappings ()) {
305
+ auto sdlMapping = std::dynamic_pointer_cast<SDLMapping>(mapping);
306
+ if (sdlMapping == nullptr ) {
307
+ continue ;
308
+ }
309
+
310
+ if (sdlMapping->ControllerLoaded ()) {
311
+ alreadyConnectedDevices.insert (sdlMapping->GetShipDeviceIndex ());
312
+ }
313
+ }
314
+
315
+ for (auto [lusIndex, mapping] : mShipDeviceIndexToPhysicalDeviceIndexMappings ) {
316
+ auto sdlMapping = dynamic_pointer_cast<ShipDeviceIndexToSDLDeviceIndexMapping>(mapping);
317
+ if (sdlMapping == nullptr ) {
318
+ continue ;
319
+ }
320
+
321
+ if (alreadyConnectedDevices.contains (lusIndex)) {
322
+ sdlMapping->SetSDLDeviceIndex (GetNewSDLDeviceIndexFromShipDeviceIndex (lusIndex));
323
+ sdlMapping->SaveToConfig ();
324
+ }
325
+ }
326
+
303
327
InitializeSDLMappingsForPort (0 , sdlDeviceIndex);
304
328
return ;
305
329
} else {
@@ -506,6 +530,8 @@ uint8_t ShipDeviceIndexMappingManager::GetPortIndexOfDisconnectedPhysicalDevice(
506
530
507
531
ShipDeviceIndex
508
532
ShipDeviceIndexMappingManager::GetShipDeviceIndexOfDisconnectedPhysicalDevice (int32_t sdlJoystickInstanceId) {
533
+ auto shipDeviceIndex = ShipDeviceIndex::Max;
534
+
509
535
for (uint8_t portIndex = 0 ; portIndex < 4 ; portIndex++) {
510
536
auto controller = Context::GetInstance ()->GetControlDeck ()->GetControllerByPort (portIndex);
511
537
@@ -516,7 +542,8 @@ ShipDeviceIndexMappingManager::GetShipDeviceIndexOfDisconnectedPhysicalDevice(in
516
542
continue ;
517
543
}
518
544
if (sdlButtonMapping->GetJoystickInstanceId () == sdlJoystickInstanceId) {
519
- return sdlButtonMapping->GetShipDeviceIndex ();
545
+ shipDeviceIndex = sdlButtonMapping->GetShipDeviceIndex ();
546
+ sdlButtonMapping->CloseController ();
520
547
}
521
548
}
522
549
}
@@ -529,15 +556,17 @@ ShipDeviceIndexMappingManager::GetShipDeviceIndexOfDisconnectedPhysicalDevice(in
529
556
continue ;
530
557
}
531
558
if (sdlAxisDirectionMapping->GetJoystickInstanceId () == sdlJoystickInstanceId) {
532
- return sdlAxisDirectionMapping->GetShipDeviceIndex ();
559
+ shipDeviceIndex = sdlAxisDirectionMapping->GetShipDeviceIndex ();
560
+ sdlAxisDirectionMapping->CloseController ();
533
561
}
534
562
}
535
563
}
536
564
}
537
565
538
566
auto sdlGyroMapping = std::dynamic_pointer_cast<SDLMapping>(controller->GetGyro ()->GetGyroMapping ());
539
567
if (sdlGyroMapping != nullptr && sdlGyroMapping->GetJoystickInstanceId () == sdlJoystickInstanceId) {
540
- return sdlGyroMapping->GetShipDeviceIndex ();
568
+ shipDeviceIndex = sdlGyroMapping->GetShipDeviceIndex ();
569
+ sdlGyroMapping->CloseController ();
541
570
}
542
571
543
572
for (auto [id, rumbleMapping] : controller->GetRumble ()->GetAllRumbleMappings ()) {
@@ -546,7 +575,8 @@ ShipDeviceIndexMappingManager::GetShipDeviceIndexOfDisconnectedPhysicalDevice(in
546
575
continue ;
547
576
}
548
577
if (sdlRumbleMapping->GetJoystickInstanceId () == sdlJoystickInstanceId) {
549
- return sdlRumbleMapping->GetShipDeviceIndex ();
578
+ shipDeviceIndex = sdlRumbleMapping->GetShipDeviceIndex ();
579
+ sdlRumbleMapping->CloseController ();
550
580
}
551
581
}
552
582
@@ -556,13 +586,13 @@ ShipDeviceIndexMappingManager::GetShipDeviceIndexOfDisconnectedPhysicalDevice(in
556
586
continue ;
557
587
}
558
588
if (sdlLEDMapping->GetJoystickInstanceId () == sdlJoystickInstanceId) {
559
- return sdlLEDMapping->GetShipDeviceIndex ();
589
+ shipDeviceIndex = sdlLEDMapping->GetShipDeviceIndex ();
590
+ sdlLEDMapping->CloseController ();
560
591
}
561
592
}
562
593
}
563
594
564
- // couldn't find one
565
- return ShipDeviceIndex::Max;
595
+ return shipDeviceIndex;
566
596
}
567
597
568
598
ShipDeviceIndex ShipDeviceIndexMappingManager::GetLowestShipDeviceIndexWithNoAssociatedButtonOrAxisDirectionMappings () {
0 commit comments