Skip to content

Commit

Permalink
Merge pull request #8548 from jordan-woyak/wiimote-source-cleanup
Browse files Browse the repository at this point in the history
Core/WiimoteReal: Wii remote connection pool and source change cleanup.
  • Loading branch information
Tilka committed Jan 25, 2020
2 parents 9ac77dc + 956339d commit 5dfc919
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 105 deletions.
28 changes: 14 additions & 14 deletions Source/Core/Core/BootManager.cpp
Expand Up @@ -98,7 +98,7 @@ struct ConfigCache
std::string strBackend;
std::string sBackend;
std::string m_strGPUDeterminismMode;
std::array<int, MAX_BBMOTES> iWiimoteSource;
std::array<WiimoteSource, MAX_BBMOTES> iWiimoteSource;
std::array<SerialInterface::SIDevices, SerialInterface::MAX_SI_CHANNELS> Pads;
std::array<ExpansionInterface::TEXIDevices, ExpansionInterface::MAX_EXI_CHANNELS> m_EXIDevice;
};
Expand Down Expand Up @@ -133,7 +133,9 @@ void ConfigCache::SaveConfig(const SConfig& config)
m_OCEnable = config.m_OCEnable;
m_bt_passthrough_enabled = config.m_bt_passthrough_enabled;

std::copy(std::begin(g_wiimote_sources), std::end(g_wiimote_sources), std::begin(iWiimoteSource));
for (int i = 0; i != MAX_BBMOTES; ++i)
iWiimoteSource[i] = WiimoteCommon::GetSource(i);

std::copy(std::begin(config.m_SIDevice), std::end(config.m_SIDevice), std::begin(Pads));
std::copy(std::begin(config.m_EXIDevice), std::end(config.m_EXIDevice), std::begin(m_EXIDevice));

Expand Down Expand Up @@ -180,10 +182,7 @@ void ConfigCache::RestoreConfig(SConfig* config)
for (unsigned int i = 0; i < MAX_BBMOTES; ++i)
{
if (bSetWiimoteSource[i])
{
g_wiimote_sources[i] = iWiimoteSource[i];
WiimoteReal::ChangeWiimoteSource(i, iWiimoteSource[i]);
}
WiimoteCommon::SetSource(i, iWiimoteSource[i]);
}
}

Expand Down Expand Up @@ -304,21 +303,22 @@ bool BootCore(std::unique_ptr<BootParameters> boot, const WindowSystemInfo& wsi)
for (unsigned int i = 0; i < MAX_WIIMOTES; ++i)
{
controls_section->Get(fmt::format("WiimoteSource{}", i), &source, -1);
if (source != -1 && g_wiimote_sources[i] != (unsigned)source &&
source >= WIIMOTE_SRC_NONE && source <= WIIMOTE_SRC_REAL)
if (source != -1 && WiimoteCommon::GetSource(i) != WiimoteSource(source) &&
WiimoteSource(source) >= WiimoteSource::None &&
WiimoteSource(source) <= WiimoteSource::Real)
{
config_cache.bSetWiimoteSource[i] = true;
g_wiimote_sources[i] = source;
WiimoteReal::ChangeWiimoteSource(i, source);
WiimoteCommon::SetSource(i, WiimoteSource(source));
}
}
controls_section->Get("WiimoteSourceBB", &source, -1);
if (source != -1 && g_wiimote_sources[WIIMOTE_BALANCE_BOARD] != (unsigned)source &&
(source == WIIMOTE_SRC_NONE || source == WIIMOTE_SRC_REAL))
if (source != -1 &&
WiimoteCommon::GetSource(WIIMOTE_BALANCE_BOARD) != WiimoteSource(source) &&
(WiimoteSource(source) == WiimoteSource::None ||
WiimoteSource(source) == WiimoteSource::Real))
{
config_cache.bSetWiimoteSource[WIIMOTE_BALANCE_BOARD] = true;
g_wiimote_sources[WIIMOTE_BALANCE_BOARD] = source;
WiimoteReal::ChangeWiimoteSource(WIIMOTE_BALANCE_BOARD, source);
WiimoteCommon::SetSource(WIIMOTE_BALANCE_BOARD, WiimoteSource(source));
}
}
}
Expand Down
65 changes: 45 additions & 20 deletions Source/Core/Core/HW/Wiimote.cpp
Expand Up @@ -25,6 +25,38 @@
// Limit the amount of wiimote connect requests, when a button is pressed in disconnected state
static std::array<u8, MAX_BBMOTES> s_last_connect_request_counter;

namespace WiimoteCommon
{
static std::array<std::atomic<WiimoteSource>, MAX_BBMOTES> s_wiimote_sources;

WiimoteSource GetSource(unsigned int index)
{
return s_wiimote_sources[index];
}

void SetSource(unsigned int index, WiimoteSource source)
{
const WiimoteSource previous_source = s_wiimote_sources[index].exchange(source);

if (previous_source == source)
{
// No change. Do nothing.
return;
}

WiimoteReal::HandleWiimoteSourceChange(index);

// Reconnect to the emulator.
Core::RunAsCPUThread([index, previous_source, source] {
if (previous_source != WiimoteSource::None)
::Wiimote::Connect(index, false);

if (source == WiimoteSource::Emulated)
::Wiimote::Connect(index, true);
});
}
} // namespace WiimoteCommon

