Skip to content

Commit

Permalink
Merge pull request #10443 from JosJuice/port-wiimote-source
Browse files Browse the repository at this point in the history
Port Wiimote source settings to the new config system
  • Loading branch information
leoetlino committed Feb 18, 2022
2 parents 9ebfdff + aff45c9 commit 0c78167
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 172 deletions.
1 change: 0 additions & 1 deletion Source/Android/jni/MainAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_RefreshWiimo
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_ReloadWiimoteConfig(JNIEnv*,
jclass)
{
WiimoteReal::LoadSettings();
Wiimote::LoadConfig();
}

Expand Down
90 changes: 0 additions & 90 deletions Source/Core/Core/BootManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,58 +53,6 @@

namespace BootManager
{
// TODO this is an ugly hack which allows us to restore values trampled by per-game settings
// Apply fire liberally
struct ConfigCache
{
public:
// fill the cache with values from the configuration
void SaveConfig(const SConfig& config);
// restore values to the configuration from the cache
void RestoreConfig(SConfig* config);

// These store if the relevant setting should be reset back later (true) or if it should be left
// alone on restore (false)
bool bSetVolume = false;
std::array<bool, MAX_BBMOTES> bSetWiimoteSource{};

private:
bool valid = false;
std::array<WiimoteSource, MAX_BBMOTES> iWiimoteSource{};
};

void ConfigCache::SaveConfig(const SConfig& config)
{
valid = true;

for (int i = 0; i != MAX_BBMOTES; ++i)
iWiimoteSource[i] = WiimoteCommon::GetSource(i);

bSetVolume = false;
bSetWiimoteSource.fill(false);
}

void ConfigCache::RestoreConfig(SConfig* config)
{
if (!valid)
return;

valid = false;

// Only change these back if they were actually set by game ini, since they can be changed while a
// game is running.
if (config->bWii)
{
for (unsigned int i = 0; i < MAX_BBMOTES; ++i)
{
if (bSetWiimoteSource[i])
WiimoteCommon::SetSource(i, iWiimoteSource[i]);
}
}
}

static ConfigCache config_cache;

// Boot the ISO or file
bool BootCore(std::unique_ptr<BootParameters> boot, const WindowSystemInfo& wsi)
{
Expand All @@ -113,46 +61,9 @@ bool BootCore(std::unique_ptr<BootParameters> boot, const WindowSystemInfo& wsi)

SConfig& StartUp = SConfig::GetInstance();

config_cache.SaveConfig(StartUp);

if (!StartUp.SetPathsAndGameMetadata(*boot))
return false;

// Load game specific settings
if (!std::holds_alternative<BootParameters::IPL>(boot->parameters))
{
IniFile game_ini = StartUp.LoadGameIni();

// General settings
IniFile::Section* controls_section = game_ini.GetOrCreateSection("Controls");

// Wii settings
if (StartUp.bWii)
{
int source;
for (unsigned int i = 0; i < MAX_WIIMOTES; ++i)
{
controls_section->Get(fmt::format("WiimoteSource{}", i), &source, -1);
if (source != -1 && WiimoteCommon::GetSource(i) != WiimoteSource(source) &&
WiimoteSource(source) >= WiimoteSource::None &&
WiimoteSource(source) <= WiimoteSource::Real)
{
config_cache.bSetWiimoteSource[i] = true;
WiimoteCommon::SetSource(i, WiimoteSource(source));
}
}
controls_section->Get("WiimoteSourceBB", &source, -1);
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;
WiimoteCommon::SetSource(WIIMOTE_BALANCE_BOARD, WiimoteSource(source));
}
}
}

// Movie settings
if (Movie::IsPlayingInput() && Movie::IsConfigSaved())
{
Expand Down Expand Up @@ -313,7 +224,6 @@ void RestoreConfig()
Config::RemoveLayer(Config::LayerType::GlobalGame);
Config::RemoveLayer(Config::LayerType::LocalGame);
SConfig::GetInstance().ResetRunningGameMetadata();
config_cache.RestoreConfig(&SConfig::GetInstance());
}

} // namespace BootManager
2 changes: 2 additions & 0 deletions Source/Core/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ add_library(core
Config/SYSCONFSettings.h
Config/UISettings.cpp
Config/UISettings.h
Config/WiimoteSettings.cpp
Config/WiimoteSettings.h
ConfigLoaders/BaseConfigLoader.cpp
ConfigLoaders/BaseConfigLoader.h
ConfigLoaders/GameConfigLoader.cpp
Expand Down
30 changes: 30 additions & 0 deletions Source/Core/Core/Config/WiimoteSettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2022 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "Core/Config/WiimoteSettings.h"

#include "Core/HW/Wiimote.h"

namespace Config
{
const Info<WiimoteSource> WIIMOTE_1_SOURCE{{System::WiiPad, "Wiimote1", "Source"},
WiimoteSource::Emulated};
const Info<WiimoteSource> WIIMOTE_2_SOURCE{{System::WiiPad, "Wiimote2", "Source"},
WiimoteSource::None};
const Info<WiimoteSource> WIIMOTE_3_SOURCE{{System::WiiPad, "Wiimote3", "Source"},
WiimoteSource::None};
const Info<WiimoteSource> WIIMOTE_4_SOURCE{{System::WiiPad, "Wiimote4", "Source"},
WiimoteSource::None};
const Info<WiimoteSource> WIIMOTE_BB_SOURCE{{System::WiiPad, "BalanceBoard", "Source"},
WiimoteSource::None};

const Info<WiimoteSource>& GetInfoForWiimoteSource(int index)
{
static const std::array<const Info<WiimoteSource>*, 5> infos{
&WIIMOTE_1_SOURCE, &WIIMOTE_2_SOURCE, &WIIMOTE_3_SOURCE,
&WIIMOTE_4_SOURCE, &WIIMOTE_BB_SOURCE,
};
return *infos[index];
}

} // namespace Config
20 changes: 20 additions & 0 deletions Source/Core/Core/Config/WiimoteSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2022 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "Common/Config/Config.h"

enum class WiimoteSource;

namespace Config
{
extern const Info<WiimoteSource> WIIMOTE_1_SOURCE;
extern const Info<WiimoteSource> WIIMOTE_2_SOURCE;
extern const Info<WiimoteSource> WIIMOTE_3_SOURCE;
extern const Info<WiimoteSource> WIIMOTE_4_SOURCE;
extern const Info<WiimoteSource> WIIMOTE_BB_SOURCE;

const Info<WiimoteSource>& GetInfoForWiimoteSource(int index);

} // namespace Config
6 changes: 6 additions & 0 deletions Source/Core/Core/ConfigLoaders/GameConfigLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "Core/Config/MainSettings.h"
#include "Core/Config/SYSCONFSettings.h"
#include "Core/Config/WiimoteSettings.h"
#include "Core/ConfigLoaders/IsSettingSaveable.h"

namespace ConfigLoaders
Expand Down Expand Up @@ -79,6 +80,11 @@ static const INIToLocationMap& GetINIToLocationMap()
{{"Controls", "PadType1"}, {Config::GetInfoForSIDevice(1).GetLocation()}},
{{"Controls", "PadType2"}, {Config::GetInfoForSIDevice(2).GetLocation()}},
{{"Controls", "PadType3"}, {Config::GetInfoForSIDevice(3).GetLocation()}},
{{"Controls", "WiimoteSource0"}, {Config::WIIMOTE_1_SOURCE.GetLocation()}},
{{"Controls", "WiimoteSource1"}, {Config::WIIMOTE_2_SOURCE.GetLocation()}},
{{"Controls", "WiimoteSource2"}, {Config::WIIMOTE_3_SOURCE.GetLocation()}},
{{"Controls", "WiimoteSource3"}, {Config::WIIMOTE_4_SOURCE.GetLocation()}},
{{"Controls", "WiimoteSourceBB"}, {Config::WIIMOTE_BB_SOURCE.GetLocation()}},
};
return ini_to_location;
}
Expand Down
9 changes: 9 additions & 0 deletions Source/Core/Core/ConfigLoaders/IsSettingSaveable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Core/Config/GraphicsSettings.h"
#include "Core/Config/MainSettings.h"
#include "Core/Config/UISettings.h"
#include "Core/Config/WiimoteSettings.h"

