Skip to content

Commit

Permalink
fix generic microsoft drivers would prevent starting
Browse files Browse the repository at this point in the history
  • Loading branch information
mgth committed May 17, 2024
1 parent 6132f62 commit 8a8d0b3
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 101 deletions.
10 changes: 5 additions & 5 deletions HLab.Sys/HLab.Sys.Windows.Monitors/DisplayDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,33 @@ public IEnumerable<MonitorDevice> AllMonitorDevices()
.OrderBy(e => e.PhysicalId);

[JsonIgnore]
public DisplayDevice Parent { get; set; }
public DisplayDevice? Parent { get; set; }

public IEnumerable<DisplayDevice> Children => _children;

/// <summary>
/// Device name as returned by EnumDisplayDevices :
/// "ROOT", "\\\\.\\DISPLAY1", "\\\\.\\DISPLAY1\monitor0"
/// </summary>
public string DeviceName { get; init; }
public string DeviceName { get; init; } = "";

/// <summary>
/// Device name in human readable format :
/// "NVIDIA GeForce RTX 3080 Ti"
/// </summary>
public string DeviceString { get; init; }
public string DeviceString { get; init; } = "";

/// <summary>
/// Device id as returned by EnumDisplayDevices :
/// "PCI\\VEN_10DE&DEV_2206&SUBSYS_3A3C1458&REV_A1"
/// </summary>
public string Id { get; init; }
public string Id { get; init; } = "";

/// <summary>
/// Path to the device registry key :
/// "\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Video\\{AC0F00F9-3A6E-11ED-84B1-EBFE3BE9690A}\\0000"
/// </summary>
public string DeviceKey { get; init; }
public string DeviceKey { get; init; } = "";

public SourceList<DisplayMode> DisplayModes { get; } = new ();

Expand Down
57 changes: 26 additions & 31 deletions HLab.Sys/HLab.Sys.Windows.Monitors/Factory/DeviceFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HLab.Sys.Windows.API;
using System;
using System.Diagnostics;
using System.Linq;
using static HLab.Sys.Windows.API.WinGdi;

