using System.Linq; using WindowsDisplayAPI.DisplayConfig; using WindowsDisplayAPI.Native.DeviceContext; namespace WindowsDisplayAPI { /// /// Represents a Windows Display Device /// public class DisplayDevice : Device { /// /// Creates a new DisplayDevice /// /// The device path /// The device name /// The device driver registry key protected DisplayDevice(string devicePath, string deviceName, string deviceKey) : base(devicePath, deviceName, deviceKey) { } /// /// Gets the corresponding instance. /// public DisplayScreen DisplayScreen { get => DisplayScreen.GetScreens().FirstOrDefault(info => info.ScreenName.Equals(ScreenName)); } /// /// Creates a new DisplayDevice /// /// The device path /// The device name /// The device driver registry key /// The device parent DisplayAdapter /// true if the device is attached, otherwise false /// true if this instance is valid, otherwise false protected DisplayDevice( string devicePath, string deviceName, string deviceKey, DisplayAdapter adapter, bool isAvailable, bool isValid) : this(devicePath, deviceName, deviceKey) { Adapter = adapter; IsAvailable = isAvailable; IsValid = isValid; } /// /// Creates a new DisplayDevice /// /// The device path /// The device name /// The device driver registry key /// The device parent DisplayAdapter /// The device source display name /// The device target display name /// true if the device is attached, otherwise false /// true if this instance is valid, otherwise false protected DisplayDevice( string devicePath, string deviceName, string deviceKey, DisplayAdapter adapter, string displayName, string displayFullName, bool isAvailable, bool isValid) : this(devicePath, deviceName, deviceKey, adapter, isAvailable, isValid) { ScreenName = displayName; DisplayName = displayFullName; } /// /// Gets the display device driving display adapter instance /// public virtual DisplayAdapter Adapter { get; } /// /// Gets the display device target name /// public virtual string DisplayName { get; } /// /// Gets the display device source name /// public virtual string ScreenName { get; } /// /// Gets a boolean value indicating if this display device is currently attached /// public virtual bool IsAvailable { get; } /// /// Gets a boolean value indicating if this instance is no longer valid, this may happen when display device attached /// status changes /// public virtual bool IsValid { get; } internal static DisplayDevice FromDeviceInformation( DisplayAdapter adapter, Native.DeviceContext.Structures.DisplayDevice sourceDevice, Native.DeviceContext.Structures.DisplayDevice targetDevice ) { return new DisplayDevice( targetDevice.DeviceId, targetDevice.DeviceString, targetDevice.DeviceKey, adapter, sourceDevice.DeviceName, targetDevice.DeviceName, targetDevice.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop), true ); } /// public override string ToString() { return string.IsNullOrWhiteSpace(DeviceName) ? $"{GetType().Name}: {DisplayName} - IsAvailable: {IsAvailable}" : $"{GetType().Name}: {DisplayName} ({DeviceName}) - IsAvailable: {IsAvailable}"; } /// /// Returns the corresponding PathDisplayTarget instance /// /// An instance of PathDisplayTarget, or null public PathDisplayTarget ToPathDisplayTarget() { return PathDisplayTarget .GetDisplayTargets() .FirstOrDefault(target => target.DevicePath.Equals(DevicePath)); } } }