summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/GCAdapter.cpp
diff options
context:
space:
mode:
authorLéo Lam <leo@innovatetechnologi.es>2017-01-15 20:50:26 +0100
committerLéo Lam <leo@innovatetechnologi.es>2017-02-05 11:36:48 +0100
commitc8a6dc6c23a5efe99b4bf937003ba3f2f7f8f200 (patch)
tree44e1e1fac10808bad10e20a72c18c54777e5f1bf /Source/Core/InputCommon/GCAdapter.cpp
parent73e55ccf4472db8c85255dd97ebb3c7596d5201f (diff)
Use a single libusb context
libusb on Windows is limited to only a single context. Trying to open more than one can cause device enumerations to fail randomly. libusb is thread-safe and we don't use the manual polling support (with `poll()`) so this should be safe.
Diffstat (limited to 'Source/Core/InputCommon/GCAdapter.cpp')
-rw-r--r--Source/Core/InputCommon/GCAdapter.cpp27
1 files changed, 11 insertions, 16 deletions
diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp
index e3dcb44739..fae7a81055 100644
--- a/Source/Core/InputCommon/GCAdapter.cpp
+++ b/Source/Core/InputCommon/GCAdapter.cpp
@@ -4,9 +4,11 @@
#include <algorithm>
#include <libusb.h>
+#include <memory>
#include <mutex>
#include "Common/Flag.h"
+#include "Common/LibusbContext.h"
#include "Common/Logging/Log.h"
#include "Common/Thread.h"
#include "Core/ConfigManager.h"
@@ -50,7 +52,7 @@ static Common::Flag s_adapter_detect_thread_running;
static std::function<void(void)> s_detect_callback;
static bool s_libusb_driver_not_supported = false;
-static libusb_context* s_libusb_context = nullptr;
+static std::shared_ptr<libusb_context> s_libusb_context;
#if defined(__FreeBSD__) && __FreeBSD__ >= 11
static bool s_libusb_hotplug_enabled = true;
#else
@@ -116,8 +118,8 @@ static void ScanThreadFunc()
if (s_libusb_hotplug_enabled)
{
if (libusb_hotplug_register_callback(
- s_libusb_context, (libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
- LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
+ s_libusb_context.get(), (libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
+ LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
LIBUSB_HOTPLUG_ENUMERATE, 0x057e, 0x0337, LIBUSB_HOTPLUG_MATCH_ANY, HotplugCallback,
nullptr, &s_hotplug_handle) != LIBUSB_SUCCESS)
s_libusb_hotplug_enabled = false;
@@ -131,7 +133,7 @@ static void ScanThreadFunc()
if (s_libusb_hotplug_enabled)
{
static timeval tv = {0, 500000};
- libusb_handle_events_timeout(s_libusb_context, &tv);
+ libusb_handle_events_timeout(s_libusb_context.get(), &tv);
}
else
{
@@ -168,11 +170,9 @@ void Init()
s_libusb_driver_not_supported = false;
- int ret = libusb_init(&s_libusb_context);
-
- if (ret)
+ s_libusb_context = LibusbContext::Get();
+ if (!s_libusb_context)
{
- ERROR_LOG(SERIALINTERFACE, "libusb_init failed with error: %d", ret);
s_libusb_driver_not_supported = true;
Shutdown();
}
@@ -203,7 +203,7 @@ void StopScanThread()
static void Setup()
{
libusb_device** list;
- ssize_t cnt = libusb_get_device_list(s_libusb_context, &list);
+ ssize_t cnt = libusb_get_device_list(s_libusb_context.get(), &list);
for (int i = 0; i < MAX_SI_CHANNELS; i++)
{
@@ -335,16 +335,11 @@ void Shutdown()
StopScanThread();
#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102
if (s_libusb_hotplug_enabled)
- libusb_hotplug_deregister_callback(s_libusb_context, s_hotplug_handle);
+ libusb_hotplug_deregister_callback(s_libusb_context.get(), s_hotplug_handle);
#endif
Reset();
- if (s_libusb_context)
- {
- libusb_exit(s_libusb_context);
- s_libusb_context = nullptr;
- }
-
+ s_libusb_context.reset();
s_libusb_driver_not_supported = false;
}