From 6eba7dadeaa28832fe2f214c9d292804d6763c9d Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Sat, 13 Jan 2024 13:24:30 +0100 Subject: [PATCH] Auto controller mode detection for Ally --- app/Ally/AllyControl.cs | 82 ++++++++++++++++++++++++++++++------ app/Gpu/AMD/AmdAdl2.cs | 41 ++++++++++++++++++ app/Gpu/AMD/AmdGpuControl.cs | 27 ++++++++++++ app/Program.cs | 6 +-- app/Settings.Designer.cs | 1 + app/Settings.cs | 18 +++++--- 6 files changed, 150 insertions(+), 25 deletions(-) diff --git a/app/Ally/AllyControl.cs b/app/Ally/AllyControl.cs index ce404457..4a2c1dad 100644 --- a/app/Ally/AllyControl.cs +++ b/app/Ally/AllyControl.cs @@ -1,49 +1,103 @@ -using GHelper.USB; +using GHelper.Gpu.AMD; +using GHelper.USB; namespace GHelper.Ally { public enum ControllerMode : int { + Auto = 0, Gamepad = 1, - WASD = 2, Mouse = 3, } public class AllyControl { + System.Timers.Timer timer = default!; + AmdGpuControl amdControl; + SettingsForm settings; - ControllerMode mode = ControllerMode.Gamepad; + + ControllerMode mode = ControllerMode.Auto; + + ControllerMode _autoMode = ControllerMode.Gamepad; + int _autoCount = 0; + public AllyControl(SettingsForm settingsForm) { + if (!AppConfig.IsAlly()) return; + settings = settingsForm; + + amdControl = new AmdGpuControl(); + + timer = new System.Timers.Timer(500); + timer.Elapsed += Timer_Elapsed; + + } + + private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e) + { + float fps = amdControl.GetFPS(); + + ControllerMode _newMode = (fps > 0) ? ControllerMode.Gamepad : ControllerMode.Mouse; + + if (_autoMode != _newMode) _autoCount++; + else _autoCount = 0; + + if (_autoCount > 2) + { + _autoMode = _newMode; + _autoCount = 0; + AsusHid.WriteInput(new byte[] { AsusHid.INPUT_ID, 0xd1, 1, 1, (byte)_autoMode }, "ControllerAuto " + _autoMode); + Logger.WriteLine(fps.ToString()); + } + } public void Init() { - if (!AppConfig.IsAlly()) - { - settings.VisualiseAlly(false); - return; - } + if (AppConfig.IsAlly()) settings.VisualiseAlly(true); + else return; - mode = (ControllerMode)AppConfig.Get("controller_mode", (int)ControllerMode.Gamepad); + mode = (ControllerMode)AppConfig.Get("controller_mode", (int)ControllerMode.Auto); SetMode(mode); } private void SetMode(ControllerMode mode) { + if (mode == ControllerMode.Auto) + { + amdControl.StartFPS(); + timer.Start(); + } else + { + timer.Stop(); + amdControl.StopFPS(); + + AsusHid.WriteInput(new byte[] { AsusHid.INPUT_ID, 0xd1, 1, 1, (byte)mode }, "ControllerMode"); + } + + AppConfig.Set("controller_mode", (int)mode); - AsusHid.WriteInput(new byte[] { AsusHid.INPUT_ID, 0xd1, 1, 1, (byte)mode }, "ControllerMode"); settings.VisualiseController(mode); } public void ToggleMode() { - if (mode == ControllerMode.Mouse) - mode = ControllerMode.Gamepad; - else - mode++; + + switch (mode) + { + case ControllerMode.Auto: + mode = ControllerMode.Gamepad; + break; + case ControllerMode.Gamepad: + mode = ControllerMode.Mouse; + break; + case ControllerMode.Mouse: + mode = ControllerMode.Auto; + break; + } SetMode(mode); } diff --git a/app/Gpu/AMD/AmdAdl2.cs b/app/Gpu/AMD/AmdAdl2.cs index d76504f8..c9fc2918 100644 --- a/app/Gpu/AMD/AmdAdl2.cs +++ b/app/Gpu/AMD/AmdAdl2.cs @@ -551,6 +551,47 @@ public class Adl2 int iAdapterIndex, int iEnabled); + // FPS + + [DllImport(Atiadlxx_FileName)] + public static extern int ADL2_Adapter_FrameMetrics_Start( + IntPtr context, + int iAdapterIndex, + int VidPnSourceId + ); + + [DllImport(Atiadlxx_FileName)] + public static extern int ADL2_Adapter_FrameMetrics_Stop( + IntPtr context, + int iAdapterIndex, + int VidPnSourceId + ); + + [DllImport(Atiadlxx_FileName)] + public static extern int ADL2_Adapter_FrameMetrics_Get( + IntPtr context, + int iAdapterIndex, + int VidPnSourceId, + out float iFramesPerSecond + ); + + [DllImport(Atiadlxx_FileName)] + public static extern int ADL2_FPS_Settings_Get(IntPtr context, int iAdapterIndex, out ADLFPSSettingsOutput lpFPSSettings); + + [StructLayout(LayoutKind.Sequential)] + public struct ADLFPSSettingsOutput + { + public int ulSize; + public int bACFPSEnabled; + public int bDCFPSEnabled; + public int ulACFPSCurrent; + public int ulDCFPSCurrent; + public int ulACFPSMaximum; + public int ulACFPSMinimum; + public int ulDCFPSMaximum; + public int ulDCFPSMinimum; + } + // Clocks [StructLayout(LayoutKind.Sequential)] diff --git a/app/Gpu/AMD/AmdGpuControl.cs b/app/Gpu/AMD/AmdGpuControl.cs index 2d0b1f38..011a0282 100644 --- a/app/Gpu/AMD/AmdGpuControl.cs +++ b/app/Gpu/AMD/AmdGpuControl.cs @@ -9,7 +9,9 @@ public class AmdGpuControl : IGpuControl { private bool _isReady; private nint _adlContextHandle; + private readonly ADLAdapterInfo _internalDiscreteAdapter; + private readonly ADLAdapterInfo? _iGPU; public bool IsNvidia => false; @@ -74,6 +76,8 @@ public class AmdGpuControl : IGpuControl _isReady = true; } + _iGPU = FindByType(ADLAsicFamilyType.Integrated); + } public bool IsValid => _isReady && _adlContextHandle != nint.Zero; @@ -139,6 +143,29 @@ public class AmdGpuControl : IGpuControl return true; } + public void StartFPS() + { + if (_adlContextHandle == nint.Zero || _iGPU == null) return; + ADL2_Adapter_FrameMetrics_Start(_adlContextHandle, ((ADLAdapterInfo)_iGPU).AdapterIndex, 0); + } + + public void StopFPS() + { + if (_adlContextHandle == nint.Zero || _iGPU == null) return; + ADL2_Adapter_FrameMetrics_Stop(_adlContextHandle, ((ADLAdapterInfo)_iGPU).AdapterIndex, 0); + } + + public float GetFPS() + { + if (_adlContextHandle == nint.Zero || _iGPU == null) return 0; + float fps; + + if (ADL2_Adapter_FrameMetrics_Get(_adlContextHandle, ((ADLAdapterInfo)_iGPU).AdapterIndex, 0, out fps) != Adl2.ADL_SUCCESS) return 0; + + return fps; + + } + public ADLODNPerformanceLevels? GetGPUClocks() { if (!IsValid) return null; diff --git a/app/Program.cs b/app/Program.cs index b94b1620..e280e9bc 100644 --- a/app/Program.cs +++ b/app/Program.cs @@ -6,8 +6,6 @@ using GHelper.Helpers; using GHelper.Input; using GHelper.Mode; using GHelper.Peripherals; -using GHelper.USB; -using Microsoft.VisualBasic.Logging; using Microsoft.Win32; using Ryzen; using System.Diagnostics; @@ -33,7 +31,7 @@ namespace GHelper public static ModeControl modeControl = new ModeControl(); public static GPUModeControl gpuControl = new GPUModeControl(settingsForm); - public static AllyControl controllerControl = new AllyControl(settingsForm); + public static AllyControl allyControl = new AllyControl(settingsForm); public static ScreenControl screenControl = new ScreenControl(); public static ClamshellModeControl clamshellControl = new ClamshellModeControl(); @@ -230,7 +228,7 @@ namespace GHelper settingsForm.AutoKeyboard(); settingsForm.matrixControl.SetMatrix(true); - controllerControl.Init(); + allyControl.Init(); } private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) diff --git a/app/Settings.Designer.cs b/app/Settings.Designer.cs index 8295764d..52a63f3c 100644 --- a/app/Settings.Designer.cs +++ b/app/Settings.Designer.cs @@ -1418,6 +1418,7 @@ namespace GHelper panelAlly.Size = new Size(827, 176); panelAlly.TabIndex = 8; panelAlly.TabStop = true; + panelAlly.Visible = false; // // label1 // diff --git a/app/Settings.cs b/app/Settings.cs index e117393c..d2273c14 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -24,7 +24,7 @@ namespace GHelper public GPUModeControl gpuControl; ScreenControl screenControl = new ScreenControl(); - AllyControl controllerControl; + AllyControl allyControl; AutoUpdateControl updateControl; AsusMouseSettings? mouseSettings; @@ -56,7 +56,7 @@ namespace GHelper gpuControl = new GPUModeControl(this); updateControl = new AutoUpdateControl(this); matrixControl = new AniMatrixControl(this); - controllerControl = new AllyControl(this); + allyControl = new AllyControl(this); buttonSilent.Text = Properties.Strings.Silent; buttonBalanced.Text = Properties.Strings.Balanced; @@ -247,12 +247,16 @@ namespace GHelper private void ButtonController_Click(object? sender, EventArgs e) { - controllerControl.ToggleMode(); + allyControl.ToggleMode(); } public void VisualiseAlly(bool visible = false) { - panelAlly.Visible = visible; + if (!visible) return; + + panelAlly.Visible = true; + labelKeyboard.Text = "Backlight"; + buttonFnLock.Visible = false; } public void VisualiseController(ControllerMode mode) @@ -262,12 +266,12 @@ namespace GHelper case ControllerMode.Gamepad: buttonController.Text = "Gamepad"; break; - case ControllerMode.WASD: - buttonController.Text = "WASD"; - break; case ControllerMode.Mouse: buttonController.Text = "Mouse"; break; + default: + buttonController.Text = "Auto"; + break; } }