From bfdb81edd059c002c3028177b91a8d98538a0601 Mon Sep 17 00:00:00 2001 From: Travis Nickles Date: Sat, 21 Apr 2018 16:44:49 -0500 Subject: [PATCH] Trim USB output report generation. Only grab audio volume when output report data changes. Possibly related to issue #261 --- DS4Windows/DS4Library/DS4Device.cs | 57 ++++++++++++++++++------------ 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/DS4Windows/DS4Library/DS4Device.cs b/DS4Windows/DS4Library/DS4Device.cs index 90fc833..e89dd5d 100644 --- a/DS4Windows/DS4Library/DS4Device.cs +++ b/DS4Windows/DS4Library/DS4Device.cs @@ -1115,6 +1115,8 @@ namespace DS4Windows lock (outReportBuffer) { + bool output = outputPendCount > 0, change = false; + if (usingBT) { outReportBuffer[0] = 0x11; @@ -1128,6 +1130,12 @@ namespace DS4Windows outReportBuffer[10] = ligtBarColor.blue; // blue outReportBuffer[11] = ledFlashOn; // flash on duration outReportBuffer[12] = ledFlashOff; // flash off duration + + fixed (byte* byteR = outputReport, byteB = outReportBuffer) + { + for (int i = 0, arlen = 13; !change && i < arlen; i++) + change = byteR[i] != byteB[i]; + } } else { @@ -1141,7 +1149,14 @@ namespace DS4Windows outReportBuffer[8] = ligtBarColor.blue; // blue outReportBuffer[9] = ledFlashOn; // flash on duration outReportBuffer[10] = ledFlashOff; // flash off duration - if (audio != null) + + fixed (byte* byteR = outputReport, byteB = outReportBuffer) + { + for (int i = 0, arlen = 11; !change && i < arlen; i++) + change = byteR[i] != byteB[i]; + } + + if (change && audio != null) { // Headphone volume levels outReportBuffer[19] = outReportBuffer[20] = @@ -1156,36 +1171,32 @@ namespace DS4Windows outputRumble = false; outputPendCount = 3; - if (usingBT) + if (change) { - Monitor.Enter(outputReport); - outReportBuffer.CopyTo(outputReport, 0); - } - - try - { - if (!writeOutput()) + if (usingBT) { - int winError = Marshal.GetLastWin32Error(); - quitOutputThread = true; + Monitor.Enter(outputReport); + outReportBuffer.CopyTo(outputReport, 0); } - } - catch { } // If it's dead already, don't worry about it. - if (usingBT) - { - Monitor.Exit(outputReport); + try + { + if (!writeOutput()) + { + int winError = Marshal.GetLastWin32Error(); + quitOutputThread = true; + } + } + catch { } // If it's dead already, don't worry about it. + + if (usingBT) + { + Monitor.Exit(outputReport); + } } } else { - bool output = outputPendCount > 0, change = false; - fixed (byte* byteR = outputReport, byteB = outReportBuffer) - { - for (int i = 0, arlen = outputReport.Length; !change && i < arlen; i++) - change = byteR[i] != byteB[i]; - } - //for (int i = 0, arlen = outputReport.Length; !change && i < arlen; i++) // change = outputReport[i] != outReportBuffer[i];