-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDualSense.cs
More file actions
260 lines (212 loc) · 7.71 KB
/
Copy pathDualSense.cs
File metadata and controls
260 lines (212 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
namespace DualSenseSharp;
using DualSenseSharp.Builder;
using DualSenseSharp.Components;
using DualSenseSharp.Input;
using DualSenseSharp.Transport;
using HidSharp;
using System.Collections.Immutable;
using static DualSenseSharp.Components.AdaptiveTrigger;
public sealed class DualSense : IDisposable
{
public enum ProductType : ushort
{
Unknown = 0x0000,
Standard = 0x0CE6,
Edge = 0x0DF2,
}
private readonly ReportBuilder _reportBuilder;
private readonly DataBuilder _dataBuilder;
private readonly IInputParser _inputParser;
private readonly int _inputReportLength;
private readonly Func<HidDevice, BaseTransport> _transportConstructor;
private UniqueId? _uniqueId;
private HidDevice? _hidDevice;
private BaseTransport? _transport;
public readonly ProductType Type;
public bool Disposed { get; private set; }
public AdaptiveTrigger LeftTrigger { get; }
public AdaptiveTrigger RightTrigger { get; }
public LightBar LightBar { get; }
public PlayerLeds PlayerLeds { get; }
public Rumble Rumble { get; }
public Microphone Microphone { get; }
public InputReader Input { get; }
public bool IsConnected => _transport?.IsConnected ?? false;
public bool IsBluetooth { get; init; }
public event EventHandler? Disconnected = null;
public UniqueId UniqueId
{
get {
if (_uniqueId == null)
throw new InvalidOperationException("UniqueId is not computed, call ComputeUniqueId() to save the mac address");
return (UniqueId)_uniqueId;
}
}
private DualSense(HidDevice device)
{
Type = Enum.GetValues<ProductType>().FirstOrDefault(x => ((uint)x) == device.ProductID, ProductType.Unknown);
LeftTrigger = new AdaptiveTrigger(Side.Left);
RightTrigger = new AdaptiveTrigger(Side.Right);
LightBar = new LightBar();
PlayerLeds = new PlayerLeds();
Rumble = new Rumble();
Microphone = new Microphone();
Input = new InputReader();
_dataBuilder = new DataBuilder(this);
if (device.GetMaxInputReportLength() == 78)
{
_reportBuilder = new BluetoothReportBuilder(_dataBuilder);
_transportConstructor = hidDevice => new BluetoothTransport(hidDevice);
_inputParser = new BluetoothInputParser();
IsBluetooth = true;
}
else if (device.GetMaxInputReportLength() == 64)
{
_reportBuilder = new UsbReportBuilder(_dataBuilder);
_transportConstructor = hidDevice => new UsbTransport(hidDevice);
_inputParser = new UsbInputParser();
IsBluetooth = false;
}
else
{
throw new UnknownDualsenseException("failed to identify the controller report length");
}
_inputReportLength = device.GetMaxInputReportLength();
_hidDevice = device;
}
public string DevicePath => _hidDevice?.DevicePath ?? throw new ObjectDisposedException(nameof(DualSense));
public void Open()
{
if (Disposed)
throw new ObjectDisposedException(nameof(DualSense));
_transport = _transportConstructor(_hidDevice!);
_transport.Closed += (sender, args) => Disconnected?.Invoke(this, EventArgs.Empty);
}
public void Disconnect()
{
if (Disposed)
throw new ObjectDisposedException(nameof(DualSense));
_transport?.Dispose();
_transport = null;
}
public void Dispose()
{
if (!Disposed)
{
Disposed = true;
_transport?.Dispose();
_transport = null;
_hidDevice = null;
}
}
public async ValueTask<bool> UpdateInputAsync()
{
if (Disposed)
throw new ObjectDisposedException(nameof(DualSense));
if (_transport == null)
throw new InvalidOperationException("Transport is not available. Call Open() first.");
var buffer = new byte[_inputReportLength];
var result = await _transport.ReadAsync(buffer);
if (!result)
return false;
Input.UpdateState(_inputParser.GetData(buffer));
return true;
}
public async ValueTask<bool> UpdateOutputAsync()
{
if (Disposed)
throw new ObjectDisposedException(nameof(DualSense));
if (_transport == null)
throw new InvalidOperationException("Transport is not available. Call Open() first.");
var buffer = _reportBuilder.Build();
return await _transport.WriteAsync(buffer);
}
public async ValueTask<UniqueId?> ComputeUniqueId()
{
if (_uniqueId != null)
return (UniqueId)_uniqueId;
if (_transport == null)
{
if(Disposed)
throw new InvalidOperationException("Transport is not available. Disposed Object.");
else
throw new InvalidOperationException("Transport is not available. Call Open() first.");
}
var buffer = new byte[20];
buffer[0] = 0x09;
var success = await _transport.GetFeature(buffer);
if (!success)
return null;
_uniqueId = new UniqueId(buffer[1..7]);
return (UniqueId)_uniqueId;
}
private static bool IsSupported(HidDevice device)
{
return Enum.GetValues<ProductType>().Any(x => ((uint)x) == device.ProductID);
}
public static List<DualSense> CreateDevices()
{
var devices = DeviceList.Local.GetHidDevices(0x054C).Where(IsSupported);
return devices.Select(device => new DualSense(device)).ToList();
}
public static class Manager
{
public static event EventHandler<DualSenseEventArgs>? ControllerConnected;
public static event EventHandler<DualSenseEventArgs>? ControllerDisconnected;
public static ImmutableList<DualSense> Controllers => _controllers.Values.ToImmutableList();
private static readonly Dictionary<string, DualSense> _controllers = new();
private static CancellationTokenSource? _cts;
public static void Start()
{
_cts = new();
Scan();
_ = PollLoop(_cts.Token);
}
public static void Stop()
{
_cts?.Cancel();
_cts = null;
_controllers.Clear();
}
private static async Task PollLoop(CancellationToken ct)
{
while (true)
{
await Task.Delay(1000, ct);
if (ct.IsCancellationRequested)
break;
Scan();
}
}
private static void Scan()
{
var devices = DeviceList.Local.GetHidDevices(0x054C).Where(IsSupported);
var current = devices.ToDictionary(d => d.DevicePath);
foreach (var pair in current)
{
if (_controllers.ContainsKey(pair.Key)) continue;
var controller = new DualSense(pair.Value);
_controllers.Add(pair.Key, controller);
ControllerConnected?.Invoke(
null,
new DualSenseEventArgs(controller));
}
foreach (var path in _controllers.Keys.Except(current.Keys).ToList())
{
var controller = _controllers[path];
_controllers.Remove(path);
ControllerDisconnected?.Invoke(
null,
new DualSenseEventArgs(controller));
}
}
}
public class DualSenseEventArgs : EventArgs
{
public readonly DualSense DualSense;
public DualSenseEventArgs(DualSense sense)
{
DualSense = sense;
}
}
}