-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
182 lines (149 loc) · 5.74 KB
/
Copy pathNativeMethods.cs
File metadata and controls
182 lines (149 loc) · 5.74 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
174
175
176
177
178
179
180
181
182
using System.Runtime.InteropServices;
namespace SecondaryMonitorStutterFix;
internal static class NativeMethods
{
internal const int WsExTransparent = 0x00000020;
internal const int WsExToolWindow = 0x00000080;
internal const int WsExNoActivate = 0x08000000;
internal const int EnumCurrentSettings = -1;
private const uint EddGetDeviceInterfaceName = 0x00000001;
private const uint TimerResolutionOneMillisecond = 1;
[DllImport("dwmapi.dll", PreserveSig = true)]
private static extern int DwmFlush();
[DllImport("winmm.dll")]
private static extern uint timeBeginPeriod(uint uPeriod);
[DllImport("winmm.dll")]
private static extern uint timeEndPeriod(uint uPeriod);
[DllImport("avrt.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern IntPtr AvSetMmThreadCharacteristics(string taskName, out uint taskIndex);
[DllImport("avrt.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool AvRevertMmThreadCharacteristics(IntPtr avrtHandle);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumDisplayDevices(
string? lpDevice,
uint iDevNum,
ref DisplayDevice lpDisplayDevice,
uint dwFlags);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumDisplaySettings(
string lpszDeviceName,
int iModeNum,
ref DevMode lpDevMode);
internal static (string FriendlyName, string PersistentId, int RefreshRate) DescribeDisplay(string deviceName)
{
var friendlyName = deviceName;
var persistentId = deviceName;
var monitor = DisplayDevice.Create();
if (EnumDisplayDevices(deviceName, 0, ref monitor, EddGetDeviceInterfaceName))
{
if (!string.IsNullOrWhiteSpace(monitor.DeviceString))
{
friendlyName = monitor.DeviceString.Trim();
}
persistentId = string.IsNullOrWhiteSpace(monitor.DeviceId)
? deviceName
: monitor.DeviceId.Trim();
}
var mode = DevMode.Create();
var refreshRate = EnumDisplaySettings(deviceName, EnumCurrentSettings, ref mode)
? mode.dmDisplayFrequency
: 0;
return (friendlyName, persistentId, refreshRate);
}
internal static void FlushComposition() => _ = DwmFlush();
internal static IDisposable EnterSynchronizedScheduling() => new SynchronizedSchedulingScope();
private sealed class SynchronizedSchedulingScope : IDisposable
{
private readonly bool _timerResolutionActive;
private readonly IntPtr _multimediaHandle;
private bool _disposed;
internal SynchronizedSchedulingScope()
{
_timerResolutionActive = timeBeginPeriod(TimerResolutionOneMillisecond) == 0;
_multimediaHandle = AvSetMmThreadCharacteristics("Playback", out _);
}
public void Dispose()
{
if (_disposed)
{
return;
}
if (_multimediaHandle != IntPtr.Zero)
{
_ = AvRevertMmThreadCharacteristics(_multimediaHandle);
}
if (_timerResolutionActive)
{
_ = timeEndPeriod(TimerResolutionOneMillisecond);
}
_disposed = true;
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct DisplayDevice
{
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceString;
public int StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceKey;
public static DisplayDevice Create() => new()
{
cb = Marshal.SizeOf<DisplayDevice>(),
DeviceName = string.Empty,
DeviceString = string.Empty,
DeviceId = string.Empty,
DeviceKey = string.Empty
};
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct DevMode
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public int dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmIcmMethod;
public int dmIcmIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
public static DevMode Create() => new()
{
dmDeviceName = string.Empty,
dmFormName = string.Empty,
dmSize = (short)Marshal.SizeOf<DevMode>()
};
}
}