Merge pull request #3555 from mathieui/gcadapter_payload

Fix a bug in GCAdapter
This commit is contained in:
Pierre Bourdon 2016-01-25 10:42:28 +01:00
commit 7b4e965465
1 changed files with 8 additions and 4 deletions

View File

@ -32,7 +32,7 @@ static std::mutex s_mutex;
static u8 s_controller_payload[37]; static u8 s_controller_payload[37];
static u8 s_controller_payload_swap[37]; static u8 s_controller_payload_swap[37];
static int s_controller_payload_size = 0; static std::atomic<int> s_controller_payload_size = {0};
static std::thread s_adapter_thread; static std::thread s_adapter_thread;
static Common::Flag s_adapter_thread_running; static Common::Flag s_adapter_thread_running;
@ -56,13 +56,15 @@ static u64 s_last_init = 0;
static void Read() static void Read()
{ {
int payload_size = 0;
while (s_adapter_thread_running.IsSet()) while (s_adapter_thread_running.IsSet())
{ {
libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap, sizeof(s_controller_payload_swap), &s_controller_payload_size, 16); libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap, sizeof(s_controller_payload_swap), &payload_size, 16);
{ {
std::lock_guard<std::mutex> lk(s_mutex); std::lock_guard<std::mutex> lk(s_mutex);
std::swap(s_controller_payload_swap, s_controller_payload); std::swap(s_controller_payload_swap, s_controller_payload);
s_controller_payload_size.store(payload_size);
} }
Common::YieldCPU(); Common::YieldCPU();
@ -359,16 +361,18 @@ void Input(int chan, GCPadStatus* pad)
if (s_handle == nullptr || !s_detected) if (s_handle == nullptr || !s_detected)
return; return;
int payload_size = 0;
u8 controller_payload_copy[37]; u8 controller_payload_copy[37];
{ {
std::lock_guard<std::mutex> lk(s_mutex); std::lock_guard<std::mutex> lk(s_mutex);
std::copy(std::begin(s_controller_payload), std::end(s_controller_payload), std::begin(controller_payload_copy)); std::copy(std::begin(s_controller_payload), std::end(s_controller_payload), std::begin(controller_payload_copy));
payload_size = s_controller_payload_size.load();
} }
if (s_controller_payload_size != sizeof(controller_payload_copy) || controller_payload_copy[0] != LIBUSB_DT_HID) if (payload_size != sizeof(controller_payload_copy) || controller_payload_copy[0] != LIBUSB_DT_HID)
{ {
INFO_LOG(SERIALINTERFACE, "error reading payload (size: %d, type: %02x)", s_controller_payload_size, controller_payload_copy[0]); INFO_LOG(SERIALINTERFACE, "error reading payload (size: %d, type: %02x)", payload_size, controller_payload_copy[0]);
Reset(); Reset();
} }
else else