Add ExternalHelpers project and move all externally acquired dependencies there

This commit is contained in:
Kamil Trzciński 2022-11-20 15:12:12 +01:00
parent b06ac16fba
commit e376b9dcc8
24 changed files with 457 additions and 574 deletions

View file

@ -14,17 +14,8 @@
<ItemGroup>
<Reference Include="RTSSSharedMemoryNET">
<HintPath>RTSSSharedMemoryNET.dll</HintPath>
<HintPath>..\ExternalHelpers\RTSSSharedMemoryNET.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Update="inpoutx64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="RTSSSharedMemoryNET.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View file

@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>True</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Management" Version="7.0.0" />
<PackageReference Include="TaskScheduler" Version="2.10.1" />
</ItemGroup>
<ItemGroup>
<None Update="hidapi.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="hidapi.net.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="inpoutx64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="RTSSSharedMemoryNET.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View file

@ -4,8 +4,11 @@ using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Input;
namespace PowerControl.External
namespace ExternalHelpers
{
/// <summary>
/// Taken and adapted from: https://stackoverflow.com/a/65412682
/// </summary>
public class GlobalHotKey : IDisposable
{
/// <summary>
@ -30,7 +33,7 @@ namespace PowerControl.External
return success;
}
public static bool RegisterHotKey(ModifierKeys aModifier, Key aKey, Action aAction, bool repeat = false)
public static bool RegisterHotKey(ModifierKeys aModifier, Key aKey, Action aAction, bool repeat)
{
if (aModifier == ModifierKeys.None && false)
{
@ -44,7 +47,7 @@ namespace PowerControl.External
System.Windows.Forms.Keys aVirtualKeyCode = (System.Windows.Forms.Keys)KeyInterop.VirtualKeyFromKey(aKey);
currentID = currentID + 1;
bool aRegistered = RegisterHotKey(window.Handle, currentID,
(uint)aModifier | (repeat ? 0 : MOD_NOREPEAT),
(uint)aModifier | (repeat ? MOD_NOREPEAT : 0),
(uint)aVirtualKeyCode);
if (aRegistered)

View file

@ -0,0 +1,177 @@
using Microsoft.VisualBasic.Logging;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
namespace PowerControl.Helpers
{
// Taken from: https://stackoverflow.com/questions/4013622/adjust-screen-brightness-using-c-sharp
public class PhysicalMonitorBrightnessController : IDisposable
{
#region DllImport
[DllImport("dxva2.dll", EntryPoint = "GetNumberOfPhysicalMonitorsFromHMONITOR")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, ref uint pdwNumberOfPhysicalMonitors);
[DllImport("dxva2.dll", EntryPoint = "GetPhysicalMonitorsFromHMONITOR")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, uint dwPhysicalMonitorArraySize, [Out] PHYSICAL_MONITOR[] pPhysicalMonitorArray);
[DllImport("dxva2.dll", EntryPoint = "GetMonitorBrightness")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetMonitorBrightness(IntPtr handle, ref uint minimumBrightness, ref uint currentBrightness, ref uint maxBrightness);
[DllImport("dxva2.dll", EntryPoint = "SetMonitorBrightness")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetMonitorBrightness(IntPtr handle, uint newBrightness);
[DllImport("dxva2.dll", EntryPoint = "DestroyPhysicalMonitor")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DestroyPhysicalMonitor(IntPtr hMonitor);
[DllImport("dxva2.dll", EntryPoint = "DestroyPhysicalMonitors")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DestroyPhysicalMonitors(uint dwPhysicalMonitorArraySize, [In] PHYSICAL_MONITOR[] pPhysicalMonitorArray);
[DllImport("user32.dll")]
static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, EnumMonitorsDelegate lpfnEnum, IntPtr dwData);
delegate bool EnumMonitorsDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData);
#endregion
private IReadOnlyCollection<MonitorInfo> Monitors { get; set; }
public PhysicalMonitorBrightnessController()
{
UpdateMonitors();
}
#region Get & Set
public void Set(uint brightness)
{
Set(brightness, true);
}
private void Set(uint brightness, bool refreshMonitorsIfNeeded)
{
bool isSomeFail = false;
foreach (var monitor in Monitors)
{
uint realNewValue = (monitor.MaxValue - monitor.MinValue) * brightness / 100 + monitor.MinValue;
if (SetMonitorBrightness(monitor.Handle, realNewValue))
{
monitor.CurrentValue = realNewValue;
}
else if (refreshMonitorsIfNeeded)
{
isSomeFail = true;
break;
}
}
if (refreshMonitorsIfNeeded && (isSomeFail || !Monitors.Any()))
{
UpdateMonitors();
Set(brightness, false);
return;
}
}
public int Get()
{
if (!Monitors.Any())
{
return -1;
}
return (int)Monitors.Average(d => d.CurrentValue);
}
#endregion
private void UpdateMonitors()
{
DisposeMonitors(this.Monitors);
var monitors = new List<MonitorInfo>();
EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, (IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData) =>
{
uint physicalMonitorsCount = 0;
if (!GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, ref physicalMonitorsCount))
{
// Cannot get monitor count
return true;
}
var physicalMonitors = new PHYSICAL_MONITOR[physicalMonitorsCount];
if (!GetPhysicalMonitorsFromHMONITOR(hMonitor, physicalMonitorsCount, physicalMonitors))
{
// Cannot get physical monitor handle
return true;
}
foreach (PHYSICAL_MONITOR physicalMonitor in physicalMonitors)
{
uint minValue = 0, currentValue = 0, maxValue = 0;
var info = new MonitorInfo
{
Handle = physicalMonitor.hPhysicalMonitor,
MinValue = minValue,
CurrentValue = currentValue,
MaxValue = maxValue,
};
monitors.Add(info);
}
return true;
}, IntPtr.Zero);
this.Monitors = monitors;
}
public void Dispose()
{
DisposeMonitors(Monitors);
GC.SuppressFinalize(this);
}
private static void DisposeMonitors(IEnumerable<MonitorInfo> monitors)
{
if (monitors?.Any() == true)
{
PHYSICAL_MONITOR[] monitorArray = monitors.Select(m => new PHYSICAL_MONITOR { hPhysicalMonitor = m.Handle }).ToArray();
DestroyPhysicalMonitors((uint)monitorArray.Length, monitorArray);
}
}
#region Classes
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct PHYSICAL_MONITOR
{
public IntPtr hPhysicalMonitor;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szPhysicalMonitorDescription;
}
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
}
public class MonitorInfo
{
public uint MinValue { get; set; }
public uint MaxValue { get; set; }
public IntPtr Handle { get; set; }
public uint CurrentValue { get; set; }
}
#endregion
}
}

View file

@ -7,7 +7,10 @@ using System.Runtime.InteropServices;
namespace PowerControl.External
{
internal enum SDCPacketType
/// <summary>
/// Taken from: https://github.com/mKenfenheuer/neptune-hidapi.net/blob/main/Hid/HidEnums.cs
/// </summary>
public enum SDCPacketType
{
PT_INPUT = 0x01,
PT_HOTPLUG = 0x03,
@ -26,7 +29,7 @@ namespace PowerControl.External
PT_RESET = 0x95,
PT_GET_SERIAL = 0xAE,
}
internal enum SDCPacketLength
public enum SDCPacketLength
{
PL_LED = 0x03,
PL_OFF = 0x04,
@ -35,14 +38,14 @@ namespace PowerControl.External
PL_CONFIGURE_BT = 0x0f,
PL_GET_SERIAL = 0x15,
}
internal enum SDCConfigType
public enum SDCConfigType
{
CT_LED = 0x2d,
CT_CONFIGURE = 0x32,
CONFIGURE_BT = 0x18,
}
internal enum SDCButton0
public enum SDCButton0
{
BTN_L5 = 0b1000000000000000,
BTN_OPTIONS = 0b0100000000000000,
@ -62,7 +65,7 @@ namespace PowerControl.External
BTN_R2 = 0b0000000000000001,
}
internal enum SDCButton1
public enum SDCButton1
{
BTN_LSTICK_PRESS = 0b01000000,
BTN_LPAD_TOUCH = 0b00001000,
@ -72,12 +75,12 @@ namespace PowerControl.External
BTN_R5 = 0b00000001,
}
internal enum SDCButton2
public enum SDCButton2
{
BTN_RSTICK_PRESS = 0b00000100,
}
internal enum SDCButton4
public enum SDCButton4
{
BTN_LSTICK_TOUCH = 0b01000000,
BTN_RSTICK_TOUCH = 0b10000000,
@ -85,14 +88,14 @@ namespace PowerControl.External
BTN_L4 = 0b00000010,
}
internal enum SDCButton5
public enum SDCButton5
{
BTN_QUICK_ACCESS = 0b00000100,
}
[StructLayout(LayoutKind.Sequential)]
internal struct SDCInput
public struct SDCInput
{
public byte ptype; //0x00
public byte _a1; //0x01
@ -128,7 +131,7 @@ namespace PowerControl.External
public short lpad_pressure; //0x38
public short rpad_pressure; //0x3A
internal static SDCInput FromBuffer(byte[] bytes)
public static SDCInput FromBuffer(byte[] bytes)
{
var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
try
@ -147,7 +150,7 @@ namespace PowerControl.External
}
[StructLayout(LayoutKind.Sequential)]
internal struct SDCHapticPacket
public struct SDCHapticPacket
{
public byte packet_type; // = 0x8f;
public byte len; // = 0x07;

View file

@ -15,23 +15,26 @@ using Microsoft.Win32.TaskScheduler;
using Action = Microsoft.Win32.TaskScheduler.Action;
using Task = Microsoft.Win32.TaskScheduler.Task;
namespace CommonHelpers.FromLibreHardwareMonitor
namespace ExternalHelpers
{
/// <summary>
/// Taken and adapter from: https://github.com/LibreHardwareMonitor/LibreHardwareMonitor/blob/master/LibreHardwareMonitor/UI/StartupManager.cs
/// </summary>
public class StartupManager
{
private const string RegistryPath = @"Software\Microsoft\Windows\CurrentVersion\Run";
private bool _startup;
public String NameOf { get; set; }
public String Description { get; set; }
public string NameOf { get; set; }
public string Description { get; set; }
public StartupManager(String name, String description = null)
public StartupManager(string name, string description = null)
{
NameOf = name;
if (description != null)
Description = description;
else
Description = String.Format("Starts {0} on Windows startup", name);
Description = string.Format("Starts {0} on Windows startup", name);
if (Environment.OSVersion.Platform >= PlatformID.Unix)
{

View file

@ -14,6 +14,11 @@ namespace PowerControl.Helpers
/// </summary>
public static class AudioManager
{
public static int GetMasterVolume(double roundValue)
{
return (int)(Math.Round(GetMasterVolume() / roundValue) * roundValue);
}
#region Master Volume Manipulation
/// <summary>
@ -42,11 +47,6 @@ namespace PowerControl.Helpers
}
}
public static int GetMasterVolume(double roundValue)
{
return (int)(Math.Round(GetMasterVolume() / roundValue) * roundValue);
}
/// <summary>
/// Gets the mute state of the master volume.
/// While the volume can be muted the <see cref="GetMasterVolume"/> will still return the pre-muted volume value.

View file

@ -7,8 +7,16 @@ using System.Threading.Tasks;
namespace PowerControl.Helpers
{
/// <summary>
/// Taken from: https://gist.github.com/maxkoshevoi/b8a1ad91f4d2a9fd3931168c14080694
/// </summary>
public static class WindowsSettingsBrightnessController
{
public static int Get(double roundValue = 10.0)
{
return (int)(Math.Round(Get() / roundValue) * roundValue);
}
public static int Get()
{
try
@ -30,11 +38,6 @@ namespace PowerControl.Helpers
}
}
public static int Get(double roundValue = 10.0)
{
return (int)(Math.Round(Get() / roundValue) * roundValue);
}
public static void Set(int brightness)
{
using var mclass = new ManagementClass("WmiMonitorBrightnessMethods")

View file

@ -18,6 +18,7 @@
<ItemGroup>
<ProjectReference Include="..\CommonHelpers\CommonHelpers.csproj" />
<ProjectReference Include="..\ExternalHelpers\ExternalHelpers.csproj" />
</ItemGroup>
<ItemGroup>

View file

@ -1,5 +1,5 @@
using CommonHelpers;
using CommonHelpers.FromLibreHardwareMonitor;
using ExternalHelpers;
using FanControl.Properties;
using System;
using System.Collections.Generic;

View file

@ -1,7 +1,6 @@
using CommonHelpers;
using CommonHelpers.FromLibreHardwareMonitor;
using ExternalHelpers;
using Microsoft.VisualBasic.Logging;
using PerformanceOverlay.External;
using RTSSSharedMemoryNET;
using System;
using System.Collections.Generic;

View file

@ -1,160 +0,0 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Input;
namespace PerformanceOverlay.External
{
public class GlobalHotKey : IDisposable
{
/// <summary>
/// Registers a global hotkey
/// </summary>
/// <param name="aKeyGesture">e.g. Alt + Shift + Control + Win + S</param>
/// <param name="aAction">Action to be called when hotkey is pressed</param>
/// <returns>true, if registration succeeded, otherwise false</returns>
public static bool RegisterHotKey(string aKeyGestureString, Action aAction)
{
var c = new KeyGestureConverter();
KeyGesture aKeyGesture = (KeyGesture)c.ConvertFrom(aKeyGestureString);
return RegisterHotKey(aKeyGesture.Modifiers, aKeyGesture.Key, aAction);
}
public static bool RegisterHotKey(ModifierKeys aModifier, Key aKey, Action aAction)
{
if (aModifier == ModifierKeys.None && false)
{
throw new ArgumentException("Modifier must not be ModifierKeys.None");
}
if (aAction is null)
{
throw new ArgumentNullException(nameof(aAction));
}
System.Windows.Forms.Keys aVirtualKeyCode = (System.Windows.Forms.Keys)KeyInterop.VirtualKeyFromKey(aKey);
currentID = currentID + 1;
bool aRegistered = RegisterHotKey(window.Handle, currentID,
(uint)aModifier | MOD_NOREPEAT,
(uint)aVirtualKeyCode);
if (aRegistered)
{
registeredHotKeys.Add(new HotKeyWithAction(aModifier, aKey, aAction));
}
return aRegistered;
}
public void Dispose()
{
// unregister all the registered hot keys.
for (int i = currentID; i > 0; i--)
{
UnregisterHotKey(window.Handle, i);
}
// dispose the inner native window.
window.Dispose();
}
static GlobalHotKey()
{
window.KeyPressed += (s, e) =>
{
registeredHotKeys.ForEach(x =>
{
if (e.Modifier == x.Modifier && e.Key == x.Key)
{
x.Action();
}
});
};
}
private static readonly InvisibleWindowForMessages window = new InvisibleWindowForMessages();
private static int currentID;
private static uint MOD_NOREPEAT = 0x4000;
private static List<HotKeyWithAction> registeredHotKeys = new List<HotKeyWithAction>();
private class HotKeyWithAction
{
public HotKeyWithAction(ModifierKeys modifier, Key key, Action action)
{
Modifier = modifier;
Key = key;
Action = action;
}
public ModifierKeys Modifier { get; }
public Key Key { get; }
public Action Action { get; }
}
// Registers a hot key with Windows.
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
// Unregisters the hot key with Windows.
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private class InvisibleWindowForMessages : System.Windows.Forms.NativeWindow, IDisposable
{
public InvisibleWindowForMessages()
{
CreateHandle(new System.Windows.Forms.CreateParams());
}
private static int WM_HOTKEY = 0x0312;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_HOTKEY)
{
var aWPFKey = KeyInterop.KeyFromVirtualKey(((int)m.LParam >> 16) & 0xFFFF);
ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);
if (KeyPressed != null)
{
KeyPressed(this, new HotKeyPressedEventArgs(modifier, aWPFKey));
}
}
}
public class HotKeyPressedEventArgs : EventArgs
{
private ModifierKeys _modifier;
private Key _key;
internal HotKeyPressedEventArgs(ModifierKeys modifier, Key key)
{
_modifier = modifier;
_key = key;
}
public ModifierKeys Modifier
{
get { return _modifier; }
}
public Key Key
{
get { return _key; }
}
}
public event EventHandler<HotKeyPressedEventArgs> KeyPressed;
#region IDisposable Members
public void Dispose()
{
this.DestroyHandle();
}
#endregion
}
}
}

View file

@ -29,11 +29,12 @@
<ItemGroup>
<ProjectReference Include="..\CommonHelpers\CommonHelpers.csproj" />
<ProjectReference Include="..\ExternalHelpers\ExternalHelpers.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="RTSSSharedMemoryNET">
<HintPath>..\CommonHelpers\RTSSSharedMemoryNET.dll</HintPath>
<HintPath>..\ExternalHelpers\RTSSSharedMemoryNET.dll</HintPath>
</Reference>
</ItemGroup>

View file

@ -1,5 +1,5 @@
using CommonHelpers;
using CommonHelpers.FromLibreHardwareMonitor;
using ExternalHelpers;
using Microsoft.VisualBasic.Logging;
using PowerControl.External;
using PowerControl.Helpers;

View file

@ -1,45 +1,165 @@
using Microsoft.VisualBasic.Logging;
using System;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using static PowerControl.Helpers.PhysicalMonitorBrightnessController;
namespace PowerControl.Helpers
{
// Taken from: https://stackoverflow.com/questions/4013622/adjust-screen-brightness-using-c-sharp
public class PhysicalMonitorBrightnessController : IDisposable
internal class DisplayResolutionController
{
#region DllImport
[DllImport("dxva2.dll", EntryPoint = "GetNumberOfPhysicalMonitorsFromHMONITOR")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, ref uint pdwNumberOfPhysicalMonitors);
public struct DisplayResolution : IComparable<DisplayResolution>
{
public int Width { get; set; }
public int Height { get; set; }
[DllImport("dxva2.dll", EntryPoint = "GetPhysicalMonitorsFromHMONITOR")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, uint dwPhysicalMonitorArraySize, [Out] PHYSICAL_MONITOR[] pPhysicalMonitorArray);
public DisplayResolution() { Width = 0; Height = 0; }
[DllImport("dxva2.dll", EntryPoint = "GetMonitorBrightness")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetMonitorBrightness(IntPtr handle, ref uint minimumBrightness, ref uint currentBrightness, ref uint maxBrightness);
public DisplayResolution(int width, int height) { Width = width; Height = height; }
[DllImport("dxva2.dll", EntryPoint = "SetMonitorBrightness")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetMonitorBrightness(IntPtr handle, uint newBrightness);
public static bool operator ==(DisplayResolution sz1, DisplayResolution sz2) => sz1.Width == sz2.Width && sz1.Height == sz2.Height;
[DllImport("dxva2.dll", EntryPoint = "DestroyPhysicalMonitor")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DestroyPhysicalMonitor(IntPtr hMonitor);
public static bool operator !=(DisplayResolution sz1, DisplayResolution sz2) => !(sz1 == sz2);
[DllImport("dxva2.dll", EntryPoint = "DestroyPhysicalMonitors")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DestroyPhysicalMonitors(uint dwPhysicalMonitorArraySize, [In] PHYSICAL_MONITOR[] pPhysicalMonitorArray);
public override readonly bool Equals([NotNullWhen(true)] object? obj) => obj is DisplayResolution && Equals((DisplayResolution)obj);
public readonly bool Equals(DisplayResolution other) => this == other;
public override readonly int GetHashCode() => HashCode.Combine(Width, Height);
public int CompareTo(DisplayResolution other)
{
var index = Width.CompareTo(other.Width);
if (index == 0) index = Height.CompareTo(other.Height);
return index;
}
public override string ToString()
{
return String.Format("{0}x{1}", Width, Height);
}
}
private static IEnumerable<DEVMODE> FindAllDisplaySettings()
{
DEVMODE dm = new DEVMODE();
for (int i = 0; EnumDisplaySettings(null, i, ref dm); i++)
{
if (dm.dmFields.HasFlag(DM.PelsWidth) && dm.dmFields.HasFlag(DM.PelsHeight) && dm.dmFields.HasFlag(DM.PelsHeight))
yield return dm;
dm = new DEVMODE();
}
}
private static DEVMODE? CurrentDisplaySettings()
{
DEVMODE dm = new DEVMODE();
if (EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref dm))
return dm;
return null;
}
public static DisplayResolution[] GetAllResolutions()
{
return FindAllDisplaySettings()
.Select((dm) => new DisplayResolution(dm.dmPelsWidth, dm.dmPelsHeight))
.ToImmutableSortedSet()
.ToArray();
}
public static DisplayResolution? GetResolution()
{
var dm = CurrentDisplaySettings();
if (dm is not null)
return new DisplayResolution(dm.Value.dmPelsWidth, dm.Value.dmPelsHeight);
return null;
}
public static bool SetResolution(DisplayResolution size)
{
DEVMODE? best = FindAllDisplaySettings()
.Where((dm) => dm.dmPelsWidth == size.Width && dm.dmPelsHeight == size.Height)
.MaxBy((dm) => dm.dmDisplayFrequency);
if (best == null)
return false;
DEVMODE dm = best.Value;
var dispChange = ChangeDisplaySettingsEx(null, ref dm, IntPtr.Zero, ChangeDisplaySettingsFlags.CDS_NONE, IntPtr.Zero);
if (!EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref dm))
return false;
Trace.WriteLine("DispChange: " + dispChange.ToString() + " Size:" + size.ToString() +
" SetSize:" + new DisplayResolution(dm.dmPelsWidth, dm.dmPelsHeight).ToString());
if (dispChange == DISP_CHANGE.Successful)
return true;
return true;
}
public static int[] GetRefreshRates(DisplayResolution? size = null)
{
if (size is null)
size = GetResolution();
if (size is null)
return new int[0];
return FindAllDisplaySettings()
.Where((dm) => dm.dmPelsWidth == size?.Width && dm.dmPelsHeight == size?.Height)
.Select((dm) => dm.dmDisplayFrequency)
.ToHashSet()
.ToArray();
}
public static int GetRefreshRate()
{
var dm = CurrentDisplaySettings();
if (dm is not null)
return dm.Value.dmDisplayFrequency;
return -1;
}
public static bool SetRefreshRate(int hz)
{
var current = CurrentDisplaySettings();
if (current is null)
return false;
DEVMODE? best = FindAllDisplaySettings()
.Where((dm) => dm.dmPelsWidth == current.Value.dmPelsWidth && dm.dmPelsHeight == current.Value.dmPelsHeight)
.Where((dm) => dm.dmDisplayFrequency == hz)
.First();
if (best is null)
return false;
DEVMODE dm = best.Value;
var dispChange = ChangeDisplaySettingsEx(null, ref dm, IntPtr.Zero, ChangeDisplaySettingsFlags.CDS_NONE, IntPtr.Zero);
if (!EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref dm))
return false;
Trace.WriteLine("DispChange: " + dispChange.ToString() + " HZ:" + hz.ToString() + " SetHZ:" + dm.dmDisplayFrequency.ToString());
if (dispChange == DISP_CHANGE.Successful)
return true;
return true;
}
[DllImport("user32.dll")]
static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, EnumMonitorsDelegate lpfnEnum, IntPtr dwData);
delegate bool EnumMonitorsDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData);
enum DISP_CHANGE : int
{
@ -192,285 +312,5 @@ namespace PowerControl.Helpers
[DllImport("user32.dll")]
static extern DISP_CHANGE ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, ChangeDisplaySettingsFlags dwflags, IntPtr lParam);
#endregion
private IReadOnlyCollection<MonitorInfo> Monitors { get; set; }
public PhysicalMonitorBrightnessController()
{
UpdateMonitors();
}
public struct DisplayResolution : IComparable<DisplayResolution>
{
public int Width { get; set; }
public int Height { get; set; }
public DisplayResolution() { Width = 0; Height = 0; }
public DisplayResolution(int width, int height) { Width = width; Height = height; }
public static bool operator ==(DisplayResolution sz1, DisplayResolution sz2) => sz1.Width == sz2.Width && sz1.Height == sz2.Height;
public static bool operator !=(DisplayResolution sz1, DisplayResolution sz2) => !(sz1 == sz2);
public override readonly bool Equals([NotNullWhen(true)] object? obj) => obj is DisplayResolution && Equals((DisplayResolution)obj);
public readonly bool Equals(DisplayResolution other) => this == other;
public override readonly int GetHashCode() => HashCode.Combine(Width, Height);
public int CompareTo(DisplayResolution other)
{
var index = Width.CompareTo(other.Width);
if (index == 0) index = Height.CompareTo(other.Height);
return index;
}
public override string ToString()
{
return String.Format("{0}x{1}", Width, Height);
}
}
#region Get & Set
public void Set(uint brightness)
{
Set(brightness, true);
}
private static IEnumerable<DEVMODE> FindAllDisplaySettings()
{
DEVMODE dm = new DEVMODE();
for (int i = 0; EnumDisplaySettings(null, i, ref dm); i++)
{
if (dm.dmFields.HasFlag(DM.PelsWidth) && dm.dmFields.HasFlag(DM.PelsHeight) && dm.dmFields.HasFlag(DM.PelsHeight))
yield return dm;
dm = new DEVMODE();
}
}
private static DEVMODE? CurrentDisplaySettings()
{
DEVMODE dm = new DEVMODE();
if (EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref dm))
return dm;
return null;
}
public static DisplayResolution[] GetAllResolutions()
{
return FindAllDisplaySettings()
.Select((dm) => new DisplayResolution(dm.dmPelsWidth, dm.dmPelsHeight))
.ToImmutableSortedSet()
.ToArray();
}
public static DisplayResolution? GetResolution()
{
var dm = CurrentDisplaySettings();
if (dm is not null)
return new DisplayResolution(dm.Value.dmPelsWidth, dm.Value.dmPelsHeight);
return null;
}
public static bool SetResolution(DisplayResolution size)
{
DEVMODE? best = FindAllDisplaySettings()
.Where((dm) => dm.dmPelsWidth == size.Width && dm.dmPelsHeight == size.Height)
.MaxBy((dm) => dm.dmDisplayFrequency);
if (best == null)
return false;
DEVMODE dm = best.Value;
var dispChange = ChangeDisplaySettingsEx(null, ref dm, IntPtr.Zero, ChangeDisplaySettingsFlags.CDS_NONE, IntPtr.Zero);
if (!EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref dm))
return false;
Trace.WriteLine("DispChange: " + dispChange.ToString() + " Size:" + size.ToString() +
" SetSize:" + new DisplayResolution(dm.dmPelsWidth, dm.dmPelsHeight).ToString());
if (dispChange == DISP_CHANGE.Successful)
return true;
return true;
}
public static int[] GetRefreshRates(DisplayResolution? size = null)
{
if (size is null)
size = GetResolution();
if (size is null)
return new int[0];
return FindAllDisplaySettings()
.Where((dm) => dm.dmPelsWidth == size?.Width && dm.dmPelsHeight == size?.Height)
.Select((dm) => dm.dmDisplayFrequency)
.ToHashSet()
.ToArray();
}
public static int GetRefreshRate()
{
var dm = CurrentDisplaySettings();
if (dm is not null)
return dm.Value.dmDisplayFrequency;
return -1;
}
public static bool SetRefreshRate(int hz)
{
var current = CurrentDisplaySettings();
if (current is null)
return false;
DEVMODE? best = FindAllDisplaySettings()
.Where((dm) => dm.dmPelsWidth == current.Value.dmPelsWidth && dm.dmPelsHeight == current.Value.dmPelsHeight)
.Where((dm) => dm.dmDisplayFrequency == hz)
.First();
if (best is null)
return false;
DEVMODE dm = best.Value;
var dispChange = ChangeDisplaySettingsEx(null, ref dm, IntPtr.Zero, ChangeDisplaySettingsFlags.CDS_NONE, IntPtr.Zero);
if (!EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref dm))
return false;
Trace.WriteLine("DispChange: " + dispChange.ToString() + " HZ:" + hz.ToString() + " SetHZ:" + dm.dmDisplayFrequency.ToString());
if (dispChange == DISP_CHANGE.Successful)
return true;
return true;
}
private void Set(uint brightness, bool refreshMonitorsIfNeeded)
{
bool isSomeFail = false;
foreach (var monitor in Monitors)
{
uint realNewValue = (monitor.MaxValue - monitor.MinValue) * brightness / 100 + monitor.MinValue;
if (SetMonitorBrightness(monitor.Handle, realNewValue))
{
monitor.CurrentValue = realNewValue;
}
else if (refreshMonitorsIfNeeded)
{
isSomeFail = true;
break;
}
}
if (refreshMonitorsIfNeeded && (isSomeFail || !Monitors.Any()))
{
UpdateMonitors();
Set(brightness, false);
return;
}
}
public int Get()
{
if (!Monitors.Any())
{
return -1;
}
return (int)Monitors.Average(d => d.CurrentValue);
}
#endregion
private void UpdateMonitors()
{
DisposeMonitors(this.Monitors);
var monitors = new List<MonitorInfo>();
EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, (IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData) =>
{
uint physicalMonitorsCount = 0;
if (!GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, ref physicalMonitorsCount))
{
// Cannot get monitor count
return true;
}
var physicalMonitors = new PHYSICAL_MONITOR[physicalMonitorsCount];
if (!GetPhysicalMonitorsFromHMONITOR(hMonitor, physicalMonitorsCount, physicalMonitors))
{
// Cannot get physical monitor handle
return true;
}
foreach (PHYSICAL_MONITOR physicalMonitor in physicalMonitors)
{
uint minValue = 0, currentValue = 0, maxValue = 0;
var info = new MonitorInfo
{
Handle = physicalMonitor.hPhysicalMonitor,
MinValue = minValue,
CurrentValue = currentValue,
MaxValue = maxValue,
};
monitors.Add(info);
}
return true;
}, IntPtr.Zero);
this.Monitors = monitors;
}
public void Dispose()
{
DisposeMonitors(Monitors);
GC.SuppressFinalize(this);
}
private static void DisposeMonitors(IEnumerable<MonitorInfo> monitors)
{
if (monitors?.Any() == true)
{
PHYSICAL_MONITOR[] monitorArray = monitors.Select(m => new PHYSICAL_MONITOR { hPhysicalMonitor = m.Handle }).ToArray();
DestroyPhysicalMonitors((uint)monitorArray.Length, monitorArray);
}
}
#region Classes
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct PHYSICAL_MONITOR
{
public IntPtr hPhysicalMonitor;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szPhysicalMonitorDescription;
}
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
}
public class MonitorInfo
{
public uint MinValue { get; set; }
public uint MaxValue { get; set; }
public IntPtr Handle { get; set; }
public uint CurrentValue { get; set; }
}
#endregion
}
}