namespace ConfigLoaders
{
Expand Down Expand Up @@ -129,6 +130,14 @@ bool IsSettingSaveable(const Config::Location& config_location)
// UI.General

&Config::MAIN_USE_DISCORD_PRESENCE.GetLocation(),

// Wiimote

&Config::WIIMOTE_1_SOURCE.GetLocation(),
&Config::WIIMOTE_2_SOURCE.GetLocation(),
&Config::WIIMOTE_3_SOURCE.GetLocation(),
&Config::WIIMOTE_4_SOURCE.GetLocation(),
&Config::WIIMOTE_BB_SOURCE.GetLocation(),
};

return std::any_of(begin(s_setting_saveable), end(s_setting_saveable),
Expand Down
33 changes: 29 additions & 4 deletions Source/Core/Core/HW/Wiimote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

#include "Core/HW/Wiimote.h"

#include <optional>

#include "Common/ChunkFile.h"
#include "Common/CommonTypes.h"
#include "Common/Config/Config.h"

#include "Core/Config/WiimoteSettings.h"
#include "Core/ConfigManager.h"
#include "Core/Core.h"
#include "Core/HW/WiimoteEmu/WiimoteEmu.h"
Expand All @@ -23,16 +27,17 @@
// 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
namespace
{
static std::array<std::atomic<WiimoteSource>, MAX_BBMOTES> s_wiimote_sources;
static std::optional<size_t> s_config_callback_id = std::nullopt;

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

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

Expand All @@ -44,9 +49,19 @@ void SetSource(unsigned int index, WiimoteSource source)

WiimoteReal::HandleWiimoteSourceChange(index);

Core::RunAsCPUThread([index] { UpdateSource(index); });
Core::RunAsCPUThread([index] { WiimoteCommon::UpdateSource(index); });
}

void RefreshConfig()
{
for (int i = 0; i < MAX_BBMOTES; ++i)
OnSourceChanged(i, Config::Get(Config::GetInfoForWiimoteSource(i)));
}

} // namespace

