Repository navigation
Expand file tree
/
Copy pathNative.cs
More file actions
173 lines (151 loc) · 5.85 KB
/
Copy pathNative.cs
File metadata and controls
173 lines (151 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// ============================================================================
// File: Native.cs
// Author: Scott Sheen (with ChatGpt)
// Created: 2026-10-02
// Updated: 2026-10-02
// Purpose: Windows interop layouts and active display discovery.
// ============================================================================
using System.Runtime.InteropServices;
namespace PowerMonitor;
internal static class Native
{
[StructLayout(LayoutKind.Sequential)]
internal struct PowerStatus
{
public byte ACLineStatus, BatteryFlag, BatteryLifePercent, SystemStatusFlag;
public int BatteryLifeTime, BatteryFullLifeTime;
}
[DllImport("kernel32.dll")]
internal static extern bool GetSystemPowerStatus(out PowerStatus status);
[DllImport("kernel32.dll")]
internal static extern bool GetSystemTimes(out ulong idle, out ulong kernel, out ulong user);
[DllImport("powrprof.dll")]
internal static extern uint PowerGetActiveScheme(IntPtr root, out IntPtr scheme);
[DllImport("kernel32.dll")]
internal static extern IntPtr LocalFree(IntPtr memory);
[StructLayout(LayoutKind.Sequential)]
struct Luid
{
public uint Low;
public int High;
}
[StructLayout(LayoutKind.Sequential)]
struct Rational
{
public uint Numerator, Denominator;
}
[StructLayout(LayoutKind.Sequential)]
struct Source
{
public Luid Adapter;
public uint Id, ModeIndex, Status;
}
[StructLayout(LayoutKind.Sequential)]
struct Target
{
public Luid Adapter;
public uint Id, ModeIndex, Technology, Rotation, Scaling;
public Rational Refresh;
public uint Scanline;
[MarshalAs(UnmanagedType.Bool)]
public bool Available;
public uint Status;
}
[StructLayout(LayoutKind.Sequential)]
struct DisplayPath
{
public Source Source;
public Target Target;
public uint Flags;
}
[StructLayout(LayoutKind.Explicit, Size = 64)]
struct Mode
{
[FieldOffset(0)]
public uint Type;
}
[StructLayout(LayoutKind.Sequential)]
struct Header
{
public uint Type, Size;
public Luid Adapter;
public uint Id;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct SourceName
{
public Header Header;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string Name;
}
[DllImport("user32.dll")]
static extern int GetDisplayConfigBufferSizes(uint flags, out uint paths, out uint modes);
[DllImport("user32.dll")]
static extern int QueryDisplayConfig(uint flags, ref uint paths, [Out] DisplayPath[] pathArray, ref uint modes, [Out] Mode[] modeArray, IntPtr topology);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern int DisplayConfigGetDeviceInfo(ref SourceName request);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct DevMode
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
public ushort SpecVersion, DriverVersion, Size, DriverExtra;
public uint Fields;
public int PositionX, PositionY;
public uint Orientation, FixedOutput;
public short Color, Duplex, YResolution, TTOption, Collate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string FormName;
public ushort LogPixels;
public uint BitsPerPel, PelsWidth, PelsHeight, DisplayFlags, DisplayFrequency, ICMMethod, ICMIntent, MediaType, DitherType, Reserved1, Reserved2, PanningWidth, PanningHeight;
}
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern bool EnumDisplaySettings(string device, int mode, ref DevMode settings);
internal record Display(string Name, int Width, int Height, int Hz, bool Internal);
internal static List<Display> ReadDisplays()
{
// Display topology can change between size discovery and retrieval; retry bounded buffer races.
for (var retry = 0; retry < 3; retry++)
{
if (GetDisplayConfigBufferSizes(2, out var paths, out var modes) != 0)
return[];
var pathArray = new DisplayPath[paths];
var modeArray = new Mode[modes];
var result = QueryDisplayConfig(2, ref paths, pathArray, ref modes, modeArray, IntPtr.Zero);
if (result == 122)
continue;
if (result != 0)
return[];
var list = new List<Display>();
foreach (var path in pathArray.Take((int)paths))
{
var request = new SourceName
{
Header = new Header
{
Type = 1,
Size = (uint)Marshal.SizeOf<SourceName>(),
Adapter = path.Source.Adapter,
Id = path.Source.Id
},
Name = ""
};
if (DisplayConfigGetDeviceInfo(ref request) != 0)
continue;
var dm = new DevMode
{
Size = (ushort)Marshal.SizeOf<DevMode>(),
DeviceName = "",
FormName = ""
};
EnumDisplaySettings(request.Name, -1, ref dm);
// Embedded DisplayPort, embedded UDI, and internal panel output technology.
var embedded = path.Target.Technology is 11 or 13 or 0x80000000;
var hz = path.Target.Refresh.Denominator > 0 ? (int)Math.Round((double)path.Target.Refresh.Numerator / path.Target.Refresh.Denominator) : (int)dm.DisplayFrequency;
list.Add(new(request.Name, (int)dm.PelsWidth, (int)dm.PelsHeight, hz, embedded));
}
return list;
}
return[];
}
}