|
1 | 1 | using Microsoft.Win32; |
2 | 2 | using Ryzen; |
3 | | -using System.Diagnostics; |
4 | 3 |
|
5 | | -namespace GHelper.Display |
| 4 | +public static class AmdDisplay |
6 | 5 | { |
7 | | - public static class AmdDisplay |
| 6 | + private const string DisplayPath0 = |
| 7 | + @"SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0001\DAL2_DATA__2_0\DisplayPath_0"; |
| 8 | + |
| 9 | + private static string _cachedAdjustmentPath = null; |
| 10 | + private static bool _isPathSearched = false; |
| 11 | + |
| 12 | + private static string GetAdjustmentPath() |
8 | 13 | { |
9 | | - private const string DisplayPath0 = |
10 | | - @"SYSTEM\CurrentControlSet\Control\Class\" + |
11 | | - @"{4d36e968-e325-11ce-bfc1-08002be10318}\" + |
12 | | - @"0001\DAL2_DATA__2_0\DisplayPath_0"; |
| 14 | + if (_isPathSearched) return _cachedAdjustmentPath; |
13 | 15 |
|
14 | | - public static bool IsOledPowerOptimizationOnBattery() |
| 16 | + try |
15 | 17 | { |
16 | | - if (!AppConfig.IsOLED()) return false; |
17 | | - //if (SystemInformation.PowerStatus.PowerLineStatus != PowerLineStatus.Offline) return false; |
18 | | - if (!RyzenControl.IsAMD()) return false; |
19 | | - |
20 | | - try |
| 18 | + using RegistryKey dp0 = Registry.LocalMachine.OpenSubKey(DisplayPath0, writable: false); |
| 19 | + if (dp0 != null) |
21 | 20 | { |
22 | | - using RegistryKey dp0 = Registry.LocalMachine.OpenSubKey(DisplayPath0, writable: false); |
23 | | - if (dp0 == null) return false; |
24 | | - |
25 | | - // Find the first EDID_* subkey — there's only one for the internal panel |
26 | 21 | string edidKey = Array.Find(dp0.GetSubKeyNames(), |
27 | 22 | name => name.StartsWith("EDID_", StringComparison.OrdinalIgnoreCase)); |
28 | 23 |
|
29 | | - if (edidKey == null) return false; |
| 24 | + if (edidKey != null) |
| 25 | + { |
| 26 | + _cachedAdjustmentPath = $@"HKEY_LOCAL_MACHINE\{DisplayPath0}\{edidKey}\Adjustment"; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + catch |
| 31 | + { |
| 32 | + // Handle permissions or missing keys silently |
| 33 | + } |
| 34 | + finally |
| 35 | + { |
| 36 | + _isPathSearched = true; |
| 37 | + } |
| 38 | + |
| 39 | + return _cachedAdjustmentPath; |
| 40 | + } |
30 | 41 |
|
31 | | - using RegistryKey adjustment = dp0.OpenSubKey($@"{edidKey}\Adjustment", writable: false); |
| 42 | + public static bool IsOledPowerOptimization() |
| 43 | + { |
| 44 | + if (!AppConfig.IsOLED() || !RyzenControl.IsAMD()) return false; |
32 | 45 |
|
33 | | - if (adjustment?.GetValue("DAL_SCE_Settings") is not byte[] data || data.Length < 5) |
34 | | - return false; |
| 46 | + try |
| 47 | + { |
| 48 | + string path = GetAdjustmentPath(); |
| 49 | + if (path == null) return false; |
35 | 50 |
|
36 | | - // Byte[4]: 0x02 = battery OLED optimization ON, 0x00 = OFF |
37 | | - return (data[4] & 0x02) != 0; |
38 | | - } catch |
| 51 | + object value = Registry.GetValue(path, "DAL_SCE_Settings", null); |
| 52 | + |
| 53 | + if (value is byte[] data && data.Length >= 5) |
39 | 54 | { |
40 | | - return false; |
| 55 | + return (data[4] & 0x02) != 0; |
41 | 56 | } |
42 | | - |
| 57 | + } catch |
| 58 | + { |
| 59 | + Logger.WriteLine("Can't check AMD OLED Optimization flag"); |
43 | 60 | } |
| 61 | + |
| 62 | + return false; |
44 | 63 | } |
45 | 64 | } |
0 commit comments