View file

@ -20,7 +20,7 @@ namespace PowerControl.Helpers
{
try
{
processId = (int?)Helpers.TopLevelWindow.GetTopLevelProcessId();
processId = (int?)GetTopLevelProcessId();
if (processId is null)
return false;
@ -81,11 +81,17 @@ namespace PowerControl.Helpers
}
}
[DllImport("kernel32.dll", EntryPoint = "GetModuleHandleW", SetLastError = true)]
public static extern IntPtr GetModuleHandle(string moduleName);
public static uint EnableFlag(uint flag, bool status)
{
var current = SetFlags(~flag, status ? flag : 0);
UpdateSettings();
return current;
}
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)]
static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpFileName);
public static void UpdateSettings()
{
PostMessage(WM_RTSS_UPDATESETTINGS, IntPtr.Zero, IntPtr.Zero);
}
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
@ -118,6 +124,21 @@ namespace PowerControl.Helpers
[DllImport("C:\\Program Files (x86)\\RivaTuner Statistics Server\\RTSSHooks64.dll", CharSet = CharSet.Ansi)]
public static extern void UpdateProfiles();
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
private static uint? GetTopLevelProcessId()
{
var hWnd = GetForegroundWindow();
var result = GetWindowThreadProcessId(hWnd, out uint processId);
if (result != 0)
return processId;
return null;
}
private static void PostMessage(uint Msg, IntPtr wParam, IntPtr lParam)
{
var hWnd = FindWindow(null, "RTSS");
@ -128,18 +149,6 @@ namespace PowerControl.Helpers
PostMessage(hWnd, Msg, wParam, lParam);
}
public static uint EnableFlag(uint flag, bool status)
{
var current = SetFlags(~flag, status ? flag : 0);
UpdateSettings();
return current;
}
public static void UpdateSettings()
{
PostMessage(WM_RTSS_UPDATESETTINGS, IntPtr.Zero, IntPtr.Zero);
}
public const uint WM_APP = 0x8000;
public const uint WM_RTSS_UPDATESETTINGS = WM_APP + 100;
public const uint WM_RTSS_SHOW_PROPERTIES = WM_APP + 102;

