Skip to content

Commit d06ff2a

Browse files
committed
fix a couple issues with handling controller connect/disconnect events
1 parent 0da318c commit d06ff2a

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

src/controller/controldevice/controller/mapping/sdl/SDLMapping.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@ bool SDLMapping::CloseController() {
4545
return true;
4646
}
4747

48-
bool SDLMapping::ControllerLoaded() {
48+
bool SDLMapping::ControllerLoaded(bool closeIfDisconnected) {
4949
SDL_GameControllerUpdate();
5050

51-
// If the controller is disconnected, close it.
51+
// If the controller is disconnected
5252
if (mController != nullptr && !SDL_GameControllerGetAttached(mController)) {
53+
if (!closeIfDisconnected) {
54+
return false;
55+
}
56+
5357
CloseController();
5458
}
5559

src/controller/controldevice/controller/mapping/sdl/SDLMapping.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SDLMapping : public ControllerMapping {
1717
int32_t GetJoystickInstanceId();
1818
int32_t GetCurrentSDLDeviceIndex();
1919
bool CloseController();
20-
bool ControllerLoaded();
20+
bool ControllerLoaded(bool closeIfDisconnected = false);
2121

2222
protected:
2323
SDL_GameControllerType GetSDLControllerType();

src/controller/deviceindex/ShipDeviceIndexMappingManager.cpp

+37-7
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,30 @@ void ShipDeviceIndexMappingManager::HandlePhysicalDeviceConnect(int32_t sdlDevic
300300
}
301301

302302
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+
303327
InitializeSDLMappingsForPort(0, sdlDeviceIndex);
304328
return;
305329
} else {
@@ -506,6 +530,8 @@ uint8_t ShipDeviceIndexMappingManager::GetPortIndexOfDisconnectedPhysicalDevice(
506530

507531
ShipDeviceIndex
508532
ShipDeviceIndexMappingManager::GetShipDeviceIndexOfDisconnectedPhysicalDevice(int32_t sdlJoystickInstanceId) {
533+
auto shipDeviceIndex = ShipDeviceIndex::Max;
534+
509535
for (uint8_t portIndex = 0; portIndex < 4; portIndex++) {
510536
auto controller = Context::GetInstance()->GetControlDeck()->GetControllerByPort(portIndex);
511537

@@ -516,7 +542,8 @@ ShipDeviceIndexMappingManager::GetShipDeviceIndexOfDisconnectedPhysicalDevice(in
516542
continue;
517543
}
518544
if (sdlButtonMapping->GetJoystickInstanceId() == sdlJoystickInstanceId) {
519-
return sdlButtonMapping->GetShipDeviceIndex();
545+
shipDeviceIndex = sdlButtonMapping->GetShipDeviceIndex();
546+
sdlButtonMapping->CloseController();
520547
}
521548
}
522549
}
@@ -529,15 +556,17 @@ ShipDeviceIndexMappingManager::GetShipDeviceIndexOfDisconnectedPhysicalDevice(in
529556
continue;
530557
}
531558
if (sdlAxisDirectionMapping->GetJoystickInstanceId() == sdlJoystickInstanceId) {
532-
return sdlAxisDirectionMapping->GetShipDeviceIndex();
559+
shipDeviceIndex = sdlAxisDirectionMapping->GetShipDeviceIndex();
560+
sdlAxisDirectionMapping->CloseController();
533561
}
534562
}
535563
}
536564
}
537565

538566
auto sdlGyroMapping = std::dynamic_pointer_cast<SDLMapping>(controller->GetGyro()->GetGyroMapping());
539567
if (sdlGyroMapping != nullptr && sdlGyroMapping->GetJoystickInstanceId() == sdlJoystickInstanceId) {
540-
return sdlGyroMapping->GetShipDeviceIndex();
568+
shipDeviceIndex = sdlGyroMapping->GetShipDeviceIndex();
569+
sdlGyroMapping->CloseController();
541570
}
542571

543572
for (auto [id, rumbleMapping] : controller->GetRumble()->GetAllRumbleMappings()) {
@@ -546,7 +575,8 @@ ShipDeviceIndexMappingManager::GetShipDeviceIndexOfDisconnectedPhysicalDevice(in
546575
continue;
547576
}
548577
if (sdlRumbleMapping->GetJoystickInstanceId() == sdlJoystickInstanceId) {
549-
return sdlRumbleMapping->GetShipDeviceIndex();
578+
shipDeviceIndex = sdlRumbleMapping->GetShipDeviceIndex();
579+
sdlRumbleMapping->CloseController();
550580
}
551581
}
552582

@@ -556,13 +586,13 @@ ShipDeviceIndexMappingManager::GetShipDeviceIndexOfDisconnectedPhysicalDevice(in
556586
continue;
557587
}
558588
if (sdlLEDMapping->GetJoystickInstanceId() == sdlJoystickInstanceId) {
559-
return sdlLEDMapping->GetShipDeviceIndex();
589+
shipDeviceIndex = sdlLEDMapping->GetShipDeviceIndex();
590+
sdlLEDMapping->CloseController();
560591
}
561592
}
562593
}
563594

564-
// couldn't find one
565-
return ShipDeviceIndex::Max;
595+
return shipDeviceIndex;
566596
}
567597

568598
ShipDeviceIndex ShipDeviceIndexMappingManager::GetLowestShipDeviceIndexWithNoAssociatedButtonOrAxisDirectionMappings() {

0 commit comments

Comments
 (0)