forked from dolphin-emu/dolphin
		
	This lets Dolphin know if a configured GameCube Controller should actually be treated as connected or not. Talked to @JMC47 a bit about this last night. My use-case is that all of my controllers are the same hardware (Xbox One controllers) so share the same configuration (modulo device number). Treating them all as always connected isn't a problem for most games, but in some (Smash Bros.) it forces me to go find a keyboard/mouse and unconfigure any controllers that I don't actually have connected. Hotplugging devices (works on macOS, at least) + this patch remove my need to ever touch the Controller Config dialog while in a game. This patch makes the following changes: - A new `BooleanSetting` in `GCPadEmu` called "Always Connected", which defaults to false. - `ControllerEmu` tracks whether the default device is connected on every call to `UpdateReferences()`. - `GCPadEmu.GetStatus()` now sets err bit to `PAD_ERR_NO_CONTROLLER` if the default device isn't connected. - `SIDevice_GCController` handles `PAD_ERR_NO_CONTROLLER` by imitating the behaviour of `SIDevice_Null` (as far as I can tell, this is the only use of the error bit from `GCPadStatus`). I wanted to add an OSD message akin to the ones when Wiimotes get connected/disconnected, but I haven't yet found where to put the logic.
		
			
				
	
	
		
			123 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			123 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| // Copyright 2010 Dolphin Emulator Project
 | |
| // Licensed under GPLv2+
 | |
| // Refer to the license.txt file included.
 | |
| 
 | |
| #include "InputCommon/ControllerEmu/ControllerEmu.h"
 | |
| 
 | |
| #include <memory>
 | |
| #include <mutex>
 | |
| #include <string>
 | |
| 
 | |
| #include "Common/IniFile.h"
 | |
| 
 | |
| #include "InputCommon/ControlReference/ControlReference.h"
 | |
| #include "InputCommon/ControllerEmu/Control/Control.h"
 | |
| #include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
 | |
| #include "InputCommon/ControllerEmu/ControlGroup/Extension.h"
 | |
| #include "InputCommon/ControllerInterface/ControllerInterface.h"
 | |
| 
 | |
| namespace ControllerEmu
 | |
| {
 | |
| static std::recursive_mutex s_get_state_mutex;
 | |
| 
 | |
| EmulatedController::~EmulatedController() = default;
 | |
| 
 | |
| // This should be called before calling GetState() or State() on a control reference
 | |
| // to prevent a race condition.
 | |
| // This is a recursive mutex because UpdateReferences is recursive.
 | |
| std::unique_lock<std::recursive_mutex> EmulatedController::GetStateLock()
 | |
| {
 | |
|   std::unique_lock<std::recursive_mutex> lock(s_get_state_mutex);
 | |
|   return lock;
 | |
| }
 | |
| 
 | |
| void EmulatedController::UpdateReferences(const ControllerInterface& devi)
 | |
| {
 | |
|   const auto lock = GetStateLock();
 | |
|   m_default_device_is_connected = devi.HasConnectedDevice(m_default_device);
 | |
| 
 | |
|   for (auto& ctrlGroup : groups)
 | |
|   {
 | |
|     for (auto& control : ctrlGroup->controls)
 | |
|       control->control_ref.get()->UpdateReference(devi, GetDefaultDevice());
 | |
| 
 | |
|     // extension
 | |
|     if (ctrlGroup->type == GroupType::Extension)
 | |
|     {
 | |
|       for (auto& attachment : ((Extension*)ctrlGroup.get())->attachments)
 | |
|         attachment->UpdateReferences(devi);
 | |
|     }
 | |
|   }
 | |
| }
 | |
| 
 | |
| bool EmulatedController::IsDefaultDeviceConnected() const
 | |
| {
 | |
|   return m_default_device_is_connected;
 | |
| }
 | |
| 
 | |
| const ciface::Core::DeviceQualifier& EmulatedController::GetDefaultDevice() const
 | |
| {
 | |
|   return m_default_device;
 | |
| }
 | |
| 
 | |
| void EmulatedController::SetDefaultDevice(const std::string& device)
 | |
| {
 | |
|   ciface::Core::DeviceQualifier devq;
 | |
|   devq.FromString(device);
 | |
|   SetDefaultDevice(std::move(devq));
 | |
| }
 | |
| 
 | |
| void EmulatedController::SetDefaultDevice(ciface::Core::DeviceQualifier devq)
 | |
| {
 | |
|   m_default_device = std::move(devq);
 | |
| 
 | |
|   for (auto& ctrlGroup : groups)
 | |
|   {
 | |
|     // extension
 | |
|     if (ctrlGroup->type == GroupType::Extension)
 | |
|     {
 | |
|       for (auto& ai : ((Extension*)ctrlGroup.get())->attachments)
 | |
|       {
 | |
|         ai->SetDefaultDevice(m_default_device);
 | |
|       }
 | |
|     }
 | |
|   }
 | |
| }
 | |
| 
 | |
| void EmulatedController::LoadConfig(IniFile::Section* sec, const std::string& base)
 | |
| {
 | |
|   std::string defdev = GetDefaultDevice().ToString();
 | |
|   if (base.empty())
 | |
|   {
 | |
|     sec->Get(base + "Device", &defdev, "");
 | |
|     SetDefaultDevice(defdev);
 | |
|   }
 | |
| 
 | |
|   for (auto& cg : groups)
 | |
|     cg->LoadConfig(sec, defdev, base);
 | |
| }
 | |
| 
 | |
| void EmulatedController::SaveConfig(IniFile::Section* sec, const std::string& base)
 | |
| {
 | |
|   const std::string defdev = GetDefaultDevice().ToString();
 | |
|   if (base.empty())
 | |
|     sec->Set(/*std::string(" ") +*/ base + "Device", defdev, "");
 | |
| 
 | |
|   for (auto& ctrlGroup : groups)
 | |
|     ctrlGroup->SaveConfig(sec, defdev, base);
 | |
| }
 | |
| 
 | |
| void EmulatedController::LoadDefaults(const ControllerInterface& ciface)
 | |
| {
 | |
|   // load an empty inifile section, clears everything
 | |
|   IniFile::Section sec;
 | |
|   LoadConfig(&sec);
 | |
| 
 | |
|   const std::string& default_device_string = ciface.GetDefaultDeviceString();
 | |
|   if (!default_device_string.empty())
 | |
|   {
 | |
|     SetDefaultDevice(default_device_string);
 | |
|   }
 | |
| }
 | |
| }  // namespace ControllerEmu
 |