From 76faaf43f7a77fff5f428d3ad93c8f70dd130175 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Wed, 24 Jun 2020 17:01:48 +0200 Subject: [PATCH] Input: Use global variables for pad modifications --- rpcs3/Emu/Cell/Modules/cellGem.cpp | 11 +++-------- rpcs3/Emu/System.h | 2 -- rpcs3/Input/pad_thread.cpp | 24 ++++++++---------------- rpcs3/Input/pad_thread.h | 26 ++++++++++++++++++++------ rpcs3/main_application.cpp | 13 ++----------- rpcs3/rpcs3qt/game_list_frame.cpp | 24 +++++------------------- rpcs3/rpcs3qt/main_window.cpp | 16 ---------------- rpcs3/rpcs3qt/pad_settings_dialog.cpp | 15 +++++++++++++-- 8 files changed, 51 insertions(+), 80 deletions(-) diff --git a/rpcs3/Emu/Cell/Modules/cellGem.cpp b/rpcs3/Emu/Cell/Modules/cellGem.cpp index d21a633a89..d0767df755 100644 --- a/rpcs3/Emu/Cell/Modules/cellGem.cpp +++ b/rpcs3/Emu/Cell/Modules/cellGem.cpp @@ -160,8 +160,6 @@ static bool ds3_input_to_pad(const u32 port_no, be_t& digital_buttons, be_t const auto handler = pad::get_current_handler(); - const PadInfo& rinfo = handler->GetInfo(); - auto& pads = handler->GetPads(); auto pad = pads[port_no]; @@ -249,9 +247,6 @@ static bool ds3_input_to_ext(const u32 port_no, CellGemExtPortData& ext) const auto handler = pad::get_current_handler(); auto& pads = handler->GetPads(); - - const PadInfo& rinfo = handler->GetInfo(); - auto pad = pads[port_no]; if (!(pad->m_port_status & CELL_PAD_STATUS_CONNECTED)) @@ -283,7 +278,7 @@ static bool mouse_input_to_pad(const u32 mouse_no, be_t& digital_buttons, b std::scoped_lock lock(handler->mutex); - if (!handler || mouse_no >= handler->GetMice().size()) + if (mouse_no >= handler->GetMice().size()) { return false; } @@ -316,7 +311,7 @@ static bool mouse_pos_to_gem_image_state(const u32 mouse_no, vm::ptrmutex); - if (!gem_image_state || !handler || mouse_no >= handler->GetMice().size()) + if (!gem_image_state || mouse_no >= handler->GetMice().size()) { return false; } @@ -346,7 +341,7 @@ static bool mouse_pos_to_gem_state(const u32 mouse_no, vm::ptr& ge std::scoped_lock lock(handler->mutex); - if (!gem_state || !handler || mouse_no >= handler->GetMice().size()) + if (!gem_state || mouse_no >= handler->GetMice().size()) { return false; } diff --git a/rpcs3/Emu/System.h b/rpcs3/Emu/System.h index 11d7304099..10d37f9a14 100644 --- a/rpcs3/Emu/System.h +++ b/rpcs3/Emu/System.h @@ -38,8 +38,6 @@ struct EmuCallbacks std::function on_stop; std::function on_ready; std::function exit; // (force_quit) close RPCS3 - std::function reset_pads; - std::function enable_pads; std::function handle_taskbar_progress; // (type, value) type: 0 for reset, 1 for increment, 2 for set_limit std::function init_kb_handler; std::function init_mouse_handler; diff --git a/rpcs3/Input/pad_thread.cpp b/rpcs3/Input/pad_thread.cpp index 4cc2049d9c..1438c21087 100644 --- a/rpcs3/Input/pad_thread.cpp +++ b/rpcs3/Input/pad_thread.cpp @@ -21,6 +21,9 @@ namespace pad atomic_t g_current = nullptr; std::recursive_mutex g_pad_mutex; std::string g_title_id; + atomic_t g_reset{false}; + atomic_t g_enabled{false}; + atomic_t g_active; } struct pad_setting @@ -43,7 +46,7 @@ pad_thread::pad_thread(void *_curthread, void *_curwindow, std::string_view titl pad_thread::~pad_thread() { pad::g_current = nullptr; - active = false; + pad::g_active = false; thread->join(); handlers.clear(); @@ -167,17 +170,6 @@ void pad_thread::SetRumble(const u32 pad, u8 largeMotor, bool smallMotor) } } -void pad_thread::Reset(std::string_view title_id) -{ - pad::g_title_id = title_id; - reset = active.load(); -} - -void pad_thread::SetEnabled(bool enabled) -{ - is_enabled = enabled; -} - void pad_thread::SetIntercepted(bool intercepted) { if (intercepted) @@ -193,16 +185,16 @@ void pad_thread::SetIntercepted(bool intercepted) void pad_thread::ThreadFunc() { - active = true; - while (active) + pad::g_active = true; + while (pad::g_active) { - if (!is_enabled) + if (!pad::g_enabled) { std::this_thread::sleep_for(1ms); continue; } - if (reset && reset.exchange(false)) + if (pad::g_reset && pad::g_reset.exchange(false)) { Init(); } diff --git a/rpcs3/Input/pad_thread.h b/rpcs3/Input/pad_thread.h index 4fe2bf2bf0..e6ce9f053d 100644 --- a/rpcs3/Input/pad_thread.h +++ b/rpcs3/Input/pad_thread.h @@ -20,8 +20,6 @@ public: auto& GetPads() { return m_pads; } void SetRumble(const u32 pad, u8 largeMotor, bool smallMotor); void Init(); - void Reset(std::string_view title_id); - void SetEnabled(bool enabled); void SetIntercepted(bool intercepted); s32 AddLddPad(); @@ -41,9 +39,6 @@ protected: PadInfo m_info{ 0, 0, false }; std::array, CELL_PAD_MAX_PORT_NUM> m_pads; - atomic_t active{ false }; - atomic_t reset{ false }; - atomic_t is_enabled{ true }; std::shared_ptr thread; u32 num_ldd_pad = 0; @@ -54,12 +49,31 @@ namespace pad extern atomic_t g_current; extern std::recursive_mutex g_pad_mutex; extern std::string g_title_id; + extern atomic_t g_enabled; + extern atomic_t g_reset; + extern atomic_t g_active; - static inline class pad_thread* get_current_handler() + static inline class pad_thread* get_current_handler(bool relaxed = false) { + if (relaxed) + { + return g_current.load(); + } + return verify(HERE, g_current.load()); } + static inline void set_enabled(bool enabled) + { + g_enabled = enabled; + } + + static inline void reset(std::string_view title_id) + { + g_title_id = title_id; + g_reset = g_active.load(); + } + static inline void SetIntercepted(bool intercepted) { std::lock_guard lock(g_pad_mutex); diff --git a/rpcs3/main_application.cpp b/rpcs3/main_application.cpp index a327247742..7eedd444f4 100644 --- a/rpcs3/main_application.cpp +++ b/rpcs3/main_application.cpp @@ -57,16 +57,7 @@ EmuCallbacks main_application::CreateCallbacks() { EmuCallbacks callbacks; - callbacks.reset_pads = [this](const std::string& title_id) - { - pad::get_current_handler()->Reset(title_id); - }; - callbacks.enable_pads = [this](bool enable) - { - pad::get_current_handler()->SetEnabled(enable); - }; - - callbacks.init_kb_handler = [=, this]() + callbacks.init_kb_handler = [this]() { switch (keyboard_handler type = g_cfg.io.keyboard) { @@ -86,7 +77,7 @@ EmuCallbacks main_application::CreateCallbacks() } }; - callbacks.init_mouse_handler = [=, this]() + callbacks.init_mouse_handler = [this]() { switch (mouse_handler type = g_cfg.io.mouse) { diff --git a/rpcs3/rpcs3qt/game_list_frame.cpp b/rpcs3/rpcs3qt/game_list_frame.cpp index fbd09c13e6..a76467d983 100644 --- a/rpcs3/rpcs3qt/game_list_frame.cpp +++ b/rpcs3/rpcs3qt/game_list_frame.cpp @@ -19,6 +19,7 @@ #include "Utilities/types.h" #include "Utilities/lockless.h" #include "util/yaml.hpp" +#include "Input/pad_thread.h" #include #include @@ -1006,28 +1007,13 @@ void game_list_frame::ShowContextMenu(const QPoint &pos) }); connect(pad_configure, &QAction::triggered, [=, this]() { - if (!Emu.IsStopped()) - { - Emu.GetCallbacks().enable_pads(false); - } pad_settings_dialog dlg(this, ¤t_game); - connect(&dlg, &QDialog::finished, [this](int/* result*/) - { - if (Emu.IsStopped()) - { - return; - } - Emu.GetCallbacks().reset_pads(Emu.GetTitleID()); - }); + if (dlg.exec() == QDialog::Accepted && !gameinfo->hasCustomPadConfig) { gameinfo->hasCustomPadConfig = true; ShowCustomConfigIcon(gameinfo); } - if (!Emu.IsStopped()) - { - Emu.GetCallbacks().enable_pads(true); - } }); connect(hide_serial, &QAction::triggered, [serial, this](bool checked) { @@ -1250,9 +1236,9 @@ bool game_list_frame::RemoveCustomPadConfiguration(const std::string& title_id, } if (!Emu.IsStopped() && Emu.GetTitleID() == title_id) { - Emu.GetCallbacks().enable_pads(false); - Emu.GetCallbacks().reset_pads(title_id); - Emu.GetCallbacks().enable_pads(true); + pad::set_enabled(false); + pad::reset(title_id); + pad::set_enabled(true); } game_list_log.notice("Removed pad configuration directory: %s", config_dir); return true; diff --git a/rpcs3/rpcs3qt/main_window.cpp b/rpcs3/rpcs3qt/main_window.cpp index de390fa09b..9f5ab975e8 100644 --- a/rpcs3/rpcs3qt/main_window.cpp +++ b/rpcs3/rpcs3qt/main_window.cpp @@ -1488,24 +1488,8 @@ void main_window::CreateConnects() auto open_pad_settings = [this] { - if (!Emu.IsStopped()) - { - Emu.GetCallbacks().enable_pads(false); - } pad_settings_dialog dlg(this); - connect(&dlg, &QDialog::finished, [this](int/* result*/) - { - if (Emu.IsStopped()) - { - return; - } - Emu.GetCallbacks().reset_pads(Emu.GetTitleID()); - }); dlg.exec(); - if (!Emu.IsStopped()) - { - Emu.GetCallbacks().enable_pads(true); - } }; connect(ui->confPadsAct, &QAction::triggered, open_pad_settings); diff --git a/rpcs3/rpcs3qt/pad_settings_dialog.cpp b/rpcs3/rpcs3qt/pad_settings_dialog.cpp index 37918ab722..94c9681e53 100644 --- a/rpcs3/rpcs3qt/pad_settings_dialog.cpp +++ b/rpcs3/rpcs3qt/pad_settings_dialog.cpp @@ -58,6 +58,8 @@ inline bool CreateConfigFile(const QString& dir, const QString& name) pad_settings_dialog::pad_settings_dialog(QWidget *parent, const GameInfo *game) : QDialog(parent), ui(new Ui::pad_settings_dialog) { + pad::set_enabled(false); + ui->setupUi(this); // load input config @@ -207,6 +209,13 @@ pad_settings_dialog::pad_settings_dialog(QWidget *parent, const GameInfo *game) pad_settings_dialog::~pad_settings_dialog() { delete ui; + + if (!Emu.IsStopped()) + { + pad::reset(Emu.GetTitleID()); + } + + pad::set_enabled(true); } void pad_settings_dialog::InitButtons() @@ -1451,8 +1460,10 @@ bool pad_settings_dialog::GetIsLddPad(int index) const if (index >= 0 && !Emu.IsStopped() && (m_title_id.empty() || m_title_id == Emu.GetTitleID())) { std::lock_guard lock(pad::g_pad_mutex); - const auto handler = pad::get_current_handler(); - return handler && handler->GetPads().at(index)->ldd; + if (const auto handler = pad::get_current_handler(true)) + { + return handler->GetPads().at(index)->ldd; + } } return false;