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
{
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);
}

View File

@@ -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)]

View File

@@ -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;

View File

@@ -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)

View File

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

View File

@@ -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;
}
}