View file

@ -1,27 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace PowerControl.Helpers
{
public class TopLevelWindow
{
[DllImport("user32.dll", SetLastError= true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
public static uint? GetTopLevelProcessId()
{
var hWnd = GetForegroundWindow();
var result = GetWindowThreadProcessId(hWnd, out uint processId);
if (result != 0)
return processId;
return null;
}
}
}

View file

@ -59,11 +59,11 @@ namespace PowerControl
ResetValue = () => {
if (!AMDAdrenaline.IsGPUScalingEnabled() && !Settings.Default.EnableExperimentalFeatures)
return null;
return Helpers.PhysicalMonitorBrightnessController.GetAllResolutions().Last();
return DisplayResolutionController.GetAllResolutions().Last();
},
OptionsValues = delegate()
{
var resolutions = Helpers.PhysicalMonitorBrightnessController.GetAllResolutions();
var resolutions = DisplayResolutionController.GetAllResolutions();
if (resolutions.Count() > 1)
return resolutions.Select(item => (object)item).ToArray();
return null;
@ -72,37 +72,37 @@ namespace PowerControl
{
if (!AMDAdrenaline.IsGPUScalingEnabled() && !Settings.Default.EnableExperimentalFeatures)
return null;
return Helpers.PhysicalMonitorBrightnessController.GetResolution();
return DisplayResolutionController.GetResolution();
},
ApplyValue = delegate(object selected)
{
Helpers.PhysicalMonitorBrightnessController.SetResolution((DisplayResolution)selected);
DisplayResolutionController.SetResolution((DisplayResolutionController.DisplayResolution)selected);
Root["Refresh Rate"].Update(); // force refresh Refresh Rate
Root["FPS Limit"].Update(); // force refresh FPS limit
return Helpers.PhysicalMonitorBrightnessController.GetResolution();
return DisplayResolutionController.GetResolution();
}
},
new Menu.MenuItemWithOptions()
{
Name = "Refresh Rate",
ApplyDelay = 1000,
ResetValue = () => { return Helpers.PhysicalMonitorBrightnessController.GetRefreshRates().Max(); },
ResetValue = () => { return DisplayResolutionController.GetRefreshRates().Max(); },
OptionsValues = delegate()
{
var refreshRates = Helpers.PhysicalMonitorBrightnessController.GetRefreshRates();
var refreshRates = DisplayResolutionController.GetRefreshRates();
if (refreshRates.Count() > 1)
return refreshRates.Select(item => (object)item).ToArray();
return null;
},
CurrentValue = delegate()
{
return Helpers.PhysicalMonitorBrightnessController.GetRefreshRate();
return DisplayResolutionController.GetRefreshRate();
},
ApplyValue = delegate(object selected)
{
Helpers.PhysicalMonitorBrightnessController.SetRefreshRate((int)selected);
DisplayResolutionController.SetRefreshRate((int)selected);
Root["FPS Limit"].Update(); // force refresh FPS limit
return Helpers.PhysicalMonitorBrightnessController.GetRefreshRate();
return DisplayResolutionController.GetRefreshRate();
}
},
new Menu.MenuItemWithOptions()
@ -112,7 +112,7 @@ namespace PowerControl
ResetValue = () => { return "Off"; },
OptionsValues = delegate()
{
var refreshRate = Helpers.PhysicalMonitorBrightnessController.GetRefreshRate();
var refreshRate = DisplayResolutionController.GetRefreshRate();
return new object[]
{
refreshRate / 4, refreshRate / 2, refreshRate, "Off"

View file

@ -29,14 +29,15 @@
<ItemGroup>
<ProjectReference Include="..\CommonHelpers\CommonHelpers.csproj" />
<ProjectReference Include="..\ExternalHelpers\ExternalHelpers.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="hidapi.net">
<HintPath>hidapi.net.dll</HintPath>
<HintPath>..\ExternalHelpers\hidapi.net.dll</HintPath>
</Reference>
<Reference Include="RTSSSharedMemoryNET">
<HintPath>..\CommonHelpers\RTSSSharedMemoryNET.dll</HintPath>
<HintPath>..\ExternalHelpers\RTSSSharedMemoryNET.dll</HintPath>
</Reference>
</ItemGroup>
@ -61,12 +62,6 @@
</ItemGroup>
<ItemGroup>
<None Update="hidapi.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="hidapi.net.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\RyzenAdj\inpoutx64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

View file

@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PerformanceOverlay", "Perfo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommonHelpers", "CommonHelpers\CommonHelpers.csproj", "{17728E90-015B-4221-8AA8-68FB359FA12F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PowerControl", "PowerControl\PowerControl.csproj", "{85A44F35-60C9-493E-B1A7-FB2284E5ACCF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PowerControl", "PowerControl\PowerControl.csproj", "{85A44F35-60C9-493E-B1A7-FB2284E5ACCF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExternalHelpers", "ExternalHelpers\ExternalHelpers.csproj", "{A3FD29A4-844F-42B1-80C5-0301BD053AC0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -69,6 +71,18 @@ Global
{85A44F35-60C9-493E-B1A7-FB2284E5ACCF}.Release|x64.Build.0 = Release|Any CPU
{85A44F35-60C9-493E-B1A7-FB2284E5ACCF}.Release|x86.ActiveCfg = Release|Any CPU
{85A44F35-60C9-493E-B1A7-FB2284E5ACCF}.Release|x86.Build.0 = Release|Any CPU
{A3FD29A4-844F-42B1-80C5-0301BD053AC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3FD29A4-844F-42B1-80C5-0301BD053AC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3FD29A4-844F-42B1-80C5-0301BD053AC0}.Debug|x64.ActiveCfg = Debug|Any CPU
{A3FD29A4-844F-42B1-80C5-0301BD053AC0}.Debug|x64.Build.0 = Debug|Any CPU
{A3FD29A4-844F-42B1-80C5-0301BD053AC0}.Debug|x86.ActiveCfg = Debug|Any CPU
{A3FD29A4-844F-42B1-80C5-0301BD053AC0}.Debug|x86.Build.0 = Debug|Any CPU
{A3FD29A4-844F-42B1-80C5-0301BD053AC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3FD29A4-844F-42B1-80C5-0301BD053AC0}.Release|Any CPU.Build.0 = Release|Any CPU
{A3FD29A4-844F-42B1-80C5-0301BD053AC0}.Release|x64.ActiveCfg = Release|Any CPU
{A3FD29A4-844F-42B1-80C5-0301BD053AC0}.Release|x64.Build.0 = Release|Any CPU
{A3FD29A4-844F-42B1-80C5-0301BD053AC0}.Release|x86.ActiveCfg = Release|Any CPU
{A3FD29A4-844F-42B1-80C5-0301BD053AC0}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE