From d37e0709c0dfcd9a64be3495dcf3136cb9f26587 Mon Sep 17 00:00:00 2001 From: Travis Nickles Date: Mon, 1 May 2017 02:29:19 -0700 Subject: [PATCH] More log messages. Tweak DisconnectBT. Other minor changes. --- DS4Windows/DS4Control/ControlSerivce.cs | 38 +++++++++++++++------ DS4Windows/DS4Forms/DS4Form.cs | 8 ++++- DS4Windows/DS4Library/DS4Device.cs | 45 ++++++++++++++++--------- DS4Windows/DS4Library/DS4Devices.cs | 4 ++- 4 files changed, 66 insertions(+), 29 deletions(-) diff --git a/DS4Windows/DS4Control/ControlSerivce.cs b/DS4Windows/DS4Control/ControlSerivce.cs index 0aa3f3f..ad3cc70 100644 --- a/DS4Windows/DS4Control/ControlSerivce.cs +++ b/DS4Windows/DS4Control/ControlSerivce.cs @@ -106,6 +106,8 @@ namespace DS4Windows if (showlog) LogDebug(Properties.Resources.Starting); + LogDebug("Connection to Scp Virtual Bus established"); + DS4Devices.isExclusiveMode = getUseExclusiveMode(); if (showlog) { @@ -120,9 +122,10 @@ namespace DS4Windows //int ind = 0; DS4LightBar.defaultLight = false; //foreach (DS4Device device in devices) + for (int i = 0, devCount = devices.Count(); i < devCount; i++) { - DS4Device device = devices.ElementAt(i); + DS4Device device = devices.ElementAt(i); if (showlog) LogDebug(Properties.Resources.FoundController + device.getMacAddress() + " (" + device.getConnectionType() + ")"); @@ -135,10 +138,14 @@ namespace DS4Windows device.LightBarColor = getMainColor(i); if (!getDInputOnly(i)) + { + LogDebug("Plugging in X360 Controller #" + (x360Bus.FirstController + i)); x360Bus.Plugin(i); + } device.Report += this.On_Report; TouchPadOn(i, device); + device.StartUpdate(); //string filename = ProfilePath[ind]; //ind++; if (showlog) @@ -197,11 +204,11 @@ namespace DS4Windows { if (DS4Controllers[i] != null) { - if (DCBTatStop && !DS4Controllers[i].isCharging() && showlog) + if (DCBTatStop && !DS4Controllers[i].isCharging()) { if (DS4Controllers[i].getConnectionType() == ConnectionType.BT) { - DS4Controllers[i].DisconnectBT(); + DS4Controllers[i].DisconnectBT(true); } else if (DS4Controllers[i].getConnectionType() == ConnectionType.SONYWA) { @@ -216,6 +223,7 @@ namespace DS4Windows DS4LightBar.updateLightBar(DS4Controllers[i], i, CurrentState[i], ExposedState[i], touchPad[i]); System.Threading.Thread.Sleep(50); } + CurrentState[i].Battery = PreviousState[i].Battery = 0; // Reset for the next connection's initial status change. x360Bus.Unplug(i); anyUnplugged = true; @@ -250,15 +258,17 @@ namespace DS4Windows { // Do first run check for Quick Charge checks. Needed so old device will // be removed before performing another controller scan - for (int i = 0, devlen = DS4Controllers.Length; i < devlen; i++) + if (getQuickCharge()) { - DS4Device device = DS4Controllers[i]; - if (device != null) + for (int i = 0, devlen = DS4Controllers.Length; i < devlen; i++) { - if (getQuickCharge() && device.getConnectionType() == ConnectionType.BT && - device.isCharging()) + DS4Device device = DS4Controllers[i]; + if (device != null) { - device.DisconnectBT(); + if (device.getConnectionType() == ConnectionType.BT && device.isCharging()) + { + device.DisconnectBT(); + } } } } @@ -268,7 +278,7 @@ namespace DS4Windows //foreach (DS4Device device in devices) for (int i = 0, devlen = devices.Count(); i < devlen; i++) { - DS4Device device = devices.ElementAt(i); + DS4Device device = devices.ElementAt(i); if (device.isDisconnectingStatus()) continue; @@ -302,8 +312,13 @@ namespace DS4Windows device.LightBarColor = getMainColor(Index); device.Report += this.On_Report; if (!getDInputOnly(Index)) + { + LogDebug("Plugging in X360 Controller #" + (x360Bus.FirstController + Index)); x360Bus.Plugin(Index); + } + TouchPadOn(Index, device); + device.StartUpdate(); //string filename = Path.GetFileName(ProfilePath[Index]); if (System.IO.File.Exists(appdatapath + "\\Profiles\\" + ProfilePath[Index] + ".xml")) @@ -664,7 +679,7 @@ namespace DS4Windows } } - public void EasterTime(int ind) + /*public void EasterTime(int ind) { DS4State cState = CurrentState[ind]; DS4StateExposed eState = ExposedState[ind]; @@ -743,6 +758,7 @@ namespace DS4Windows else if (!pb) buttonsdown[ind] = false; } + */ public string GetInputkeys(int ind) { diff --git a/DS4Windows/DS4Forms/DS4Form.cs b/DS4Windows/DS4Forms/DS4Form.cs index 234d963..1213873 100644 --- a/DS4Windows/DS4Forms/DS4Form.cs +++ b/DS4Windows/DS4Forms/DS4Form.cs @@ -922,6 +922,7 @@ namespace DS4Windows else if (btnStartStop.Text == Properties.Resources.StopText) { + blankControllerTab(); Program.rootHub.Stop(log); hotkeysTimer.Stop(); btnStartStop.Text = Properties.Resources.StartText; @@ -938,7 +939,7 @@ namespace DS4Windows bool skipHotplug = false; protected override void WndProc(ref Message m) { - if (runHotPlug && !skipHotplug) + if (runHotPlug) { try { @@ -1933,12 +1934,14 @@ namespace DS4Windows e.Cancel = true; return; } + if (cBCloseMini.Checked && !contextclose) { this.WindowState = FormWindowState.Minimized; e.Cancel = true; return; } + if (systemShutdown) // Reset the variable because the user might cancel the // shutdown. @@ -1946,6 +1949,7 @@ namespace DS4Windows systemShutdown = false; DS4LightBar.shuttingdown = true; } + if (oldsize == new System.Drawing.Size(0, 0)) { FormWidth = this.Width; @@ -1956,9 +1960,11 @@ namespace DS4Windows FormWidth = oldsize.Width; FormHeight = oldsize.Height; } + if (!String.IsNullOrEmpty(appdatapath)) { Save(); + blankControllerTab(); Program.rootHub.Stop(); } } diff --git a/DS4Windows/DS4Library/DS4Device.cs b/DS4Windows/DS4Library/DS4Device.cs index 640dd9a..efd75db 100644 --- a/DS4Windows/DS4Library/DS4Device.cs +++ b/DS4Windows/DS4Library/DS4Device.cs @@ -727,7 +727,7 @@ namespace DS4Windows { if (conType == ConnectionType.BT) { - if (DisconnectBT()) + if (DisconnectBT(true)) return; // all done } else if (conType == ConnectionType.SONYWA) @@ -837,7 +837,7 @@ namespace DS4Windows } } - public bool DisconnectBT() + public bool DisconnectBT(bool callRemoval = false) { if (Mac != null) { @@ -855,32 +855,40 @@ namespace DS4Windows long lbtAddr = BitConverter.ToInt64(btAddr, 0); - NativeMethods.BLUETOOTH_FIND_RADIO_PARAMS p = new NativeMethods.BLUETOOTH_FIND_RADIO_PARAMS(); - p.dwSize = Marshal.SizeOf(typeof(NativeMethods.BLUETOOTH_FIND_RADIO_PARAMS)); - IntPtr searchHandle = NativeMethods.BluetoothFindFirstRadio(ref p, ref btHandle); - int bytesReturned = 0; bool success = false; - while (!success && btHandle != IntPtr.Zero) + // Wait for output report to be written + lock (outputReport) { - success = NativeMethods.DeviceIoControl(btHandle, IOCTL_BTH_DISCONNECT_DEVICE, ref lbtAddr, 8, IntPtr.Zero, 0, ref bytesReturned, IntPtr.Zero); - NativeMethods.CloseHandle(btHandle); - if (!success) + NativeMethods.BLUETOOTH_FIND_RADIO_PARAMS p = new NativeMethods.BLUETOOTH_FIND_RADIO_PARAMS(); + p.dwSize = Marshal.SizeOf(typeof(NativeMethods.BLUETOOTH_FIND_RADIO_PARAMS)); + IntPtr searchHandle = NativeMethods.BluetoothFindFirstRadio(ref p, ref btHandle); + int bytesReturned = 0; + + while (!success && btHandle != IntPtr.Zero) { - if (!NativeMethods.BluetoothFindNextRadio(searchHandle, ref btHandle)) - btHandle = IntPtr.Zero; + success = NativeMethods.DeviceIoControl(btHandle, IOCTL_BTH_DISCONNECT_DEVICE, ref lbtAddr, 8, IntPtr.Zero, 0, ref bytesReturned, IntPtr.Zero); + NativeMethods.CloseHandle(btHandle); + if (!success) + { + if (!NativeMethods.BluetoothFindNextRadio(searchHandle, ref btHandle)) + btHandle = IntPtr.Zero; + } } + + NativeMethods.BluetoothFindRadioClose(searchHandle); + Console.WriteLine("Disconnect successful: " + success); } - NativeMethods.BluetoothFindRadioClose(searchHandle); - Console.WriteLine("Disconnect successful: " + success); success = true; // XXX return value indicates failure, but it still works? if (success) { IsDisconnecting = true; StopOutputUpdate(); - if (Removal != null) - Removal(this, EventArgs.Empty); + if (callRemoval) + { + Removal?.Invoke(this, EventArgs.Empty); + } } return success; @@ -1040,5 +1048,10 @@ namespace DS4Windows { return Mac; } + + public void removeReportHandlers() + { + this.Report = null; + } } } diff --git a/DS4Windows/DS4Library/DS4Devices.cs b/DS4Windows/DS4Library/DS4Devices.cs index 38b6276..2a51513 100644 --- a/DS4Windows/DS4Library/DS4Devices.cs +++ b/DS4Windows/DS4Library/DS4Devices.cs @@ -23,6 +23,7 @@ namespace DS4Windows { deviceInstanceId = deviceInstanceId.Remove(deviceInstanceId.Length - 1); } + return deviceInstanceId; } @@ -102,7 +103,6 @@ namespace DS4Windows ds4Device.Removal += On_Removal; Devices.Add(ds4Device.MacAddress, ds4Device); DevicePaths.Add(hDevice.DevicePath); - ds4Device.StartUpdate(); } } } @@ -216,6 +216,8 @@ namespace DS4Windows throw new Exception("Error enabling device, error code = " + Marshal.GetLastWin32Error()); } + System.Threading.Thread.Sleep(50); + NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet); } }