InputConfig: Clean up controller management

This commit is contained in:
Lioncash 2015-10-25 22:28:15 -04:00
parent 59b54a77d3
commit b9975694ab
17 changed files with 146 additions and 171 deletions

View File

@ -22,26 +22,22 @@ InputConfig* GetConfig()
void Shutdown()
{
std::vector<ControllerEmu*>::const_iterator
i = s_config.controllers.begin(),
e = s_config.controllers.end();
for ( ; i!=e; ++i )
delete *i;
s_config.controllers.clear();
s_config.ClearControllers();
g_controller_interface.Shutdown();
}
// if plugin isn't initialized, init and load config
void Initialize(void* const hwnd)
{
if (s_config.controllers.empty())
if (s_config.ControllersNeedToBeCreated())
{
for (unsigned int i = 0; i < 4; ++i)
s_config.controllers.push_back(new GCKeyboard(i));
s_config.CreateController<GCKeyboard>(i);
}
g_controller_interface.Initialize(hwnd);
// load the saved controller config
// Load the saved controller config
s_config.LoadConfig(true);
}
@ -50,13 +46,13 @@ void LoadConfig()
s_config.LoadConfig(true);
}
void GetStatus(u8 _port, KeyboardStatus* _pKeyboardStatus)
void GetStatus(u8 port, KeyboardStatus* keyboard_status)
{
memset(_pKeyboardStatus, 0, sizeof(*_pKeyboardStatus));
_pKeyboardStatus->err = PAD_ERR_NONE;
memset(keyboard_status, 0, sizeof(*keyboard_status));
keyboard_status->err = PAD_ERR_NONE;
// get input
((GCKeyboard*)s_config.controllers[_port])->GetInput(_pKeyboardStatus);
// Get input
static_cast<GCKeyboard*>(s_config.GetController(port))->GetInput(keyboard_status);
}
}

View File

@ -18,6 +18,6 @@ void LoadConfig();
InputConfig* GetConfig();
void GetStatus(u8 _port, KeyboardStatus* _pKeyboardStatus);
void GetStatus(u8 port, KeyboardStatus* keyboard_status);
}

View File

@ -23,27 +23,22 @@ InputConfig* GetConfig()
void Shutdown()
{
std::vector<ControllerEmu*>::const_iterator
i = s_config.controllers.begin(),
e = s_config.controllers.end();
for ( ; i!=e; ++i )
delete *i;
s_config.controllers.clear();
s_config.ClearControllers();
g_controller_interface.Shutdown();
}
// if plugin isn't initialized, init and load config
void Initialize(void* const hwnd)
{
// add 4 gcpads
if (s_config.controllers.empty())
if (s_config.ControllersNeedToBeCreated())
{
for (unsigned int i = 0; i < 4; ++i)
s_config.controllers.push_back(new GCPad(i));
s_config.CreateController<GCPad>(i);
}
g_controller_interface.Initialize(hwnd);
// load the saved controller config
// Load the saved controller config
s_config.LoadConfig(true);
}
@ -53,31 +48,31 @@ void LoadConfig()
}
void GetStatus(u8 _numPAD, GCPadStatus* _pPADStatus)
void GetStatus(u8 pad_num, GCPadStatus* pad_status)
{
memset(_pPADStatus, 0, sizeof(*_pPADStatus));
_pPADStatus->err = PAD_ERR_NONE;
memset(pad_status, 0, sizeof(*pad_status));
pad_status->err = PAD_ERR_NONE;
// if we are on the next input cycle, update output and input
static int _last_numPAD = 4;
if (_numPAD <= _last_numPAD)
// If we are on the next input cycle, update output and input
static int last_pad_num = 4;
if (pad_num <= last_pad_num)
{
g_controller_interface.UpdateInput();
}
_last_numPAD = _numPAD;
last_pad_num = pad_num;
// get input
((GCPad*)s_config.controllers[_numPAD])->GetInput(_pPADStatus);
// Get input
static_cast<GCPad*>(s_config.GetController(pad_num))->GetInput(pad_status);
}
void Rumble(u8 _numPAD, const ControlState strength)
void Rumble(const u8 pad_num, const ControlState strength)
{
((GCPad*)s_config.controllers[ _numPAD ])->SetOutput(strength);
static_cast<GCPad*>(s_config.GetController(pad_num))->SetOutput(strength);
}
bool GetMicButton(u8 pad)
bool GetMicButton(const u8 pad_num)
{
return ((GCPad*)s_config.controllers[pad])->GetMicButton();
return static_cast<GCPad*>(s_config.GetController(pad_num))->GetMicButton();
}
}

