using System;
using System.Linq;
using NvAPIWrapper.Native;
using NvAPIWrapper.Native.Display;
using NvAPIWrapper.Native.Display.Structures;
using NvAPIWrapper.Native.Exceptions;
using NvAPIWrapper.Native.General;
using NvAPIWrapper.Native.Helpers;
using NvAPIWrapper.Native.Interfaces.Display;
namespace NvAPIWrapper.Display
{
///
/// Represents a configuration path
///
public class PathInfo : IEquatable
{
///
/// Creates a new PathInfo
///
/// Display resolution
/// Display color format
/// Target configuration informations
public PathInfo(Resolution resolution, ColorFormat colorFormat, PathTargetInfo[] targetInfos)
{
Resolution = resolution;
ColorFormat = colorFormat;
TargetsInfo = targetInfos;
}
///
/// Creates a new PathInfo
///
/// IPathInfo implamented object
public PathInfo(IPathInfo info)
{
SourceId = info.SourceId;
Resolution = info.SourceModeInfo.Resolution;
ColorFormat = info.SourceModeInfo.ColorFormat;
Position = info.SourceModeInfo.Position;
SpanningOrientation = info.SourceModeInfo.SpanningOrientation;
IsGDIPrimary = info.SourceModeInfo.IsGDIPrimary;
IsSLIFocus = info.SourceModeInfo.IsSLIFocus;
TargetsInfo =
info.TargetsInfo.Select(targetInfo => new PathTargetInfo(targetInfo)).ToArray();
if (info is PathInfoV2)
{
OSAdapterLUID = ((PathInfoV2) info).OSAdapterLUID;
}
}
///
/// Gets or sets the display color format
///
public ColorFormat ColorFormat { get; set; }
///
/// Gets or sets a boolean value indicating if the this is the primary GDI display
///
public bool IsGDIPrimary { get; set; }
///
/// Gets or sets a boolean value indicating if the this is the SLI focus display
///
public bool IsSLIFocus { get; set; }
///
/// Gets OS Adapter of LUID for Non-NVIDIA adapters
///
public LUID? OSAdapterLUID { get; }
///
/// Gets or sets the display position
///
public Position Position { get; set; }
///
/// Gets or sets the display resolution
///
public Resolution Resolution { get; set; }
///
/// Gets or sets the Windows CCD display source identification. This can be optionally set.
///
public uint SourceId { get; set; }
///
/// Gets or sets the display spanning orientation, valid for XP only
///
public SpanningOrientation SpanningOrientation { get; set; }
///
/// Gets information about path targets
///
public PathTargetInfo[] TargetsInfo { get; }
///
/// Checks for equality with a PathInfo instance
///
/// The PathInfo object to check with
/// true if both objects are equal, otherwise false
public bool Equals(PathInfo other)
{
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return Resolution.Equals(other.Resolution) &&
ColorFormat == other.ColorFormat &&
Position.Equals(other.Position) &&
SpanningOrientation == other.SpanningOrientation &&
IsGDIPrimary == other.IsGDIPrimary &&
IsSLIFocus == other.IsSLIFocus &&
TargetsInfo.SequenceEqual(other.TargetsInfo);
}
///
/// Creates and fills a PathInfo object
///
/// The newly created PathInfo object
public static PathInfo[] GetDisplaysConfig()
{
var configs = DisplayApi.GetDisplayConfig();
var logicalDisplays = configs.Select(info => new PathInfo(info)).ToArray();
configs.DisposeAll();
return logicalDisplays;
}
///
/// Checks for equality between two objects of same type
///
/// The first object
/// The second object
/// true, if both objects are equal, otherwise false
public static bool operator ==(PathInfo left, PathInfo right)
{
return right?.Equals(left) ?? ReferenceEquals(left, null);
}
///
/// Checks for inequality between two objects of same type
///
/// The first object
/// The second object
/// true, if both objects are not equal, otherwise false
public static bool operator !=(PathInfo left, PathInfo right)
{
return !(left == right);
}
///
/// Applies one or more path information configurations
///
/// An array of path information configuration
/// DisplayConfigFlags flags
public static void SetDisplaysConfig(PathInfo[] pathInfos, DisplayConfigFlags flags)
{
try
{
var configsV2 = pathInfos.Select(config => config.GetPathInfoV2()).Cast().ToArray();
DisplayApi.SetDisplayConfig(configsV2, flags);
configsV2.DisposeAll();
}
catch (NVIDIAApiException ex)
{
if (ex.Status != Status.IncompatibleStructureVersion)
{
throw;
}
}
catch (NVIDIANotSupportedException)
{
// ignore
}
var configsV1 = pathInfos.Select(config => config.GetPathInfoV1()).Cast().ToArray();
DisplayApi.SetDisplayConfig(configsV1, flags);
configsV1.DisposeAll();
}
///
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj.GetType() != GetType())
{
return false;
}
return Equals((PathInfo) obj);
}
///
public override int GetHashCode()
{
unchecked
{
var hashCode = Resolution.GetHashCode();
hashCode = (hashCode * 397) ^ (int) ColorFormat;
hashCode = (hashCode * 397) ^ Position.GetHashCode();
hashCode = (hashCode * 397) ^ (int) SpanningOrientation;
hashCode = (hashCode * 397) ^ IsGDIPrimary.GetHashCode();
hashCode = (hashCode * 397) ^ IsSLIFocus.GetHashCode();
hashCode = (hashCode * 397) ^ (TargetsInfo?.GetHashCode() ?? 0);
return hashCode;
}
}
///
public override string ToString()
{
return $"{Resolution} @ {Position} [{TargetsInfo.Length}]";
}
///
/// Creates and fills a GetPathInfoV1 object
///
/// The newly created GetPathInfoV1 object
public PathInfoV1 GetPathInfoV1()
{
var sourceModeInfo = GetSourceModeInfo();
var pathTargetInfoV1 = GetPathTargetInfoV1Array();
return new PathInfoV1(pathTargetInfoV1, sourceModeInfo, SourceId);
}
///
/// Creates and fills a GetPathInfoV2 object
///
/// The newly created GetPathInfoV2 object
public PathInfoV2 GetPathInfoV2()
{
var sourceModeInfo = GetSourceModeInfo();
var pathTargetInfoV2 = GetPathTargetInfoV2Array();
return new PathInfoV2(pathTargetInfoV2, sourceModeInfo, SourceId);
}
///
/// Creates and fills an array of GetPathTargetInfoV1 object
///
/// The newly created array of GetPathTargetInfoV1 objects
public PathTargetInfoV1[] GetPathTargetInfoV1Array()
{
return TargetsInfo.Select(config => config.GetPathTargetInfoV1()).ToArray();
}
///
/// Creates and fills an array of GetPathTargetInfoV2 object
///
/// The newly created array of GetPathTargetInfoV2 objects
public PathTargetInfoV2[] GetPathTargetInfoV2Array()
{
return TargetsInfo.Select(config => config.GetPathTargetInfoV2()).ToArray();
}
///
/// Creates and fills a SourceModeInfo object
///
/// The newly created SourceModeInfo object
public SourceModeInfo GetSourceModeInfo()
{
return new SourceModeInfo(Resolution, ColorFormat, Position, SpanningOrientation, IsGDIPrimary, IsSLIFocus);
}
}
}