From 5dd969da05ddee98807a2d2a024a9598eefb95c4 Mon Sep 17 00:00:00 2001 From: Jun Wu Date: Sat, 2 Feb 2019 01:30:41 -0800 Subject: [PATCH] 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. --- BetterJoyForCemu/Joycon.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/BetterJoyForCemu/Joycon.cs b/BetterJoyForCemu/Joycon.cs index 4ed51b6..f5412ea 100644 --- a/BetterJoyForCemu/Joycon.cs +++ b/BetterJoyForCemu/Joycon.cs @@ -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. } - ++attempts; } }