2014-03-28 02:50:40 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
using System.Linq;
|
2015-11-28 06:47:26 +01:00
|
|
|
|
using System.Drawing;
|
2016-11-22 02:06:38 +01:00
|
|
|
|
using DS4Windows.DS4Library;
|
2015-11-28 06:47:26 +01:00
|
|
|
|
|
2015-02-08 22:51:52 +01:00
|
|
|
|
namespace DS4Windows
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
2019-01-16 12:07:19 +01:00
|
|
|
|
public struct DS4Color : IEquatable<DS4Color>
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
|
|
|
|
public byte red;
|
|
|
|
|
public byte green;
|
|
|
|
|
public byte blue;
|
2017-05-12 16:48:58 +02:00
|
|
|
|
public DS4Color(Color c)
|
2015-02-08 22:51:52 +01:00
|
|
|
|
{
|
|
|
|
|
red = c.R;
|
|
|
|
|
green = c.G;
|
|
|
|
|
blue = c.B;
|
|
|
|
|
}
|
2017-04-22 06:22:36 +02:00
|
|
|
|
|
2015-02-08 22:51:52 +01:00
|
|
|
|
public DS4Color(byte r, byte g, byte b)
|
|
|
|
|
{
|
|
|
|
|
red = r;
|
|
|
|
|
green = g;
|
|
|
|
|
blue = b;
|
|
|
|
|
}
|
2017-04-22 06:22:36 +02:00
|
|
|
|
|
2019-01-16 12:07:19 +01:00
|
|
|
|
public bool Equals(DS4Color other)
|
2014-09-02 01:23:02 +02:00
|
|
|
|
{
|
2019-01-16 12:07:19 +01:00
|
|
|
|
return this.red == other.red && this.green == other.green && this.blue == other.blue;
|
2014-09-02 01:23:02 +02:00
|
|
|
|
}
|
2017-04-22 06:22:36 +02:00
|
|
|
|
|
2015-11-28 06:47:26 +01:00
|
|
|
|
public Color ToColor => Color.FromArgb(red, green, blue);
|
|
|
|
|
public Color ToColorA
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
byte alphacolor = Math.Max(red, Math.Max(green, blue));
|
|
|
|
|
Color reg = Color.FromArgb(red, green, blue);
|
2019-02-19 10:16:42 +01:00
|
|
|
|
Color full = HuetoRGB(reg.GetHue(), reg.GetBrightness(), ref reg);
|
2015-11-28 06:47:26 +01:00
|
|
|
|
return Color.FromArgb((alphacolor > 205 ? 255 : (alphacolor + 50)), full);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 10:16:42 +01:00
|
|
|
|
private Color HuetoRGB(float hue, float light, ref Color rgb)
|
2015-11-28 06:47:26 +01:00
|
|
|
|
{
|
|
|
|
|
float L = (float)Math.Max(.5, light);
|
|
|
|
|
float C = (1 - Math.Abs(2 * L - 1));
|
|
|
|
|
float X = (C * (1 - Math.Abs((hue / 60) % 2 - 1)));
|
|
|
|
|
float m = L - C / 2;
|
|
|
|
|
float R = 0, G = 0, B = 0;
|
|
|
|
|
if (light == 1) return Color.White;
|
|
|
|
|
else if (rgb.R == rgb.G && rgb.G == rgb.B) return Color.White;
|
|
|
|
|
else if (0 <= hue && hue < 60) { R = C; G = X; }
|
|
|
|
|
else if (60 <= hue && hue < 120) { R = X; G = C; }
|
|
|
|
|
else if (120 <= hue && hue < 180) { G = C; B = X; }
|
|
|
|
|
else if (180 <= hue && hue < 240) { G = X; B = C; }
|
|
|
|
|
else if (240 <= hue && hue < 300) { R = X; B = C; }
|
|
|
|
|
else if (300 <= hue && hue < 360) { R = C; B = X; }
|
|
|
|
|
return Color.FromArgb((int)((R + m) * 255), (int)((G + m) * 255), (int)((B + m) * 255));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool TryParse(string value, ref DS4Color ds4color)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string[] ss = value.Split(',');
|
2017-06-29 15:16:02 +02:00
|
|
|
|
return byte.TryParse(ss[0], out ds4color.red) && byte.TryParse(ss[1], out ds4color.green) && byte.TryParse(ss[2], out ds4color.blue);
|
2015-11-28 06:47:26 +01:00
|
|
|
|
}
|
|
|
|
|
catch { return false; }
|
|
|
|
|
}
|
2017-04-22 06:22:36 +02:00
|
|
|
|
|
Version 1.4.266
Flash Lightbar when at high latency now has the option to choose what
you decide is high latency
Show Notifications now has the option to only show warnings, such as
when a controller cannot be grabbed exclusively
Speaking of bad news for Windows 10 users: Hide DS4 has now been
disabled, until i can figure out why this is, it will be disabled, this
means some games that rely on this may not work properly or at all,
sorry about that
As for good news for Windows 10, did you know you can press Windows + G
to open a game bar which can record games. For Windows 10 users, there's
a new special action: Xbox Game DVR. Pick a trigger (only one button)
and tapping/holding/or double tapping does various things, such as
start/stop recording, save an ongoing recording, take a screenshot (via
the xbox app's option or your own hotkey ie form steam), or just open
the gamebar
Much of the code has been updated with c# 6.0
Added manifest so DS4Windows can notice Windows 10 and high DPIs, also
reorganized files
2015-07-31 05:34:22 +02:00
|
|
|
|
public override string ToString() => $"Red: {red} Green: {green} Blue: {blue}";
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-07 17:59:15 +02:00
|
|
|
|
public enum ConnectionType : byte { BT, SONYWA, USB }; // Prioritize Bluetooth when both BT and USB are connected.
|
2014-03-29 06:29:08 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The haptics engine uses a stack of these states representing the light bar and rumble motor settings.
|
|
|
|
|
* It (will) handle composing them and the details of output report management.
|
|
|
|
|
*/
|
|
|
|
|
public struct DS4HapticState
|
|
|
|
|
{
|
|
|
|
|
public DS4Color LightBarColor;
|
|
|
|
|
public bool LightBarExplicitlyOff;
|
|
|
|
|
public byte LightBarFlashDurationOn, LightBarFlashDurationOff;
|
|
|
|
|
public byte RumbleMotorStrengthLeftHeavySlow, RumbleMotorStrengthRightLightFast;
|
|
|
|
|
public bool RumbleMotorsExplicitlyOff;
|
2017-04-22 06:22:36 +02:00
|
|
|
|
|
2014-03-29 06:29:08 +01:00
|
|
|
|
public bool IsLightBarSet()
|
|
|
|
|
{
|
|
|
|
|
return LightBarExplicitlyOff || LightBarColor.red != 0 || LightBarColor.green != 0 || LightBarColor.blue != 0;
|
|
|
|
|
}
|
2017-04-22 06:22:36 +02:00
|
|
|
|
|
2014-03-29 06:29:08 +01:00
|
|
|
|
public bool IsRumbleSet()
|
|
|
|
|
{
|
2018-11-11 11:56:10 +01:00
|
|
|
|
const byte zero = 0;
|
|
|
|
|
return RumbleMotorsExplicitlyOff || RumbleMotorStrengthLeftHeavySlow != zero || RumbleMotorStrengthRightLightFast != zero;
|
2014-03-29 06:29:08 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-29 10:19:45 +02:00
|
|
|
|
|
2014-03-28 02:50:40 +01:00
|
|
|
|
public class DS4Device
|
|
|
|
|
{
|
2017-09-05 10:34:49 +02:00
|
|
|
|
internal const int BT_OUTPUT_REPORT_LENGTH = 78;
|
|
|
|
|
internal const int BT_INPUT_REPORT_LENGTH = 547;
|
2018-05-28 22:06:48 +02:00
|
|
|
|
internal const int BT_OUTPUT_CHANGE_LENGTH = 13;
|
|
|
|
|
internal const int USB_OUTPUT_CHANGE_LENGTH = 11;
|
2017-03-22 10:41:04 +01:00
|
|
|
|
// Use large value for worst case scenario
|
2017-09-05 10:34:49 +02:00
|
|
|
|
internal const int READ_STREAM_TIMEOUT = 3000;
|
2017-03-23 09:12:50 +01:00
|
|
|
|
// Isolated BT report can have latency as high as 15 ms
|
|
|
|
|
// due to hardware.
|
2017-09-05 10:34:49 +02:00
|
|
|
|
internal const int WARN_INTERVAL_BT = 20;
|
|
|
|
|
internal const int WARN_INTERVAL_USB = 10;
|
2017-04-22 16:00:12 +02:00
|
|
|
|
// Maximum values for battery level when no USB cable is connected
|
|
|
|
|
// and when a USB cable is connected
|
2017-09-05 10:34:49 +02:00
|
|
|
|
internal const int BATTERY_MAX = 8;
|
|
|
|
|
internal const int BATTERY_MAX_USB = 11;
|
2017-05-25 11:51:28 +02:00
|
|
|
|
public const string blankSerial = "00:00:00:00:00:00";
|
2014-03-28 02:50:40 +01:00
|
|
|
|
private HidDevice hDevice;
|
|
|
|
|
private string Mac;
|
|
|
|
|
private DS4State cState = new DS4State();
|
|
|
|
|
private DS4State pState = new DS4State();
|
|
|
|
|
private ConnectionType conType;
|
|
|
|
|
private byte[] accel = new byte[6];
|
|
|
|
|
private byte[] gyro = new byte[6];
|
2014-03-29 06:29:08 +01:00
|
|
|
|
private byte[] inputReport;
|
2014-03-28 02:50:40 +01:00
|
|
|
|
private byte[] btInputReport = null;
|
2017-12-10 13:16:40 +01:00
|
|
|
|
private byte[] outReportBuffer, outputReport;
|
2014-03-28 02:50:40 +01:00
|
|
|
|
private readonly DS4Touchpad touchpad = null;
|
2015-11-28 06:47:26 +01:00
|
|
|
|
private readonly DS4SixAxis sixAxis = null;
|
2014-03-29 06:29:08 +01:00
|
|
|
|
private Thread ds4Input, ds4Output;
|
2014-03-28 02:50:40 +01:00
|
|
|
|
private int battery;
|
2017-05-11 17:13:51 +02:00
|
|
|
|
private DS4Audio audio = null;
|
|
|
|
|
private DS4Audio micAudio = null;
|
2014-05-22 21:13:38 +02:00
|
|
|
|
public DateTime lastActive = DateTime.UtcNow;
|
2015-02-08 22:51:52 +01:00
|
|
|
|
public DateTime firstActive = DateTime.UtcNow;
|
2014-03-29 06:29:08 +01:00
|
|
|
|
private bool charging;
|
2017-03-23 09:12:50 +01:00
|
|
|
|
private int warnInterval = WARN_INTERVAL_USB;
|
2017-04-07 17:59:15 +02:00
|
|
|
|
public int getWarnInterval()
|
|
|
|
|
{
|
|
|
|
|
return warnInterval;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 23:50:37 +01:00
|
|
|
|
public Int32 wheelPrevPhysicalAngle = 0;
|
2018-12-06 23:09:09 +01:00
|
|
|
|
public Int32 wheelPrevFullAngle = 0;
|
2018-12-03 23:50:37 +01:00
|
|
|
|
public Int32 wheelFullTurnCount = 0;
|
|
|
|
|
|
2018-11-17 00:41:21 +01:00
|
|
|
|
public Point wheelCenterPoint;
|
|
|
|
|
public Point wheel90DegPointLeft;
|
|
|
|
|
public Point wheelCircleCenterPointLeft;
|
|
|
|
|
public Point wheel90DegPointRight;
|
|
|
|
|
public Point wheelCircleCenterPointRight;
|
|
|
|
|
|
|
|
|
|
public DateTime wheelPrevRecalibrateTime;
|
|
|
|
|
|
2018-12-03 23:50:37 +01:00
|
|
|
|
private int wheelRecalibrateActiveState = 0;
|
|
|
|
|
public int WheelRecalibrateActiveState
|
2018-11-17 00:41:21 +01:00
|
|
|
|
{
|
2018-12-03 23:50:37 +01:00
|
|
|
|
get { return wheelRecalibrateActiveState; }
|
2018-11-17 00:41:21 +01:00
|
|
|
|
set
|
|
|
|
|
{
|
2018-12-03 23:50:37 +01:00
|
|
|
|
wheelRecalibrateActiveState = value;
|
2018-11-17 00:41:21 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum WheelCalibrationPoint
|
|
|
|
|
{
|
|
|
|
|
None = 0,
|
|
|
|
|
Center = 1,
|
|
|
|
|
Right90 = 2,
|
|
|
|
|
Left90 = 4,
|
|
|
|
|
All = Center | Right90 | Left90
|
|
|
|
|
}
|
|
|
|
|
public WheelCalibrationPoint wheelCalibratedAxisBitmask;
|
|
|
|
|
|
2017-03-24 16:06:01 +01:00
|
|
|
|
private bool exitOutputThread = false;
|
2018-03-26 06:36:46 +02:00
|
|
|
|
public bool ExitOutputThread => exitOutputThread;
|
2017-05-01 12:40:37 +02:00
|
|
|
|
private bool exitInputThread = false;
|
2017-03-24 16:06:01 +01:00
|
|
|
|
private object exitLocker = new object();
|
2017-05-25 11:51:28 +02:00
|
|
|
|
|
2019-02-24 22:03:12 +01:00
|
|
|
|
public delegate void ReportHandler<TEventArgs>(DS4Device sender, TEventArgs args);
|
|
|
|
|
|
|
|
|
|
//public event EventHandler<EventArgs> Report = null;
|
|
|
|
|
public event ReportHandler<EventArgs> Report = null;
|
2014-03-28 02:50:40 +01:00
|
|
|
|
public event EventHandler<EventArgs> Removal = null;
|
2017-05-25 11:51:28 +02:00
|
|
|
|
public event EventHandler<EventArgs> SyncChange = null;
|
|
|
|
|
public event EventHandler<EventArgs> SerialChange = null;
|
2019-02-24 22:03:12 +01:00
|
|
|
|
//public EventHandler<EventArgs> MotionEvent = null;
|
|
|
|
|
public ReportHandler<EventArgs> MotionEvent = null;
|
2014-03-28 02:50:40 +01:00
|
|
|
|
|
Version 1.4.266
Flash Lightbar when at high latency now has the option to choose what
you decide is high latency
Show Notifications now has the option to only show warnings, such as
when a controller cannot be grabbed exclusively
Speaking of bad news for Windows 10 users: Hide DS4 has now been
disabled, until i can figure out why this is, it will be disabled, this
means some games that rely on this may not work properly or at all,
sorry about that
As for good news for Windows 10, did you know you can press Windows + G
to open a game bar which can record games. For Windows 10 users, there's
a new special action: Xbox Game DVR. Pick a trigger (only one button)
and tapping/holding/or double tapping does various things, such as
start/stop recording, save an ongoing recording, take a screenshot (via
the xbox app's option or your own hotkey ie form steam), or just open
the gamebar
Much of the code has been updated with c# 6.0
Added manifest so DS4Windows can notice Windows 10 and high DPIs, also
reorganized files
2015-07-31 05:34:22 +02:00
|
|
|
|
public HidDevice HidDevice => hDevice;
|
|
|
|
|
public bool IsExclusive => HidDevice.IsExclusive;
|
2017-04-25 11:24:14 +02:00
|
|
|
|
public bool isExclusive()
|
|
|
|
|
{
|
|
|
|
|
return HidDevice.IsExclusive;
|
|
|
|
|
}
|
2017-05-11 17:13:51 +02:00
|
|
|
|
|
2017-04-09 01:13:56 +02:00
|
|
|
|
private bool isDisconnecting = false;
|
|
|
|
|
public bool IsDisconnecting
|
|
|
|
|
{
|
|
|
|
|
get { return isDisconnecting; }
|
|
|
|
|
private set
|
|
|
|
|
{
|
|
|
|
|
this.isDisconnecting = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-11 17:13:51 +02:00
|
|
|
|
|
2017-04-21 05:09:08 +02:00
|
|
|
|
public bool isDisconnectingStatus()
|
|
|
|
|
{
|
|
|
|
|
return this.isDisconnecting;
|
|
|
|
|
}
|
2017-04-09 01:13:56 +02:00
|
|
|
|
|
|
|
|
|
private bool isRemoving = false;
|
|
|
|
|
public bool IsRemoving
|
|
|
|
|
{
|
|
|
|
|
get { return isRemoving; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
this.isRemoving = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool isRemoved = false;
|
|
|
|
|
public bool IsRemoved
|
|
|
|
|
{
|
|
|
|
|
get { return isRemoved; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
this.isRemoved = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-30 02:44:10 +02:00
|
|
|
|
public object removeLocker = new object();
|
2014-03-28 02:50:40 +01:00
|
|
|
|
|
Version 1.4.266
Flash Lightbar when at high latency now has the option to choose what
you decide is high latency
Show Notifications now has the option to only show warnings, such as
when a controller cannot be grabbed exclusively
Speaking of bad news for Windows 10 users: Hide DS4 has now been
disabled, until i can figure out why this is, it will be disabled, this
means some games that rely on this may not work properly or at all,
sorry about that
As for good news for Windows 10, did you know you can press Windows + G
to open a game bar which can record games. For Windows 10 users, there's
a new special action: Xbox Game DVR. Pick a trigger (only one button)
and tapping/holding/or double tapping does various things, such as
start/stop recording, save an ongoing recording, take a screenshot (via
the xbox app's option or your own hotkey ie form steam), or just open
the gamebar
Much of the code has been updated with c# 6.0
Added manifest so DS4Windows can notice Windows 10 and high DPIs, also
reorganized files
2015-07-31 05:34:22 +02:00
|
|
|
|
public string MacAddress => Mac;
|
2017-04-21 05:09:08 +02:00
|
|
|
|
public string getMacAddress()
|
|
|
|
|
{
|
|
|
|
|
return this.Mac;
|
|
|
|
|
}
|
2014-03-28 02:50:40 +01:00
|
|
|
|
|
Version 1.4.266
Flash Lightbar when at high latency now has the option to choose what
you decide is high latency
Show Notifications now has the option to only show warnings, such as
when a controller cannot be grabbed exclusively
Speaking of bad news for Windows 10 users: Hide DS4 has now been
disabled, until i can figure out why this is, it will be disabled, this
means some games that rely on this may not work properly or at all,
sorry about that
As for good news for Windows 10, did you know you can press Windows + G
to open a game bar which can record games. For Windows 10 users, there's
a new special action: Xbox Game DVR. Pick a trigger (only one button)
and tapping/holding/or double tapping does various things, such as
start/stop recording, save an ongoing recording, take a screenshot (via
the xbox app's option or your own hotkey ie form steam), or just open
the gamebar
Much of the code has been updated with c# 6.0
Added manifest so DS4Windows can notice Windows 10 and high DPIs, also
reorganized files
2015-07-31 05:34:22 +02:00
|
|
|
|
public ConnectionType ConnectionType => conType;
|
2017-04-16 08:22:04 +02:00
|
|
|
|
public ConnectionType getConnectionType()
|
|
|
|
|
{
|
|
|
|
|
return this.conType;
|
|
|
|
|
}
|
2017-04-06 22:05:16 +02:00
|
|
|
|
|
|
|
|
|
// behavior only active when > 0
|
|
|
|
|
private int idleTimeout = 0;
|
2017-04-26 23:51:15 +02:00
|
|
|
|
public int IdleTimeout
|
|
|
|
|
{
|
2017-04-06 22:05:16 +02:00
|
|
|
|
get { return idleTimeout; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
idleTimeout = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-28 02:50:40 +01:00
|
|
|
|
|
2017-04-26 23:51:15 +02:00
|
|
|
|
public int getIdleTimeout()
|
|
|
|
|
{
|
|
|
|
|
return idleTimeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setIdleTimeout(int value)
|
|
|
|
|
{
|
|
|
|
|
if (idleTimeout != value)
|
|
|
|
|
{
|
|
|
|
|
idleTimeout = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Version 1.4.266
Flash Lightbar when at high latency now has the option to choose what
you decide is high latency
Show Notifications now has the option to only show warnings, such as
when a controller cannot be grabbed exclusively
Speaking of bad news for Windows 10 users: Hide DS4 has now been
disabled, until i can figure out why this is, it will be disabled, this
means some games that rely on this may not work properly or at all,
sorry about that
As for good news for Windows 10, did you know you can press Windows + G
to open a game bar which can record games. For Windows 10 users, there's
a new special action: Xbox Game DVR. Pick a trigger (only one button)
and tapping/holding/or double tapping does various things, such as
start/stop recording, save an ongoing recording, take a screenshot (via
the xbox app's option or your own hotkey ie form steam), or just open
the gamebar
Much of the code has been updated with c# 6.0
Added manifest so DS4Windows can notice Windows 10 and high DPIs, also
reorganized files
2015-07-31 05:34:22 +02:00
|
|
|
|
public int Battery => battery;
|
2017-03-24 03:32:33 +01:00
|
|
|
|
public int getBattery()
|
|
|
|
|
{
|
|
|
|
|
return battery;
|
|
|
|
|
}
|
|
|
|
|
|
Version 1.4.266
Flash Lightbar when at high latency now has the option to choose what
you decide is high latency
Show Notifications now has the option to only show warnings, such as
when a controller cannot be grabbed exclusively
Speaking of bad news for Windows 10 users: Hide DS4 has now been
disabled, until i can figure out why this is, it will be disabled, this
means some games that rely on this may not work properly or at all,
sorry about that
As for good news for Windows 10, did you know you can press Windows + G
to open a game bar which can record games. For Windows 10 users, there's
a new special action: Xbox Game DVR. Pick a trigger (only one button)
and tapping/holding/or double tapping does various things, such as
start/stop recording, save an ongoing recording, take a screenshot (via
the xbox app's option or your own hotkey ie form steam), or just open
the gamebar
Much of the code has been updated with c# 6.0
Added manifest so DS4Windows can notice Windows 10 and high DPIs, also
reorganized files
2015-07-31 05:34:22 +02:00
|
|
|
|
public bool Charging => charging;
|
2017-03-24 03:32:33 +01:00
|
|
|
|
public bool isCharging()
|
|
|
|
|
{
|
|
|
|
|
return charging;
|
|
|
|
|
}
|
2014-03-28 02:50:40 +01:00
|
|
|
|
|
2017-04-16 08:22:04 +02:00
|
|
|
|
private long lastTimeElapsed = 0;
|
|
|
|
|
public long getLastTimeElapsed()
|
|
|
|
|
{
|
|
|
|
|
return lastTimeElapsed;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 17:50:28 +02:00
|
|
|
|
public double lastTimeElapsedDouble = 0.0;
|
|
|
|
|
public double getLastTimeElapsedDouble()
|
|
|
|
|
{
|
|
|
|
|
return lastTimeElapsedDouble;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-28 02:50:40 +01:00
|
|
|
|
public byte RightLightFastRumble
|
|
|
|
|
{
|
2019-01-11 01:37:39 +01:00
|
|
|
|
get { return currentHap.RumbleMotorStrengthRightLightFast; }
|
2014-03-28 02:50:40 +01:00
|
|
|
|
set
|
|
|
|
|
{
|
2019-01-11 01:37:39 +01:00
|
|
|
|
if (currentHap.RumbleMotorStrengthRightLightFast != value)
|
|
|
|
|
currentHap.RumbleMotorStrengthRightLightFast = value;
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte LeftHeavySlowRumble
|
|
|
|
|
{
|
2019-01-11 01:37:39 +01:00
|
|
|
|
get { return currentHap.RumbleMotorStrengthLeftHeavySlow; }
|
2014-03-28 02:50:40 +01:00
|
|
|
|
set
|
|
|
|
|
{
|
2019-01-11 01:37:39 +01:00
|
|
|
|
if (currentHap.RumbleMotorStrengthLeftHeavySlow != value)
|
|
|
|
|
currentHap.RumbleMotorStrengthLeftHeavySlow = value;
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 03:32:33 +01:00
|
|
|
|
public byte getLeftHeavySlowRumble()
|
|
|
|
|
{
|
2019-01-11 01:37:39 +01:00
|
|
|
|
return currentHap.RumbleMotorStrengthLeftHeavySlow;
|
2017-03-24 03:32:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-28 02:50:40 +01:00
|
|
|
|
public DS4Color LightBarColor
|
|
|
|
|
{
|
2019-01-11 01:37:39 +01:00
|
|
|
|
get { return currentHap.LightBarColor; }
|
2014-03-28 02:50:40 +01:00
|
|
|
|
set
|
|
|
|
|
{
|
2019-01-11 01:37:39 +01:00
|
|
|
|
if (currentHap.LightBarColor.red != value.red || currentHap.LightBarColor.green != value.green || currentHap.LightBarColor.blue != value.blue)
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
2019-01-11 01:37:39 +01:00
|
|
|
|
currentHap.LightBarColor = value;
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-24 03:32:33 +01:00
|
|
|
|
|
|
|
|
|
public byte getLightBarOnDuration()
|
|
|
|
|
{
|
2019-01-11 01:37:39 +01:00
|
|
|
|
return currentHap.LightBarFlashDurationOn;
|
2017-03-24 03:32:33 +01:00
|
|
|
|
}
|
2014-03-28 02:50:40 +01:00
|
|
|
|
|
2017-05-17 08:02:12 +02:00
|
|
|
|
// Specify the poll rate interval used for the DS4 hardware when
|
|
|
|
|
// connected via Bluetooth
|
|
|
|
|
private int btPollRate = 0;
|
|
|
|
|
public int BTPollRate
|
|
|
|
|
{
|
|
|
|
|
get { return btPollRate; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (btPollRate != value && value >= 0 && value <= 16)
|
|
|
|
|
{
|
|
|
|
|
btPollRate = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getBTPollRate()
|
|
|
|
|
{
|
|
|
|
|
return btPollRate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setBTPollRate(int value)
|
|
|
|
|
{
|
|
|
|
|
if (btPollRate != value && value >= 0 && value <= 16)
|
|
|
|
|
{
|
|
|
|
|
btPollRate = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-28 02:50:40 +01:00
|
|
|
|
public DS4Touchpad Touchpad { get { return touchpad; } }
|
2015-11-28 06:47:26 +01:00
|
|
|
|
public DS4SixAxis SixAxis { get { return sixAxis; } }
|
2014-03-28 02:50:40 +01:00
|
|
|
|
|
2014-03-29 06:29:08 +01:00
|
|
|
|
public static ConnectionType HidConnectionType(HidDevice hidDevice)
|
|
|
|
|
{
|
2017-04-06 01:51:20 +02:00
|
|
|
|
ConnectionType result = ConnectionType.USB;
|
|
|
|
|
if (hidDevice.Capabilities.InputReportByteLength == 64)
|
|
|
|
|
{
|
|
|
|
|
if (hidDevice.Capabilities.NumberFeatureDataIndices == 22)
|
|
|
|
|
{
|
|
|
|
|
result = ConnectionType.SONYWA;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result = ConnectionType.BT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2014-03-29 06:29:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-14 00:01:43 +02:00
|
|
|
|
private Queue<Action> eventQueue = new Queue<Action>();
|
|
|
|
|
private object eventQueueLock = new object();
|
2017-05-09 12:11:50 +02:00
|
|
|
|
|
2017-06-01 05:37:53 +02:00
|
|
|
|
private Thread timeoutCheckThread = null;
|
|
|
|
|
private bool timeoutExecuted = false;
|
|
|
|
|
private bool timeoutEvent = false;
|
2018-07-10 07:57:55 +02:00
|
|
|
|
private bool runCalib;
|
2018-10-25 09:51:50 +02:00
|
|
|
|
private bool hasInputEvts = false;
|
2018-07-10 07:57:55 +02:00
|
|
|
|
public bool ShouldRunCalib()
|
|
|
|
|
{
|
|
|
|
|
return runCalib;
|
|
|
|
|
}
|
2017-06-01 05:37:53 +02:00
|
|
|
|
|
2014-03-28 02:50:40 +01:00
|
|
|
|
public DS4Device(HidDevice hidDevice)
|
2017-10-11 00:45:42 +02:00
|
|
|
|
{
|
2014-03-28 02:50:40 +01:00
|
|
|
|
hDevice = hidDevice;
|
2014-03-29 06:29:08 +01:00
|
|
|
|
conType = HidConnectionType(hDevice);
|
2014-03-28 02:50:40 +01:00
|
|
|
|
Mac = hDevice.readSerial();
|
2018-07-10 07:57:55 +02:00
|
|
|
|
runCalib = true;
|
2017-04-06 03:37:38 +02:00
|
|
|
|
if (conType == ConnectionType.USB || conType == ConnectionType.SONYWA)
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
2014-03-29 06:29:08 +01:00
|
|
|
|
inputReport = new byte[64];
|
2014-03-28 02:50:40 +01:00
|
|
|
|
outputReport = new byte[hDevice.Capabilities.OutputReportByteLength];
|
2017-12-10 13:16:40 +01:00
|
|
|
|
outReportBuffer = new byte[hDevice.Capabilities.OutputReportByteLength];
|
2017-04-06 03:37:38 +02:00
|
|
|
|
if (conType == ConnectionType.USB)
|
|
|
|
|
{
|
|
|
|
|
warnInterval = WARN_INTERVAL_USB;
|
2017-10-02 10:16:57 +02:00
|
|
|
|
HidDeviceAttributes tempAttr = hDevice.Attributes;
|
|
|
|
|
if (tempAttr.VendorId == 0x054C && tempAttr.ProductId == 0x09CC)
|
|
|
|
|
{
|
|
|
|
|
audio = new DS4Audio();
|
|
|
|
|
micAudio = new DS4Audio(DS4Library.CoreAudio.DataFlow.Capture);
|
|
|
|
|
}
|
2018-06-23 23:04:29 +02:00
|
|
|
|
else if (tempAttr.VendorId == 0x146B)
|
|
|
|
|
{
|
2018-07-10 07:46:25 +02:00
|
|
|
|
runCalib = false;
|
2018-06-23 23:04:29 +02:00
|
|
|
|
}
|
2017-10-02 10:16:57 +02:00
|
|
|
|
|
2017-05-25 11:51:28 +02:00
|
|
|
|
synced = true;
|
2017-04-06 03:37:38 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
warnInterval = WARN_INTERVAL_BT;
|
2017-05-11 17:13:51 +02:00
|
|
|
|
audio = new DS4Audio();
|
2017-06-17 18:06:33 +02:00
|
|
|
|
micAudio = new DS4Audio(DS4Library.CoreAudio.DataFlow.Capture);
|
2018-08-20 20:14:34 +02:00
|
|
|
|
runCalib = synced = isValidSerial();
|
2017-04-06 03:37:38 +02:00
|
|
|
|
}
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
btInputReport = new byte[BT_INPUT_REPORT_LENGTH];
|
2017-12-02 04:31:05 +01:00
|
|
|
|
inputReport = new byte[BT_INPUT_REPORT_LENGTH - 2];
|
2014-03-28 02:50:40 +01:00
|
|
|
|
outputReport = new byte[BT_OUTPUT_REPORT_LENGTH];
|
2017-12-10 13:16:40 +01:00
|
|
|
|
outReportBuffer = new byte[BT_OUTPUT_REPORT_LENGTH];
|
2017-03-23 09:12:50 +01:00
|
|
|
|
warnInterval = WARN_INTERVAL_BT;
|
2017-05-25 11:51:28 +02:00
|
|
|
|
synced = isValidSerial();
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
2017-04-22 09:26:44 +02:00
|
|
|
|
|
2014-03-28 02:50:40 +01:00
|
|
|
|
touchpad = new DS4Touchpad();
|
2015-11-28 06:47:26 +01:00
|
|
|
|
sixAxis = new DS4SixAxis();
|
2018-03-05 05:43:49 +01:00
|
|
|
|
Crc32Algorithm.InitializeTable(DefaultPolynomial);
|
2018-07-10 07:46:25 +02:00
|
|
|
|
if (runCalib)
|
2018-07-10 08:18:27 +02:00
|
|
|
|
RefreshCalibration();
|
2018-03-26 06:36:46 +02:00
|
|
|
|
|
|
|
|
|
if (!hDevice.IsFileStreamOpen())
|
|
|
|
|
{
|
|
|
|
|
hDevice.OpenFileStream(inputReport.Length);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-06 08:59:45 +02:00
|
|
|
|
sendOutputReport(true, true); // initialize the output report
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 08:18:27 +02:00
|
|
|
|
private void TimeoutTestThread()
|
2017-06-01 05:37:53 +02:00
|
|
|
|
{
|
|
|
|
|
while (!timeoutExecuted)
|
|
|
|
|
{
|
|
|
|
|
if (timeoutEvent)
|
|
|
|
|
{
|
|
|
|
|
timeoutExecuted = true;
|
2018-05-06 08:59:45 +02:00
|
|
|
|
this.sendOutputReport(true, true); // Kick Windows into noticing the disconnection.
|
2017-06-01 05:37:53 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
timeoutEvent = true;
|
|
|
|
|
Thread.Sleep(READ_STREAM_TIMEOUT);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 05:43:49 +01:00
|
|
|
|
const int DS4_FEATURE_REPORT_5_LEN = 41;
|
|
|
|
|
const int DS4_FEATURE_REPORT_5_CRC32_POS = DS4_FEATURE_REPORT_5_LEN - 4;
|
2018-07-10 08:18:27 +02:00
|
|
|
|
public void RefreshCalibration()
|
2017-10-12 01:55:15 +02:00
|
|
|
|
{
|
|
|
|
|
byte[] calibration = new byte[41];
|
|
|
|
|
calibration[0] = conType == ConnectionType.BT ? (byte)0x05 : (byte)0x02;
|
2018-03-05 05:43:49 +01:00
|
|
|
|
|
|
|
|
|
if (conType == ConnectionType.BT)
|
|
|
|
|
{
|
|
|
|
|
bool found = false;
|
|
|
|
|
for (int tries = 0; !found && tries < 5; tries++)
|
|
|
|
|
{
|
|
|
|
|
hDevice.readFeatureData(calibration);
|
|
|
|
|
uint recvCrc32 = calibration[DS4_FEATURE_REPORT_5_CRC32_POS] |
|
|
|
|
|
(uint)(calibration[DS4_FEATURE_REPORT_5_CRC32_POS + 1] << 8) |
|
|
|
|
|
(uint)(calibration[DS4_FEATURE_REPORT_5_CRC32_POS + 2] << 16) |
|
|
|
|
|
(uint)(calibration[DS4_FEATURE_REPORT_5_CRC32_POS + 3] << 24);
|
|
|
|
|
|
|
|
|
|
uint calcCrc32 = ~Crc32Algorithm.Compute(new byte[] { 0xA3 });
|
|
|
|
|
calcCrc32 = ~Crc32Algorithm.CalculateBasicHash(ref calcCrc32, ref calibration, 0, DS4_FEATURE_REPORT_5_LEN - 4);
|
|
|
|
|
bool validCrc = recvCrc32 == calcCrc32;
|
|
|
|
|
if (!validCrc && tries >= 5)
|
|
|
|
|
{
|
2018-09-29 11:42:22 +02:00
|
|
|
|
AppLogger.LogToGui("Gyro Calibration Failed", true);
|
2018-03-05 05:43:49 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else if (validCrc)
|
|
|
|
|
{
|
|
|
|
|
found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sixAxis.setCalibrationData(ref calibration, conType == ConnectionType.USB);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
hDevice.readFeatureData(calibration);
|
|
|
|
|
sixAxis.setCalibrationData(ref calibration, conType == ConnectionType.USB);
|
|
|
|
|
}
|
2017-10-12 01:55:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-28 02:50:40 +01:00
|
|
|
|
public void StartUpdate()
|
|
|
|
|
{
|
2014-03-29 06:29:08 +01:00
|
|
|
|
if (ds4Input == null)
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
2018-04-16 12:03:47 +02:00
|
|
|
|
if (conType == ConnectionType.BT)
|
|
|
|
|
{
|
|
|
|
|
ds4Output = new Thread(performDs4Output);
|
2019-01-09 11:24:29 +01:00
|
|
|
|
ds4Output.Priority = ThreadPriority.Normal;
|
2018-04-16 12:03:47 +02:00
|
|
|
|
ds4Output.Name = "DS4 Output thread: " + Mac;
|
|
|
|
|
ds4Output.IsBackground = true;
|
|
|
|
|
ds4Output.Start();
|
|
|
|
|
|
2018-07-10 08:18:27 +02:00
|
|
|
|
timeoutCheckThread = new Thread(TimeoutTestThread);
|
2018-04-16 12:03:47 +02:00
|
|
|
|
timeoutCheckThread.Priority = ThreadPriority.BelowNormal;
|
|
|
|
|
timeoutCheckThread.Name = "DS4 Timeout thread: " + Mac;
|
|
|
|
|
timeoutCheckThread.IsBackground = true;
|
|
|
|
|
timeoutCheckThread.Start();
|
|
|
|
|
}
|
2018-10-27 03:58:43 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ds4Output = new Thread(OutReportCopy);
|
2019-01-09 11:24:29 +01:00
|
|
|
|
ds4Output.Priority = ThreadPriority.Normal;
|
2018-10-27 03:58:43 +02:00
|
|
|
|
ds4Output.Name = "DS4 Arr Copy thread: " + Mac;
|
|
|
|
|
ds4Output.IsBackground = true;
|
|
|
|
|
ds4Output.Start();
|
|
|
|
|
}
|
2017-03-22 08:52:54 +01:00
|
|
|
|
|
2014-03-29 06:29:08 +01:00
|
|
|
|
ds4Input = new Thread(performDs4Input);
|
2017-03-23 03:35:18 +01:00
|
|
|
|
ds4Input.Priority = ThreadPriority.AboveNormal;
|
2014-03-29 06:29:08 +01:00
|
|
|
|
ds4Input.Name = "DS4 Input thread: " + Mac;
|
2017-03-24 16:06:01 +01:00
|
|
|
|
ds4Input.IsBackground = true;
|
2014-03-29 06:29:08 +01:00
|
|
|
|
ds4Input.Start();
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Console.WriteLine("Thread already running for DS4: " + Mac);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StopUpdate()
|
|
|
|
|
{
|
2017-04-23 04:46:50 +02:00
|
|
|
|
if (ds4Input != null &&
|
|
|
|
|
ds4Input.IsAlive && !ds4Input.ThreadState.HasFlag(System.Threading.ThreadState.Stopped) &&
|
2017-03-24 16:06:01 +01:00
|
|
|
|
!ds4Input.ThreadState.HasFlag(System.Threading.ThreadState.AbortRequested))
|
2014-03-29 06:29:08 +01:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-05-01 12:40:37 +02:00
|
|
|
|
exitInputThread = true;
|
|
|
|
|
//ds4Input.Abort();
|
2014-03-29 06:29:08 +01:00
|
|
|
|
ds4Input.Join();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-22 16:49:01 +02:00
|
|
|
|
|
2014-03-29 06:29:08 +01:00
|
|
|
|
StopOutputUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StopOutputUpdate()
|
|
|
|
|
{
|
2017-03-24 16:06:01 +01:00
|
|
|
|
lock (exitLocker)
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
2017-04-23 04:46:50 +02:00
|
|
|
|
if (ds4Output != null &&
|
|
|
|
|
ds4Output.IsAlive && !ds4Output.ThreadState.HasFlag(System.Threading.ThreadState.Stopped) &&
|
2017-03-24 16:06:01 +01:00
|
|
|
|
!ds4Output.ThreadState.HasFlag(System.Threading.ThreadState.AbortRequested))
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
2017-03-24 16:06:01 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
exitOutputThread = true;
|
2017-03-25 09:29:25 +01:00
|
|
|
|
ds4Output.Interrupt();
|
2017-03-24 16:06:01 +01:00
|
|
|
|
ds4Output.Join();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e.Message);
|
|
|
|
|
}
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-29 06:29:08 +01:00
|
|
|
|
private bool writeOutput()
|
|
|
|
|
{
|
|
|
|
|
if (conType == ConnectionType.BT)
|
|
|
|
|
{
|
|
|
|
|
return hDevice.WriteOutputReportViaControl(outputReport);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-12-10 13:16:40 +01:00
|
|
|
|
return hDevice.WriteOutputReportViaInterrupt(outReportBuffer, READ_STREAM_TIMEOUT);
|
2014-03-29 06:29:08 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 09:09:42 +02:00
|
|
|
|
private byte outputPendCount = 0;
|
2019-01-09 10:45:39 +01:00
|
|
|
|
private readonly Stopwatch standbySw = new Stopwatch();
|
2018-03-07 00:16:46 +01:00
|
|
|
|
private unsafe void performDs4Output()
|
2014-03-29 06:29:08 +01:00
|
|
|
|
{
|
2017-10-07 07:55:14 +02:00
|
|
|
|
try
|
2014-03-29 06:29:08 +01:00
|
|
|
|
{
|
2017-10-07 07:55:14 +02:00
|
|
|
|
int lastError = 0;
|
2017-11-18 20:15:15 +01:00
|
|
|
|
bool result = false, currentRumble = false;
|
2017-10-07 07:55:14 +02:00
|
|
|
|
while (!exitOutputThread)
|
2014-03-29 06:29:08 +01:00
|
|
|
|
{
|
2017-11-16 02:57:58 +01:00
|
|
|
|
if (currentRumble)
|
2017-03-23 07:39:31 +01:00
|
|
|
|
{
|
2017-11-15 09:14:20 +01:00
|
|
|
|
lock(outputReport)
|
|
|
|
|
{
|
|
|
|
|
result = writeOutput();
|
|
|
|
|
}
|
2017-10-07 07:55:14 +02:00
|
|
|
|
|
2017-11-16 02:57:58 +01:00
|
|
|
|
currentRumble = false;
|
2017-10-07 07:55:14 +02:00
|
|
|
|
if (!result)
|
|
|
|
|
{
|
2017-11-16 02:57:58 +01:00
|
|
|
|
currentRumble = true;
|
|
|
|
|
exitOutputThread = true;
|
2017-10-07 07:55:14 +02:00
|
|
|
|
int thisError = Marshal.GetLastWin32Error();
|
|
|
|
|
if (lastError != thisError)
|
2017-03-25 09:29:25 +01:00
|
|
|
|
{
|
2017-10-07 07:55:14 +02:00
|
|
|
|
Console.WriteLine(Mac.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> encountered write failure: " + thisError);
|
|
|
|
|
//Log.LogToGui(Mac.ToString() + " encountered write failure: " + thisError, true);
|
|
|
|
|
lastError = thisError;
|
2017-03-23 07:39:31 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-07 07:55:14 +02:00
|
|
|
|
}
|
2017-03-25 09:29:25 +01:00
|
|
|
|
|
2017-11-16 02:57:58 +01:00
|
|
|
|
if (!currentRumble)
|
2017-10-07 07:55:14 +02:00
|
|
|
|
{
|
|
|
|
|
lastError = 0;
|
2017-12-10 13:16:40 +01:00
|
|
|
|
lock (outReportBuffer)
|
2017-11-16 02:57:58 +01:00
|
|
|
|
{
|
2017-12-10 13:16:40 +01:00
|
|
|
|
Monitor.Wait(outReportBuffer);
|
2018-03-07 00:16:46 +01:00
|
|
|
|
fixed (byte* byteR = outputReport, byteB = outReportBuffer)
|
|
|
|
|
{
|
2018-05-28 22:06:48 +02:00
|
|
|
|
for (int i = 0, arlen = BT_OUTPUT_CHANGE_LENGTH; i < arlen; i++)
|
2018-03-07 00:16:46 +01:00
|
|
|
|
byteR[i] = byteB[i];
|
|
|
|
|
}
|
|
|
|
|
//outReportBuffer.CopyTo(outputReport, 0);
|
2019-01-10 06:57:22 +01:00
|
|
|
|
if (outputPendCount > 1)
|
2019-01-09 12:41:20 +01:00
|
|
|
|
outputPendCount--;
|
2019-01-10 06:57:22 +01:00
|
|
|
|
else if (outputPendCount == 1)
|
|
|
|
|
{
|
|
|
|
|
outputPendCount--;
|
|
|
|
|
standbySw.Restart();
|
|
|
|
|
}
|
|
|
|
|
else
|
2019-01-10 02:30:05 +01:00
|
|
|
|
standbySw.Restart();
|
2017-11-16 02:57:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentRumble = true;
|
2017-10-07 07:55:14 +02:00
|
|
|
|
}
|
2014-03-29 06:29:08 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-15 09:14:20 +01:00
|
|
|
|
catch (ThreadInterruptedException) { }
|
2014-03-29 06:29:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Is the device alive and receiving valid sensor input reports? */
|
|
|
|
|
public bool IsAlive()
|
|
|
|
|
{
|
|
|
|
|
return priorInputReport30 != 0xff;
|
|
|
|
|
}
|
2017-04-22 09:26:44 +02:00
|
|
|
|
|
2014-03-29 06:29:08 +01:00
|
|
|
|
private byte priorInputReport30 = 0xff;
|
2017-05-25 11:51:28 +02:00
|
|
|
|
|
|
|
|
|
private bool synced = false;
|
|
|
|
|
public bool Synced
|
|
|
|
|
{
|
|
|
|
|
get { return synced; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (synced != value)
|
|
|
|
|
{
|
|
|
|
|
synced = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool isSynced()
|
|
|
|
|
{
|
|
|
|
|
return synced;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 17:50:28 +02:00
|
|
|
|
public double Latency = 0.0;
|
2014-12-17 19:29:22 +01:00
|
|
|
|
public string error;
|
2017-10-19 10:21:33 +02:00
|
|
|
|
public bool firstReport = true;
|
2017-05-17 10:59:09 +02:00
|
|
|
|
public bool oldCharging = false;
|
2017-06-19 17:50:28 +02:00
|
|
|
|
double curTimeDouble = 0.0;
|
|
|
|
|
double oldTimeDouble = 0.0;
|
2017-06-25 04:10:09 +02:00
|
|
|
|
DateTime utcNow = DateTime.UtcNow;
|
|
|
|
|
bool ds4InactiveFrame = true;
|
|
|
|
|
bool idleInput = true;
|
2017-04-22 09:26:44 +02:00
|
|
|
|
|
2017-07-26 11:30:50 +02:00
|
|
|
|
bool timeStampInit = false;
|
|
|
|
|
uint timeStampPrevious = 0;
|
|
|
|
|
uint deltaTimeCurrent = 0;
|
|
|
|
|
|
2018-03-04 21:37:47 +01:00
|
|
|
|
|
|
|
|
|
const int BT_INPUT_REPORT_CRC32_POS = BT_OUTPUT_REPORT_LENGTH - 4; //last 4 bytes of the 78-sized input report are crc32
|
|
|
|
|
const uint DefaultPolynomial = 0xedb88320u;
|
|
|
|
|
uint HamSeed = 2351727372;
|
|
|
|
|
|
2018-03-05 15:57:30 +01:00
|
|
|
|
private unsafe void performDs4Input()
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
unchecked
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
firstActive = DateTime.UtcNow;
|
|
|
|
|
NativeMethods.HidD_SetNumInputBuffers(hDevice.safeReadHandle.DangerousGetHandle(), 2);
|
|
|
|
|
Queue<long> latencyQueue = new Queue<long>(21); // Set capacity at max + 1 to avoid any resizing
|
|
|
|
|
int tempLatencyCount = 0;
|
|
|
|
|
long oldtime = 0;
|
|
|
|
|
string currerror = string.Empty;
|
|
|
|
|
long curtime = 0;
|
|
|
|
|
Stopwatch sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
timeoutEvent = false;
|
|
|
|
|
ds4InactiveFrame = true;
|
|
|
|
|
idleInput = true;
|
|
|
|
|
bool syncWriteReport = conType != ConnectionType.BT;
|
2019-03-14 00:41:10 +01:00
|
|
|
|
bool forceWrite = false;
|
2018-10-11 10:38:52 +02:00
|
|
|
|
|
|
|
|
|
int maxBatteryValue = 0;
|
|
|
|
|
int tempBattery = 0;
|
|
|
|
|
uint tempStamp = 0;
|
|
|
|
|
double elapsedDeltaTime = 0.0;
|
|
|
|
|
uint tempDelta = 0;
|
|
|
|
|
byte tempByte = 0;
|
|
|
|
|
int CRC32_POS_1 = BT_INPUT_REPORT_CRC32_POS + 1,
|
|
|
|
|
CRC32_POS_2 = BT_INPUT_REPORT_CRC32_POS + 2,
|
|
|
|
|
CRC32_POS_3 = BT_INPUT_REPORT_CRC32_POS + 3;
|
|
|
|
|
int crcpos = BT_INPUT_REPORT_CRC32_POS;
|
|
|
|
|
int crcoffset = 0;
|
2018-11-16 12:24:47 +01:00
|
|
|
|
long latencySum = 0;
|
2019-01-09 10:45:39 +01:00
|
|
|
|
standbySw.Start();
|
2018-10-11 10:38:52 +02:00
|
|
|
|
|
|
|
|
|
while (!exitInputThread)
|
2017-05-19 02:51:01 +02:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
oldCharging = charging;
|
|
|
|
|
currerror = string.Empty;
|
|
|
|
|
|
|
|
|
|
if (tempLatencyCount >= 20)
|
|
|
|
|
{
|
2018-11-16 12:24:47 +01:00
|
|
|
|
latencySum -= latencyQueue.Dequeue();
|
2018-10-11 10:38:52 +02:00
|
|
|
|
tempLatencyCount--;
|
|
|
|
|
}
|
Shift modifier: Hold an action to use another set of controls, if nothing is set to the shifted control, in falls back to the default action
View input of controls in profiles, see exactly when a deadzone is passed and check the input delay for controllers (special thanks to jhebbel), click the on sixaxis panel
Click the Empty text on in the lightbar box to copy the lightbar color from full to empty.
While opened, option to keep the window size after closing the profile's settings
Old profiles are automatically upgraded if it's missing new settings, such as how colors are now saved, sixaxis deadzones, and shift controls
Other UI changes for profile settings, flipped touchpad and other settings boxes
Others:
Fix for when clicking the semicolon in the select an action screen
Fix assigning Sixaxis action to a key
minor UI changes and bug fixes, such as auto resize of the log listview
DS4Updater: Also now works for the new numbering system, can read the version number right from the exe instead of in profiles.xml, UI additions to better notify users of errors, Bug fixes for non-portable users
2014-07-07 21:22:42 +02:00
|
|
|
|
|
2018-11-16 12:24:47 +01:00
|
|
|
|
latencySum += this.lastTimeElapsed;
|
2018-10-11 10:38:52 +02:00
|
|
|
|
latencyQueue.Enqueue(this.lastTimeElapsed);
|
|
|
|
|
tempLatencyCount++;
|
2017-07-12 15:04:37 +02:00
|
|
|
|
|
2018-11-16 12:24:47 +01:00
|
|
|
|
//Latency = latencyQueue.Average();
|
|
|
|
|
Latency = latencySum / tempLatencyCount;
|
Shift modifier: Hold an action to use another set of controls, if nothing is set to the shifted control, in falls back to the default action
View input of controls in profiles, see exactly when a deadzone is passed and check the input delay for controllers (special thanks to jhebbel), click the on sixaxis panel
Click the Empty text on in the lightbar box to copy the lightbar color from full to empty.
While opened, option to keep the window size after closing the profile's settings
Old profiles are automatically upgraded if it's missing new settings, such as how colors are now saved, sixaxis deadzones, and shift controls
Other UI changes for profile settings, flipped touchpad and other settings boxes
Others:
Fix for when clicking the semicolon in the select an action screen
Fix assigning Sixaxis action to a key
minor UI changes and bug fixes, such as auto resize of the log listview
DS4Updater: Also now works for the new numbering system, can read the version number right from the exe instead of in profiles.xml, UI additions to better notify users of errors, Bug fixes for non-portable users
2014-07-07 21:22:42 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
if (conType == ConnectionType.BT)
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
//HidDevice.ReadStatus res = hDevice.ReadFile(btInputReport);
|
|
|
|
|
//HidDevice.ReadStatus res = hDevice.ReadAsyncWithFileStream(btInputReport, READ_STREAM_TIMEOUT);
|
|
|
|
|
HidDevice.ReadStatus res = hDevice.ReadWithFileStream(btInputReport);
|
|
|
|
|
timeoutEvent = false;
|
|
|
|
|
if (res == HidDevice.ReadStatus.Success)
|
2018-03-05 15:57:30 +01:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
//Array.Copy(btInputReport, 2, inputReport, 0, inputReport.Length);
|
|
|
|
|
fixed (byte* byteP = &btInputReport[2], imp = inputReport)
|
2018-03-05 15:57:30 +01:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
for (int j = 0; j < BT_INPUT_REPORT_LENGTH - 2; j++)
|
|
|
|
|
{
|
|
|
|
|
imp[j] = byteP[j];
|
|
|
|
|
}
|
2018-03-05 15:57:30 +01:00
|
|
|
|
}
|
2018-03-04 21:37:47 +01:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
//uint recvCrc32 = BitConverter.ToUInt32(btInputReport, BT_INPUT_REPORT_CRC32_POS);
|
|
|
|
|
uint recvCrc32 = btInputReport[BT_INPUT_REPORT_CRC32_POS] |
|
|
|
|
|
(uint)(btInputReport[CRC32_POS_1] << 8) |
|
|
|
|
|
(uint)(btInputReport[CRC32_POS_2] << 16) |
|
|
|
|
|
(uint)(btInputReport[CRC32_POS_3] << 24);
|
2018-03-04 21:37:47 +01:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
uint calcCrc32 = ~Crc32Algorithm.CalculateFasterBTHash(ref HamSeed, ref btInputReport, ref crcoffset, ref crcpos);
|
|
|
|
|
if (recvCrc32 != calcCrc32)
|
|
|
|
|
{
|
|
|
|
|
//Log.LogToGui("Crc check failed", true);
|
|
|
|
|
//Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "" +
|
|
|
|
|
// "> invalid CRC32 in BT input report: 0x" + recvCrc32.ToString("X8") + " expected: 0x" + calcCrc32.ToString("X8"));
|
2018-03-04 21:37:47 +01:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
cState.PacketCounter = pState.PacketCounter + 1; //still increase so we know there were lost packets
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-05-12 16:48:58 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
if (res == HidDevice.ReadStatus.WaitTimedOut)
|
|
|
|
|
{
|
|
|
|
|
AppLogger.LogToGui(Mac.ToString() + " disconnected due to timeout", true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int winError = Marshal.GetLastWin32Error();
|
|
|
|
|
Console.WriteLine(Mac.ToString() + " " + DateTime.UtcNow.ToString("o") + "> disconnect due to read failure: " + winError);
|
|
|
|
|
//Log.LogToGui(Mac.ToString() + " disconnected due to read failure: " + winError, true);
|
|
|
|
|
}
|
2017-05-12 16:48:58 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
sendOutputReport(true, true); // Kick Windows into noticing the disconnection.
|
|
|
|
|
StopOutputUpdate();
|
|
|
|
|
isDisconnecting = true;
|
|
|
|
|
Removal?.Invoke(this, EventArgs.Empty);
|
2017-05-09 12:11:50 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
timeoutExecuted = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
2018-10-11 10:38:52 +02:00
|
|
|
|
else
|
2014-04-27 21:32:09 +02:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
//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.ReadWithFileStream(inputReport);
|
|
|
|
|
if (res != HidDevice.ReadStatus.Success)
|
2017-05-12 16:48:58 +02:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
if (res == HidDevice.ReadStatus.WaitTimedOut)
|
|
|
|
|
{
|
|
|
|
|
AppLogger.LogToGui(Mac.ToString() + " disconnected due to timeout", true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int winError = Marshal.GetLastWin32Error();
|
|
|
|
|
Console.WriteLine(Mac.ToString() + " " + DateTime.UtcNow.ToString("o") + "> disconnect due to read failure: " + winError);
|
|
|
|
|
//Log.LogToGui(Mac.ToString() + " disconnected due to read failure: " + winError, true);
|
|
|
|
|
}
|
2017-05-12 16:48:58 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
StopOutputUpdate();
|
|
|
|
|
isDisconnecting = true;
|
|
|
|
|
Removal?.Invoke(this, EventArgs.Empty);
|
2017-05-09 12:11:50 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
timeoutExecuted = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-04-27 21:32:09 +02:00
|
|
|
|
}
|
2017-04-06 20:58:47 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
curTimeDouble = sw.Elapsed.TotalMilliseconds;
|
|
|
|
|
curtime = sw.ElapsedMilliseconds;
|
2017-07-12 15:04:37 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
lastTimeElapsed = curtime - oldtime;
|
|
|
|
|
lastTimeElapsedDouble = (curTimeDouble - oldTimeDouble);
|
2017-07-12 15:04:37 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
oldtime = curtime;
|
|
|
|
|
oldTimeDouble = curTimeDouble;
|
2017-07-12 15:04:37 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
if (conType == ConnectionType.BT && btInputReport[0] != 0x11)
|
|
|
|
|
{
|
|
|
|
|
//Received incorrect report, skip it
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-04-06 20:58:47 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
utcNow = DateTime.UtcNow; // timestamp with UTC in case system time zone changes
|
2019-01-11 02:07:49 +01:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
cState.PacketCounter = pState.PacketCounter + 1;
|
|
|
|
|
cState.ReportTimeStamp = utcNow;
|
|
|
|
|
cState.LX = inputReport[1];
|
|
|
|
|
cState.LY = inputReport[2];
|
|
|
|
|
cState.RX = inputReport[3];
|
|
|
|
|
cState.RY = inputReport[4];
|
|
|
|
|
cState.L2 = inputReport[8];
|
|
|
|
|
cState.R2 = inputReport[9];
|
|
|
|
|
|
|
|
|
|
tempByte = inputReport[5];
|
|
|
|
|
cState.Triangle = (tempByte & (1 << 7)) != 0;
|
|
|
|
|
cState.Circle = (tempByte & (1 << 6)) != 0;
|
|
|
|
|
cState.Cross = (tempByte & (1 << 5)) != 0;
|
|
|
|
|
cState.Square = (tempByte & (1 << 4)) != 0;
|
|
|
|
|
|
|
|
|
|
// First 4 bits denote dpad state. Clock representation
|
|
|
|
|
// with 8 meaning centered and 0 meaning DpadUp.
|
|
|
|
|
byte dpad_state = (byte)(tempByte & 0x0F);
|
|
|
|
|
|
|
|
|
|
switch (dpad_state)
|
|
|
|
|
{
|
|
|
|
|
case 0: cState.DpadUp = true; cState.DpadDown = false; cState.DpadLeft = false; cState.DpadRight = false; break;
|
|
|
|
|
case 1: cState.DpadUp = true; cState.DpadDown = false; cState.DpadLeft = false; cState.DpadRight = true; break;
|
|
|
|
|
case 2: cState.DpadUp = false; cState.DpadDown = false; cState.DpadLeft = false; cState.DpadRight = true; break;
|
|
|
|
|
case 3: cState.DpadUp = false; cState.DpadDown = true; cState.DpadLeft = false; cState.DpadRight = true; break;
|
|
|
|
|
case 4: cState.DpadUp = false; cState.DpadDown = true; cState.DpadLeft = false; cState.DpadRight = false; break;
|
|
|
|
|
case 5: cState.DpadUp = false; cState.DpadDown = true; cState.DpadLeft = true; cState.DpadRight = false; break;
|
|
|
|
|
case 6: cState.DpadUp = false; cState.DpadDown = false; cState.DpadLeft = true; cState.DpadRight = false; break;
|
|
|
|
|
case 7: cState.DpadUp = true; cState.DpadDown = false; cState.DpadLeft = true; cState.DpadRight = false; break;
|
|
|
|
|
case 8:
|
|
|
|
|
default: cState.DpadUp = false; cState.DpadDown = false; cState.DpadLeft = false; cState.DpadRight = false; break;
|
|
|
|
|
}
|
2014-03-28 02:50:40 +01:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
tempByte = inputReport[6];
|
|
|
|
|
cState.R3 = (tempByte & (1 << 7)) != 0;
|
|
|
|
|
cState.L3 = (tempByte & (1 << 6)) != 0;
|
|
|
|
|
cState.Options = (tempByte & (1 << 5)) != 0;
|
|
|
|
|
cState.Share = (tempByte & (1 << 4)) != 0;
|
|
|
|
|
cState.R2Btn = (inputReport[6] & (1 << 3)) != 0;
|
|
|
|
|
cState.L2Btn = (inputReport[6] & (1 << 2)) != 0;
|
|
|
|
|
cState.R1 = (tempByte & (1 << 1)) != 0;
|
|
|
|
|
cState.L1 = (tempByte & (1 << 0)) != 0;
|
|
|
|
|
|
|
|
|
|
tempByte = inputReport[7];
|
|
|
|
|
cState.PS = (tempByte & (1 << 0)) != 0;
|
|
|
|
|
cState.TouchButton = (tempByte & 0x02) != 0;
|
|
|
|
|
cState.FrameCounter = (byte)(tempByte >> 2);
|
|
|
|
|
|
|
|
|
|
tempByte = inputReport[30];
|
|
|
|
|
charging = (tempByte & 0x10) != 0;
|
|
|
|
|
maxBatteryValue = charging ? BATTERY_MAX_USB : BATTERY_MAX;
|
|
|
|
|
tempBattery = (tempByte & 0x0f) * 100 / maxBatteryValue;
|
|
|
|
|
battery = Math.Min((byte)tempBattery, (byte)100);
|
|
|
|
|
cState.Battery = (byte)battery;
|
|
|
|
|
//System.Diagnostics.Debug.WriteLine("CURRENT BATTERY: " + (inputReport[30] & 0x0f) + " | " + tempBattery + " | " + battery);
|
|
|
|
|
if (tempByte != priorInputReport30)
|
|
|
|
|
{
|
|
|
|
|
priorInputReport30 = tempByte;
|
|
|
|
|
//Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> power subsystem octet: 0x" + inputReport[30].ToString("x02"));
|
|
|
|
|
}
|
2017-04-06 20:58:47 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
tempStamp = (uint)((ushort)(inputReport[11] << 8) | inputReport[10]);
|
|
|
|
|
if (timeStampInit == false)
|
|
|
|
|
{
|
|
|
|
|
timeStampInit = true;
|
|
|
|
|
deltaTimeCurrent = tempStamp * 16u / 3u;
|
|
|
|
|
}
|
|
|
|
|
else if (timeStampPrevious > tempStamp)
|
|
|
|
|
{
|
|
|
|
|
tempDelta = ushort.MaxValue - timeStampPrevious + tempStamp + 1u;
|
|
|
|
|
deltaTimeCurrent = tempDelta * 16u / 3u;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tempDelta = tempStamp - timeStampPrevious;
|
|
|
|
|
deltaTimeCurrent = tempDelta * 16u / 3u;
|
|
|
|
|
}
|
2018-01-03 10:55:40 +01:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
timeStampPrevious = tempStamp;
|
|
|
|
|
elapsedDeltaTime = 0.000001 * deltaTimeCurrent; // Convert from microseconds to seconds
|
|
|
|
|
cState.elapsedTime = elapsedDeltaTime;
|
|
|
|
|
cState.totalMicroSec = pState.totalMicroSec + deltaTimeCurrent;
|
2018-05-06 09:10:49 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
//Simpler touch storing
|
|
|
|
|
cState.TrackPadTouch0.Id = (byte)(inputReport[35] & 0x7f);
|
|
|
|
|
cState.TrackPadTouch0.IsActive = (inputReport[35] & 0x80) == 0;
|
|
|
|
|
cState.TrackPadTouch0.X = (short)(((ushort)(inputReport[37] & 0x0f) << 8) | (ushort)(inputReport[36]));
|
|
|
|
|
cState.TrackPadTouch0.Y = (short)(((ushort)(inputReport[38]) << 4) | ((ushort)(inputReport[37] & 0xf0) >> 4));
|
2018-05-06 09:10:49 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
cState.TrackPadTouch1.Id = (byte)(inputReport[39] & 0x7f);
|
|
|
|
|
cState.TrackPadTouch1.IsActive = (inputReport[39] & 0x80) == 0;
|
|
|
|
|
cState.TrackPadTouch1.X = (short)(((ushort)(inputReport[41] & 0x0f) << 8) | (ushort)(inputReport[40]));
|
|
|
|
|
cState.TrackPadTouch1.Y = (short)(((ushort)(inputReport[42]) << 4) | ((ushort)(inputReport[41] & 0xf0) >> 4));
|
2018-01-03 10:55:40 +01:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
// XXX DS4State mapping needs fixup, turn touches into an array[4] of structs. And include the touchpad details there instead.
|
|
|
|
|
try
|
2014-12-17 19:29:22 +01:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
// Only care if one touch packet is detected. Other touch packets
|
|
|
|
|
// don't seem to contain relevant data. ds4drv does not use them either.
|
|
|
|
|
for (int touches = Math.Max((int)(inputReport[-1 + DS4Touchpad.TOUCHPAD_DATA_OFFSET - 1]), 1), touchOffset = 0; touches > 0; touches--, touchOffset += 9)
|
|
|
|
|
//for (int touches = inputReport[-1 + DS4Touchpad.TOUCHPAD_DATA_OFFSET - 1], touchOffset = 0; touches > 0; touches--, touchOffset += 9)
|
|
|
|
|
{
|
|
|
|
|
cState.TouchPacketCounter = inputReport[-1 + DS4Touchpad.TOUCHPAD_DATA_OFFSET + touchOffset];
|
|
|
|
|
cState.Touch1 = (inputReport[0 + DS4Touchpad.TOUCHPAD_DATA_OFFSET + touchOffset] >> 7) != 0 ? false : true; // finger 1 detected
|
|
|
|
|
cState.Touch1Identifier = (byte)(inputReport[0 + DS4Touchpad.TOUCHPAD_DATA_OFFSET + touchOffset] & 0x7f);
|
|
|
|
|
cState.Touch2 = (inputReport[4 + DS4Touchpad.TOUCHPAD_DATA_OFFSET + touchOffset] >> 7) != 0 ? false : true; // finger 2 detected
|
|
|
|
|
cState.Touch2Identifier = (byte)(inputReport[4 + DS4Touchpad.TOUCHPAD_DATA_OFFSET + touchOffset] & 0x7f);
|
|
|
|
|
cState.Touch1Finger = cState.Touch1 || cState.Touch2; // >= 1 touch detected
|
|
|
|
|
cState.Touch2Fingers = cState.Touch1 && cState.Touch2; // 2 touches detected
|
|
|
|
|
int touchX = (((inputReport[2 + DS4Touchpad.TOUCHPAD_DATA_OFFSET + touchOffset] & 0xF) << 8) | inputReport[1 + DS4Touchpad.TOUCHPAD_DATA_OFFSET + touchOffset]);
|
|
|
|
|
cState.TouchLeft = touchX >= 1920 * 2 / 5 ? false : true;
|
|
|
|
|
cState.TouchRight = touchX < 1920 * 2 / 5 ? false : true;
|
|
|
|
|
// Even when idling there is still a touch packet indicating no touch 1 or 2
|
|
|
|
|
touchpad.handleTouchpad(inputReport, cState, touchOffset);
|
|
|
|
|
}
|
2014-12-17 19:29:22 +01:00
|
|
|
|
}
|
2018-10-11 10:38:52 +02:00
|
|
|
|
catch { currerror = "Index out of bounds: touchpad"; }
|
2017-04-06 20:58:47 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
// Store Gyro and Accel values
|
|
|
|
|
//Array.Copy(inputReport, 13, gyro, 0, 6);
|
|
|
|
|
//Array.Copy(inputReport, 19, accel, 0, 6);
|
|
|
|
|
fixed (byte* pbInput = &inputReport[13], pbGyro = gyro, pbAccel = accel)
|
2018-03-07 06:20:26 +01:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
|
{
|
|
|
|
|
pbGyro[i] = pbInput[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 6; i < 12; i++)
|
|
|
|
|
{
|
|
|
|
|
pbAccel[i - 6] = pbInput[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sixAxis.handleSixaxis(pbGyro, pbAccel, cState, elapsedDeltaTime);
|
2018-03-07 06:20:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
/* Debug output of incoming HID data:
|
|
|
|
|
if (cState.L2 == 0xff && cState.R2 == 0xff)
|
2018-03-07 06:20:26 +01:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
Console.Write(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + ">");
|
|
|
|
|
for (int i = 0; i < inputReport.Length; i++)
|
|
|
|
|
Console.Write(" " + inputReport[i].ToString("x2"));
|
|
|
|
|
Console.WriteLine();
|
2018-03-07 06:20:26 +01:00
|
|
|
|
}
|
2018-10-11 10:38:52 +02:00
|
|
|
|
*/
|
2018-07-10 07:46:25 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
if (conType == ConnectionType.SONYWA)
|
2017-05-25 11:51:28 +02:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
bool controllerSynced = inputReport[31] == 0;
|
|
|
|
|
if (controllerSynced != synced)
|
|
|
|
|
{
|
|
|
|
|
runCalib = synced = controllerSynced;
|
|
|
|
|
SyncChange?.Invoke(this, EventArgs.Empty);
|
2019-03-14 00:41:10 +01:00
|
|
|
|
if (synced)
|
|
|
|
|
{
|
|
|
|
|
forceWrite = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
standbySw.Reset();
|
|
|
|
|
}
|
2018-10-11 10:38:52 +02:00
|
|
|
|
}
|
2018-04-13 12:33:59 +02:00
|
|
|
|
}
|
2017-05-25 11:51:28 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
ds4InactiveFrame = cState.FrameCounter == pState.FrameCounter;
|
|
|
|
|
if (!ds4InactiveFrame)
|
2017-06-23 07:14:58 +02:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
isRemoved = false;
|
2017-06-23 07:14:58 +02:00
|
|
|
|
}
|
2018-10-11 10:38:52 +02:00
|
|
|
|
|
|
|
|
|
if (conType == ConnectionType.USB)
|
2017-06-23 07:14:58 +02:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
if (idleTimeout == 0)
|
2017-06-25 04:10:09 +02:00
|
|
|
|
{
|
|
|
|
|
lastActive = utcNow;
|
|
|
|
|
}
|
2018-10-11 10:38:52 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
idleInput = isDS4Idle();
|
|
|
|
|
if (!idleInput)
|
|
|
|
|
{
|
|
|
|
|
lastActive = utcNow;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-23 07:14:58 +02:00
|
|
|
|
}
|
2018-10-11 10:38:52 +02:00
|
|
|
|
else
|
2014-04-27 21:32:09 +02:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
bool shouldDisconnect = false;
|
|
|
|
|
if (!isRemoved && idleTimeout > 0)
|
2014-04-27 21:32:09 +02:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
idleInput = isDS4Idle();
|
|
|
|
|
if (idleInput)
|
|
|
|
|
{
|
|
|
|
|
DateTime timeout = lastActive + TimeSpan.FromSeconds(idleTimeout);
|
|
|
|
|
if (!charging)
|
|
|
|
|
shouldDisconnect = utcNow >= timeout;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
lastActive = utcNow;
|
|
|
|
|
}
|
2014-04-27 21:32:09 +02:00
|
|
|
|
}
|
2017-04-06 22:05:16 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
lastActive = utcNow;
|
|
|
|
|
}
|
2017-05-12 16:48:58 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
if (shouldDisconnect)
|
2017-04-07 05:24:16 +02:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
AppLogger.LogToGui(Mac.ToString() + " disconnecting due to idle disconnect", false);
|
|
|
|
|
|
|
|
|
|
if (conType == ConnectionType.BT)
|
2017-06-10 08:45:19 +02:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
if (DisconnectBT(true))
|
|
|
|
|
{
|
|
|
|
|
timeoutExecuted = true;
|
|
|
|
|
return; // all done
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (conType == ConnectionType.SONYWA)
|
|
|
|
|
{
|
|
|
|
|
DisconnectDongle();
|
2017-06-10 08:45:19 +02:00
|
|
|
|
}
|
2017-04-07 05:24:16 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-06 20:58:47 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
if (conType == ConnectionType.BT && oldCharging != charging)
|
2017-06-10 08:45:19 +02:00
|
|
|
|
{
|
2018-10-11 10:38:52 +02:00
|
|
|
|
if (Global.getQuickCharge() && charging)
|
|
|
|
|
{
|
|
|
|
|
DisconnectBT(true);
|
|
|
|
|
timeoutExecuted = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-06-10 08:45:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
if (Report != null)
|
|
|
|
|
Report(this, EventArgs.Empty);
|
2017-04-16 08:22:04 +02:00
|
|
|
|
|
2019-03-14 00:41:10 +01:00
|
|
|
|
sendOutputReport(syncWriteReport, forceWrite);
|
|
|
|
|
forceWrite = false;
|
2017-04-16 08:22:04 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(currerror))
|
|
|
|
|
error = currerror;
|
|
|
|
|
else if (!string.IsNullOrEmpty(error))
|
|
|
|
|
error = string.Empty;
|
2017-04-16 08:22:04 +02:00
|
|
|
|
|
2018-10-11 10:38:52 +02:00
|
|
|
|
cState.CopyTo(pState);
|
2017-05-14 00:01:43 +02:00
|
|
|
|
|
2018-10-25 09:51:50 +02:00
|
|
|
|
if (hasInputEvts)
|
2017-05-14 00:01:43 +02:00
|
|
|
|
{
|
2018-10-25 09:51:50 +02:00
|
|
|
|
lock (eventQueueLock)
|
2018-10-15 10:01:13 +02:00
|
|
|
|
{
|
2018-10-25 09:51:50 +02:00
|
|
|
|
Action tempAct = null;
|
|
|
|
|
for (int actInd = 0, actLen = eventQueue.Count; actInd < actLen; actInd++)
|
|
|
|
|
{
|
|
|
|
|
tempAct = eventQueue.Dequeue();
|
|
|
|
|
tempAct.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasInputEvts = false;
|
2018-10-15 10:01:13 +02:00
|
|
|
|
}
|
2017-05-14 00:01:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
2017-06-01 05:37:53 +02:00
|
|
|
|
|
|
|
|
|
timeoutExecuted = true;
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-18 22:23:41 +01:00
|
|
|
|
public void FlushHID()
|
|
|
|
|
{
|
|
|
|
|
hDevice.flush_Queue();
|
|
|
|
|
}
|
2017-04-12 04:26:08 +02:00
|
|
|
|
|
2018-05-06 08:59:45 +02:00
|
|
|
|
private unsafe void sendOutputReport(bool synchronous, bool force = false)
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
2019-01-11 01:15:14 +01:00
|
|
|
|
MergeStates();
|
|
|
|
|
//setTestRumble();
|
|
|
|
|
//setHapticState();
|
2017-04-22 09:26:44 +02:00
|
|
|
|
|
2017-10-07 07:55:14 +02:00
|
|
|
|
bool quitOutputThread = false;
|
2017-12-10 12:09:15 +01:00
|
|
|
|
bool usingBT = conType == ConnectionType.BT;
|
|
|
|
|
|
2017-12-11 07:33:38 +01:00
|
|
|
|
lock (outReportBuffer)
|
2017-12-10 12:09:15 +01:00
|
|
|
|
{
|
2018-05-06 08:59:45 +02:00
|
|
|
|
bool output = outputPendCount > 0, change = force;
|
2019-01-16 11:08:51 +01:00
|
|
|
|
bool haptime = output || standbySw.ElapsedMilliseconds >= 4000L;
|
2018-04-21 23:44:49 +02:00
|
|
|
|
|
2017-12-10 13:16:40 +01:00
|
|
|
|
if (usingBT)
|
|
|
|
|
{
|
2017-12-11 07:33:38 +01:00
|
|
|
|
outReportBuffer[0] = 0x11;
|
|
|
|
|
outReportBuffer[1] = (byte)(0x80 | btPollRate); // input report rate
|
|
|
|
|
// enable rumble (0x01), lightbar (0x02), flash (0x04)
|
|
|
|
|
outReportBuffer[3] = 0xf7;
|
2019-01-11 01:15:14 +01:00
|
|
|
|
outReportBuffer[6] = currentHap.RumbleMotorStrengthRightLightFast; // fast motor
|
|
|
|
|
outReportBuffer[7] = currentHap.RumbleMotorStrengthLeftHeavySlow; // slow motor
|
|
|
|
|
outReportBuffer[8] = currentHap.LightBarColor.red; // red
|
|
|
|
|
outReportBuffer[9] = currentHap.LightBarColor.green; // green
|
|
|
|
|
outReportBuffer[10] = currentHap.LightBarColor.blue; // blue
|
|
|
|
|
outReportBuffer[11] = currentHap.LightBarFlashDurationOn; // flash on duration
|
|
|
|
|
outReportBuffer[12] = currentHap.LightBarFlashDurationOff; // flash off duration
|
2018-04-21 23:44:49 +02:00
|
|
|
|
|
|
|
|
|
fixed (byte* byteR = outputReport, byteB = outReportBuffer)
|
|
|
|
|
{
|
2018-05-28 22:06:48 +02:00
|
|
|
|
for (int i = 0, arlen = BT_OUTPUT_CHANGE_LENGTH; !change && i < arlen; i++)
|
2018-04-21 23:44:49 +02:00
|
|
|
|
change = byteR[i] != byteB[i];
|
|
|
|
|
}
|
2019-01-16 11:08:51 +01:00
|
|
|
|
|
|
|
|
|
haptime = haptime || change;
|
2017-12-10 13:16:40 +01:00
|
|
|
|
}
|
2017-12-11 07:33:38 +01:00
|
|
|
|
else
|
2017-10-07 07:55:14 +02:00
|
|
|
|
{
|
2017-12-11 07:33:38 +01:00
|
|
|
|
outReportBuffer[0] = 0x05;
|
|
|
|
|
// enable rumble (0x01), lightbar (0x02), flash (0x04)
|
|
|
|
|
outReportBuffer[1] = 0xf7;
|
2019-01-11 01:15:14 +01:00
|
|
|
|
outReportBuffer[4] = currentHap.RumbleMotorStrengthRightLightFast; // fast motor
|
|
|
|
|
outReportBuffer[5] = currentHap.RumbleMotorStrengthLeftHeavySlow; // slow motor
|
|
|
|
|
outReportBuffer[6] = currentHap.LightBarColor.red; // red
|
|
|
|
|
outReportBuffer[7] = currentHap.LightBarColor.green; // green
|
|
|
|
|
outReportBuffer[8] = currentHap.LightBarColor.blue; // blue
|
|
|
|
|
outReportBuffer[9] = currentHap.LightBarFlashDurationOn; // flash on duration
|
|
|
|
|
outReportBuffer[10] = currentHap.LightBarFlashDurationOff; // flash off duration
|
2018-04-21 23:44:49 +02:00
|
|
|
|
|
|
|
|
|
fixed (byte* byteR = outputReport, byteB = outReportBuffer)
|
|
|
|
|
{
|
2018-05-28 22:06:48 +02:00
|
|
|
|
for (int i = 0, arlen = USB_OUTPUT_CHANGE_LENGTH; !change && i < arlen; i++)
|
2018-04-21 23:44:49 +02:00
|
|
|
|
change = byteR[i] != byteB[i];
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-16 11:08:51 +01:00
|
|
|
|
haptime = haptime || change;
|
|
|
|
|
if (haptime && audio != null)
|
2017-10-07 07:55:14 +02:00
|
|
|
|
{
|
2017-12-11 07:33:38 +01:00
|
|
|
|
// Headphone volume levels
|
|
|
|
|
outReportBuffer[19] = outReportBuffer[20] =
|
|
|
|
|
Convert.ToByte(audio.getVolume());
|
|
|
|
|
// Microphone volume level
|
|
|
|
|
outReportBuffer[21] = Convert.ToByte(micAudio.getVolume());
|
2017-10-07 07:55:14 +02:00
|
|
|
|
}
|
2017-05-09 12:11:50 +02:00
|
|
|
|
}
|
2017-11-15 09:14:20 +01:00
|
|
|
|
|
2017-12-11 07:33:38 +01:00
|
|
|
|
if (synchronous)
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
2019-01-16 11:08:51 +01:00
|
|
|
|
if (output || haptime)
|
2018-04-16 12:03:47 +02:00
|
|
|
|
{
|
2019-01-09 12:41:20 +01:00
|
|
|
|
if (change)
|
|
|
|
|
{
|
|
|
|
|
outputPendCount = 3;
|
2019-01-10 02:30:05 +01:00
|
|
|
|
standbySw.Reset();
|
2019-01-09 12:41:20 +01:00
|
|
|
|
}
|
2019-01-10 06:57:22 +01:00
|
|
|
|
else if (outputPendCount > 1)
|
2019-01-09 12:41:20 +01:00
|
|
|
|
outputPendCount--;
|
2019-01-10 06:57:22 +01:00
|
|
|
|
else if (outputPendCount == 1)
|
|
|
|
|
{
|
|
|
|
|
outputPendCount--;
|
|
|
|
|
standbySw.Restart();
|
|
|
|
|
}
|
|
|
|
|
else
|
2019-01-10 02:30:05 +01:00
|
|
|
|
standbySw.Restart();
|
2019-01-09 10:45:39 +01:00
|
|
|
|
|
2018-04-21 23:44:49 +02:00
|
|
|
|
if (usingBT)
|
|
|
|
|
{
|
|
|
|
|
Monitor.Enter(outputReport);
|
2018-10-26 08:37:51 +02:00
|
|
|
|
outReportBuffer.CopyTo(outputReport, 0);
|
2018-04-21 23:44:49 +02:00
|
|
|
|
}
|
2017-12-10 12:09:15 +01:00
|
|
|
|
|
2018-04-21 23:44:49 +02:00
|
|
|
|
try
|
2017-12-11 07:33:38 +01:00
|
|
|
|
{
|
2018-04-21 23:44:49 +02:00
|
|
|
|
if (!writeOutput())
|
|
|
|
|
{
|
|
|
|
|
int winError = Marshal.GetLastWin32Error();
|
|
|
|
|
quitOutputThread = true;
|
|
|
|
|
}
|
2017-12-11 07:33:38 +01:00
|
|
|
|
}
|
2018-04-21 23:44:49 +02:00
|
|
|
|
catch { } // If it's dead already, don't worry about it.
|
2017-12-11 07:33:38 +01:00
|
|
|
|
|
2018-04-21 23:44:49 +02:00
|
|
|
|
if (usingBT)
|
|
|
|
|
{
|
|
|
|
|
Monitor.Exit(outputReport);
|
|
|
|
|
}
|
2018-10-27 03:58:43 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Monitor.Pulse(outReportBuffer);
|
|
|
|
|
}
|
2018-04-16 12:03:47 +02:00
|
|
|
|
}
|
2017-12-11 07:33:38 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-03-07 00:01:18 +01:00
|
|
|
|
//for (int i = 0, arlen = outputReport.Length; !change && i < arlen; i++)
|
|
|
|
|
// change = outputReport[i] != outReportBuffer[i];
|
2017-12-11 07:33:38 +01:00
|
|
|
|
|
2019-01-16 11:08:51 +01:00
|
|
|
|
if (output || haptime)
|
2017-12-11 07:33:38 +01:00
|
|
|
|
{
|
|
|
|
|
if (change)
|
|
|
|
|
{
|
|
|
|
|
outputPendCount = 3;
|
2019-01-10 02:30:05 +01:00
|
|
|
|
standbySw.Reset();
|
2017-12-11 07:33:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Monitor.Pulse(outReportBuffer);
|
|
|
|
|
}
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-30 02:44:10 +02:00
|
|
|
|
|
|
|
|
|
if (quitOutputThread)
|
|
|
|
|
{
|
|
|
|
|
StopOutputUpdate();
|
2018-03-26 06:36:46 +02:00
|
|
|
|
exitOutputThread = true;
|
2017-03-30 02:44:10 +02:00
|
|
|
|
}
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-27 03:58:43 +02:00
|
|
|
|
public void OutReportCopy()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
while (!exitOutputThread)
|
|
|
|
|
{
|
|
|
|
|
lock (outReportBuffer)
|
|
|
|
|
{
|
|
|
|
|
outReportBuffer.CopyTo(outputReport, 0);
|
|
|
|
|
Monitor.Wait(outReportBuffer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (ThreadInterruptedException) { }
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-01 11:29:19 +02:00
|
|
|
|
public bool DisconnectBT(bool callRemoval = false)
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
|
|
|
|
if (Mac != null)
|
|
|
|
|
{
|
2017-11-12 05:52:17 +01:00
|
|
|
|
// Wait for output report to be written
|
|
|
|
|
StopOutputUpdate();
|
2014-04-27 21:32:09 +02:00
|
|
|
|
Console.WriteLine("Trying to disconnect BT device " + Mac);
|
2014-03-28 02:50:40 +01:00
|
|
|
|
IntPtr btHandle = IntPtr.Zero;
|
|
|
|
|
int IOCTL_BTH_DISCONNECT_DEVICE = 0x41000c;
|
|
|
|
|
|
|
|
|
|
byte[] btAddr = new byte[8];
|
|
|
|
|
string[] sbytes = Mac.Split(':');
|
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
|
{
|
2017-04-28 20:57:33 +02:00
|
|
|
|
// parse hex byte in reverse order
|
2014-03-28 02:50:40 +01:00
|
|
|
|
btAddr[5 - i] = Convert.ToByte(sbytes[i], 16);
|
|
|
|
|
}
|
2017-04-27 03:39:33 +02:00
|
|
|
|
|
2014-03-28 02:50:40 +01:00
|
|
|
|
long lbtAddr = BitConverter.ToInt64(btAddr, 0);
|
|
|
|
|
|
|
|
|
|
bool success = false;
|
2017-05-01 11:29:19 +02:00
|
|
|
|
|
2017-11-15 09:14:20 +01:00
|
|
|
|
lock (outputReport)
|
2017-11-12 05:52:17 +01:00
|
|
|
|
{
|
2017-11-15 09:14:20 +01:00
|
|
|
|
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)
|
2017-04-27 03:39:33 +02:00
|
|
|
|
{
|
2017-11-15 09:14:20 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
2017-04-27 03:39:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-15 09:14:20 +01:00
|
|
|
|
NativeMethods.BluetoothFindRadioClose(searchHandle);
|
|
|
|
|
Console.WriteLine("Disconnect successful: " + success);
|
|
|
|
|
}
|
2017-11-12 05:52:17 +01:00
|
|
|
|
|
2014-03-29 06:29:08 +01:00
|
|
|
|
success = true; // XXX return value indicates failure, but it still works?
|
2017-04-27 03:39:33 +02:00
|
|
|
|
if (success)
|
2014-03-29 06:29:08 +01:00
|
|
|
|
{
|
|
|
|
|
IsDisconnecting = true;
|
2017-04-22 16:49:01 +02:00
|
|
|
|
|
2017-05-01 11:29:19 +02:00
|
|
|
|
if (callRemoval)
|
|
|
|
|
{
|
2018-08-20 11:35:41 +02:00
|
|
|
|
Removal?.Invoke(this, EventArgs.Empty);
|
2017-06-11 17:15:45 +02:00
|
|
|
|
|
|
|
|
|
//System.Threading.Tasks.Task.Factory.StartNew(() => { Removal?.Invoke(this, EventArgs.Empty); });
|
2017-05-01 11:29:19 +02:00
|
|
|
|
}
|
2014-03-29 06:29:08 +01:00
|
|
|
|
}
|
2017-04-27 03:39:33 +02:00
|
|
|
|
|
2014-03-28 02:50:40 +01:00
|
|
|
|
return success;
|
|
|
|
|
}
|
2017-04-27 03:39:33 +02:00
|
|
|
|
|
2014-03-28 02:50:40 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-09 01:13:56 +02:00
|
|
|
|
public bool DisconnectDongle(bool remove = false)
|
2017-04-06 03:37:38 +02:00
|
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
byte[] disconnectReport = new byte[65];
|
|
|
|
|
disconnectReport[0] = 0xe2;
|
|
|
|
|
disconnectReport[1] = 0x02;
|
2017-04-09 01:13:56 +02:00
|
|
|
|
Array.Clear(disconnectReport, 2, 63);
|
2017-04-06 17:30:41 +02:00
|
|
|
|
|
2017-11-12 05:52:17 +01:00
|
|
|
|
if (remove)
|
|
|
|
|
StopOutputUpdate();
|
|
|
|
|
|
2017-11-15 09:14:20 +01:00
|
|
|
|
lock (outputReport)
|
|
|
|
|
{
|
|
|
|
|
result = hDevice.WriteFeatureReport(disconnectReport);
|
|
|
|
|
}
|
2017-05-01 11:50:02 +02:00
|
|
|
|
|
2017-04-06 17:30:41 +02:00
|
|
|
|
if (result && remove)
|
2017-04-06 03:37:38 +02:00
|
|
|
|
{
|
2017-04-09 01:13:56 +02:00
|
|
|
|
isDisconnecting = true;
|
2017-04-27 03:39:33 +02:00
|
|
|
|
|
2018-08-20 11:35:41 +02:00
|
|
|
|
Removal?.Invoke(this, EventArgs.Empty);
|
2017-06-11 17:15:45 +02:00
|
|
|
|
|
|
|
|
|
//System.Threading.Tasks.Task.Factory.StartNew(() => { Removal?.Invoke(this, EventArgs.Empty); });
|
|
|
|
|
//Removal?.Invoke(this, EventArgs.Empty);
|
2017-04-06 03:37:38 +02:00
|
|
|
|
}
|
2017-04-09 01:13:56 +02:00
|
|
|
|
else if (result && !remove)
|
|
|
|
|
{
|
|
|
|
|
isRemoved = true;
|
|
|
|
|
}
|
2017-04-06 03:37:38 +02:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-29 06:29:08 +01:00
|
|
|
|
private DS4HapticState testRumble = new DS4HapticState();
|
2017-04-22 06:22:36 +02:00
|
|
|
|
|
2014-03-29 06:29:08 +01:00
|
|
|
|
public void setRumble(byte rightLightFastMotor, byte leftHeavySlowMotor)
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
2014-03-29 06:29:08 +01:00
|
|
|
|
testRumble.RumbleMotorStrengthRightLightFast = rightLightFastMotor;
|
|
|
|
|
testRumble.RumbleMotorStrengthLeftHeavySlow = leftHeavySlowMotor;
|
|
|
|
|
testRumble.RumbleMotorsExplicitlyOff = rightLightFastMotor == 0 && leftHeavySlowMotor == 0;
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-11 01:15:14 +01:00
|
|
|
|
private void MergeStates()
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
2014-03-29 06:29:08 +01:00
|
|
|
|
if (testRumble.IsRumbleSet())
|
|
|
|
|
{
|
|
|
|
|
if (testRumble.RumbleMotorsExplicitlyOff)
|
|
|
|
|
testRumble.RumbleMotorsExplicitlyOff = false;
|
2019-01-11 01:15:14 +01:00
|
|
|
|
|
|
|
|
|
currentHap.RumbleMotorStrengthLeftHeavySlow = testRumble.RumbleMotorStrengthLeftHeavySlow;
|
|
|
|
|
currentHap.RumbleMotorStrengthRightLightFast = testRumble.RumbleMotorStrengthRightLightFast;
|
2014-03-29 06:29:08 +01:00
|
|
|
|
}
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DS4State getCurrentState()
|
|
|
|
|
{
|
2014-03-29 06:29:08 +01:00
|
|
|
|
return cState.Clone();
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DS4State getPreviousState()
|
|
|
|
|
{
|
2014-03-29 06:29:08 +01:00
|
|
|
|
return pState.Clone();
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void getCurrentState(DS4State state)
|
|
|
|
|
{
|
2014-04-27 21:32:09 +02:00
|
|
|
|
cState.CopyTo(state);
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void getPreviousState(DS4State state)
|
|
|
|
|
{
|
2014-04-27 21:32:09 +02:00
|
|
|
|
pState.CopyTo(state);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 13:13:18 +02:00
|
|
|
|
public DS4State getCurrentStateRef()
|
|
|
|
|
{
|
|
|
|
|
return cState;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 04:38:34 +02:00
|
|
|
|
public DS4State getPreviousStateRef()
|
|
|
|
|
{
|
|
|
|
|
return pState;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-06 09:10:49 +02:00
|
|
|
|
public bool isDS4Idle()
|
2014-04-27 21:32:09 +02:00
|
|
|
|
{
|
|
|
|
|
if (cState.Square || cState.Cross || cState.Circle || cState.Triangle)
|
|
|
|
|
return false;
|
|
|
|
|
if (cState.DpadUp || cState.DpadLeft || cState.DpadDown || cState.DpadRight)
|
|
|
|
|
return false;
|
|
|
|
|
if (cState.L3 || cState.R3 || cState.L1 || cState.R1 || cState.Share || cState.Options)
|
|
|
|
|
return false;
|
|
|
|
|
if (cState.L2 != 0 || cState.R2 != 0)
|
|
|
|
|
return false;
|
|
|
|
|
// TODO calibrate to get an accurate jitter and center-play range and centered position
|
|
|
|
|
const int slop = 64;
|
|
|
|
|
if (cState.LX <= 127 - slop || cState.LX >= 128 + slop || cState.LY <= 127 - slop || cState.LY >= 128 + slop)
|
|
|
|
|
return false;
|
|
|
|
|
if (cState.RX <= 127 - slop || cState.RX >= 128 + slop || cState.RY <= 127 - slop || cState.RY >= 128 + slop)
|
|
|
|
|
return false;
|
|
|
|
|
if (cState.Touch1 || cState.Touch2 || cState.TouchButton)
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
2014-03-29 06:29:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-11 01:15:14 +01:00
|
|
|
|
private DS4HapticState currentHap = new DS4HapticState();
|
|
|
|
|
public void SetHapticState(ref DS4HapticState hs)
|
2014-03-29 06:29:08 +01:00
|
|
|
|
{
|
2019-01-11 01:15:14 +01:00
|
|
|
|
currentHap = hs;
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override
|
2017-06-29 15:16:02 +02:00
|
|
|
|
public string ToString()
|
2014-03-28 02:50:40 +01:00
|
|
|
|
{
|
|
|
|
|
return Mac;
|
|
|
|
|
}
|
2017-05-01 11:29:19 +02:00
|
|
|
|
|
2017-05-01 12:40:37 +02:00
|
|
|
|
public void runRemoval()
|
|
|
|
|
{
|
|
|
|
|
Removal?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-01 11:29:19 +02:00
|
|
|
|
public void removeReportHandlers()
|
|
|
|
|
{
|
|
|
|
|
this.Report = null;
|
|
|
|
|
}
|
2017-05-14 00:01:43 +02:00
|
|
|
|
|
|
|
|
|
public void queueEvent(Action act)
|
|
|
|
|
{
|
|
|
|
|
lock (eventQueueLock)
|
|
|
|
|
{
|
|
|
|
|
eventQueue.Enqueue(act);
|
2018-10-25 09:51:50 +02:00
|
|
|
|
hasInputEvts = true;
|
2017-05-14 00:01:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-25 11:51:28 +02:00
|
|
|
|
|
|
|
|
|
public void updateSerial()
|
|
|
|
|
{
|
|
|
|
|
hDevice.resetSerial();
|
|
|
|
|
string tempMac = hDevice.readSerial();
|
|
|
|
|
if (tempMac != Mac)
|
|
|
|
|
{
|
|
|
|
|
Mac = tempMac;
|
|
|
|
|
SerialChange?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool isValidSerial()
|
|
|
|
|
{
|
|
|
|
|
return !Mac.Equals(blankSerial);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool isValidSerial(string test)
|
|
|
|
|
{
|
|
|
|
|
return !test.Equals(blankSerial);
|
|
|
|
|
}
|
2014-03-28 02:50:40 +01:00
|
|
|
|
}
|
|
|
|
|
}
|