DisplayMagician/HeliosDisplayManagement.Shared/Topology/ProfileViewportTargetDisplay.cs

179 lines
5.4 KiB
C#
Raw Normal View History

2017-02-26 19:23:31 +00:00
using System;
using System.Linq;
using WindowsDisplayAPI.DisplayConfig;
using HeliosPlus.Shared.NVIDIA;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
2017-02-26 19:23:31 +00:00
namespace HeliosPlus.Shared.Topology
2017-02-26 19:23:31 +00:00
{
2020-05-12 10:46:23 +00:00
public class ProfileViewportTargetDisplay : IEquatable<ProfileViewportTargetDisplay>
2017-02-26 19:23:31 +00:00
{
2020-05-12 10:46:23 +00:00
public ProfileViewportTargetDisplay(PathTargetInfo targetInfo, SurroundTopology surround = null)
2017-02-26 19:23:31 +00:00
{
DevicePath = targetInfo.DisplayTarget.DevicePath;
var index = DevicePath.IndexOf("{", StringComparison.InvariantCultureIgnoreCase);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (index > 0)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
DevicePath = DevicePath.Substring(0, index).TrimEnd('#');
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
FrequencyInMillihertz = targetInfo.FrequencyInMillihertz;
Rotation = targetInfo.Rotation.ToRotation();
Scaling = targetInfo.Scaling.ToScaling();
ScanLineOrdering = targetInfo.ScanLineOrdering.ToScanLineOrdering();
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
try
{
DisplayName = targetInfo.DisplayTarget.FriendlyName;
}
catch
{
DisplayName = null;
}
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
SurroundTopology = surround ?? SurroundTopology.FromPathTargetInfo(targetInfo);
}
2017-08-10 14:21:45 +00:00
2017-02-26 19:23:31 +00:00
2020-05-12 10:46:23 +00:00
public ProfileViewportTargetDisplay()
2017-02-26 19:23:31 +00:00
{
}
2017-08-10 14:21:45 +00:00
public string DevicePath { get; set; }
2017-02-26 19:23:31 +00:00
public string DisplayName { get; set; }
2017-02-26 19:23:31 +00:00
public ulong FrequencyInMillihertz { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
2017-02-26 19:23:31 +00:00
public Rotation Rotation { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public Scaling Scaling { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public ScanLineOrdering ScanLineOrdering { get; set; }
2017-08-10 14:21:45 +00:00
public SurroundTopology SurroundTopology { get; set; }
2017-02-26 19:23:31 +00:00
/// <inheritdoc />
2020-05-12 10:46:23 +00:00
public bool Equals(ProfileViewportTargetDisplay other)
2017-02-26 19:23:31 +00:00
{
2018-10-20 00:27:25 +00:00
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
2020-05-12 10:46:23 +00:00
if (FrequencyInMillihertz.Equals(other.FrequencyInMillihertz) &&
Rotation.Equals(other.Rotation) &&
Scaling.Equals(other.Scaling) &&
ScanLineOrdering.Equals(other.ScanLineOrdering) &&
DevicePath.Equals(other.DevicePath))
{
if (SurroundTopology == null)
{
if (other.SurroundTopology == null)
{
return true;
}
else
{
return false;
}
}
else
{
if (other.SurroundTopology == null)
{
return false;
}
else
{
return SurroundTopology.Equals(other.SurroundTopology);
}
}
}
return false;
2017-02-26 19:23:31 +00:00
}
2020-05-12 10:46:23 +00:00
public static bool operator ==(ProfileViewportTargetDisplay left, ProfileViewportTargetDisplay right)
2017-02-26 19:23:31 +00:00
{
return Equals(left, right) || left?.Equals(right) == true;
2017-02-26 19:23:31 +00:00
}
2020-05-12 10:46:23 +00:00
public static bool operator !=(ProfileViewportTargetDisplay left, ProfileViewportTargetDisplay right)
2017-02-26 19:23:31 +00:00
{
return !(left == right);
2017-02-26 19:23:31 +00:00
}
/// <inheritdoc />
public override bool Equals(object obj)
{
2018-10-20 00:27:25 +00:00
if (ReferenceEquals(null, obj))
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj.GetType() != GetType())
{
return false;
}
2020-05-12 10:46:23 +00:00
return Equals((ProfileViewportTargetDisplay) obj);
2017-02-26 19:23:31 +00:00
}
/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
var hashCode = FrequencyInMillihertz.GetHashCode();
2018-10-20 00:27:25 +00:00
hashCode = (hashCode * 397) ^ (int) Rotation;
hashCode = (hashCode * 397) ^ (int) Scaling;
hashCode = (hashCode * 397) ^ (int) ScanLineOrdering;
hashCode = (hashCode * 397) ^ DevicePath.GetHashCode();
hashCode = (hashCode * 397) ^ SurroundTopology?.GetHashCode() ?? 0;
2017-02-26 19:23:31 +00:00
return hashCode;
}
}
/// <inheritdoc />
public override string ToString()
{
return DisplayName ?? $"PathTarget {DevicePath}";
}
public PathTargetInfo ToPathTargetInfo()
{
var targetDevice =
PathDisplayTarget.GetDisplayTargets()
2017-08-10 14:21:45 +00:00
.FirstOrDefault(
2018-10-20 00:27:25 +00:00
target => target.DevicePath.StartsWith(DevicePath,
StringComparison.InvariantCultureIgnoreCase));
2017-02-26 19:23:31 +00:00
if (targetDevice == null)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
return null;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
return new PathTargetInfo(new PathDisplayTarget(targetDevice.Adapter, targetDevice.TargetId),
2018-10-20 00:27:25 +00:00
FrequencyInMillihertz, ScanLineOrdering.ToDisplayConfigScanLineOrdering(),
Rotation.ToDisplayConfigRotation(), Scaling.ToDisplayConfigScaling());
2017-02-26 19:23:31 +00:00
}
}
}