From c4b38869abd819b5b39ab23214bea95ae7916e16 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Fri, 5 Aug 2022 14:49:32 +0200 Subject: [PATCH] Input: fix pad initialization --- rpcs3/Input/pad_thread.cpp | 27 +++++++++++++++++++++------ rpcs3/Input/pad_thread.h | 7 ++++--- rpcs3/main_application.cpp | 2 +- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/rpcs3/Input/pad_thread.cpp b/rpcs3/Input/pad_thread.cpp index cccafad18a..01e7357cb1 100644 --- a/rpcs3/Input/pad_thread.cpp +++ b/rpcs3/Input/pad_thread.cpp @@ -28,6 +28,7 @@ namespace pad atomic_t g_current = nullptr; shared_mutex g_pad_mutex; std::string g_title_id; + atomic_t g_started{false}; atomic_t g_reset{false}; atomic_t g_enabled{true}; } @@ -40,11 +41,11 @@ struct pad_setting s32 ldd_handle = -1; }; -pad_thread::pad_thread(void *_curthread, void *_curwindow, std::string_view title_id) : curthread(_curthread), curwindow(_curwindow) +pad_thread::pad_thread(void* curthread, void* curwindow, std::string_view title_id) : m_curthread(curthread), m_curwindow(curwindow) { pad::g_title_id = title_id; pad::g_current = this; - pad::g_reset = true; + pad::g_started = false; } pad_thread::~pad_thread() @@ -138,8 +139,8 @@ void pad_thread::Init() { case pad_handler::keyboard: keyptr = std::make_shared(); - keyptr->moveToThread(static_cast(curthread)); - keyptr->SetTargetWindow(static_cast(curwindow)); + keyptr->moveToThread(static_cast(m_curthread)); + keyptr->SetTargetWindow(static_cast(m_curwindow)); cur_pad_handler = keyptr; break; case pad_handler::ds3: @@ -216,13 +217,19 @@ void pad_thread::SetIntercepted(bool intercepted) void pad_thread::operator()() { Init(); + pad::g_reset = false; + pad::g_started = true; + + bool mode_changed = true; atomic_t pad_mode{g_cfg.io.pad_mode.get()}; std::vector>>> threads; const auto stop_threads = [&threads]() { + input_log.notice("Stopping pad threads..."); + for (auto& thread : threads) { if (thread) @@ -233,6 +240,8 @@ void pad_thread::operator()() } } threads.clear(); + + input_log.notice("Pad threads stopped"); }; const auto start_threads = [this, &threads, &pad_mode]() @@ -242,13 +251,15 @@ void pad_thread::operator()() return; } + input_log.notice("Starting pad threads..."); + for (const auto& handler : handlers) { if (handler.first == pad_handler::null) { continue; } - + threads.push_back(std::make_unique>>(fmt::format("%s Thread", handler.second->m_type), [&handler = handler.second, &pad_mode]() { while (thread_ctrl::state() != thread_state::aborting) @@ -265,6 +276,8 @@ void pad_thread::operator()() } })); } + + input_log.notice("Pad threads started"); }; while (thread_ctrl::state() != thread_state::aborting) @@ -277,11 +290,13 @@ void pad_thread::operator()() // Update variables const bool needs_reset = pad::g_reset && pad::g_reset.exchange(false); - const bool mode_changed = pad_mode != pad_mode.exchange(g_cfg.io.pad_mode.get()); + mode_changed |= pad_mode != pad_mode.exchange(g_cfg.io.pad_mode.get()); // Reset pad handlers if necessary if (needs_reset || mode_changed) { + mode_changed = false; + stop_threads(); if (needs_reset) diff --git a/rpcs3/Input/pad_thread.h b/rpcs3/Input/pad_thread.h index 9d08e61799..069464aa40 100644 --- a/rpcs3/Input/pad_thread.h +++ b/rpcs3/Input/pad_thread.h @@ -17,7 +17,7 @@ class PadHandlerBase; class pad_thread { public: - pad_thread(void* _curthread, void* _curwindow, std::string_view title_id); // void * instead of QThread * and QWindow * because of include in emucore + pad_thread(void* curthread, void* curwindow, std::string_view title_id); // void * instead of QThread * and QWindow * because of include in emucore pad_thread(const pad_thread&) = delete; pad_thread& operator=(const pad_thread&) = delete; ~pad_thread(); @@ -45,8 +45,8 @@ protected: std::map> handlers; // Used for pad_handler::keyboard - void *curthread; - void *curwindow; + void* m_curthread = nullptr; + void* m_curwindow = nullptr; PadInfo m_info{ 0, 0, false }; std::array, CELL_PAD_MAX_PORT_NUM> m_pads; @@ -61,6 +61,7 @@ namespace pad extern std::string g_title_id; extern atomic_t g_enabled; extern atomic_t g_reset; + extern atomic_t g_started; static inline class pad_thread* get_current_handler(bool relaxed = false) { diff --git a/rpcs3/main_application.cpp b/rpcs3/main_application.cpp index 6549f13b40..bb7ae11538 100644 --- a/rpcs3/main_application.cpp +++ b/rpcs3/main_application.cpp @@ -106,7 +106,7 @@ EmuCallbacks main_application::CreateCallbacks() callbacks.init_pad_handler = [this](std::string_view title_id) { ensure(g_fxo->init>(get_thread(), m_game_window, title_id)); - while (pad::g_reset) std::this_thread::yield(); + while (!pad::g_started) std::this_thread::yield(); }; callbacks.get_audio = []() -> std::shared_ptr