Skip to content

Commit

Permalink
Fixed error with cloned equality testing
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymacdonald committed Jun 3, 2022
1 parent 21a5d10 commit 3752d43
Showing 1 changed file with 57 additions and 5 deletions.
62 changes: 57 additions & 5 deletions CCDInfo/WinLibrary.cs
Expand Up @@ -52,15 +52,15 @@ public struct DISPLAY_SOURCE : IEquatable<DISPLAY_SOURCE>

public override bool Equals(object obj) => obj is DISPLAY_SOURCE other && this.Equals(other);
public bool Equals(DISPLAY_SOURCE other)
=> SourceId.Equals(other.SourceId) &&
TargetId.Equals(other.TargetId) &&
=> //SourceId.Equals(other.SourceId) && // Source ID needs to be ignored in this case, as windows moves the source ids around :(
TargetId.Equals(other.TargetId) &&
DevicePath.Equals(other.DevicePath) &&
SourceDpiScalingRel.Equals(other.SourceDpiScalingRel);
//=> true;
public override int GetHashCode()
{
//return 300;
return (SourceId, TargetId, DevicePath, SourceDpiScalingRel).GetHashCode();
//return (SourceId, TargetId, DevicePath, SourceDpiScalingRel).GetHashCode(); // Source ID needs to be ignored in this case, as windows moves the source ids around :(
return (TargetId, DevicePath, SourceDpiScalingRel).GetHashCode();
}

public static bool operator ==(DISPLAY_SOURCE lhs, DISPLAY_SOURCE rhs) => lhs.Equals(rhs);
Expand Down Expand Up @@ -91,7 +91,6 @@ public bool Equals(WINDOWS_DISPLAY_CONFIG other)
if (!(IsCloned == other.IsCloned &&
DisplayConfigPaths.SequenceEqual(other.DisplayConfigPaths) &&
DisplayConfigModes.SequenceEqual(other.DisplayConfigModes) &&
DisplayHDRStates.SequenceEqual(other.DisplayHDRStates) &&
// The dictionary keys sometimes change after returning from NVIDIA Surround, so we need to only focus on comparing the values of the GDISettings.
// Additionally, we had to disable the DEviceKey from the equality testing within the GDI library itself as that waould also change after changing back from NVIDIA surround
// This still allows us to detect when refresh rates change, which will allow DisplayMagician to detect profile differences.
Expand All @@ -101,6 +100,12 @@ public bool Equals(WINDOWS_DISPLAY_CONFIG other)
return false;
}

// Now we need to go through the HDR states comparing vaues, as the order changes if there is a cloned display
if (!WinLibrary.EqualButDifferentOrder<ADVANCED_HDR_INFO_PER_PATH>(DisplayHDRStates, other.DisplayHDRStates))
{
return false;
}

// Now we need to go through the values to make sure they are the same, but ignore the keys (as they change after each reboot!)
for (int i = 0; i < DisplaySources.Count; i++)
{
Expand Down Expand Up @@ -2361,6 +2366,53 @@ private static void RefreshTrayArea(IntPtr windowHandle)
Utils.SendMessage(windowHandle, Utils.WM_MOUSEMOVE, 0, (y << 16) + x);
}

public static bool EqualButDifferentOrder<T>(IList<T> list1, IList<T> list2)
{

if (list1.Count != list2.Count)
{
return false;
}

// Now we need to go through the list1, checking that all it's items are in list2
foreach (T item1 in list1)
{
bool foundIt = false;
foreach (T item2 in list2)
{
if (item1.Equals(item2))
{
foundIt = true;
break;
}
}
if (!foundIt)
{
return false;
}
}

// Now we need to go through the list2, checking that all it's items are in list1
foreach (T item2 in list2)
{
bool foundIt = false;
foreach (T item1 in list1)
{
if (item1.Equals(item2))
{
foundIt = true;
break;
}
}
if (!foundIt)
{
return false;
}
}

return true;
}

}

[global::System.Serializable]
Expand Down

0 comments on commit 3752d43

Please sign in to comment.