namespace Wiimote
{
static InputConfig s_config(WIIMOTE_INI_NAME, _trans("Wii Remote"), "Wiimote");
Expand Down Expand Up @@ -155,7 +187,7 @@ void Pause()
// 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_EMU == g_wiimote_sources[number])
if (WiimoteCommon::GetSource(number) == WiimoteSource::Emulated)
{
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(number))
->ControlChannel(channel_id, data, size);
Expand All @@ -169,7 +201,7 @@ void ControlChannel(int number, u16 channel_id, const void* data, 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_EMU == g_wiimote_sources[number])
if (WiimoteCommon::GetSource(number) == WiimoteSource::Emulated)
{
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(number))
->InterruptChannel(channel_id, data, size);
Expand All @@ -182,24 +214,26 @@ void InterruptChannel(int number, u16 channel_id, const void* data, u32 size)

bool ButtonPressed(int number)
{
const WiimoteSource source = WiimoteCommon::GetSource(number);

if (s_last_connect_request_counter[number] > 0)
{
--s_last_connect_request_counter[number];
if (g_wiimote_sources[number] && NetPlay::IsNetPlayRunning())
if (source != WiimoteSource::None && NetPlay::IsNetPlayRunning())
Wiimote::NetPlay_GetButtonPress(number, false);
return false;
}

bool button_pressed = false;

if (WIIMOTE_SRC_EMU & g_wiimote_sources[number])
if (source == WiimoteSource::Emulated)
button_pressed =
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(number))->CheckForButtonPress();

if (WIIMOTE_SRC_REAL & g_wiimote_sources[number])
if (source == WiimoteSource::Real)
button_pressed = WiimoteReal::CheckForButtonPress(number);

if (g_wiimote_sources[number] && NetPlay::IsNetPlayRunning())
if (source != WiimoteSource::None && NetPlay::IsNetPlayRunning())
button_pressed = Wiimote::NetPlay_GetButtonPress(number, button_pressed);

return button_pressed;
Expand All @@ -210,7 +244,7 @@ void Update(int number, bool connected)
{
if (connected)
{
if (WIIMOTE_SRC_EMU & g_wiimote_sources[number])
if (WiimoteCommon::GetSource(number) == WiimoteSource::Emulated)
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(number))->Update();
else
WiimoteReal::Update(number);
Expand All @@ -227,25 +261,16 @@ void Update(int number, bool connected)
}
}

// Get a mask of attached the pads (eg: controller 1 & 4 -> 0x9)
unsigned int GetAttached()
{
unsigned int attached = 0;
for (unsigned int i = 0; i < MAX_BBMOTES; ++i)
if (g_wiimote_sources[i])
attached |= (1 << i);
return attached;
}

// Save/Load state
void DoState(PointerWrap& p)
{
for (int i = 0; i < MAX_BBMOTES; ++i)
{
auto state_wiimote_source = u8(g_wiimote_sources[i]);
const WiimoteSource source = WiimoteCommon::GetSource(i);
auto state_wiimote_source = u8(source);
p.Do(state_wiimote_source);

if (WIIMOTE_SRC_EMU == state_wiimote_source)
if (WiimoteSource(state_wiimote_source) == WiimoteSource::Emulated)
{
// Sync complete state of emulated wiimotes.
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(i))->DoState(p);
Expand All @@ -255,7 +280,7 @@ void DoState(PointerWrap& p)
{
// If using a real wiimote or the save-state source does not match the current source,
// then force a reconnection on load.
if (WIIMOTE_SRC_REAL == g_wiimote_sources[i] || state_wiimote_source != g_wiimote_sources[i])
if (source == WiimoteSource::Real || source != WiimoteSource(state_wiimote_source))
{
Connect(i, false);
Connect(i, true);
Expand Down
16 changes: 10 additions & 6 deletions Source/Core/Core/HW/Wiimote.h
Expand Up @@ -44,14 +44,18 @@ enum

#define WIIMOTE_INI_NAME "WiimoteNew"

enum
enum class WiimoteSource
{
WIIMOTE_SRC_NONE = 0,
WIIMOTE_SRC_EMU = 1,
WIIMOTE_SRC_REAL = 2,
None = 0,
Emulated = 1,
Real = 2,
};

extern std::array<std::atomic<u32>, MAX_BBMOTES> g_wiimote_sources;
namespace WiimoteCommon
{
WiimoteSource GetSource(unsigned int index);
void SetSource(unsigned int index, WiimoteSource source);
} // namespace WiimoteCommon

namespace Wiimote
{
Expand All @@ -74,7 +78,6 @@ void LoadConfig();
void Resume();
void Pause();

unsigned int GetAttached();
void DoState(PointerWrap& p);
InputConfig* GetConfig();
ControllerEmu::ControlGroup* GetWiimoteGroup(int number, WiimoteEmu::WiimoteGroup group);
Expand Down Expand Up @@ -105,4 +108,5 @@ void Pause();
void Refresh();

void LoadSettings();

} // namespace WiimoteReal

0 comments on commit 5dfc919

Please sign in to comment.