Auto controller mode detection for Ally

This commit is contained in:
Serge
2024-01-13 13:24:30 +01:00
parent 772bd8e7a5
commit 6eba7dadea
6 changed files with 150 additions and 25 deletions

View File

@@ -1,49 +1,103 @@
using GHelper.USB; using GHelper.Gpu.AMD;
using GHelper.USB;
namespace GHelper.Ally namespace GHelper.Ally
{ {
public enum ControllerMode : int public enum ControllerMode : int
{ {
Auto = 0,
Gamepad = 1, Gamepad = 1,
WASD = 2,
Mouse = 3, Mouse = 3,
} }
public class AllyControl public class AllyControl
{ {
System.Timers.Timer timer = default!;
AmdGpuControl amdControl;
SettingsForm settings; SettingsForm settings;
ControllerMode mode = ControllerMode.Gamepad;
ControllerMode mode = ControllerMode.Auto;
ControllerMode _autoMode = ControllerMode.Gamepad;
int _autoCount = 0;
public AllyControl(SettingsForm settingsForm) public AllyControl(SettingsForm settingsForm)
{ {
if (!AppConfig.IsAlly()) return;
settings = settingsForm; 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() public void Init()
{ {
if (!AppConfig.IsAlly()) if (AppConfig.IsAlly()) settings.VisualiseAlly(true);
{ else return;
settings.VisualiseAlly(false);
return;
}
mode = (ControllerMode)AppConfig.Get("controller_mode", (int)ControllerMode.Gamepad); mode = (ControllerMode)AppConfig.Get("controller_mode", (int)ControllerMode.Auto);
SetMode(mode); SetMode(mode);
} }
private void SetMode(ControllerMode mode) private void SetMode(ControllerMode mode)
{ {
AppConfig.Set("controller_mode", (int)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"); AsusHid.WriteInput(new byte[] { AsusHid.INPUT_ID, 0xd1, 1, 1, (byte)mode }, "ControllerMode");
}
AppConfig.Set("controller_mode", (int)mode);
settings.VisualiseController(mode); settings.VisualiseController(mode);
} }
public void ToggleMode() public void ToggleMode()
{ {
if (mode == ControllerMode.Mouse)
switch (mode)
{
case ControllerMode.Auto:
mode = ControllerMode.Gamepad; mode = ControllerMode.Gamepad;
else break;
mode++; case ControllerMode.Gamepad:
mode = ControllerMode.Mouse;
break;
case ControllerMode.Mouse:
mode = ControllerMode.Auto;
break;
}
SetMode(mode); SetMode(mode);
} }

View File

@@ -551,6 +551,47 @@ public class Adl2
int iAdapterIndex, int iAdapterIndex,
int iEnabled); 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 // Clocks
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]

View File

@@ -9,7 +9,9 @@ public class AmdGpuControl : IGpuControl
{ {
private bool _isReady; private bool _isReady;
private nint _adlContextHandle; private nint _adlContextHandle;
private readonly ADLAdapterInfo _internalDiscreteAdapter; private readonly ADLAdapterInfo _internalDiscreteAdapter;
private readonly ADLAdapterInfo? _iGPU;
public bool IsNvidia => false; public bool IsNvidia => false;
@@ -74,6 +76,8 @@ public class AmdGpuControl : IGpuControl
_isReady = true; _isReady = true;
} }
_iGPU = FindByType(ADLAsicFamilyType.Integrated);
} }
public bool IsValid => _isReady && _adlContextHandle != nint.Zero; public bool IsValid => _isReady && _adlContextHandle != nint.Zero;
@@ -139,6 +143,29 @@ public class AmdGpuControl : IGpuControl
return true; 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() public ADLODNPerformanceLevels? GetGPUClocks()
{ {
if (!IsValid) return null; if (!IsValid) return null;

View File

@@ -6,8 +6,6 @@ using GHelper.Helpers;
using GHelper.Input; using GHelper.Input;
using GHelper.Mode; using GHelper.Mode;
using GHelper.Peripherals; using GHelper.Peripherals;
using GHelper.USB;
using Microsoft.VisualBasic.Logging;
using Microsoft.Win32; using Microsoft.Win32;
using Ryzen; using Ryzen;
using System.Diagnostics; using System.Diagnostics;
@@ -33,7 +31,7 @@ namespace GHelper
public static ModeControl modeControl = new ModeControl(); public static ModeControl modeControl = new ModeControl();
public static GPUModeControl gpuControl = new GPUModeControl(settingsForm); 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 ScreenControl screenControl = new ScreenControl();
public static ClamshellModeControl clamshellControl = new ClamshellModeControl(); public static ClamshellModeControl clamshellControl = new ClamshellModeControl();
@@ -230,7 +228,7 @@ namespace GHelper
settingsForm.AutoKeyboard(); settingsForm.AutoKeyboard();
settingsForm.matrixControl.SetMatrix(true); settingsForm.matrixControl.SetMatrix(true);
controllerControl.Init(); allyControl.Init();
} }
private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)

View File

@@ -1418,6 +1418,7 @@ namespace GHelper
panelAlly.Size = new Size(827, 176); panelAlly.Size = new Size(827, 176);
panelAlly.TabIndex = 8; panelAlly.TabIndex = 8;
panelAlly.TabStop = true; panelAlly.TabStop = true;
panelAlly.Visible = false;
// //
// label1 // label1
// //

View File

@@ -24,7 +24,7 @@ namespace GHelper
public GPUModeControl gpuControl; public GPUModeControl gpuControl;
ScreenControl screenControl = new ScreenControl(); ScreenControl screenControl = new ScreenControl();
AllyControl controllerControl; AllyControl allyControl;
AutoUpdateControl updateControl; AutoUpdateControl updateControl;
AsusMouseSettings? mouseSettings; AsusMouseSettings? mouseSettings;
@@ -56,7 +56,7 @@ namespace GHelper
gpuControl = new GPUModeControl(this); gpuControl = new GPUModeControl(this);
updateControl = new AutoUpdateControl(this); updateControl = new AutoUpdateControl(this);
matrixControl = new AniMatrixControl(this); matrixControl = new AniMatrixControl(this);
controllerControl = new AllyControl(this); allyControl = new AllyControl(this);
buttonSilent.Text = Properties.Strings.Silent; buttonSilent.Text = Properties.Strings.Silent;
buttonBalanced.Text = Properties.Strings.Balanced; buttonBalanced.Text = Properties.Strings.Balanced;
@@ -247,12 +247,16 @@ namespace GHelper
private void ButtonController_Click(object? sender, EventArgs e) private void ButtonController_Click(object? sender, EventArgs e)
{ {
controllerControl.ToggleMode(); allyControl.ToggleMode();
} }
public void VisualiseAlly(bool visible = false) 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) public void VisualiseController(ControllerMode mode)
@@ -262,12 +266,12 @@ namespace GHelper
case ControllerMode.Gamepad: case ControllerMode.Gamepad:
buttonController.Text = "Gamepad"; buttonController.Text = "Gamepad";
break; break;
case ControllerMode.WASD:
buttonController.Text = "WASD";
break;
case ControllerMode.Mouse: case ControllerMode.Mouse:
buttonController.Text = "Mouse"; buttonController.Text = "Mouse";
break; break;
default:
buttonController.Text = "Auto";
break;
} }
} }