| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2010 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2010-06-12 17:15:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-29 22:32:04 -05:00
										 |  |  | #include "InputCommon/ControllerEmu/ControllerEmu.h"
 | 
					
						
							| 
									
										
										
										
											2017-02-08 22:15:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-03 15:37:25 -04:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2017-02-08 22:15:43 -05:00
										 |  |  | #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"
 | 
					
						
							| 
									
										
										
										
											2017-04-04 15:37:31 -04:00
										 |  |  | #include "InputCommon/ControllerInterface/ControllerInterface.h"
 | 
					
						
							| 
									
										
										
										
											2017-02-08 22:15:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace ControllerEmu | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | static std::recursive_mutex s_get_state_mutex; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | EmulatedController::~EmulatedController() = default; | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-14 17:45:59 +02:00
										 |  |  | // 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.
 | 
					
						
							| 
									
										
										
										
											2017-02-08 22:15:43 -05:00
										 |  |  | std::unique_lock<std::recursive_mutex> EmulatedController::GetStateLock() | 
					
						
							| 
									
										
										
										
											2016-07-14 17:45:59 +02:00
										 |  |  | { | 
					
						
							|  |  |  |   std::unique_lock<std::recursive_mutex> lock(s_get_state_mutex); | 
					
						
							|  |  |  |   return lock; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-11 00:29:22 -05:00
										 |  |  | void EmulatedController::UpdateReferences(const ControllerInterface& devi) | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-02-08 22:15:43 -05:00
										 |  |  |   const auto lock = GetStateLock(); | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (auto& ctrlGroup : groups) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     for (auto& control : ctrlGroup->controls) | 
					
						
							| 
									
										
										
										
											2016-10-11 17:48:38 -07:00
										 |  |  |       control->control_ref.get()->UpdateReference(devi, default_device); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     // extension
 | 
					
						
							| 
									
										
										
										
											2017-02-25 00:35:02 -05:00
										 |  |  |     if (ctrlGroup->type == GroupType::Extension) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |       for (auto& attachment : ((Extension*)ctrlGroup.get())->attachments) | 
					
						
							|  |  |  |         attachment->UpdateReferences(devi); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 22:15:43 -05:00
										 |  |  | void EmulatedController::UpdateDefaultDevice() | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (auto& ctrlGroup : groups) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     // extension
 | 
					
						
							| 
									
										
										
										
											2017-02-25 00:35:02 -05:00
										 |  |  |     if (ctrlGroup->type == GroupType::Extension) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |       for (auto& ai : ((Extension*)ctrlGroup.get())->attachments) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         ai->default_device = default_device; | 
					
						
							|  |  |  |         ai->UpdateDefaultDevice(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 22:15:43 -05:00
										 |  |  | void EmulatedController::LoadConfig(IniFile::Section* sec, const std::string& base) | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::string defdev = default_device.ToString(); | 
					
						
							|  |  |  |   if (base.empty()) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     sec->Get(base + "Device", &defdev, ""); | 
					
						
							|  |  |  |     default_device.FromString(defdev); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (auto& cg : groups) | 
					
						
							|  |  |  |     cg->LoadConfig(sec, defdev, base); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 22:15:43 -05:00
										 |  |  | void EmulatedController::SaveConfig(IniFile::Section* sec, const std::string& base) | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   const std::string defdev = default_device.ToString(); | 
					
						
							|  |  |  |   if (base.empty()) | 
					
						
							|  |  |  |     sec->Set(/*std::string(" ") +*/ base + "Device", defdev, ""); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (auto& ctrlGroup : groups) | 
					
						
							|  |  |  |     ctrlGroup->SaveConfig(sec, defdev, base); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 22:15:43 -05:00
										 |  |  | void EmulatedController::LoadDefaults(const ControllerInterface& ciface) | 
					
						
							| 
									
										
										
										
											2010-07-10 06:48:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // load an empty inifile section, clears everything
 | 
					
						
							|  |  |  |   IniFile::Section sec; | 
					
						
							|  |  |  |   LoadConfig(&sec); | 
					
						
							| 
									
										
										
										
											2010-07-10 06:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 17:46:40 -08:00
										 |  |  |   const std::string& default_device_string = ciface.GetDefaultDeviceString(); | 
					
						
							| 
									
										
										
										
											2016-06-25 21:46:39 +02:00
										 |  |  |   if (!default_device_string.empty()) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2016-06-25 21:46:39 +02:00
										 |  |  |     default_device.FromString(default_device_string); | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     UpdateDefaultDevice(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-07-10 06:48:24 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-02-08 22:15:43 -05:00
										 |  |  | }  // namespace ControllerEmu
 |