using System;
using System.Collections.Generic;
using System.Linq;
using WindowsDisplayAPI.Exceptions;
using WindowsDisplayAPI.Native;
using WindowsDisplayAPI.Native.DeviceContext;
using WindowsDisplayAPI.Native.DeviceContext.Structures;
namespace WindowsDisplayAPI
{
///
/// Represents a Windows Attached Display Device
///
public class Display : DisplayDevice
{
///
/// Creates a new Display
///
/// The DisplayDevice instance to copy information from
protected Display(DisplayDevice device)
: base(
device.DevicePath,
device.DeviceName,
device.DeviceKey,
device.Adapter,
device.IsAvailable,
false
)
{
}
///
/// Gets the display capabilities.
///
public MonitorCapabilities Capabilities
{
get
{
var handle = DCHandle.CreateFromDevice(ScreenName, DevicePath);
if (!IsValid || handle?.IsInvalid != false)
{
throw new InvalidDisplayException(DevicePath);
}
return new MonitorCapabilities(handle);
}
}
///
public override string DisplayName
{
get
{
if (IsValid)
{
return DisplayAdapter.GetDisplayAdapters()
.SelectMany(adapter => adapter.GetDisplayDevices(base.IsAvailable))
.FirstOrDefault(
device => device.DevicePath.Equals(DevicePath) && device.DeviceKey.Equals(DeviceKey)
)?.DisplayName;
}
return ToUnAttachedDisplay()?.DisplayName;
}
}
///
/// Gets or sets the display gamma ramp look up table.
///
public DisplayGammaRamp GammaRamp
{
get
{
var handle = DCHandle.CreateFromDevice(ScreenName, DevicePath);
if (!IsValid || handle?.IsInvalid != false)
{
throw new InvalidDisplayException(DevicePath);
}
var gammaRamp = new GammaRamp();
return DeviceContextApi.GetDeviceGammaRamp(handle, ref gammaRamp)
? new DisplayGammaRamp(gammaRamp)
: null;
}
set
{
var handle = DCHandle.CreateFromDevice(ScreenName, DevicePath);
if (!IsValid || handle?.IsInvalid != false)
{
throw new InvalidDisplayException(DevicePath);
}
var gammaRamp = value.AsRamp();
if (!DeviceContextApi.SetDeviceGammaRamp(handle, ref gammaRamp))
{
//throw new ArgumentException("Invalid argument or value passed.", nameof(value));
}
}
}
///
public override bool IsAvailable
{
get => base.IsAvailable && IsValid;
}
///
public override bool IsValid
{
get
{
return DisplayAdapter.GetDisplayAdapters()
.SelectMany(adapter => adapter.GetDisplayDevices(base.IsAvailable))
.Any(
device => device.DevicePath.Equals(DevicePath) && device.DeviceKey.Equals(DeviceKey)
);
}
}
///
public override string ScreenName
{
get
{
if (IsValid)
{
return
DisplayAdapter.GetDisplayAdapters()
.SelectMany(adapter => adapter.GetDisplayDevices(base.IsAvailable))
.FirstOrDefault(
device => device.DevicePath.Equals(DevicePath) && device.DeviceKey.Equals(DeviceKey)
)?.ScreenName;
}
return ToUnAttachedDisplay()?.ScreenName;
}
}
///
/// Returns a list of all attached displays on this machine
///
/// An enumerable list of Displays
public static IEnumerable GetDisplays()
{
return DisplayAdapter.GetDisplayAdapters()
.SelectMany(adapter => adapter.GetDisplayDevices(true))
.Select(device => new Display(device));
}
///
public override string ToString()
{
return IsValid ? $"{GetType().Name}: {DisplayName} ({DeviceName})" : $"{GetType().Name}: Invalid";
}
///
/// Returns the corresponding UnAttachedDisplay device for this display. Only valid when this instance is invalidated
/// due to display detachment.
///
///
public UnAttachedDisplay ToUnAttachedDisplay()
{
if (IsValid)
{
return null;
}
return UnAttachedDisplay.GetUnAttachedDisplays()
.FirstOrDefault(
display => display.DevicePath.Equals(DevicePath) && display.DeviceKey.Equals(DeviceKey)
);
}
}
}