View File

@ -19,8 +19,8 @@ void LoadConfig();
InputConfig* GetConfig();
void GetStatus(u8 _numPAD, GCPadStatus* _pPADStatus);
void Rumble(u8 _numPAD, const ControlState strength);
void GetStatus(u8 pad_num, GCPadStatus* pad_status);
void Rumble(u8 pad_num, ControlState strength);
bool GetMicButton(u8 pad);
bool GetMicButton(u8 pad_num);
}

View File

@ -25,24 +25,20 @@ InputConfig* GetConfig()
void Shutdown()
{
for (const ControllerEmu* i : s_config.controllers)
{
delete i;
}
s_config.controllers.clear();
s_config.ClearControllers();
WiimoteReal::Stop();
g_controller_interface.Shutdown();
}
// if plugin isn't initialized, init and load config
void Initialize(void* const hwnd, bool wait)
{
// add 4 Wiimotes
if (s_config.controllers.empty())
if (s_config.ControllersNeedToBeCreated())
{
for (unsigned int i = WIIMOTE_CHAN_0; i < MAX_BBMOTES; ++i)
s_config.controllers.push_back(new WiimoteEmu::Wiimote(i));
s_config.CreateController<WiimoteEmu::Wiimote>(i);
}
g_controller_interface.Initialize(hwnd);
@ -50,7 +46,7 @@ void Initialize(void* const hwnd, bool wait)
WiimoteReal::Initialize(wait);
// reload Wiimotes with our settings
// Reload Wiimotes with our settings
if (Movie::IsMovieActive())
Movie::ChangeWiiPads();
}
@ -72,72 +68,41 @@ void Pause()
}
// __________________________________________________________________________________________________
// Function: ControlChannel
// Purpose: An L2CAP packet is passed from the Core to the Wiimote,
// on the HID CONTROL channel.
//
// Inputs: _number [Description needed]
// _channelID [Description needed]
// _pData [Description needed]
// _Size [Description needed]
//
// Output: none
//
void ControlChannel(int _number, u16 _channelID, const void* _pData, u32 _Size)
// An L2CAP packet is passed from the Core to the Wiimote on the HID CONTROL channel.
void ControlChannel(int number, u16 channel_id, const void* data, u32 size)
{
if (WIIMOTE_SRC_HYBRID & g_wiimote_sources[_number])
((WiimoteEmu::Wiimote*)s_config.controllers[_number])->ControlChannel(_channelID, _pData, _Size);
if (WIIMOTE_SRC_HYBRID & g_wiimote_sources[number])
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(number))->ControlChannel(channel_id, data, size);
}
// __________________________________________________________________________________________________
// Function: InterruptChannel
// Purpose: An L2CAP packet is passed from the Core to the Wiimote,
// on the HID INTERRUPT channel.
//
// Inputs: _number [Description needed]
// _channelID [Description needed]
// _pData [Description needed]
// _Size [Description needed]
//
// Output: none
//
void InterruptChannel(int _number, u16 _channelID, const void* _pData, u32 _Size)
// An L2CAP packet is passed from the Core to the Wiimote on the HID INTERRUPT channel.
void InterruptChannel(int number, u16 channel_id, const void* data, u32 size)
{
if (WIIMOTE_SRC_HYBRID & g_wiimote_sources[_number])
((WiimoteEmu::Wiimote*)s_config.controllers[_number])->InterruptChannel(_channelID, _pData, _Size);
if (WIIMOTE_SRC_HYBRID & g_wiimote_sources[number])
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(number))->InterruptChannel(channel_id, data, size);
}
// __________________________________________________________________________________________________
// Function: Update
// Purpose: This function is called periodically by the Core. // TODO: Explain why.
// input: _number: [Description needed]
// output: none
//
void Update(int _number, bool _connected)
// This function is called periodically by the Core to update Wiimote state.
void Update(int number, bool connected)
{
if (_connected)
if (connected)
{
if (WIIMOTE_SRC_EMU & g_wiimote_sources[_number])
((WiimoteEmu::Wiimote*)s_config.controllers[_number])->Update();
if (WIIMOTE_SRC_EMU & g_wiimote_sources[number])
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(number))->Update();
else
WiimoteReal::Update(_number);
WiimoteReal::Update(number);
}
else
{
if (WIIMOTE_SRC_EMU & g_wiimote_sources[_number])
((WiimoteEmu::Wiimote*)s_config.controllers[_number])->ConnectOnInput();
if (WIIMOTE_SRC_REAL & g_wiimote_sources[_number])
WiimoteReal::ConnectOnInput(_number);
if (WIIMOTE_SRC_EMU & g_wiimote_sources[number])
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(number))->ConnectOnInput();
if (WIIMOTE_SRC_REAL & g_wiimote_sources[number])
WiimoteReal::ConnectOnInput(number);
}
}
// __________________________________________________________________________________________________
// Function: GetAttached
// Purpose: Get mask of attached pads (eg: controller 1 & 4 -> 0x9)
// input: none
// output: The number of attached pads
//
// Get a mask of attached the pads (eg: controller 1 & 4 -> 0x9)
unsigned int GetAttached()
{
unsigned int attached = 0;
@ -147,29 +112,16 @@ unsigned int GetAttached()
return attached;
}
// ___________________________________________________________________________
// Function: DoState
// Purpose: Saves/load state
// input/output: ptr: [Description Needed]
// input: mode [Description needed]
//
// Save/Load state
void DoState(PointerWrap& p)
{
// TODO:
for (unsigned int i=0; i<MAX_BBMOTES; ++i)
((WiimoteEmu::Wiimote*)s_config.controllers[i])->DoState(p);
for (int i = 0; i < MAX_BBMOTES; ++i)
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(i))->DoState(p);
}
// ___________________________________________________________________________
// Function: EmuStateChange
// Purpose: Notifies the plugin of a change in emulation state
// input: newState - The new state for the Wiimote to change to.
// output: none
//
// Notifies the plugin of a change in emulation state
void EmuStateChange(EMUSTATE_CHANGE newState)
{
// TODO
WiimoteReal::StateChange(newState);
}

