DisplayMagician/HeliosDisplayManagement.Shared/Topology/PathTarget.cs
temacdonald a9bb295d1f Renamed app to HeliosPlus namespace and more
Renamed app to HeliosPlus namespace so that the updated
changes don't interfere with HeliosDisplayManagement if
that is also installed. The fact that I've changed so much of
the app means that my changes would be unlikely to be
accepted by Soroush, so I'm best to release this work in a
similar way to other projects like Notepad++, by keeping
the same root name, and adding a plus.
    I've also changed the Shortcut form to put all the games
in a single list to reduce the number of clicks a user has to
do in order for them to create a shortcut. I have begun to
prepare the form so that it will support multiple game
libraries, but now I am at a point that I need to fix the Steam
and Uplay game detection mechanisms so that they report
the correct information for the lv_games list view.
2020-04-23 20:16:16 +12:00

154 lines
4.6 KiB
C#

using System;
using System.Linq;
using WindowsDisplayAPI.DisplayConfig;
using HeliosPlus.Shared.NVIDIA;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace HeliosPlus.Shared.Topology
{
public class PathTarget : IEquatable<PathTarget>
{
public PathTarget(PathTargetInfo targetInfo, SurroundTopology surround = null)
{
DevicePath = targetInfo.DisplayTarget.DevicePath;
var index = DevicePath.IndexOf("{", StringComparison.InvariantCultureIgnoreCase);
if (index > 0)
{
DevicePath = DevicePath.Substring(0, index).TrimEnd('#');
}
FrequencyInMillihertz = targetInfo.FrequencyInMillihertz;
Rotation = targetInfo.Rotation.ToRotation();
Scaling = targetInfo.Scaling.ToScaling();
ScanLineOrdering = targetInfo.ScanLineOrdering.ToScanLineOrdering();
try
{
DisplayName = targetInfo.DisplayTarget.FriendlyName;
}
catch
{
DisplayName = null;
}
SurroundTopology = surround ?? SurroundTopology.FromPathTargetInfo(targetInfo);
}
public PathTarget()
{
}
public string DevicePath { get; set; }
public string DisplayName { get; set; }
public ulong FrequencyInMillihertz { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public Rotation Rotation { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public Scaling Scaling { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public ScanLineOrdering ScanLineOrdering { get; set; }
public SurroundTopology SurroundTopology { get; set; }
/// <inheritdoc />
public bool Equals(PathTarget other)
{
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return FrequencyInMillihertz == other.FrequencyInMillihertz &&
Rotation == other.Rotation &&
Scaling == other.Scaling &&
ScanLineOrdering == other.ScanLineOrdering &&
DevicePath == other.DevicePath &&
SurroundTopology == other.SurroundTopology;
}
public static bool operator ==(PathTarget left, PathTarget right)
{
return Equals(left, right) || left?.Equals(right) == true;
}
public static bool operator !=(PathTarget left, PathTarget right)
{
return !(left == right);
}
/// <inheritdoc />
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((PathTarget) obj);
}
/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
var hashCode = FrequencyInMillihertz.GetHashCode();
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;
return hashCode;
}
}
/// <inheritdoc />
public override string ToString()
{
return DisplayName ?? $"PathTarget {DevicePath}";
}
public PathTargetInfo ToPathTargetInfo()
{
var targetDevice =
PathDisplayTarget.GetDisplayTargets()
.FirstOrDefault(
target => target.DevicePath.StartsWith(DevicePath,
StringComparison.InvariantCultureIgnoreCase));
if (targetDevice == null)
{
return null;
}
return new PathTargetInfo(new PathDisplayTarget(targetDevice.Adapter, targetDevice.TargetId),
FrequencyInMillihertz, ScanLineOrdering.ToDisplayConfigScanLineOrdering(),
Rotation.ToDisplayConfigRotation(), Scaling.ToDisplayConfigScaling());
}
}
}