diff --git a/DS4Windows/DS4Control/ControlService.cs b/DS4Windows/DS4Control/ControlService.cs index 225a2bc..a136a23 100644 --- a/DS4Windows/DS4Control/ControlService.cs +++ b/DS4Windows/DS4Control/ControlService.cs @@ -516,7 +516,20 @@ namespace DS4Windows if (removingStatus) { CurrentState[ind].Battery = PreviousState[ind].Battery = 0; // Reset for the next connection's initial status change. - x360Bus.Unplug(ind); + if (!useDInputOnly[ind]) + { + bool unplugResult = x360Bus.Unplug(ind); + int xinputIndex = x360Bus.FirstController + ind; + if (unplugResult) + { + LogDebug("X360 Controller # " + xinputIndex + " unplugged"); + } + else + { + LogDebug("X360 Controller # " + xinputIndex + " failed to unplug"); + } + } + string removed = Properties.Resources.ControllerWasRemoved.Replace("*Mac address*", (ind + 1).ToString()); if (device.getBattery() <= 20 && device.getConnectionType() == ConnectionType.BT && !device.isCharging()) diff --git a/DS4Windows/DS4Library/DS4Device.cs b/DS4Windows/DS4Library/DS4Device.cs index bea8b97..c6d3f0f 100644 --- a/DS4Windows/DS4Library/DS4Device.cs +++ b/DS4Windows/DS4Library/DS4Device.cs @@ -352,6 +352,8 @@ namespace DS4Windows } private SynchronizationContext uiContext = null; + private Queue eventQueue = new Queue(); + private object eventQueueLock = new object(); public DS4Device(HidDevice hidDevice) { @@ -479,7 +481,8 @@ namespace DS4Windows } else { - return hDevice.WriteAsyncOutputReportViaInterrupt(outputReport); + return hDevice.WriteOutputReportViaInterrupt(outputReport, READ_STREAM_TIMEOUT); + //return hDevice.WriteAsyncOutputReportViaInterrupt(outputReport); } } @@ -567,14 +570,15 @@ namespace DS4Windows if (conType == ConnectionType.BT) { //HidDevice.ReadStatus res = hDevice.ReadFile(btInputReport); - HidDevice.ReadStatus res = hDevice.ReadAsyncWithFileStream(btInputReport, READ_STREAM_TIMEOUT); + //HidDevice.ReadStatus res = hDevice.ReadAsyncWithFileStream(btInputReport, READ_STREAM_TIMEOUT); + HidDevice.ReadStatus res = hDevice.ReadWithFileStream(btInputReport); + //HidDevice.ReadStatus res = hDevice.ReadFileOverlapped(btInputReport, READ_STREAM_TIMEOUT); if (res == HidDevice.ReadStatus.Success) { Array.Copy(btInputReport, 2, inputReport, 0, inputReport.Length); } else { - if (res == HidDevice.ReadStatus.WaitTimedOut) { Log.LogToGui(Mac.ToString() + " disconnected due to timeout", true); @@ -595,14 +599,15 @@ namespace DS4Windows }), null); return; - } } else { //HidDevice.ReadStatus res = hDevice.ReadFile(inputReport); //Array.Clear(inputReport, 0, inputReport.Length); - HidDevice.ReadStatus res = hDevice.ReadAsyncWithFileStream(inputReport, READ_STREAM_TIMEOUT); + //HidDevice.ReadStatus res = hDevice.ReadAsyncWithFileStream(inputReport, READ_STREAM_TIMEOUT); + HidDevice.ReadStatus res = hDevice.ReadWithFileStream(inputReport); + //HidDevice.ReadStatus res = hDevice.ReadFileOverlapped(inputReport, READ_STREAM_TIMEOUT); if (res != HidDevice.ReadStatus.Success) { if (res == HidDevice.ReadStatus.WaitTimedOut) @@ -797,6 +802,18 @@ namespace DS4Windows error = currerror; cState.CopyTo(pState); + + /*lock (eventQueueLock) + { + Action tempAct = null; + //while (eventQueue.TryDequeue(out tempAct)) + for (int actInd = 0, actLen = eventQueue.Count; actInd < actLen; actInd++) + { + tempAct = eventQueue.Dequeue(); + tempAct.Invoke(); + } + } + */ } } @@ -1117,5 +1134,13 @@ namespace DS4Windows { this.Report = null; } + + public void queueEvent(Action act) + { + lock (eventQueueLock) + { + eventQueue.Enqueue(act); + } + } } } diff --git a/DS4Windows/HidLibrary/HidDevice.cs b/DS4Windows/HidLibrary/HidDevice.cs index d2e8e33..d98e19d 100644 --- a/DS4Windows/HidLibrary/HidDevice.cs +++ b/DS4Windows/HidLibrary/HidDevice.cs @@ -242,6 +242,25 @@ namespace DS4Windows } } + public ReadStatus ReadWithFileStream(byte[] inputBuffer) + { + try + { + if (fileStream.Read(inputBuffer, 0, inputBuffer.Length) > 0) + { + return ReadStatus.Success; + } + else + { + return ReadStatus.NoDataRead; + } + } + catch (Exception) + { + return ReadStatus.ReadError; + } + } + public ReadStatus ReadWithFileStream(byte[] inputBuffer, int timeout) { try