Skip to content

Commit

Permalink
input: use static hid singleton for init and exit
Browse files Browse the repository at this point in the history
  • Loading branch information
Megamouse committed Sep 23, 2024
1 parent 31b133b commit 18a99a7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
72 changes: 54 additions & 18 deletions rpcs3/Input/hid_pad_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,58 @@

LOG_CHANNEL(hid_log, "HID");

static std::mutex s_hid_mutex; // hid_pad_handler is created by pad_thread and pad_settings_dialog
static u8 s_hid_instances{0};
struct hid_instance
{
public:
hid_instance() = default;
~hid_instance()
{
std::lock_guard lock(m_hid_mutex);

// Only exit HIDAPI once on exit. HIDAPI uses a global state internally...
if (m_initialized)
{
hid_log.notice("Exiting HIDAPI...");

if (hid_exit() != 0)
{
hid_log.error("hid_exit failed!");
}
}
}

static hid_instance& get_instance()
{
static hid_instance instance {};
return instance;
}

bool initialize()
{
std::lock_guard lock(m_hid_mutex);

// Only init HIDAPI once. HIDAPI uses a global state internally...
if (m_initialized)
{
return true;
}

hid_log.notice("Initializing HIDAPI ...");

if (hid_init() != 0)
{
hid_log.fatal("hid_init error");
return false;
}

m_initialized = true;
return true;
}

private:
bool m_initialized = false;
std::mutex m_hid_mutex;
};

void HidDevice::close()
{
Expand All @@ -33,8 +83,6 @@ template <class Device>
hid_pad_handler<Device>::hid_pad_handler(pad_handler type, std::vector<id_pair> ids)
: PadHandlerBase(type), m_ids(std::move(ids))
{
std::scoped_lock lock(s_hid_mutex);
ensure(s_hid_instances++ < 255);
};

template <class Device>
Expand All @@ -54,17 +102,6 @@ hid_pad_handler<Device>::~hid_pad_handler()
controller.second->close();
}
}

std::scoped_lock lock(s_hid_mutex);
ensure(s_hid_instances-- > 0);
if (s_hid_instances == 0)
{
// Call hid_exit after all hid_pad_handlers are finished
if (hid_exit() != 0)
{
hid_log.error("hid_exit failed!");
}
}
}

template <class Device>
Expand All @@ -73,9 +110,8 @@ bool hid_pad_handler<Device>::Init()
if (m_is_init)
return true;

const int res = hid_init();
if (res != 0)
fmt::throw_exception("%s hidapi-init error.threadproc", m_type);
if (!hid_instance::get_instance().initialize())
return false;

#if defined(__APPLE__)
hid_darwin_set_open_exclusive(0);
Expand Down
9 changes: 7 additions & 2 deletions rpcs3/Input/sdl_pad_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ struct sdl_instance
}
}

static sdl_instance& get_instance()
{
static sdl_instance instance {};
return instance;
}

bool initialize()
{
// Only init SDL once. SDL uses a global state internally...
Expand Down Expand Up @@ -256,8 +262,7 @@ bool sdl_pad_handler::Init()
if (m_is_init)
return true;

static sdl_instance s_sdl_instance {};
if (!s_sdl_instance.initialize())
if (!sdl_instance::get_instance().initialize())
return false;

if (g_cfg.io.load_sdl_mappings)
Expand Down

0 comments on commit 18a99a7

Please sign in to comment.