Read Joycon inputs in a non-blocking way

This makes sure ReceiveRaw() won't block forever, so Poll() has chance
to SendRumble().

Practically, it seems the Joycons frequently send inputs so it's not
necessary, at least for the bluetooth version I got.
This commit is contained in:
Jun Wu 2019-02-02 01:30:41 -08:00
parent 85f77cb91c
commit 5dd969da05

View file

@ -411,9 +411,9 @@ namespace BetterJoyForCemu {
private byte ts_en;
private int ReceiveRaw() {
if (handle == IntPtr.Zero) return -2;
HIDapi.hid_set_nonblocking(handle, 0);
HIDapi.hid_set_nonblocking(handle, 1);
byte[] raw_buf = new byte[report_len];
int ret = HIDapi.hid_read(handle, raw_buf, new UIntPtr(report_len));
int ret = HIDapi.hid_read_timeout(handle, raw_buf, new UIntPtr(report_len), 5000);
if (ret > 0) {
// Process packets as soon as they come
for (int n = 0; n < 3; n++) {
@ -473,11 +473,15 @@ namespace BetterJoyForCemu {
DebugPrint("Connection lost. Is the Joy-Con connected?", DebugType.ALL);
break;
} else {
} else if (a < 0) {
// An error on read.
//form.AppendTextBox("Pause 5ms");
Thread.Sleep((Int32)5);
}
++attempts;
} else if (a == 0) {
// The non-blocking read timed out. No need to sleep.
// No need to increase attempts because it's not an error.
}
}
}