forked from dancol90/mi-360
/
XInputManager.cs
96 lines (73 loc) · 2.45 KB
/
XInputManager.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HidLibrary;
using Nefarius.ViGEm.Client;
namespace mi360
{
class XInputManager : IDisposable
{
public event EventHandler GamepadRunning;
public event EventHandler GamepadRemoved;
private Dictionary<string, MiGamepad> _Gamepads;
private ViGEmClient _ViGEmClient;
public XInputManager()
{
_ViGEmClient = new ViGEmClient();
_Gamepads = new Dictionary<string, MiGamepad>();
}
public void Dispose()
{
// When calling Stop() the device will get removed from the dictionary, do this to avoid exceptions in enumeration
var devices = _Gamepads.Values.ToArray();
foreach (var device in devices)
{
device.Stop();
device.Dispose();
}
_ViGEmClient.Dispose();
}
public Dictionary<ushort, ushort> DeviceStatus => _Gamepads.ToDictionary(g => g.Value.LedNumber, g => g.Value.BatteryLevel);
#region Methods
public bool AddAndStart(string device)
{
if (Contains(device))
return false;
var gamepad = new MiGamepad(device, _ViGEmClient);
_Gamepads.Add(device, gamepad);
gamepad.Started += Gamepad_Started;
gamepad.Ended += Gamepad_Ended;
gamepad.Start();
return true;
}
public void StopAndRemove(string device)
{
if (!Contains(device))
return;
var gamepad = _Gamepads[device];
if (gamepad.IsActive)
gamepad.Stop();
gamepad.Started -= Gamepad_Started;
gamepad.Ended -= Gamepad_Ended;
gamepad.Dispose();
_Gamepads.Remove(device);
}
public bool Contains(string device)
{
return _Gamepads.ContainsKey(device);
}
#endregion
private void Gamepad_Ended(object sender, EventArgs eventArgs)
{
var gamepad = sender as MiGamepad;
StopAndRemove(gamepad.Device.DevicePath);
GamepadRemoved?.Invoke(this, EventArgs.Empty);
}
private void Gamepad_Started(object sender, EventArgs eventArgs)
{
GamepadRunning?.Invoke(this, EventArgs.Empty);
}
}
}