Skip to content

Commit

Permalink
Forward InstanceID to events and use it with HidHide APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
dancol90 committed Aug 22, 2021
1 parent b67b1eb commit fd5234a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
24 changes: 17 additions & 7 deletions Source/mi-360/Mi360Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Mi360Application : ApplicationContext
private ILogger _Logger = Log.ForContext<Mi360Application>();

private NotifyIcon _NotifyIcon;
private IMonitor _Monitor;
private HidMonitor _Monitor;
private XInputManager _Manager;

public Mi360Application()
Expand All @@ -36,6 +36,9 @@ public Mi360Application()
_Monitor.DeviceRemoved += Monitor_DeviceRemoved;
_Monitor.Start();

using (var hh = new HidHide())
hh.WhitelistCurrentApplication();

_Logger.Information("mi-360 is running");
}

Expand Down Expand Up @@ -100,16 +103,23 @@ private void Application_ApplicationExit(object sender, EventArgs eventArgs)
_NotifyIcon.Visible = false;
}

private void Monitor_DeviceAttached(object sender, string s)
private void Monitor_DeviceAttached(object sender, DeviceEventArgs e)
{
_Logger.Information("New HID device connected: {Device}", s);
_Manager.AddAndStart(s);
_Logger.Information("New HID device connected: {Descr} {Device}", e.Description, e.Path);

using (var hh = new HidHide())
hh.SetDeviceHideStatus(e.InstanceID, true);

_Manager.AddAndStart(e.Path);
}

private void Monitor_DeviceRemoved(object sender, string s)
private void Monitor_DeviceRemoved(object sender, DeviceEventArgs e)
{
_Logger.Information("HID device disconnected: {Device}", s);
_Manager.StopAndRemove(s);
_Logger.Information("HID device disconnected: {Descr} {Device}", e.Description, e.Path);
_Manager.StopAndRemove(e.Path);

using (var hh = new HidHide())
hh.SetDeviceHideStatus(e.InstanceID, false);
}

private void Manager_GamepadRemoved(object sender, EventArgs eventArgs)
Expand Down
43 changes: 32 additions & 11 deletions Source/mi-360/Win32/HidMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,38 @@

namespace mi360.Win32
{
class HidMonitor : IMonitor
public class DeviceEventArgs : EventArgs
{
internal DeviceEventArgs(HidDevices.DeviceInfo info) : base()
{
Path = info.Path;
Description = info.Description;
InstanceID = info.InstanceID;
}

public string Path { get; set; }
public string Description { get; set; }
public string InstanceID { get; set; }
}

class DeviceInfoEqualityComparer : IEqualityComparer<HidDevices.DeviceInfo>
{
public bool Equals(HidDevices.DeviceInfo x, HidDevices.DeviceInfo y) => x.Path == y.Path;
public int GetHashCode(HidDevices.DeviceInfo di) => di.Path.GetHashCode();
}

class HidMonitor
{
#region Constants & Fields

public event EventHandler<string> DeviceAttached;
public event EventHandler<string> DeviceRemoved;
public event EventHandler<DeviceEventArgs> DeviceAttached;
public event EventHandler<DeviceEventArgs> DeviceRemoved;

private ILogger _Logger = Log.ForContext<HidMonitor>();

private Timer _MonitorTimer;
private string _Filter;
private string[] _SeenDevices;
private HidDevices.DeviceInfo[] _SeenDevices;

#endregion

Expand All @@ -33,7 +53,7 @@ public HidMonitor(string filter)
_Filter = filter;
_MonitorTimer = new Timer(SearchForDevice);

_SeenDevices = new string[0];
_SeenDevices = new HidDevices.DeviceInfo[0];
}

#endregion
Expand All @@ -57,26 +77,27 @@ private void SearchForDevice(object state)
var filter = _Filter.ToLower();
var devices = HidDevices
.EnumerateDevices()
.Select(d => d.Path)
.Where(p => p.ToLower().Contains(filter))
.Where(p => p.Path.ToLower().Contains(filter))
.ToArray();

var comp = new DeviceInfoEqualityComparer();

// Get all the devices that has connected since the last check
var newDevices = devices.Except(_SeenDevices);
var newDevices = devices.Except(_SeenDevices, comp);

// Get all the device that has disconnected since the last check
var removedDevices = _SeenDevices.Except(devices);
var removedDevices = _SeenDevices.Except(devices, comp);

foreach (var device in newDevices)
{
_Logger.Information("Detected attached HID devices matching filter {Filter}", _Filter);
DeviceAttached?.Invoke(this, device);
DeviceAttached?.Invoke(this, new DeviceEventArgs(device));
}

foreach (var device in removedDevices)
{
_Logger.Information("Detected removed HID devices matching filter {Filter}", _Filter);
DeviceRemoved?.Invoke(this, device);
DeviceRemoved?.Invoke(this, new DeviceEventArgs(device));
}

_SeenDevices = devices;
Expand Down

0 comments on commit fd5234a

Please sign in to comment.