From ad21f48a671031374e6ab21d70ceb46ee7e08eb6 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Mon, 16 Dec 2019 16:46:43 +1000 Subject: [PATCH] Fix Android build after controller changes --- .../app/src/cpp/android_host_interface.cpp | 43 ++++++++----------- android/app/src/cpp/android_host_interface.h | 5 --- src/core/settings.cpp | 32 +++++++------- src/core/settings.h | 8 ++-- src/core/system.cpp | 41 ++++++------------ src/core/types.h | 6 +++ src/duckstation/sdl_host_interface.cpp | 11 ++--- 7 files changed, 58 insertions(+), 88 deletions(-) diff --git a/android/app/src/cpp/android_host_interface.cpp b/android/app/src/cpp/android_host_interface.cpp index a020472e8..7d145f3f0 100644 --- a/android/app/src/cpp/android_host_interface.cpp +++ b/android/app/src/cpp/android_host_interface.cpp @@ -57,7 +57,8 @@ AndroidHostInterface::AndroidHostInterface(jobject java_object) : m_java_object( { m_settings.SetDefaults(); m_settings.bios_path = "/sdcard/PSX/BIOS/scph1001.bin"; - m_settings.memory_card_a_path = "/sdcard/PSX/memory_card_a.mcd"; + m_settings.controller_types[0] = ControllerType::DigitalController; + m_settings.memory_card_paths[0] = "/sdcard/PSX/memory_card_1.mcd"; m_settings.cpu_execution_mode = CPUExecutionMode::Recompiler; //m_settings.cpu_execution_mode = CPUExecutionMode::CachedInterpreter; //m_settings.gpu_renderer = GPURenderer::Software; @@ -333,19 +334,18 @@ void AndroidHostInterface::SurfaceChanged(ANativeWindow* window, int format, int void AndroidHostInterface::SetControllerType(u32 index, std::string_view type_name) { + ControllerType type = Settings::ParseControllerTypeName(std::string(type_name).c_str()).value_or(ControllerType::None); + if (!IsEmulationThreadRunning()) { - m_controller_type_names[index] = type_name; + m_settings.controller_types[index] = type; return; } - std::string type_name_copy(type_name); - RunOnEmulationThread([this, index, type_name_copy]() { - Log_InfoPrintf("Changing controller slot %d to %s", index, type_name_copy.c_str()); - m_controller_type_names[index] = std::move(type_name_copy); - - m_controllers[index] = Controller::Create(m_controller_type_names[index]); - m_system->SetController(index, m_controllers[index]); + RunOnEmulationThread([this, index, type]() { + Log_InfoPrintf("Changing controller slot %d to %s", index, Settings::GetControllerTypeName(type)); + m_settings.controller_types[index] = type; + m_system->UpdateControllers(); }, false); } @@ -355,27 +355,14 @@ void AndroidHostInterface::SetControllerButtonState(u32 index, s32 button_code, return; RunOnEmulationThread([this, index, button_code, pressed]() { - if (!m_controllers[index]) + Controller* controller = m_system->GetController(index); + if (!controller) return; - m_controllers[index]->SetButtonState(button_code, pressed); + controller->SetButtonState(button_code, pressed); }, false); } -void AndroidHostInterface::ConnectControllers() -{ - for (u32 i = 0; i < NUM_CONTROLLERS; i++) - { - if (m_controller_type_names[i].empty()) - continue; - - Log_InfoPrintf("Connecting controller '%s' to port %u", m_controller_type_names[i].c_str(), i); - m_controllers[i] = Controller::Create(m_controller_type_names[i]); - if (m_controllers[i]) - m_system->SetController(i, m_controllers[i]); - } -} - extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) { Log::GetInstance().SetDebugOutputParams(true, nullptr, LOGLEVEL_DEV); @@ -491,7 +478,11 @@ DEFINE_JNI_ARGS_METHOD(void, AndroidHostInterface_setControllerButtonState, jobj DEFINE_JNI_ARGS_METHOD(jint, AndroidHostInterface_getControllerButtonCode, jobject unused, jstring controller_type, jstring button_name) { - std::optional code = Controller::GetButtonCodeByName(JStringToString(env, controller_type), JStringToString(env, button_name)); + std::optional type = Settings::ParseControllerTypeName(JStringToString(env, controller_type).c_str()); + if (!type) + return -1; + + std::optional code = Controller::GetButtonCodeByName(type.value(), JStringToString(env, button_name)); return code.value_or(-1); } diff --git a/android/app/src/cpp/android_host_interface.h b/android/app/src/cpp/android_host_interface.h index c18115928..94a41e45a 100644 --- a/android/app/src/cpp/android_host_interface.h +++ b/android/app/src/cpp/android_host_interface.h @@ -48,8 +48,6 @@ private: void DrawFPSWindow(); - void ConnectControllers() override; - jobject m_java_object = {}; std::mutex m_callback_mutex; @@ -59,7 +57,4 @@ private: std::atomic_bool m_emulation_thread_stop_request{false}; std::atomic_bool m_emulation_thread_start_result{false}; Event m_emulation_thread_started; - - std::array m_controller_type_names; - std::array, NUM_CONTROLLERS> m_controllers; }; diff --git a/src/core/settings.cpp b/src/core/settings.cpp index 2d1c0c93e..4d71d8994 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -33,11 +33,11 @@ void Settings::SetDefaults() bios_patch_tty_enable = false; bios_patch_fast_boot = false; - controller_1_type = ControllerType::DigitalController; - controller_2_type = ControllerType::None; + controller_types[0] = ControllerType::DigitalController; + controller_types[1] = ControllerType::None; - memory_card_1_path = "memory_card_1.mcd"; - memory_card_2_path.clear(); + memory_card_paths[0] = "memory_card_1.mcd"; + memory_card_paths[1].clear(); } void Settings::Load(const char* filename) @@ -72,13 +72,13 @@ void Settings::Load(const char* filename) bios_patch_tty_enable = ini.GetBoolValue("BIOS", "PatchTTYEnable", true); bios_patch_fast_boot = ini.GetBoolValue("BIOS", "PatchFastBoot", false); - controller_1_type = ParseControllerTypeName(ini.GetValue("Ports", "Controller1Type", "DigitalController")) + controller_types[0] = ParseControllerTypeName(ini.GetValue("Ports", "Controller1Type", "DigitalController")) .value_or(ControllerType::DigitalController); - controller_2_type = + controller_types[1] = ParseControllerTypeName(ini.GetValue("Ports", "Controller2Type", "None")).value_or(ControllerType::None); - memory_card_1_path = ini.GetValue("Ports", "MemoryCard1Path", "memory_card_1.mcd"); - memory_card_2_path = ini.GetValue("Ports", "MemoryCard2Path", ""); + memory_card_paths[0] = ini.GetValue("Ports", "MemoryCard1Path", "memory_card_1.mcd"); + memory_card_paths[1] = ini.GetValue("Ports", "MemoryCard2Path", ""); } bool Settings::Save(const char* filename) const @@ -110,23 +110,23 @@ bool Settings::Save(const char* filename) const ini.SetBoolValue("BIOS", "PatchTTYEnable", bios_patch_tty_enable); ini.SetBoolValue("BIOS", "PatchFastBoot", bios_patch_fast_boot); - if (controller_1_type != ControllerType::None) - ini.SetValue("Ports", "Controller1Type", GetControllerTypeName(controller_1_type)); + if (controller_types[0] != ControllerType::None) + ini.SetValue("Ports", "Controller1Type", GetControllerTypeName(controller_types[0])); else ini.DeleteValue("Ports", "Controller1Type", nullptr); - if (controller_2_type != ControllerType::None) - ini.SetValue("Ports", "Controller2Type", GetControllerTypeName(controller_2_type)); + if (controller_types[1] != ControllerType::None) + ini.SetValue("Ports", "Controller2Type", GetControllerTypeName(controller_types[1])); else ini.DeleteValue("Ports", "Controller2Type", nullptr); - if (!memory_card_1_path.empty()) - ini.SetValue("Ports", "MemoryCard1Path", memory_card_1_path.c_str()); + if (!memory_card_paths[0].empty()) + ini.SetValue("Ports", "MemoryCard1Path", memory_card_paths[0].c_str()); else ini.DeleteValue("Ports", "MemoryCard1Path", nullptr); - if (!memory_card_2_path.empty()) - ini.SetValue("Ports", "MemoryCard2Path", memory_card_2_path.c_str()); + if (!memory_card_paths[1].empty()) + ini.SetValue("Ports", "MemoryCard2Path", memory_card_paths[1].c_str()); else ini.DeleteValue("Ports", "MemoryCard2Path", nullptr); diff --git a/src/core/settings.h b/src/core/settings.h index 923a3ff81..b2a5d77b0 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -1,5 +1,6 @@ #pragma once #include "types.h" +#include #include #include @@ -45,11 +46,8 @@ struct Settings bool bios_patch_tty_enable = false; bool bios_patch_fast_boot = false; - ControllerType controller_1_type = ControllerType::None; - ControllerType controller_2_type = ControllerType::None; - - std::string memory_card_1_path; - std::string memory_card_2_path; + std::array controller_types{}; + std::array memory_card_paths{}; void SetDefaults(); void Load(const char* filename); diff --git a/src/core/system.cpp b/src/core/system.cpp index e9683c061..738809cd3 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -459,43 +459,28 @@ Controller* System::GetController(u32 slot) const void System::UpdateControllers() { - m_pad->SetController(0, nullptr); - m_pad->SetController(1, nullptr); - const Settings& settings = m_host_interface->GetSettings(); - if (settings.controller_1_type != ControllerType::None) - { - std::unique_ptr controller = Controller::Create(settings.controller_1_type); - if (controller) - m_pad->SetController(0, std::move(controller)); - } - - if (settings.controller_2_type != ControllerType::None) - { - std::unique_ptr controller = Controller::Create(settings.controller_2_type); - if (controller) - m_pad->SetController(1, std::move(controller)); + for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++) { + m_pad->SetController(i, nullptr); + + const ControllerType type = settings.controller_types[i]; + if (type != ControllerType::None) { + std::unique_ptr controller = Controller::Create(type); + if (controller) + m_pad->SetController(i, std::move(controller)); + } } } void System::UpdateMemoryCards() { - m_pad->SetMemoryCard(0, nullptr); - m_pad->SetMemoryCard(1, nullptr); - const Settings& settings = m_host_interface->GetSettings(); - if (!settings.memory_card_1_path.empty()) - { - std::unique_ptr card = MemoryCard::Open(this, settings.memory_card_1_path); - if (card) - m_pad->SetMemoryCard(0, std::move(card)); - } - - if (!settings.memory_card_2_path.empty()) + for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++) { - std::unique_ptr card = MemoryCard::Open(this, settings.memory_card_2_path); + m_pad->SetMemoryCard(i, nullptr); + std::unique_ptr card = MemoryCard::Open(this, settings.memory_card_paths[i]); if (card) - m_pad->SetMemoryCard(1, std::move(card)); + m_pad->SetMemoryCard(i, std::move(card)); } } diff --git a/src/core/types.h b/src/core/types.h index 132a9ba55..cace499aa 100644 --- a/src/core/types.h +++ b/src/core/types.h @@ -55,6 +55,12 @@ enum class ControllerType Count }; +enum : u32 +{ + NUM_CONTROLLER_AND_CARD_PORTS = 2 +}; + + enum : u32 { CPU_CODE_CACHE_PAGE_SIZE = 1024, diff --git a/src/duckstation/sdl_host_interface.cpp b/src/duckstation/sdl_host_interface.cpp index d84c873d3..5426da49a 100644 --- a/src/duckstation/sdl_host_interface.cpp +++ b/src/duckstation/sdl_host_interface.cpp @@ -1166,8 +1166,7 @@ void SDLHostInterface::DrawSettingsWindow() ImGui::Text("Controller:"); ImGui::SameLine(indent); - int controller_type = - static_cast((i == 0) ? m_settings.controller_1_type : m_settings.controller_2_type); + int controller_type = static_cast(m_settings.controller_types[i]); if (ImGui::Combo( "##controller_type", &controller_type, [](void*, int index, const char** out_text) { @@ -1176,11 +1175,7 @@ void SDLHostInterface::DrawSettingsWindow() }, nullptr, static_cast(ControllerType::Count))) { - if (i == 0) - m_settings.controller_1_type = static_cast(controller_type); - else - m_settings.controller_2_type = static_cast(controller_type); - + m_settings.controller_types[i] = static_cast(controller_type); settings_changed = true; if (m_system) { @@ -1193,7 +1188,7 @@ void SDLHostInterface::DrawSettingsWindow() ImGui::Text("Memory Card Path:"); ImGui::SameLine(indent); - std::string* path_ptr = (i == 0) ? &m_settings.memory_card_1_path : &m_settings.memory_card_2_path; + std::string* path_ptr = &m_settings.memory_card_paths[i]; if (DrawFileChooser(TinyString::FromFormat("##memcard_%c_path", 'a' + i), path_ptr)) { settings_changed = true;