Skip to content

Commit

Permalink
add ddc support
Browse files Browse the repository at this point in the history
  • Loading branch information
tombayley committed Aug 21, 2023
1 parent 607b06d commit 7b07cc4
Show file tree
Hide file tree
Showing 26 changed files with 1,197 additions and 34 deletions.
21 changes: 21 additions & 0 deletions NightGlow.MonitorConfig/Monitors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace NightGlow.MonitorConfig;

public class Monitors
{

public List<VirtualMonitor> VirtualMonitors = new List<VirtualMonitor>();

public bool Scan()
{
VirtualMonitors.Clear();
bool success = WinApi.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, MonitorEnumCallback, IntPtr.Zero);
return success;
}

private bool MonitorEnumCallback(IntPtr hMonitor, IntPtr hdcMonitor, ref WinApi.RECT lprcMonitor, IntPtr dwData)
{
VirtualMonitors.Add(new VirtualMonitor(hMonitor, VirtualMonitors.Count));
return true;
}

}
9 changes: 9 additions & 0 deletions NightGlow.MonitorConfig/NightGlow.MonitorConfig.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
45 changes: 45 additions & 0 deletions NightGlow.MonitorConfig/PhysicalMonitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using static NightGlow.MonitorConfig.WinApi;

namespace NightGlow.MonitorConfig;

public class PhysicalMonitor
{

PHYSICAL_MONITOR Monitor;

public string Description;

Setting Brightness = new();
Setting Contrast = new();

public PhysicalMonitor(PHYSICAL_MONITOR monitor)
{
Monitor = monitor;
Description = new string(Monitor.szPhysicalMonitorDescription);
}

public Setting GetBrightness()
{
bool success = GetMonitorBrightness(Monitor.hPhysicalMonitor, out Brightness.Min, out Brightness.Current, out Brightness.Max);
return Brightness;
}

public void SetBrightness(uint value)
{
bool success = SetMonitorBrightness(Monitor.hPhysicalMonitor, value);
Brightness.Current = value;
}

public Setting GetContrast()
{
bool success = GetMonitorContrast(Monitor.hPhysicalMonitor, out Contrast.Min, out Contrast.Current, out Contrast.Max);
return Contrast;
}

public void SetContrast(uint value)
{
bool success = SetMonitorContrast(Monitor.hPhysicalMonitor, value);
Contrast.Current = value;
}

}
24 changes: 24 additions & 0 deletions NightGlow.MonitorConfig/Setting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace NightGlow.MonitorConfig;

public class Setting
{

public uint Min;
public uint Max;
public uint Current;

public Setting()
{
Min = 0;
Max = 0;
Current = 0;
}

public Setting(uint min, uint max, uint current)
{
Min = min;
Max = max;
Current = current;
}

}
106 changes: 106 additions & 0 deletions NightGlow.MonitorConfig/VirtualMonitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using static NightGlow.MonitorConfig.WinApi;

namespace NightGlow.MonitorConfig;

public class VirtualMonitor
{

public string DeviceName = "";
public string FriendlyName = "";

public List<PhysicalMonitor> PhysicalMonitors = new();

private MONITORINFOEX MonitorInfo;

public VirtualMonitor(IntPtr hMonitor, int index)
{
MonitorInfo = new MONITORINFOEX { cbSize = (uint)Marshal.SizeOf<MONITORINFOEX>() };

if (!GetMonitorInfo(hMonitor, ref MonitorInfo))
{
// TODO throw error or log
Debug.WriteLine("Error: GetMonitorInfo");
}

DeviceName = MonitorInfo.szDevice;

LoadFriendlyName(index);

GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, out uint physicalMonitorCount);
if (physicalMonitorCount == 0)
return;

PHYSICAL_MONITOR[] physicalMonitorArray = new PHYSICAL_MONITOR[physicalMonitorCount];
GetPhysicalMonitorsFromHMONITOR(hMonitor, physicalMonitorCount, physicalMonitorArray);

for (int i = 0; i < physicalMonitorCount; i++)
{
PhysicalMonitors.Add(new PhysicalMonitor(physicalMonitorArray[i]));
}
}

public bool IsPrimary()
{
return (MonitorInfo.dwFlags & MONITORINFOF.MONITORINFOF_PRIMARY) == MONITORINFOF.MONITORINFOF_PRIMARY;
}

private void LoadFriendlyName(int index)
{
FriendlyName = "";

uint pathCount = 0, modeCount = 0;

long error = GetDisplayConfigBufferSizes(QUERY_DEVICE_CONFIG_FLAGS.QDC_ONLY_ACTIVE_PATHS,
ref pathCount, ref modeCount);
if (error != ERROR_SUCCESS)
{
// TODO throw error or log
Debug.WriteLine("Error: GetDisplayConfigBufferSizes");
return;
}

DISPLAYCONFIG_PATH_INFO[] displayPaths = new DISPLAYCONFIG_PATH_INFO[pathCount];
DISPLAYCONFIG_MODE_INFO[] displayModes = new DISPLAYCONFIG_MODE_INFO[modeCount];

error = QueryDisplayConfig(QUERY_DEVICE_CONFIG_FLAGS.QDC_ONLY_ACTIVE_PATHS,
ref pathCount, displayPaths, ref modeCount, displayModes, IntPtr.Zero);
if (error != ERROR_SUCCESS)
{
// TODO throw error or log
Debug.WriteLine("Error: QueryDisplayConfig");
return;
}

int modeTargetCount = 0;
for (int i = 0; i < modeCount; i++)
{
if (displayModes[i].infoType != DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_TARGET)
continue;

if (modeTargetCount != index)
{
modeTargetCount++;
continue;
}

DISPLAYCONFIG_TARGET_DEVICE_NAME deviceName = new DISPLAYCONFIG_TARGET_DEVICE_NAME();
deviceName.header.size = (uint)Marshal.SizeOf(typeof(DISPLAYCONFIG_TARGET_DEVICE_NAME));
deviceName.header.adapterId = displayModes[i].adapterId;
deviceName.header.id = displayModes[i].id;
deviceName.header.type = DISPLAYCONFIG_DEVICE_INFO_TYPE.DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME;

error = DisplayConfigGetDeviceInfo(ref deviceName);
if (error != ERROR_SUCCESS)
{
// TODO throw error or log
Debug.WriteLine("Error: DisplayConfigGetDeviceInfo");
return;
}
FriendlyName = deviceName.monitorFriendlyDeviceName;
return;
}
}

}
Loading

0 comments on commit 7b07cc4

Please sign in to comment.