public Mouse(int deviceID, DS4Device d) { deviceNum = deviceID; dev = d; cursor = new MouseCursor(deviceNum); wheel = new MouseWheel(deviceNum); }
//enumerates ds4 controllers in the system public static void FindControllers() { lock (devices) { // https://github.com/boganhobo/DS4Windows/commit/051029095add79ac82b0d403c5d47dd4416212ad int[] pid = { 0xBA0, 0x5C4 }; IEnumerable<HidDevice> hDevices = HidDevices.Enumerate(0x054C, pid); // Sort Bluetooth first in case USB is also connected on the same controller. hDevices = hDevices.OrderBy(DS4Device.HidConnectionType); foreach (HidDevice hDevice in hDevices) { if (devicePaths.Contains(hDevice.DevicePath)) { continue; // BT/USB endpoint already open once } if (!hDevice.IsOpen) { hDevice.OpenDevice(IsExclusiveMode); // TODO in exclusive mode, try to hold both open when both are connected if (IsExclusiveMode && !hDevice.IsOpen) { hDevice.OpenDevice(false); } } if (hDevice.IsOpen) { if (devices.ContainsKey(hDevice.ReadSerial())) { continue; // happens when the BT endpoint already is open and the USB is plugged into the same host } DS4Device ds4Device = new DS4Device(hDevice); ds4Device.Removal += On_Removal; devices.Add(ds4Device.MacAddress, ds4Device); devicePaths.Add(hDevice.DevicePath); ds4Device.StartUpdate(); } } } }
public static void updateLightBar(DS4Device device, int deviceNum, DS4State cState, DS4StateExposed eState, Mouse tp) { DS4Color color; if (!defualtLight && !forcelight[deviceNum]) { if (Global.UseCustomLed[deviceNum]) { if (Global.LedAsBatteryIndicator[deviceNum]) { DS4Color fullColor = Global.CustomColor[deviceNum]; DS4Color lowColor = Global.LowColor[deviceNum]; color = Global.getTransitionedColor(lowColor, fullColor, device.Battery); } else { color = Global.CustomColor[deviceNum]; } } else { if (Global.Rainbow[deviceNum] > 0) { // Display rainbow DateTime now = DateTime.UtcNow; if (now >= oldnow + TimeSpan.FromMilliseconds(10)) //update by the millisecond that way it's a smooth transtion { oldnow = now; if (device.Charging) { counters[deviceNum] -= 1.5 * 3 / Global.Rainbow[deviceNum]; } else { counters[deviceNum] += 1.5 * 3 / Global.Rainbow[deviceNum]; } } if (counters[deviceNum] < 0) { counters[deviceNum] = 180000; } if (counters[deviceNum] > 180000) { counters[deviceNum] = 0; } color = Global.LedAsBatteryIndicator[deviceNum] ? HuetoRGB((float)counters[deviceNum] % 360, (byte)(2.55 * device.Battery)) : HuetoRGB((float)counters[deviceNum] % 360, 255); } else if (Global.LedAsBatteryIndicator[deviceNum]) { //if (device.Charging == false || device.Battery >= 100) // when charged, don't show the charging animation { DS4Color fullColor = Global.MainColor[deviceNum]; DS4Color lowColor = Global.LowColor[deviceNum]; color = Global.getTransitionedColor(lowColor, fullColor, (uint)device.Battery); } } else { color = Global.MainColor[deviceNum]; } } if ((device.Battery <= Global.FlashAt[deviceNum]) && !defualtLight && !device.Charging) { if (!((Global.FlashColor[deviceNum].red == 0) && (Global.FlashColor[deviceNum].green == 0) && (Global.FlashColor[deviceNum].blue == 0))) { color = Global.FlashColor[deviceNum]; } if (Global.FlashType[deviceNum] == 1) { if (fadetimer[deviceNum] <= 0) { fadedirection[deviceNum] = true; } else if (fadetimer[deviceNum] >= 100) { fadedirection[deviceNum] = false; } if (fadedirection[deviceNum]) { fadetimer[deviceNum] += 1; } else { fadetimer[deviceNum] -= 1; } color = Global.getTransitionedColor(color, new DS4Color(0, 0, 0), fadetimer[deviceNum]); } } if ((Global.IdleDisconnectTimeout[deviceNum] > 0) && Global.LedAsBatteryIndicator[deviceNum] && (!device.Charging || (device.Battery >= 100))) { //Fade lightbar by idle time TimeSpan timeratio = new TimeSpan(DateTime.UtcNow.Ticks - device.LastActive.Ticks); double botratio = timeratio.TotalMilliseconds; double topratio = TimeSpan.FromSeconds(Global.IdleDisconnectTimeout[deviceNum]).TotalMilliseconds; double ratio = botratio / topratio * 100; if ((ratio >= 50) && (ratio <= 100)) { color = Global.getTransitionedColor(color, new DS4Color(0, 0, 0), (uint)((ratio - 50) * 2)); } else if (ratio >= 100) { color = Global.getTransitionedColor(color, new DS4Color(0, 0, 0), 100); } } if (device.Charging && (device.Battery < 100)) { switch (Global.ChargingType[deviceNum]) { case 1: if (fadetimer[deviceNum] <= 0) { fadedirection[deviceNum] = true; } else if (fadetimer[deviceNum] >= 105) { fadedirection[deviceNum] = false; } if (fadedirection[deviceNum]) { fadetimer[deviceNum] += .1; } else { fadetimer[deviceNum] -= .1; } color = Global.getTransitionedColor(color, new DS4Color(0, 0, 0), fadetimer[deviceNum]); break; case 2: counters[deviceNum] += .167; color = HuetoRGB((float)counters[deviceNum] % 360, 255); break; case 3: color = Global.ChargingColor[deviceNum]; break; } } } else if (forcelight[deviceNum]) { color = forcedColor[deviceNum]; } else if (shuttingdown) { color = new DS4Color(0, 0, 0); } else { color = device.ConnectionType == ConnectionType.BT ? new DS4Color(32, 64, 64) : new DS4Color(0, 0, 0); } bool distanceprofile = Global.ProfilePath[deviceNum].ToLower().Contains("distance") || Global.tempprofilename[deviceNum].ToLower().Contains("distance"); if (distanceprofile && !defualtLight) { //Thing I did for Distance float rumble = device.LeftHeavySlowRumble / 2.55f; byte max = Math.Max(color.red, Math.Max(color.green, color.blue)); color = device.LeftHeavySlowRumble > 100 ? Global.getTransitionedColor(new DS4Color(max, max, 0), new DS4Color(255, 0, 0), rumble) : Global.getTransitionedColor(color, Global.getTransitionedColor(new DS4Color(max, max, 0), new DS4Color(255, 0, 0), 39.6078f), device.LeftHeavySlowRumble); } DS4HapticState haptics = new DS4HapticState { LightBarColor = color }; if (haptics.IsLightBarSet()) { if (forcelight[deviceNum] && (forcedFlash[deviceNum] > 0)) { haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = (byte)(25 - forcedFlash[deviceNum]); haptics.LightBarExplicitlyOff = true; } else if ((device.Battery <= Global.FlashAt[deviceNum]) && (Global.FlashType[deviceNum] == 0) && !defualtLight && !device.Charging) { int level = device.Battery / 10; //if (level >= 10) //level = 0; // all values of ~0% or >~100% are rendered the same haptics.LightBarFlashDurationOn = BatteryIndicatorDurations[level, 0]; haptics.LightBarFlashDurationOff = BatteryIndicatorDurations[level, 1]; } else if (distanceprofile && (device.LeftHeavySlowRumble > 155)) //also part of Distance { haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = (byte)(-device.LeftHeavySlowRumble + 265); haptics.LightBarExplicitlyOff = true; } else { //haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = 1; haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = 0; haptics.LightBarExplicitlyOff = true; } } else { haptics.LightBarExplicitlyOff = true; } if ((device.LightBarOnDuration != haptics.LightBarFlashDurationOn) && (device.LightBarOnDuration != 1) && (haptics.LightBarFlashDurationOn == 0)) { haptics.LightBarFlashDurationOff = haptics.LightBarFlashDurationOn = 1; } if (device.LightBarOnDuration == 1) //helps better reset the color { Thread.Sleep(5); } device.PushHapticState(haptics); }
private async void WarnExclusiveModeFailure(DS4Device device) { if (!DS4Devices.IsExclusiveMode || device.IsExclusive) { return; } await Task.Delay(5); string message = Resources.CouldNotOpenDS4.Replace("*Mac address*", device.MacAddress) + " " + Resources.QuitOtherPrograms; LogDebug(message, true); Log.LogToTray(message, LogWarning.Yes); }
public void TimeoutConnection(DS4Device d) { try { Stopwatch sw = new Stopwatch(); sw.Start(); while (!d.IsAlive()) { if (sw.ElapsedMilliseconds < 1000) { Thread.SpinWait(500); } //If weve been waiting less than 1 second let the thread keep its processing chunk else { Thread.Sleep(500); } //If weve been waiting more than 1 second give up some resources if (sw.ElapsedMilliseconds > 5000) { throw new TimeoutException(); //Weve waited long enough } } sw.Reset(); } catch (TimeoutException) { Stop(false); Start(false); } }
public void TouchPadOn(int ind, DS4Device device) { ITouchpadBehaviour tPad = touchPad[ind]; device.Touchpad.TouchButtonDown += tPad.touchButtonDown; device.Touchpad.TouchButtonUp += tPad.touchButtonUp; device.Touchpad.TouchesBegan += tPad.touchesBegan; device.Touchpad.TouchesMoved += tPad.touchesMoved; device.Touchpad.TouchesEnded += tPad.touchesEnded; device.Touchpad.TouchUnchanged += tPad.touchUnchanged; device.SixAxis.SixAccelMoved += tPad.sixaxisMoved; //LogDebug("Touchpad mode for " + device.MacAddress + " is now " + tmode.ToString()); //Log.LogToTray("Touchpad mode for " + device.MacAddress + " is now " + tmode.ToString()); Global.ControllerStatusChanged(this); }
//returns DS4 controllers that were found and are running public static IEnumerable<DS4Device> GetDS4Controllers() { lock (devices) { DS4Device[] controllers = new DS4Device[devices.Count]; devices.Values.CopyTo(controllers, 0); return controllers; } }