Expand Down Expand Up @@ -33,6 +34,7 @@ public static MonitorDeviceConnection BuildMonitorDevice(DisplayDevice parent, W
var connection = new MonitorDeviceConnection
{
Parent = parent as PhysicalAdapter,
Monitor = monitor,

//DisplayDevice
Id = device.DeviceID,
Expand All @@ -43,8 +45,6 @@ public static MonitorDeviceConnection BuildMonitorDevice(DisplayDevice parent, W
State = MonitorDeviceHelper.BuildDeviceState(device.StateFlags),
Capabilities = MonitorDeviceHelper.BuildDeviceCaps(device.DeviceName),
CurrentMode = MonitorDeviceHelper.GetCurrentMode(device.DeviceName),

Monitor = monitor,
};

monitor.Connections.Add(connection);
Expand Down Expand Up @@ -94,39 +94,34 @@ WinGdi.DisplayDevice dev
return result;
}

static DisplayDevice BuildDisplayDevice(DisplayDevice parent, WinGdi.DisplayDevice device)
static DisplayDevice BuildDisplayDevice(DisplayDevice? parent, WinGdi.DisplayDevice device)
{
switch (device.DeviceID.Split('\\')[0])
if(device.DeviceID == "ROOT")
{
case "ROOT":
return new DisplayDevice
{
Parent = parent,
//DisplayDevice
Id = device.DeviceID,
DeviceName = device.DeviceName,
DeviceString = device.DeviceString,
DeviceKey = device.DeviceKey,

State = MonitorDeviceHelper.BuildDeviceState(device.StateFlags),
Capabilities = MonitorDeviceHelper.BuildDeviceCaps(device.DeviceName),

CurrentMode = MonitorDeviceHelper.GetCurrentMode(device.DeviceName),
};

case "MONITOR":

return BuildMonitorDevice(parent, device);

case "RdpIdd_IndirectDisplay":
case string s when s.StartsWith("VID_DATRONICSOFT_PID_SPACEDESK_VIRTUAL_DISPLAY_"):
case "PCI":
default:
if(parent.Id=="ROOT")
return BuildPhysicalAdapter(parent, device);
break;
return new DisplayDevice
{
Parent = parent,
//DisplayDevice
Id = device.DeviceID,
DeviceName = device.DeviceName,
DeviceString = device.DeviceString,
DeviceKey = device.DeviceKey,

State = MonitorDeviceHelper.BuildDeviceState(device.StateFlags),
Capabilities = MonitorDeviceHelper.BuildDeviceCaps(device.DeviceName),

CurrentMode = MonitorDeviceHelper.GetCurrentMode(device.DeviceName),
};
}

Debug.Assert(parent != null);

if (parent.Id=="ROOT")
return BuildPhysicalAdapter(parent, device);

//if(device.DeviceID.Split('\\')[0] == "MONITOR")
return BuildMonitorDevice(parent, device);

throw new ArgumentException($"Unknown device type {device.DeviceName}");

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<Platforms>x64;x86;AnyCpu</Platforms>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>
<RootNamespace>HLab.Sys.Windows.Monitors</RootNamespace>
<Configurations>Debug;Release</Configurations>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
131 changes: 69 additions & 62 deletions HLab.Sys/HLab.Sys.Windows.Monitors/MonitorDevice.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.Serialization;
using Avalonia;
using Avalonia.Controls;
Expand All @@ -10,12 +11,12 @@ namespace HLab.Sys.Windows.Monitors;

public class MonitorDevice : IEquatable<MonitorDevice>
{
[DataMember] public string Id { get; init; }
[DataMember] public string PnpCode { get; init; }
[DataMember] public string PhysicalId { get; set; }
[DataMember] public string SourceId { get; set; }
[DataMember] public string Id { get; init; } = "";
[DataMember] public string PnpCode { get; init; } = "";
[DataMember] public string PhysicalId { get; set; } = "";
[DataMember] public string SourceId { get; set; } = "";
[DataMember] public IEdid Edid { get; init; }
[DataMember] public string MonitorNumber { get; set; }
[DataMember] public string MonitorNumber { get; set; } = "";

public List<MonitorDeviceConnection> Connections = new ();

Expand All @@ -34,7 +35,10 @@ public class MonitorDeviceConnection : DisplayDevice
{
public new PhysicalAdapter Parent
{
get => base.Parent as PhysicalAdapter;
get {
if (base.Parent is PhysicalAdapter adapter) return adapter;
throw new InvalidOperationException("Parent is not a PhysicalAdapter");
}
set => base.Parent = value;
}

Expand Down Expand Up @@ -104,7 +108,7 @@ void OpenRegKey(string keyString) {
keyString = keyString.Replace(@"\MACHINE\",@"\HKEY_LOCAL_MACHINE\");
keyString = keyString.Replace(@"\USER\",@"\HKEY_CURRENT_USER\");

using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", true)) {
using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", true)) {

if(key == null) return;
var value = key.GetValue("LastKey").ToString();
Expand All @@ -121,64 +125,67 @@ void OpenRegKey(string keyString) {
}


public void DisplayValues(Action<string, string, Action, bool> addValue) {
public void DisplayValues(Action<string, string, Action?, bool> addValue) {
//addValue("Registry", Edid.HKeyName, () => { OpenRegKey(Edid.HKeyName); }, false);
//addValue("Microsoft Id", PhysicalId, null, false);

// EnumDisplaySettings
addValue("", "EnumDisplaySettings", null, true);
addValue("DisplayOrientation", Parent?.CurrentMode?.DisplayOrientation.ToString(), null, false);
addValue("Position", Parent?.CurrentMode?.Position.ToString(), null, false);
addValue("Pels", Parent?.CurrentMode?.Pels.ToString(), null, false);
addValue("BitsPerPixel", Parent?.CurrentMode?.BitsPerPixel.ToString(), null, false);
addValue("DisplayFrequency", Parent?.CurrentMode?.DisplayFrequency.ToString(), null, false);
addValue("DisplayFlags", Parent?.CurrentMode?.DisplayFlags.ToString(), null, false);
addValue("DisplayFixedOutput", Parent?.CurrentMode?.DisplayFixedOutput.ToString(), null, false);

// GetDeviceCaps
addValue("", "GetDeviceCaps", null, true);
addValue("Size", Parent.Capabilities.Size.ToString(), null, false);
addValue("Res", Parent.Capabilities.Resolution.ToString(), null, false);
addValue("LogPixels", Parent.Capabilities.LogPixels.ToString(), null, false);
addValue("BitsPixel", Parent.Capabilities.BitsPixel.ToString(), null, false);
//AddValue("Color Planes", Monitor.Adapter.DeviceCaps.Planes.ToString());
addValue("Aspect", Parent.Capabilities.Aspect.ToString(), null, false);
//AddValue("BltAlignment", Monitor.Adapter.DeviceCaps.BltAlignment.ToString());

//GetDpiForMonitor
addValue("", "GetDpiForMonitor", null, true);
addValue("EffectiveDpi", Parent.EffectiveDpi.ToString(), null, false);
addValue("AngularDpi", Parent.AngularDpi.ToString(), null, false);
addValue("RawDpi", Parent.RawDpi.ToString(), null, false);

// GetMonitorInfo
addValue("", "GetMonitorInfo", null, true);
addValue("Primary", Parent.Primary.ToString(), null, false);
addValue("MonitorArea", Parent.MonitorArea.ToString(), null, false);
addValue("WorkArea", Parent.WorkArea.ToString(), null, false);


//// EDID
//addValue("", "EDID", null, true);
//addValue("ManufacturerCode", Edid?.ManufacturerCode, null, false);
//addValue("ProductCode", Edid?.ProductCode, null, false);
//addValue("Serial", Edid?.Serial, null, false);
//addValue("Model", Edid?.Model, null, false);
//addValue("SerialNo", Edid?.SerialNumber, null, false);
//addValue("SizeInMm", Edid?.PhysicalSize.ToString(), null, false);
//addValue("VideoInterface", Edid?.VideoInterface.ToString(), null, false);

// GetScaleFactorForMonitor
addValue("", "GetScaleFactorForMonitor", null, true);
addValue("ScaleFactor", Parent.ScaleFactor.ToString(), null, false);

// EnumDisplayDevices
addValue("", "EnumDisplayDevices", null, true);
addValue("DeviceId", Parent?.Id, null, false);
addValue("DeviceKey", Parent?.DeviceKey, null, false);
addValue("DeviceString", Parent?.DeviceString, null, false);
addValue("DeviceName", Parent?.DeviceName, null, false);
addValue("StateFlags", Parent?.State.ToString(), null, false);
if(Parent != null)
{
// EnumDisplaySettings
addValue("", "EnumDisplaySettings", null, true);
addValue("DisplayOrientation", Parent.CurrentMode?.DisplayOrientation.ToString() ?? "", null, false);
addValue("Position", Parent.CurrentMode?.Position.ToString() ?? "", null, false);
addValue("Pels", Parent.CurrentMode?.Pels.ToString() ?? "", null, false);
addValue("BitsPerPixel", Parent.CurrentMode?.BitsPerPixel.ToString() ?? "", null, false);
addValue("DisplayFrequency", Parent.CurrentMode?.DisplayFrequency.ToString() ?? "", null, false);
addValue("DisplayFlags", Parent.CurrentMode?.DisplayFlags.ToString() ?? "", null, false);
addValue("DisplayFixedOutput", Parent.CurrentMode?.DisplayFixedOutput.ToString() ?? "", null, false);

// GetDeviceCaps
addValue("", "GetDeviceCaps", null, true);
addValue("Size", Parent.Capabilities.Size.ToString(), null, false);
addValue("Res", Parent.Capabilities.Resolution.ToString(), null, false);
addValue("LogPixels", Parent.Capabilities.LogPixels.ToString(), null, false);
addValue("BitsPixel", Parent.Capabilities.BitsPixel.ToString(), null, false);
//AddValue("Color Planes", Monitor.Adapter.DeviceCaps.Planes.ToString());
addValue("Aspect", Parent.Capabilities.Aspect.ToString(), null, false);
//AddValue("BltAlignment", Monitor.Adapter.DeviceCaps.BltAlignment.ToString());

//GetDpiForMonitor
addValue("", "GetDpiForMonitor", null, true);
addValue("EffectiveDpi", Parent.EffectiveDpi.ToString(), null, false);
addValue("AngularDpi", Parent.AngularDpi.ToString(), null, false);
addValue("RawDpi", Parent.RawDpi.ToString(), null, false);

// GetMonitorInfo
addValue("", "GetMonitorInfo", null, true);
addValue("Primary", Parent.Primary.ToString(), null, false);
addValue("MonitorArea", Parent.MonitorArea.ToString(), null, false);
addValue("WorkArea", Parent.WorkArea.ToString(), null, false);


//// EDID
//addValue("", "EDID", null, true);
//addValue("ManufacturerCode", Edid?.ManufacturerCode, null, false);
//addValue("ProductCode", Edid?.ProductCode, null, false);
//addValue("Serial", Edid?.Serial, null, false);
//addValue("Model", Edid?.Model, null, false);
//addValue("SerialNo", Edid?.SerialNumber, null, false);
//addValue("SizeInMm", Edid?.PhysicalSize.ToString(), null, false);
//addValue("VideoInterface", Edid?.VideoInterface.ToString(), null, false);

// GetScaleFactorForMonitor
addValue("", "GetScaleFactorForMonitor", null, true);
addValue("ScaleFactor", Parent.ScaleFactor.ToString(CultureInfo.CurrentCulture) ?? "", null, false);

// EnumDisplayDevices
addValue("", "EnumDisplayDevices", null, true);
addValue("DeviceId", Parent.Id, null, false);
addValue("DeviceKey", Parent.DeviceKey, null, false);
addValue("DeviceString", Parent.DeviceString, null, false);
addValue("DeviceName", Parent.DeviceName, null, false);
addValue("StateFlags", Parent.State.ToString(), null, false);
}

addValue("", "EnumDisplayDevices", null, true);
addValue("DeviceId", Id, null, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ public static DisplaySource CreateDisplaySource(this MonitorDevice monitor)

public static DisplaySource UpdateFrom(this DisplaySource source, MonitorDevice monitor)
{
if(monitor.Connections.Count == 0) return source;
var device = monitor.Connections[0];

source.DisplayName = device.Parent?.DeviceName;
if(device.Parent == null) return source;

source.DisplayName = device.Parent.DeviceName;
source.DeviceName = device.DeviceName;

source.SourceName = $"{monitor.Edid?.VideoInterface??"Unknown"}:{device.DeviceName}";
Expand All @@ -97,7 +100,7 @@ public static DisplaySource UpdateFrom(this DisplaySource source, MonitorDevice
source.DpiAwareAngularDpi.Set(device.Parent.AngularDpi);
source.RawDpi.Set(device.Parent.RawDpi);

if (device.Parent?.CurrentMode is { } mode)
if (device.Parent.CurrentMode is { } mode)
{
source.DisplayFrequency = mode.DisplayFrequency;

Expand Down

0 comments on commit 8a8d0b3

Please sign in to comment.