From 6c436633fc33b0140c41c48f35f0e214d9ac5e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Fri, 25 Nov 2022 20:27:53 +0100 Subject: [PATCH] Introduce HidHideCLI --- SteamController/Context.cs | 1 + SteamController/Controller.cs | 11 ++++ SteamController/Helpers/HidHideCLI.cs | 74 ++++++++++++++++++++++ SteamController/Managers/HidHideManager.cs | 55 ++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 SteamController/Helpers/HidHideCLI.cs create mode 100644 SteamController/Managers/HidHideManager.cs diff --git a/SteamController/Context.cs b/SteamController/Context.cs index cabc28b..621eea9 100644 --- a/SteamController/Context.cs +++ b/SteamController/Context.cs @@ -25,6 +25,7 @@ namespace SteamController public bool RequestDesktopMode { get; set; } = true; public bool SteamRunning { get; set; } = false; public bool SteamUsesController { get; set; } = false; + public bool ControllerHidden { get; set; } = false; public bool Enabled { diff --git a/SteamController/Controller.cs b/SteamController/Controller.cs index 7724c23..ff874e8 100644 --- a/SteamController/Controller.cs +++ b/SteamController/Controller.cs @@ -55,6 +55,17 @@ namespace SteamController contextMenu.Opening += delegate { desktopModeItem.Checked = context.RequestDesktopMode; }; contextMenu.Items.Add(desktopModeItem); + var hidHideItem = new ToolStripMenuItem("Use &HidHide"); + hidHideItem.Checked = Settings.Default.EnableHidHide; + hidHideItem.Enabled = HidHideCLI.IsAvailable; + hidHideItem.Click += delegate + { + Settings.Default.EnableHidHide = !Settings.Default.EnableHidHide; + Settings.Default.Save(); + }; + contextMenu.Opening += delegate { hidHideItem.Checked = Settings.Default.EnableHidHide; }; + contextMenu.Items.Add(hidHideItem); + var steamDetectionItem = new ToolStripMenuItem("Auto-disable on &Steam"); steamDetectionItem.Checked = Settings.Default.EnableSteamDetection; steamDetectionItem.Click += delegate diff --git a/SteamController/Helpers/HidHideCLI.cs b/SteamController/Helpers/HidHideCLI.cs new file mode 100644 index 0000000..b89822a --- /dev/null +++ b/SteamController/Helpers/HidHideCLI.cs @@ -0,0 +1,74 @@ +using System.Diagnostics; +using System.Runtime.InteropServices; +using Microsoft.Win32; + +namespace SteamController.Helpers +{ + internal static class HidHideCLI + { + public const String CLIPath = @"C:\Program Files\Nefarius Software Solutions\HidHide\x64\HidHideCLI.exe"; + + static HidHideCLI() + { + IsAvailable = File.Exists(CLIPath); + } + + public static bool IsAvailable + { + get; private set; + } + + public static bool Cloak(bool enable = true) + { + if (enable) + return StartCLI("--cloak-on"); + else + return StartCLI("--cloak-off"); + } + + public static bool RegisterApplication(String path, bool seeHidden = true) + { + if (seeHidden) + return StartCLI("--app-reg", path); + else + return StartCLI("--app-unreg", path); + } + + public static bool HideDevice(String path, bool hide = true) + { + if (hide) + return StartCLI("--dev-hide", path); + else + return StartCLI("--dev-unhide", path); + } + + public static bool RegisterApplication(bool seeHidden) + { + var process = Process.GetCurrentProcess(); + var fullPath = process?.MainModule?.FileName; + if (fullPath is not null) + return RegisterApplication(fullPath, seeHidden); + + return true; + } + + private static bool StartCLI(params string[] args) + { + var si = new ProcessStartInfo() + { + FileName = CLIPath, + WindowStyle = ProcessWindowStyle.Hidden, + UseShellExecute = false, + CreateNoWindow = true + }; + + foreach (var arg in args) + { + si.ArgumentList.Add(arg); + } + + var process = Process.Start(si); + return process is not null; + } + } +} diff --git a/SteamController/Managers/HidHideManager.cs b/SteamController/Managers/HidHideManager.cs new file mode 100644 index 0000000..f58071b --- /dev/null +++ b/SteamController/Managers/HidHideManager.cs @@ -0,0 +1,55 @@ +using System.Diagnostics; +using SteamController.Helpers; + +namespace SteamController.Managers +{ + public sealed class HidHideManager : Manager + { + public const String NeptuneDevicePath = @"HID\VID_28DE&PID_1205&MI_02\8&a5f3a41&0&0000"; + + private bool? applicationRegistered; + private bool? deviceHidden; + private bool? clockDevices; + + public override void Tick(Context context) + { + if (applicationRegistered != true) + { + HidHideCLI.RegisterApplication(true); + applicationRegistered = true; + } + + if (clockDevices != true) + { + HidHideCLI.Cloak(true); + clockDevices = true; + } + + if (!Settings.Default.EnableHidHide) + { + HideDevice(false); + return; + } + + if (context.SteamUsesController) + { + HideDevice(false); + context.ControllerHidden = false; + } + else + { + HideDevice(true); + context.ControllerHidden = true; + } + } + + private void HideDevice(bool hidden) + { + if (deviceHidden == hidden) + return; + + HidHideCLI.HideDevice(NeptuneDevicePath, hidden); + deviceHidden = hidden; + } + } +}