Input: Use global variables for pad modifications

This commit is contained in:
Megamouse 2020-06-24 17:01:48 +02:00
parent 9679ae68cb
commit 76faaf43f7
8 changed files with 51 additions and 80 deletions

View file

@ -160,8 +160,6 @@ static bool ds3_input_to_pad(const u32 port_no, be_t<u16>& digital_buttons, be_t
const auto handler = pad::get_current_handler(); const auto handler = pad::get_current_handler();
const PadInfo& rinfo = handler->GetInfo();
auto& pads = handler->GetPads(); auto& pads = handler->GetPads();
auto pad = pads[port_no]; 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(); const auto handler = pad::get_current_handler();
auto& pads = handler->GetPads(); auto& pads = handler->GetPads();
const PadInfo& rinfo = handler->GetInfo();
auto pad = pads[port_no]; auto pad = pads[port_no];
if (!(pad->m_port_status & CELL_PAD_STATUS_CONNECTED)) 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<u16>& digital_buttons, b
std::scoped_lock lock(handler->mutex); std::scoped_lock lock(handler->mutex);
if (!handler || mouse_no >= handler->GetMice().size()) if (mouse_no >= handler->GetMice().size())
{ {
return false; return false;
} }
@ -316,7 +311,7 @@ static bool mouse_pos_to_gem_image_state(const u32 mouse_no, vm::ptr<CellGemImag
std::scoped_lock lock(handler->mutex); std::scoped_lock lock(handler->mutex);
if (!gem_image_state || !handler || mouse_no >= handler->GetMice().size()) if (!gem_image_state || mouse_no >= handler->GetMice().size())
{ {
return false; return false;
} }
@ -346,7 +341,7 @@ static bool mouse_pos_to_gem_state(const u32 mouse_no, vm::ptr<CellGemState>& ge
std::scoped_lock lock(handler->mutex); 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; return false;
} }

View file

@ -38,8 +38,6 @@ struct EmuCallbacks
std::function<void()> on_stop; std::function<void()> on_stop;
std::function<void()> on_ready; std::function<void()> on_ready;
std::function<void(bool)> exit; // (force_quit) close RPCS3 std::function<void(bool)> exit; // (force_quit) close RPCS3
std::function<void(const std::string&)> reset_pads;
std::function<void(bool)> enable_pads;
std::function<void(s32, s32)> handle_taskbar_progress; // (type, value) type: 0 for reset, 1 for increment, 2 for set_limit std::function<void(s32, s32)> handle_taskbar_progress; // (type, value) type: 0 for reset, 1 for increment, 2 for set_limit
std::function<void()> init_kb_handler; std::function<void()> init_kb_handler;
std::function<void()> init_mouse_handler; std::function<void()> init_mouse_handler;

View file

@ -21,6 +21,9 @@ namespace pad
atomic_t<pad_thread*> g_current = nullptr; atomic_t<pad_thread*> g_current = nullptr;
std::recursive_mutex g_pad_mutex; std::recursive_mutex g_pad_mutex;
std::string g_title_id; std::string g_title_id;
atomic_t<bool> g_reset{false};
atomic_t<bool> g_enabled{false};
atomic_t<bool> g_active;
} }
struct pad_setting struct pad_setting
@ -43,7 +46,7 @@ pad_thread::pad_thread(void *_curthread, void *_curwindow, std::string_view titl
pad_thread::~pad_thread() pad_thread::~pad_thread()
{ {
pad::g_current = nullptr; pad::g_current = nullptr;
active = false; pad::g_active = false;
thread->join(); thread->join();
handlers.clear(); 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) void pad_thread::SetIntercepted(bool intercepted)
{ {
if (intercepted) if (intercepted)
@ -193,16 +185,16 @@ void pad_thread::SetIntercepted(bool intercepted)
void pad_thread::ThreadFunc() void pad_thread::ThreadFunc()
{ {
active = true; pad::g_active = true;
while (active) while (pad::g_active)
{ {
if (!is_enabled) if (!pad::g_enabled)
{ {
std::this_thread::sleep_for(1ms); std::this_thread::sleep_for(1ms);
continue; continue;
} }
if (reset && reset.exchange(false)) if (pad::g_reset && pad::g_reset.exchange(false))
{ {
Init(); Init();
} }

View file

@ -20,8 +20,6 @@ public:
auto& GetPads() { return m_pads; } auto& GetPads() { return m_pads; }
void SetRumble(const u32 pad, u8 largeMotor, bool smallMotor); void SetRumble(const u32 pad, u8 largeMotor, bool smallMotor);
void Init(); void Init();
void Reset(std::string_view title_id);
void SetEnabled(bool enabled);
void SetIntercepted(bool intercepted); void SetIntercepted(bool intercepted);
s32 AddLddPad(); s32 AddLddPad();
@ -41,9 +39,6 @@ protected:
PadInfo m_info{ 0, 0, false }; PadInfo m_info{ 0, 0, false };
std::array<std::shared_ptr<Pad>, CELL_PAD_MAX_PORT_NUM> m_pads; std::array<std::shared_ptr<Pad>, CELL_PAD_MAX_PORT_NUM> m_pads;
atomic_t<bool> active{ false };
atomic_t<bool> reset{ false };
atomic_t<bool> is_enabled{ true };
std::shared_ptr<std::thread> thread; std::shared_ptr<std::thread> thread;
u32 num_ldd_pad = 0; u32 num_ldd_pad = 0;
@ -54,12 +49,31 @@ namespace pad
extern atomic_t<pad_thread*> g_current; extern atomic_t<pad_thread*> g_current;
extern std::recursive_mutex g_pad_mutex; extern std::recursive_mutex g_pad_mutex;
extern std::string g_title_id; extern std::string g_title_id;
extern atomic_t<bool> g_enabled;
extern atomic_t<bool> g_reset;
extern atomic_t<bool> 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()); 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) static inline void SetIntercepted(bool intercepted)
{ {
std::lock_guard lock(g_pad_mutex); std::lock_guard lock(g_pad_mutex);

View file

@ -57,16 +57,7 @@ EmuCallbacks main_application::CreateCallbacks()
{ {
EmuCallbacks callbacks; EmuCallbacks callbacks;
callbacks.reset_pads = [this](const std::string& title_id) callbacks.init_kb_handler = [this]()
{
pad::get_current_handler()->Reset(title_id);
};
callbacks.enable_pads = [this](bool enable)
{
pad::get_current_handler()->SetEnabled(enable);
};
callbacks.init_kb_handler = [=, this]()
{ {
switch (keyboard_handler type = g_cfg.io.keyboard) 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) switch (mouse_handler type = g_cfg.io.mouse)
{ {

View file

@ -19,6 +19,7 @@
#include "Utilities/types.h" #include "Utilities/types.h"
#include "Utilities/lockless.h" #include "Utilities/lockless.h"
#include "util/yaml.hpp" #include "util/yaml.hpp"
#include "Input/pad_thread.h"
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
@ -1006,28 +1007,13 @@ void game_list_frame::ShowContextMenu(const QPoint &pos)
}); });
connect(pad_configure, &QAction::triggered, [=, this]() connect(pad_configure, &QAction::triggered, [=, this]()
{ {
if (!Emu.IsStopped())
{
Emu.GetCallbacks().enable_pads(false);
}
pad_settings_dialog dlg(this, &current_game); pad_settings_dialog dlg(this, &current_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) if (dlg.exec() == QDialog::Accepted && !gameinfo->hasCustomPadConfig)
{ {
gameinfo->hasCustomPadConfig = true; gameinfo->hasCustomPadConfig = true;
ShowCustomConfigIcon(gameinfo); ShowCustomConfigIcon(gameinfo);
} }
if (!Emu.IsStopped())
{
Emu.GetCallbacks().enable_pads(true);
}
}); });
connect(hide_serial, &QAction::triggered, [serial, this](bool checked) 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) if (!Emu.IsStopped() && Emu.GetTitleID() == title_id)
{ {
Emu.GetCallbacks().enable_pads(false); pad::set_enabled(false);
Emu.GetCallbacks().reset_pads(title_id); pad::reset(title_id);
Emu.GetCallbacks().enable_pads(true); pad::set_enabled(true);
} }
game_list_log.notice("Removed pad configuration directory: %s", config_dir); game_list_log.notice("Removed pad configuration directory: %s", config_dir);
return true; return true;

View file

@ -1488,24 +1488,8 @@ void main_window::CreateConnects()
auto open_pad_settings = [this] auto open_pad_settings = [this]
{ {
if (!Emu.IsStopped())
{
Emu.GetCallbacks().enable_pads(false);
}
pad_settings_dialog dlg(this); pad_settings_dialog dlg(this);
connect(&dlg, &QDialog::finished, [this](int/* result*/)
{
if (Emu.IsStopped())
{
return;
}
Emu.GetCallbacks().reset_pads(Emu.GetTitleID());
});
dlg.exec(); dlg.exec();
if (!Emu.IsStopped())
{
Emu.GetCallbacks().enable_pads(true);
}
}; };
connect(ui->confPadsAct, &QAction::triggered, open_pad_settings); connect(ui->confPadsAct, &QAction::triggered, open_pad_settings);

View file

@ -58,6 +58,8 @@ inline bool CreateConfigFile(const QString& dir, const QString& name)
pad_settings_dialog::pad_settings_dialog(QWidget *parent, const GameInfo *game) pad_settings_dialog::pad_settings_dialog(QWidget *parent, const GameInfo *game)
: QDialog(parent), ui(new Ui::pad_settings_dialog) : QDialog(parent), ui(new Ui::pad_settings_dialog)
{ {
pad::set_enabled(false);
ui->setupUi(this); ui->setupUi(this);
// load input config // load input config
@ -207,6 +209,13 @@ pad_settings_dialog::pad_settings_dialog(QWidget *parent, const GameInfo *game)
pad_settings_dialog::~pad_settings_dialog() pad_settings_dialog::~pad_settings_dialog()
{ {
delete ui; delete ui;
if (!Emu.IsStopped())
{
pad::reset(Emu.GetTitleID());
}
pad::set_enabled(true);
} }
void pad_settings_dialog::InitButtons() 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())) if (index >= 0 && !Emu.IsStopped() && (m_title_id.empty() || m_title_id == Emu.GetTitleID()))
{ {
std::lock_guard lock(pad::g_pad_mutex); std::lock_guard lock(pad::g_pad_mutex);
const auto handler = pad::get_current_handler(); if (const auto handler = pad::get_current_handler(true))
return handler && handler->GetPads().at(index)->ldd; {
return handler->GetPads().at(index)->ldd;
}
} }
return false; return false;