Skip to content

Commit

Permalink
Replace deprecated HidGuardian with exclusive access to devices
Browse files Browse the repository at this point in the history
  • Loading branch information
dancol90 committed Apr 20, 2020
1 parent 65fc490 commit 2372c6d
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 138 deletions.
107 changes: 0 additions & 107 deletions Source/mi-360/HidGuardian.cs

This file was deleted.

29 changes: 0 additions & 29 deletions Source/mi-360/Mi360Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public Mi360Application()

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

EnableHidGuardian();

_Manager = new XInputManager();
_Manager.GamepadRunning += Manager_GamepadRunning;
_Manager.GamepadRemoved += Manager_GamepadRemoved;
Expand Down Expand Up @@ -79,32 +77,6 @@ private void ShowNotification(string title, string message, int timeout = 2000)
_NotifyIcon.ShowBalloonTip(timeout);
}

#region Hardware utilities

private void EnableHidGuardian()
{
// Temp
HidGuardian.ClearWhitelistedProcesses();
HidGuardian.ClearAffectedDevices();

HidGuardian.AddDeviceToAffectedList(XiaomiGamepadHardwareId);
HidGuardian.AddToWhitelist(Process.GetCurrentProcess().Id);

// Disable and reenable the device to let the driver hide the HID gamepad and show Xbox360 one
DeviceStateManager.DisableReEnableDevice(XiaomiGamepadHardwareId);
}

private void DisableHidGuardian()
{
HidGuardian.RemoveDeviceFromAffectedList(XiaomiGamepadHardwareId);
HidGuardian.RemoveFromWhitelist(Process.GetCurrentProcess().Id);

// Disable and reenable the device to let the driver hide the emulated gamepad and show the HID one again
DeviceStateManager.DisableReEnableDevice(XiaomiGamepadHardwareId);
}

#endregion

#region Event Handlers

private void Exit_OnClick(object sender, EventArgs eventArgs)
Expand All @@ -115,7 +87,6 @@ private void Exit_OnClick(object sender, EventArgs eventArgs)
private void Application_ApplicationExit(object sender, EventArgs eventArgs)
{
_NotifyIcon.Visible = false;
DisableHidGuardian();
}

private void Monitor_DeviceAttached(object sender, string s)
Expand Down
6 changes: 5 additions & 1 deletion Source/mi-360/MiGamepad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ private void DeviceWorker()
{
Console.WriteLine("Starting worker thread for {0}", _Device.ToString());

DeviceStateManager.DisableReEnableDevice(_Device.DevicePath);

// Open HID device to read input from the gamepad
_Device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.ShareRead | ShareMode.ShareWrite);
_Device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.Exclusive);

// Init Xiaomi Gamepad vibration
_Device.WriteFeatureData(new byte[] {0x20, 0x00, 0x00});
Expand Down Expand Up @@ -215,6 +217,8 @@ [19] MI button
// Close the HID device
_Device.CloseDevice();

DeviceStateManager.DisableReEnableDevice(_Device.DevicePath);

Console.WriteLine("Exiting worker thread for {0}", _Device.ToString());
Ended?.Invoke(this, EventArgs.Empty);
}
Expand Down
1 change: 0 additions & 1 deletion Source/mi-360/mi360.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="HidGuardian.cs" />
<Compile Include="Mi360Application.cs" />
<Compile Include="MiGamepad.cs" />
<Compile Include="Program.cs" />
Expand Down

0 comments on commit 2372c6d

Please sign in to comment.