namespace WiimoteCommon
{
void UpdateSource(unsigned int index)
{
const auto bluetooth = WiiUtils::GetBluetoothEmuDevice();
Expand Down Expand Up @@ -144,6 +159,12 @@ void Shutdown()
s_config.ClearControllers();

WiimoteReal::Stop();

if (s_config_callback_id)
{
Config::RemoveConfigChangedCallback(*s_config_callback_id);
s_config_callback_id = std::nullopt;
}
}

void Initialize(InitializeMode init_mode)
Expand All @@ -158,6 +179,10 @@ void Initialize(InitializeMode init_mode)

LoadConfig();

if (!s_config_callback_id)
s_config_callback_id = Config::AddConfigChangedCallback(RefreshConfig);
RefreshConfig();

WiimoteReal::Initialize(init_mode);

// Reload Wiimotes with our settings
Expand Down Expand Up @@ -191,7 +216,7 @@ void DoState(PointerWrap& p)
{
for (int i = 0; i < MAX_BBMOTES; ++i)
{
const WiimoteSource source = WiimoteCommon::GetSource(i);
const WiimoteSource source = GetSource(i);
auto state_wiimote_source = u8(source);
p.Do(state_wiimote_source);

Expand Down
5 changes: 0 additions & 5 deletions Source/Core/Core/HW/Wiimote.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ namespace WiimoteCommon
{
class HIDWiimote;

WiimoteSource GetSource(unsigned int index);
void SetSource(unsigned int index, WiimoteSource source);

// Used to reconnect WiimoteDevice instance to HID source.
// Must be run from CPU thread.
void UpdateSource(unsigned int index);
Expand Down Expand Up @@ -108,6 +105,4 @@ void Resume();
void Pause();
void Refresh();

void LoadSettings();

} // namespace WiimoteReal
Loading

0 comments on commit 0c78167

Please sign in to comment.