View File

@ -10,7 +10,9 @@
#include <bluetooth/hci_lib.h>
#include <bluetooth/l2cap.h>
#include "Common/Common.h"
#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
#include "Core/HW/WiimoteEmu/WiimoteHid.h"
#include "Core/HW/WiimoteReal/WiimoteReal.h"
namespace WiimoteReal

View File

@ -19,6 +19,7 @@
#include "Common/StringUtil.h"
#include "Common/Thread.h"
#include "Core/HW/WiimoteEmu/WiimoteHid.h"
#include "Core/HW/WiimoteReal/WiimoteReal.h"
//#define AUTHENTICATE_WIIMOTES

View File

@ -1,6 +1,8 @@
#define BLUETOOTH_VERSION_USE_CURRENT
#include "Common/Common.h"
#include "Common/Logging/Log.h"
#include "Core/HW/WiimoteEmu/WiimoteHid.h"
#include "Core/HW/WiimoteReal/WiimoteReal.h"
@interface SearchBT: NSObject {

View File

@ -14,8 +14,10 @@
#include "Common/Timer.h"
#include "Core/ConfigManager.h"
#include "Core/Host.h"
#include "Core/HW/WiimoteEmu/WiimoteEmu.h"
#include "Core/HW/WiimoteEmu/WiimoteHid.h"
#include "Core/HW/WiimoteReal/WiimoteReal.h"
#include "InputCommon/InputConfig.h"
#include "SFML/Network.hpp"
@ -153,7 +155,7 @@ void Wiimote::InterruptChannel(const u16 channel, const void* const _data, const
auto const data = static_cast<const u8*>(_data);
Report rpt(data, data + size);
WiimoteEmu::Wiimote *const wm = (WiimoteEmu::Wiimote*)::Wiimote::GetConfig()->controllers[m_index];
WiimoteEmu::Wiimote* const wm = static_cast<WiimoteEmu::Wiimote*>(::Wiimote::GetConfig()->GetController(m_index));
// Convert output DATA packets to SET_REPORT packets.
// Nintendo Wiimotes work without this translation, but 3rd
@ -382,7 +384,7 @@ void Wiimote::EmuStop()
void Wiimote::EmuResume()
{
WiimoteEmu::Wiimote *const wm = (WiimoteEmu::Wiimote*)::Wiimote::GetConfig()->controllers[m_index];
WiimoteEmu::Wiimote* const wm = static_cast<WiimoteEmu::Wiimote*>(::Wiimote::GetConfig()->GetController(m_index));
m_last_input_report.clear();

View File

@ -16,9 +16,7 @@
#include "Common/NonCopyable.h"
#include "Common/Timer.h"
#include "Core/HW/Wiimote.h"
#include "Core/HW/WiimoteEmu/WiimoteEmu.h"
#include "Core/HW/WiimoteReal/WiimoteRealBase.h"
#include "InputCommon/InputConfig.h"
class PointerWrap;
@ -29,7 +27,6 @@ namespace WiimoteReal
class Wiimote : NonCopyable
{
friend class WiimoteEmu::Wiimote;
public:
virtual ~Wiimote() {}
// This needs to be called in derived destructors!

View File

@ -8,6 +8,7 @@
#include "Common/Common.h"
#include "Core/ConfigManager.h"
#include "Core/HotkeyManager.h"
#include "InputCommon/GCPadStatus.h"
const std::string hotkey_labels[] =
{
@ -147,8 +148,8 @@ void GetStatus()
{
s_hotkey.err = PAD_ERR_NONE;
// get input
((HotkeyManager*)s_config.controllers[0])->GetInput(&s_hotkey);
// Get input
static_cast<HotkeyManager*>(s_config.GetController(0))->GetInput(&s_hotkey);
}
bool IsEnabled()
@ -182,8 +183,8 @@ bool IsPressed(int Id, bool held)
void Initialize(void* const hwnd)
{
if (s_config.controllers.empty())
s_config.controllers.push_back(new HotkeyManager());
if (s_config.ControllersNeedToBeCreated())
s_config.CreateController<HotkeyManager>();
g_controller_interface.Initialize(hwnd);
@ -203,12 +204,7 @@ void LoadConfig()
void Shutdown()
{
std::vector<ControllerEmu*>::const_iterator
i = s_config.controllers.begin(),
e = s_config.controllers.end();
for (; i != e; ++i)
delete *i;
s_config.controllers.clear();
s_config.ClearControllers();
g_controller_interface.Shutdown();
}

View File

@ -5,6 +5,7 @@
#pragma once
#include <string>
#include "InputCommon/ControllerEmu.h"
#include "InputCommon/InputConfig.h"
enum Hotkey

View File

@ -170,7 +170,7 @@ void InputConfigDialog::UpdateProfileComboBox()
{
std::string pname(File::GetUserPath(D_CONFIG_IDX));
pname += PROFILES_PATH;
pname += m_config.profile_name;
pname += m_config.GetProfileName();
std::vector<std::string> sv = DoFileSearch({".ini"}, {pname});
@ -652,7 +652,7 @@ void GamepadPage::GetProfilePath(std::string& path)
path = File::GetUserPath(D_CONFIG_IDX);
path += PROFILES_PATH;
path += m_config.profile_name;
path += m_config.GetProfileName();
path += '/';
path += WxStrToStr(profile_cbox->GetValue());
path += ".ini";
@ -984,14 +984,14 @@ ControlGroupsSizer::ControlGroupsSizer(ControllerEmu* const controller, wxWindow
Add(stacked_groups, 0, /*wxEXPAND|*/wxBOTTOM|wxRIGHT, 5);
}
GamepadPage::GamepadPage(wxWindow* parent, InputConfig& config, const unsigned int pad_num, InputConfigDialog* const config_dialog)
GamepadPage::GamepadPage(wxWindow* parent, InputConfig& config, const int pad_num, InputConfigDialog* const config_dialog)
: wxPanel(parent, wxID_ANY)
,controller(config.controllers[pad_num])
, controller(config.GetController(pad_num))
, m_config_dialog(config_dialog)
, m_config(config)
{
wxBoxSizer* control_group_sizer = new ControlGroupsSizer(m_config.controllers[pad_num], this, this, &control_groups);
wxBoxSizer* control_group_sizer = new ControlGroupsSizer(controller, this, this, &control_groups);
wxStaticBoxSizer* profile_sbox = new wxStaticBoxSizer(wxHORIZONTAL, this, _("Profile"));
@ -1060,7 +1060,7 @@ InputConfigDialog::InputConfigDialog(wxWindow* const parent, InputConfig& config
m_pad_notebook = new wxNotebook(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNB_DEFAULT);
GamepadPage* gp = new GamepadPage(m_pad_notebook, m_config, tab_num, this);
m_padpages.push_back(gp);
m_pad_notebook->AddPage(gp, wxString::Format("%s [%u]", wxGetTranslation(StrToWxStr(m_config.gui_name)), 1 + tab_num));
m_pad_notebook->AddPage(gp, wxString::Format("%s [%u]", wxGetTranslation(StrToWxStr(m_config.GetGUIName())), 1 + tab_num));
m_pad_notebook->SetSelection(0);

View File

@ -200,7 +200,7 @@ class GamepadPage : public wxPanel
friend class ControlDialog;
public:
GamepadPage(wxWindow* parent, InputConfig& config, const unsigned int pad_num, InputConfigDialog* const config_dialog);
GamepadPage(wxWindow* parent, InputConfig& config, const int pad_num, InputConfigDialog* const config_dialog);
void UpdateGUI();

View File

@ -142,7 +142,7 @@ void TASInputDlg::CreateWiiLayout(int num)
if (Core::IsRunning())
{
m_ext = ((WiimoteEmu::Wiimote*)Wiimote::GetConfig()->controllers[num])->CurrentExtension();
m_ext = static_cast<WiimoteEmu::Wiimote*>(Wiimote::GetConfig()->GetController(num))->CurrentExtension();
}
else
{

View File

@ -2,10 +2,15 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "Common/CommonPaths.h"
#include <vector>
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Core/ConfigManager.h"
#include "Core/HW/Wiimote.h"
#include "InputCommon/ControllerEmu.h"
#include "InputCommon/InputConfig.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"
bool InputConfig::LoadConfig(bool isGC)
{
@ -52,25 +57,25 @@ bool InputConfig::LoadConfig(bool isGC)
}
}
if (inifile.Load(File::GetUserPath(D_CONFIG_IDX) + ini_name + ".ini"))
if (inifile.Load(File::GetUserPath(D_CONFIG_IDX) + m_ini_name + ".ini"))
{
int n = 0;
for (ControllerEmu* pad : controllers)
for (auto& controller : m_controllers)
{
// Load settings from ini
if (useProfile[n])
{
IniFile profile_ini;
profile_ini.Load(File::GetUserPath(D_CONFIG_IDX) + path + profile[n] + ".ini");
pad->LoadConfig(profile_ini.GetOrCreateSection("Profile"));
controller->LoadConfig(profile_ini.GetOrCreateSection("Profile"));
}
else
{
pad->LoadConfig(inifile.GetOrCreateSection(pad->GetName()));
controller->LoadConfig(inifile.GetOrCreateSection(controller->GetName()));
}
// Update refs
pad->UpdateReferences(g_controller_interface);
controller->UpdateReferences(g_controller_interface);
// Next profile
n++;
@ -79,21 +84,36 @@ bool InputConfig::LoadConfig(bool isGC)
}
else
{
controllers[0]->LoadDefaults(g_controller_interface);
controllers[0]->UpdateReferences(g_controller_interface);
m_controllers[0]->LoadDefaults(g_controller_interface);
m_controllers[0]->UpdateReferences(g_controller_interface);
return false;
}
}
void InputConfig::SaveConfig()
{
std::string ini_filename = File::GetUserPath(D_CONFIG_IDX) + ini_name + ".ini";
std::string ini_filename = File::GetUserPath(D_CONFIG_IDX) + m_ini_name + ".ini";
IniFile inifile;
inifile.Load(ini_filename);
for (ControllerEmu* pad : controllers)
pad->SaveConfig(inifile.GetOrCreateSection(pad->GetName()));
for (auto& controller : m_controllers)
controller->SaveConfig(inifile.GetOrCreateSection(controller->GetName()));
inifile.Save(ini_filename);
}
ControllerEmu* InputConfig::GetController(int index)
{
return m_controllers.at(index).get();
}
void InputConfig::ClearControllers()
{
m_controllers.clear();
}
bool InputConfig::ControllersNeedToBeCreated() const
{
return m_controllers.empty();
}

View File

@ -4,29 +4,40 @@
#pragma once
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Common/Thread.h"
#include "InputCommon/ControllerEmu.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"
class ControllerEmu;
class InputConfig
{
public:
InputConfig(const char* const _ini_name, const char* const _gui_name,
const char* const _profile_name)
: ini_name(_ini_name), gui_name(_gui_name), profile_name(_profile_name) {}
InputConfig(const std::string& ini_name, const std::string& gui_name, const std::string& profile_name)
: m_ini_name(ini_name), m_gui_name(gui_name), m_profile_name(profile_name)
{
}
bool LoadConfig(bool isGC);
void SaveConfig();
std::vector<ControllerEmu*> controllers;
template <typename T, typename... Args>
void CreateController(Args&&... args)
{
m_controllers.emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
}
const char* const ini_name;
const char* const gui_name;
const char* const profile_name;
ControllerEmu* GetController(int index);
void ClearControllers();
bool ControllersNeedToBeCreated() const;
std::string GetGUIName() const { return m_gui_name; }
std::string GetProfileName() const { return m_profile_name; }
private:
std::vector<std::unique_ptr<ControllerEmu>> m_controllers;
const std::string m_ini_name;
const std::string m_gui_name;
const std::string m_profile_name;
};