Skip to content

Commit

Permalink
Merge pull request #8933 from jordan-woyak/gcadapter-nyko
Browse files Browse the repository at this point in the history
InputCommon/GCAdapter: Fix offbrand "GCAdapters".
  • Loading branch information
lioncash committed Jul 8, 2020
2 parents cd51552 + f2fd5c7 commit a1195fb
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions Source/Core/InputCommon/GCAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ enum
};

// Current adapter status: detected/not detected/in error (holds the error code)
static int s_status = NO_ADAPTER_DETECTED;
static std::atomic<int> s_status = NO_ADAPTER_DETECTED;
static libusb_device_handle* s_handle = nullptr;
static u8 s_controller_type[SerialInterface::MAX_SI_CHANNELS] = {
ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE,
Expand All @@ -60,6 +60,7 @@ static Common::Event s_rumble_data_available;
static std::mutex s_init_mutex;
static std::thread s_adapter_detect_thread;
static Common::Flag s_adapter_detect_thread_running;
static Common::Event s_hotplug_event;

static std::function<void(void)> s_detect_callback;

Expand Down Expand Up @@ -125,15 +126,8 @@ static int HotplugCallback(libusb_context* ctx, libusb_device* dev, libusb_hotpl
{
if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED)
{
if (s_handle == nullptr && CheckDeviceAccess(dev))
{
std::lock_guard<std::mutex> lk(s_init_mutex);
AddGCAdapter(dev);
}
else if (s_status < 0 && s_detect_callback != nullptr)
{
s_detect_callback();
}
if (s_handle == nullptr)
s_hotplug_event.Set();
}
else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
{
Expand Down Expand Up @@ -175,17 +169,18 @@ static void ScanThreadFunc()
}
#endif

if (s_libusb_hotplug_enabled)
return;

while (s_adapter_detect_thread_running.IsSet())
{
if (s_handle == nullptr)
{
std::lock_guard<std::mutex> lk(s_init_mutex);
Setup();
}
Common::SleepCurrentThread(500);

if (s_libusb_hotplug_enabled)
s_hotplug_event.Wait();
else
Common::SleepCurrentThread(500);
}
NOTICE_LOG(SERIALINTERFACE, "GC Adapter scanning thread stopped");
}
Expand Down Expand Up @@ -228,6 +223,7 @@ void StopScanThread()
{
if (s_adapter_detect_thread_running.TestAndClear())
{
s_hotplug_event.Set();
s_adapter_detect_thread.join();
}
}
Expand Down Expand Up @@ -307,6 +303,10 @@ static bool CheckDeviceAccess(libusb_device* device)
ERROR_LOG(SERIALINTERFACE, "libusb_detach_kernel_driver failed with error: %d", ret);
}

ret = libusb_control_transfer(s_handle, 0x21, 11, 0x0001, 0, nullptr, 0, 10000);
if (ret < 0)
ERROR_LOG(SERIALINTERFACE, "libusb_control_transfer failed with error: %d", ret);

// this split is needed so that we don't avoid claiming the interface when
// detaching the kernel driver is successful
if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED)
Expand Down Expand Up @@ -431,9 +431,9 @@ GCPadStatus Input(int chan)
if (payload_size != sizeof(controller_payload_copy) ||
controller_payload_copy[0] != LIBUSB_DT_HID)
{
// This can occur for a few frames on initialization.
ERROR_LOG(SERIALINTERFACE, "error reading payload (size: %d, type: %02x)", payload_size,
controller_payload_copy[0]);
Reset();
}
else
{
Expand Down Expand Up @@ -575,7 +575,7 @@ bool IsDetected(const char** error_message)
}

if (error_message)
*error_message = libusb_strerror(static_cast<libusb_error>(s_status));
*error_message = libusb_strerror(static_cast<libusb_error>(s_status.load()));

return false;
}
Expand Down

0 comments on commit a1195fb

Please sign in to comment.