From f8e9eecf810d930bc14ae55ead0e8b6465c47399 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Tue, 7 Sep 2021 21:26:42 +1200 Subject: [PATCH] Removed derived ProfileItem classes The derived ProfilteItem classes were an unnecessary distraction and wasted time I could be spending on other things. This set of changes are the first part of removing those derived classes and moving to a single ProfilItem class again, but with different configuration slots within it. Makes equality comparison SO MUCH EASIER! --- DisplayMagicianShared/AMD/AMDProfileItem.cs | 409 ------- .../DisplayMagicianShared.csproj | 3 - .../NVIDIA/NVIDIAProfileItem.cs | 598 ----------- DisplayMagicianShared/ProfileItem.cs | 996 +++++++++++++++++- DisplayMagicianShared/ProfileRepository.cs | 206 +--- .../Windows/WinProfileItem.cs | 366 ------- displaymagician/uiforms/displayprofileform.cs | 24 +- 7 files changed, 991 insertions(+), 1611 deletions(-) delete mode 100644 DisplayMagicianShared/AMD/AMDProfileItem.cs delete mode 100644 DisplayMagicianShared/NVIDIA/NVIDIAProfileItem.cs delete mode 100644 DisplayMagicianShared/Windows/WinProfileItem.cs diff --git a/DisplayMagicianShared/AMD/AMDProfileItem.cs b/DisplayMagicianShared/AMD/AMDProfileItem.cs deleted file mode 100644 index 0fdc2ce..0000000 --- a/DisplayMagicianShared/AMD/AMDProfileItem.cs +++ /dev/null @@ -1,409 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Windows.Forms; -using DisplayMagicianShared.Resources; -using Newtonsoft.Json; -using System.Drawing; -using DisplayMagicianShared.Windows; -using System.Drawing.Imaging; - -namespace DisplayMagicianShared.AMD -{ - - public class AMDProfileItem : ProfileItem, IEquatable - { - private static List _allSavedProfiles = new List(); - private ProfileIcon _profileIcon; - private Bitmap _profileBitmap, _profileShortcutBitmap; - private List _profileDisplayIdentifiers = new List(); - private List _screens; - private AMD_DISPLAY_CONFIG _amdDisplayConfig = new AMD_DISPLAY_CONFIG(); - private WINDOWS_DISPLAY_CONFIG _windowsDisplayConfig = new WINDOWS_DISPLAY_CONFIG(); - private static readonly string uuidV4Regex = @"(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$"; - - private string _uuid = ""; - private bool _isPossible = false; - private Keys _hotkey = Keys.None; - - public AMDProfileItem() - { - } - - public new static Version Version = new Version(2, 1); - - #region Instance Properties - - [JsonIgnore] - public override bool IsPossible - { - get - { - // Return the cached answer - return _isPossible; - } - set - { - _isPossible = value; - } - } - - [JsonIgnore] - public override bool IsActive - { - get - { - - if (this.Equals(ProfileRepository.CurrentProfile)) - return true; - else - return false; - - } - } - - public override VIDEO_MODE VideoMode { get; } = VIDEO_MODE.AMD; - - public override string Name { get; set; } - - [JsonRequired] - public AMD_DISPLAY_CONFIG AMDDisplayConfig - { - get - { - return _amdDisplayConfig; - } - set - { - _amdDisplayConfig = value; - } - } - - [JsonRequired] - public WINDOWS_DISPLAY_CONFIG WindowsDisplayConfig - { - get - { - return _windowsDisplayConfig; - } - set - { - _windowsDisplayConfig = value; - } - } - - - public override List ProfileDisplayIdentifiers - { - get - { - if (_profileDisplayIdentifiers.Count == 0) - { - _profileDisplayIdentifiers = AMDLibrary.GetLibrary().GetCurrentDisplayIdentifiers(); - } - return _profileDisplayIdentifiers; - } - set - { - if (value is List) - _profileDisplayIdentifiers = value; - } - } - - #endregion - - public override bool IsValid() - { - - if (AMDLibrary.GetLibrary().IsValidConfig(_amdDisplayConfig) && - ProfileIcon is ProfileIcon && - System.IO.File.Exists(SavedProfileIconCacheFilename) && - ProfileBitmap is Bitmap && - ProfileTightestBitmap is Bitmap && - ProfileDisplayIdentifiers.Count > 0) - { - if (AMDDisplayConfig.AdapterConfigs.Count > 0) - return true; - else - return false; - } - else - return false; - } - - - - public bool CopyTo(AMDProfileItem profile, bool overwriteId = true) - { - if (!(profile is AMDProfileItem)) - return false; - - if (overwriteId == true) - profile.UUID = UUID; - - // Copy all our profile data over to the other profile - profile.Name = Name; - profile.AMDDisplayConfig = AMDDisplayConfig; - profile.WindowsDisplayConfig = WindowsDisplayConfig; - profile.ProfileIcon = ProfileIcon; - profile.SavedProfileIconCacheFilename = SavedProfileIconCacheFilename; - profile.ProfileBitmap = ProfileBitmap; - profile.ProfileTightestBitmap = ProfileTightestBitmap; - profile.ProfileDisplayIdentifiers = ProfileDisplayIdentifiers; - //profile.Screens = Screens; - return true; - } - - public override bool PreSave() - { - // Prepare our profile data for saving - if (_profileDisplayIdentifiers.Count == 0) - { - _profileDisplayIdentifiers = AMDLibrary.GetLibrary().GetCurrentDisplayIdentifiers(); - } - - // Return if it is valid and we should continue - return IsValid(); - } - - public override void RefreshPossbility() - { - // Check whether this profile is possible - if (AMDLibrary.GetLibrary().IsPossibleConfig(_amdDisplayConfig)) - { - - SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The AMD profile {Name} is possible!"); - _isPossible = true; - - } - else - { - SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The AMD profile {Name} is NOT possible!"); - _isPossible = false; - } - - } - - // Actually set this profile active - public override bool SetActive() - { - AMDLibrary amdLibrary = AMDLibrary.GetLibrary(); - WinLibrary winLibrary = WinLibrary.GetLibrary(); - if (amdLibrary.IsInstalled) - { - if (!amdLibrary.IsActiveConfig(_amdDisplayConfig) && !winLibrary.IsActiveConfig(_windowsDisplayConfig)) - { - if (amdLibrary.IsPossibleConfig(_amdDisplayConfig)) - { - SharedLogger.logger.Trace($"ProfileRepository/SetActive: The AMD display settings within profile {Name} are possible to use right now, so we'll use attempt to use them."); - bool itWorkedforAMD = amdLibrary.SetActiveConfig(_amdDisplayConfig); - - if (itWorkedforAMD) - { - SharedLogger.logger.Trace($"ProfileRepository/SetActive: The AMD display settings within profile {Name} were successfully applied."); - // Then let's try to also apply the windows changes - // Note: we are unable to check if the Windows CCD display config is possible, as it won't match if either the current display config is a ADL config, - // or if the display config we want to change to is a ADL config. So we just have to assume that it will work! - bool itWorkedforWindows = winLibrary.SetActiveConfig(_windowsDisplayConfig); - if (itWorkedforWindows) - { - SharedLogger.logger.Trace($"ProfileRepository/SetActive: The Windows CCD display settings within profile {Name} were successfully applied."); - return true; - } - else - { - SharedLogger.logger.Trace($"ProfileRepository/SetActive: The Windows CCD display settings within profile {Name} were NOT applied correctly."); - } - - } - else - { - SharedLogger.logger.Trace($"ProfileRepository/SetActive: The AMD display settings within profile {Name} were NOT applied correctly."); - } - - } - else - { - SharedLogger.logger.Error($"ProfileRepository/SetActive: ERROR - Cannot apply the AMD display config in profile {Name} as it is not currently possible to use it."); - } - } - else - { - SharedLogger.logger.Info($"ProfileRepository/SetActive: The display settings in profile {Name} are already installed. No need to install them again. Exiting."); - } - } - return false; - } - - public override bool CreateProfileFromCurrentDisplaySettings() - { - - AMDLibrary amdLibrary = AMDLibrary.GetLibrary(); - if (amdLibrary.IsInstalled) - { - // Create the profile data from the current config - _amdDisplayConfig = amdLibrary.GetActiveConfig(); - _windowsDisplayConfig = WinLibrary.GetLibrary().GetActiveConfig(); - _profileDisplayIdentifiers = amdLibrary.GetCurrentDisplayIdentifiers(); - - // Now, since the ActiveProfile has changed, we need to regenerate screen positions - _screens = GetScreenPositions(); - - return true; - } - else - { - return false; - } - } - - public override bool PerformPostLoadingTasks() - { - // First thing we do is to set up the Screens - _screens = GetScreenPositions(); - - return true; - } - - public override List GetScreenPositions() - { - - // Now we create the screens structure from the AMD profile information - _screens = new List(); - - int pathCount = _windowsDisplayConfig.DisplayConfigPaths.Length; - // First of all we need to figure out how many display paths we have. - if (pathCount < 1) - { - // Return an empty screen if we have no Display Config Paths to use! - return _screens; - } - - foreach (var path in _windowsDisplayConfig.DisplayConfigPaths) - { - // For each path we go through and get the relevant info we need. - if (_windowsDisplayConfig.DisplayConfigPaths.Length > 0) - { - // Set some basics about the screen - ScreenPosition screen = new ScreenPosition(); - screen.Library = "NVIDIA"; - - UInt32 targetId = path.TargetInfo.Id; - - foreach (DISPLAYCONFIG_MODE_INFO displayMode in _windowsDisplayConfig.DisplayConfigModes) - { - // Find the matching Display Config Source Mode - if (displayMode.InfoType != DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE && displayMode.Id == targetId) - { - screen.Name = targetId.ToString(); - //screen.DisplayConnector = displayMode.DisplayConnector; - screen.ScreenX = displayMode.SourceMode.Position.X; - screen.ScreenY = displayMode.SourceMode.Position.Y; - screen.ScreenWidth = (int)displayMode.SourceMode.Width; - screen.ScreenHeight = (int)displayMode.SourceMode.Height; - - // If we're at the 0,0 coordinate then we're the primary monitor - if (screen.ScreenX == 0 && screen.ScreenY == 0) - { - screen.IsPrimary = true; - } - } - } - - foreach (ADVANCED_HDR_INFO_PER_PATH hdrInfo in _windowsDisplayConfig.DisplayHDRStates) - { - // Find the matching HDR information - if (hdrInfo.Id == targetId) - { - // HDR information - if (hdrInfo.AdvancedColorInfo.AdvancedColorSupported) - { - screen.HDRSupported = true; - if (hdrInfo.AdvancedColorInfo.AdvancedColorEnabled) - { - screen.HDREnabled = true; - } - else - { - screen.HDREnabled = false; - } - - } - else - { - screen.HDRSupported = false; - screen.HDREnabled = false; - } - - } - } - - - // Now we need to check for Spanned screens - /*if (_amdDisplayConfig) - { - screen.IsSpanned = true; - screen.Colour = Color.FromArgb(221,0,49); // represents AMD Red - screen.SpannedName = "AMD Eyefinity"; - } - else - { - screen.IsSpanned = false; - screen.Colour = Color.FromArgb(195, 195, 195); // represents normal screen colour - }*/ - - screen.IsSpanned = false; - screen.Colour = Color.FromArgb(195, 195, 195); // represents normal screen colour - - _screens.Add(screen); - } - } - - - return _screens; - } - - // The object specific Equals - public bool Equals(AMDProfileItem other) - { - // Check references - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - - // Check the object fields - return base.Equals(other) && - AMDDisplayConfig == other.AMDDisplayConfig && - WindowsDisplayConfig == other.WindowsDisplayConfig; - } - - // The public override for the Object.Equals - public bool Equals(Object obj) - { - // Check references - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - // If different types then can't be true - if (obj.GetType() == this.GetType()) return false; - // Check the object fields as this must the same object as obj, and we need to test in more detail - return Equals((AMDProfileItem)obj); - } - - public override int GetHashCode() - { - // Calculate the hash code for the product. - return (base.GetHashCode(), AMDDisplayConfig, WindowsDisplayConfig).GetHashCode(); - } - - public static bool operator ==(AMDProfileItem lhs, AMDProfileItem rhs) - { - return Equals(lhs, rhs); - } - - public static bool operator !=(AMDProfileItem lhs, AMDProfileItem rhs) - { - return !Equals(lhs, rhs); - } - } - -} \ No newline at end of file diff --git a/DisplayMagicianShared/DisplayMagicianShared.csproj b/DisplayMagicianShared/DisplayMagicianShared.csproj index a187b2d..5aa5fdc 100644 --- a/DisplayMagicianShared/DisplayMagicianShared.csproj +++ b/DisplayMagicianShared/DisplayMagicianShared.csproj @@ -53,10 +53,8 @@ - - @@ -86,7 +84,6 @@ DisplayView.cs - diff --git a/DisplayMagicianShared/NVIDIA/NVIDIAProfileItem.cs b/DisplayMagicianShared/NVIDIA/NVIDIAProfileItem.cs deleted file mode 100644 index 51b1a1d..0000000 --- a/DisplayMagicianShared/NVIDIA/NVIDIAProfileItem.cs +++ /dev/null @@ -1,598 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Windows.Forms; -using DisplayMagicianShared.Resources; -using Newtonsoft.Json; -using System.Drawing; -using System.Drawing.Imaging; -using System.Text.RegularExpressions; -using IWshRuntimeLibrary; -using DisplayMagicianShared.Windows; - -namespace DisplayMagicianShared.NVIDIA -{ - - public class NVIDIAProfileItem : ProfileItem, IEquatable - { - private static List _allSavedProfiles = new List(); - private ProfileIcon _profileIcon; - private Bitmap _profileBitmap, _profileShortcutBitmap; - private List _profileDisplayIdentifiers = new List(); - private List _screens; - private NVIDIA_DISPLAY_CONFIG _nvidiaDisplayConfig = new NVIDIA_DISPLAY_CONFIG(); - private WINDOWS_DISPLAY_CONFIG _windowsDisplayConfig = new WINDOWS_DISPLAY_CONFIG(); - private static readonly string uuidV4Regex = @"(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$"; - - private string _uuid = ""; - private bool _isPossible = false; - private Keys _hotkey = Keys.None; - - public NVIDIAProfileItem() - { - } - - public new static Version Version = new Version(2, 1); - - #region Instance Properties - - [JsonIgnore] - public override bool IsPossible - { - get - { - // Return the cached answer - return _isPossible; - } - set - { - _isPossible = value; - } - } - - [JsonIgnore] - public override bool IsActive - { - get - { - - if (this.Equals(ProfileRepository.CurrentProfile)) - return true; - else - return false; - - } - } - - public override VIDEO_MODE VideoMode { get; } = VIDEO_MODE.NVIDIA; - - public override string Name { get; set; } - - [JsonRequired] - public NVIDIA_DISPLAY_CONFIG NVIDIADisplayConfig - { - get - { - return _nvidiaDisplayConfig; - } - set - { - _nvidiaDisplayConfig = value; - } - } - - [JsonRequired] - public WINDOWS_DISPLAY_CONFIG WindowsDisplayConfig - { - get - { - return _windowsDisplayConfig; - } - set - { - _windowsDisplayConfig = value; - } - } - - public override List ProfileDisplayIdentifiers - { - get - { - if (_profileDisplayIdentifiers.Count == 0) - { - _profileDisplayIdentifiers = NVIDIALibrary.GetLibrary().GetCurrentDisplayIdentifiers(); - } - return _profileDisplayIdentifiers; - } - set - { - if (value is List) - _profileDisplayIdentifiers = value; - } - } - - #endregion - - public override bool IsValid() - { - - if (NVIDIALibrary.GetLibrary().IsValidConfig(_nvidiaDisplayConfig) && - ProfileIcon is ProfileIcon && - System.IO.File.Exists(SavedProfileIconCacheFilename) && - ProfileBitmap is Bitmap && - ProfileTightestBitmap is Bitmap && - ProfileDisplayIdentifiers.Count > 0) - return true; - else - return false; - } - - - - public bool CopyTo(NVIDIAProfileItem profile, bool overwriteId = true) - { - if (!(profile is NVIDIAProfileItem)) - return false; - - if (overwriteId == true) - profile.UUID = UUID; - - // Copy all our profile data over to the other profile - profile.Name = Name; - profile.WindowsDisplayConfig = WindowsDisplayConfig; - profile.NVIDIADisplayConfig = NVIDIADisplayConfig; - profile.ProfileIcon = ProfileIcon; - profile.SavedProfileIconCacheFilename = SavedProfileIconCacheFilename; - profile.ProfileBitmap = ProfileBitmap; - profile.ProfileTightestBitmap = ProfileTightestBitmap; - profile.ProfileDisplayIdentifiers = ProfileDisplayIdentifiers; - profile.WallpaperMode = WallpaperMode; - profile.WallpaperBitmapFilename = WallpaperBitmapFilename; - profile.WallpaperStyle = WallpaperStyle; - return true; - } - - public override bool PreSave() - { - // Prepare our profile data for saving - if (_profileDisplayIdentifiers.Count == 0) - { - _profileDisplayIdentifiers = NVIDIALibrary.GetLibrary().GetCurrentDisplayIdentifiers(); - } - - // Return if it is valid and we should continue - return IsValid(); - } - - public override void RefreshPossbility() - { - // Check whether this profile is possible - if (ProfileRepository.CurrentVideoMode == VIDEO_MODE.NVIDIA && NVIDIALibrary.GetLibrary().IsInstalled) - { - if (NVIDIALibrary.GetLibrary().IsPossibleConfig(_nvidiaDisplayConfig)) - { - - SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The NVIDIA profile {Name} is possible!"); - _isPossible = true; - - } - else - { - SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The NVIDIA profile {Name} is NOT possible!"); - _isPossible = false; - } - } - else - { - _isPossible = false; - } - } - - // Actually set this profile active - public override bool SetActive() - { - NVIDIALibrary nvidiaLibrary = NVIDIALibrary.GetLibrary(); - WinLibrary winLibrary = WinLibrary.GetLibrary(); - if (nvidiaLibrary.IsInstalled) - { - if (!nvidiaLibrary.IsActiveConfig(_nvidiaDisplayConfig) && !winLibrary.IsActiveConfig(_windowsDisplayConfig)) - { - if (nvidiaLibrary.IsPossibleConfig(_nvidiaDisplayConfig)) - { - SharedLogger.logger.Trace($"ProfileRepository/SetActive: The NVIDIA display settings within profile {Name} are possible to use right now, so we'll use attempt to use them."); - bool itWorkedforNVIDIA = nvidiaLibrary.SetActiveConfig(_nvidiaDisplayConfig); - - if (itWorkedforNVIDIA) - { - SharedLogger.logger.Trace($"ProfileRepository/SetActive: The NVIDIA display settings within profile {Name} were successfully applied."); - // Then let's try to also apply the windows changes - // Note: we are unable to check if the Windows CCD display config is possible, as it won't match if either the current display config is a Mosaic config, - // or if the display config we want to change to is a Mosaic config. So we just have to assume that it will work! - bool itWorkedforWindows = winLibrary.SetActiveConfig(_windowsDisplayConfig); - if (itWorkedforWindows) - { - SharedLogger.logger.Trace($"ProfileRepository/SetActive: The Windows CCD display settings within profile {Name} were successfully applied."); - return true; - } - else - { - SharedLogger.logger.Trace($"ProfileRepository/SetActive: The Windows CCD display settings within profile {Name} were NOT applied correctly."); - } - - } - else - { - SharedLogger.logger.Trace($"ProfileRepository/SetActive: The NVIDIA display settings within profile {Name} were NOT applied correctly."); - } - - } - else - { - SharedLogger.logger.Error($"ProfileRepository/SetActive: ERROR - Cannot apply the NVIDIA display config in profile {Name} as it is not currently possible to use it."); - } - } - else - { - SharedLogger.logger.Info($"ProfileRepository/SetActive: The display settings in profile {Name} are already installed. No need to install them again. Exiting."); - } - } - return false; - } - - public override bool CreateProfileFromCurrentDisplaySettings() - { - - NVIDIALibrary nvidiaLibrary = NVIDIALibrary.GetLibrary(); - if (nvidiaLibrary.IsInstalled) - { - // Create the profile data from the current config - _nvidiaDisplayConfig = nvidiaLibrary.GetActiveConfig(); - _windowsDisplayConfig= WinLibrary.GetLibrary().GetActiveConfig(); - _profileDisplayIdentifiers = nvidiaLibrary.GetCurrentDisplayIdentifiers(); - - // Now, since the ActiveProfile has changed, we need to regenerate screen positions - _screens = GetScreenPositions(); - - return true; - } - else - { - return false; - } - } - - public override bool PerformPostLoadingTasks() - { - // First thing we do is to set up the Screens - //_screens = GetScreenPositions(); - - return true; - } - - public override List GetScreenPositions() - { - // Set up some colours - Color primaryScreenColor = Color.FromArgb(0, 174, 241); // represents Primary screen blue - Color spannedScreenColor = Color.FromArgb(118, 185, 0); // represents NVIDIA Green - Color normalScreenColor = Color.FromArgb(155, 155, 155); // represents normal screen colour (gray) - - // Now we create the screens structure from the AMD profile information - _screens = new List(); - - int pathCount = _windowsDisplayConfig.DisplayConfigPaths.Length; - // First of all we need to figure out how many display paths we have. - if (pathCount < 1) - { - // Return an empty screen if we have no Display Config Paths to use! - return _screens; - } - // Now we need to check for Spanned screens - if (_nvidiaDisplayConfig.MosaicConfig.IsMosaicEnabled) - { - // TODO: Make the NVIDIA displays show the individual screens and overlap! - - - - // Create a dictionary of all the screen sizes we want - //Dictionary MosaicScreens = new Dictionary(); - for (int i = 0; i < _nvidiaDisplayConfig.MosaicConfig.MosaicGridCount; i++) - { - ScreenPosition screen = new ScreenPosition(); - screen.Library = "NVIDIA"; - screen.Colour = normalScreenColor; - if (_nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].DisplayCount > 1) - { - // Set some basics about the screen - screen.SpannedScreens = new List(); - screen.Name = "NVIDIA Surround/Mosaic"; - screen.IsSpanned = true; - screen.SpannedRows = (int)_nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].Rows; - screen.SpannedColumns = (int)_nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].Columns; - screen.Colour = spannedScreenColor; - - // This is a combined surround/mosaic screen - // We need to build the size of the screen to match it later so we check the MosaicViewports - uint minX = 0; - uint minY = 0; - uint maxX = 0; - uint maxY = 0; - uint overallX = 0; - uint overallY = 0; - int overallWidth = 0; - int overallHeight = 0; - for (int j = 0; j < _nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].DisplayCount; j++) - { - SpannedScreenPosition spannedScreen = new SpannedScreenPosition(); - spannedScreen.Name = _nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].Displays[j].DisplayId.ToString(); - spannedScreen.Colour = spannedScreenColor; - - // Calculate screen size - NV_RECT viewRect = _nvidiaDisplayConfig.MosaicConfig.MosaicViewports[i][j]; - if (viewRect.Left < minX) - { - minX = viewRect.Left; - } - if (viewRect.Top < minY) - { - minY = viewRect.Top; - } - if (viewRect.Right > maxX) - { - maxX = viewRect.Right; - } - if (viewRect.Bottom > maxY) - { - maxY = viewRect.Bottom; - } - uint width = viewRect.Right - viewRect.Left + 1; - uint height = viewRect.Bottom - viewRect.Top + 1; - spannedScreen.ScreenX = (int)viewRect.Left; - spannedScreen.ScreenY = (int)viewRect.Top; - spannedScreen.ScreenWidth = (int)width; - spannedScreen.ScreenHeight = (int)height; - - // Figure out the overall figures for the screen - if (viewRect.Left < overallX) - { - overallX = viewRect.Left; - } - if (viewRect.Top < overallY) - { - overallY = viewRect.Top; - } - - overallWidth = (int)maxX - (int)minX + 1; - overallHeight = (int)maxY - (int)minY + 1; - - spannedScreen.Row = i + 1; - spannedScreen.Column = j + 1; - - // Add the spanned screen to the screen - screen.SpannedScreens.Add(spannedScreen); - } - - //screen.Name = targetId.ToString(); - //screen.DisplayConnector = displayMode.DisplayConnector; - screen.ScreenX = (int)overallX; - screen.ScreenY = (int)overallY; - screen.ScreenWidth = (int)overallWidth; - screen.ScreenHeight = (int)overallHeight; - - // If we're at the 0,0 coordinate then we're the primary monitor - if (screen.ScreenX == 0 && screen.ScreenY == 0) - { - // Record we're primary screen - screen.IsPrimary = true; - // Change the colour to be the primary colour, but only if it isn't a surround screen - if (screen.Colour != spannedScreenColor) - { - screen.Colour = primaryScreenColor; - } - } - - } - else if (_nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].DisplayCount == 1) - { - // This is a single screen with an adjoining mosaic screen - // Set some basics about the screen - uint displayId = _nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].Displays[0].DisplayId; - string windowsDisplayName = _nvidiaDisplayConfig.DisplayNames[displayId]; - uint sourceIndex = _windowsDisplayConfig.DisplaySources[windowsDisplayName]; - for (int x = 0; x < _windowsDisplayConfig.DisplayConfigModes.Length; x++) - { - // Skip this if its not a source info config type - if (_windowsDisplayConfig.DisplayConfigModes[x].InfoType != DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE) - { - continue; - } - - // If the source index matches the index of the source info object we're looking at, then process it! - if (_windowsDisplayConfig.DisplayConfigModes[x].Id == sourceIndex) - { - screen.Name = displayId.ToString(); - - screen.ScreenX = (int)_windowsDisplayConfig.DisplayConfigModes[x].SourceMode.Position.X; - screen.ScreenY = (int)_windowsDisplayConfig.DisplayConfigModes[x].SourceMode.Position.Y; - screen.ScreenWidth = (int)_windowsDisplayConfig.DisplayConfigModes[x].SourceMode.Width; - screen.ScreenHeight = (int)_windowsDisplayConfig.DisplayConfigModes[x].SourceMode.Height; - break; - } - } - - - // If we're at the 0,0 coordinate then we're the primary monitor - if (screen.ScreenX == 0 && screen.ScreenY == 0) - { - screen.IsPrimary = true; - screen.Colour = primaryScreenColor; - } - - } - - _screens.Add(screen); - } - } - else - { - foreach (var path in _windowsDisplayConfig.DisplayConfigPaths) - { - // For each path we go through and get the relevant info we need. - if (_windowsDisplayConfig.DisplayConfigPaths.Length > 0) - { - // Set some basics about the screen - ScreenPosition screen = new ScreenPosition(); - screen.Library = "NVIDIA"; - screen.IsSpanned = false; - screen.Colour = normalScreenColor; // this is the default unless overridden by the primary screen - - UInt32 sourceId = path.SourceInfo.Id; - UInt32 targetId = path.TargetInfo.Id; - - - // Go through the screens as Windows knows them, and then enhance the info with Mosaic data if it applies - foreach (DISPLAYCONFIG_MODE_INFO displayMode in _windowsDisplayConfig.DisplayConfigModes) - { - // Find the matching Display Config Source Mode - if (displayMode.InfoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE && displayMode.Id == sourceId) - { - screen.Name = targetId.ToString(); - //screen.DisplayConnector = displayMode.DisplayConnector; - screen.ScreenX = displayMode.SourceMode.Position.X; - screen.ScreenY = displayMode.SourceMode.Position.Y; - screen.ScreenWidth = (int)displayMode.SourceMode.Width; - screen.ScreenHeight = (int)displayMode.SourceMode.Height; - - // If we're at the 0,0 coordinate then we're the primary monitor - if (screen.ScreenX == 0 && screen.ScreenY == 0) - { - screen.IsPrimary = true; - screen.Colour = primaryScreenColor; - } - break; - } - } - - foreach (ADVANCED_HDR_INFO_PER_PATH hdrInfo in _windowsDisplayConfig.DisplayHDRStates) - { - // Find the matching HDR information - if (hdrInfo.Id == targetId) - { - // HDR information - if (hdrInfo.AdvancedColorInfo.AdvancedColorSupported) - { - screen.HDRSupported = true; - if (hdrInfo.AdvancedColorInfo.AdvancedColorEnabled) - { - screen.HDREnabled = true; - } - else - { - screen.HDREnabled = false; - } - - } - else - { - screen.HDRSupported = false; - screen.HDREnabled = false; - } - break; - } - } - - _screens.Add(screen); - } - } - } - - - - - - /* - // Go through the screens, and update the Mosaic screens with their info (if there are any) - if (_nvidiaDisplayConfig.MosaicConfig.IsMosaicEnabled && _nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos.Length) - { - // *** Enum values for the mosaic topology type *** - // NV_MOSAIC_TOPO_1x2_BASIC = 1 - // NV_MOSAIC_TOPO_2x1_BASIC = 2, - // NV_MOSAIC_TOPO_1x3_BASIC = 3, - // NV_MOSAIC_TOPO_3x1_BASIC = 4, - // NV_MOSAIC_TOPO_1x4_BASIC = 5, - // NV_MOSAIC_TOPO_4x1_BASIC = 6, - // NV_MOSAIC_TOPO_2x2_BASIC = 7, - // NV_MOSAIC_TOPO_2x3_BASIC = 8, - // NV_MOSAIC_TOPO_2x4_BASIC = 9, - // NV_MOSAIC_TOPO_3x2_BASIC = 10, - // NV_MOSAIC_TOPO_4x2_BASIC = 11, - // NV_MOSAIC_TOPO_1x5_BASIC = 12, - // NV_MOSAIC_TOPO_1x6_BASIC = 13, - // NV_MOSAIC_TOPO_7x1_BASIC = 14, - - // *** Enum values for the mosaic topology type *** - // NV_MOSAIC_TOPO_1x2_PASSIVE_STEREO = 23, - // NV_MOSAIC_TOPO_2x1_PASSIVE_STEREO = 24, - // NV_MOSAIC_TOPO_1x3_PASSIVE_STEREO = 25, - // NV_MOSAIC_TOPO_3x1_PASSIVE_STEREO = 26, - // NV_MOSAIC_TOPO_1x4_PASSIVE_STEREO = 27, - // NV_MOSAIC_TOPO_4x1_PASSIVE_STEREO = 28, - // NV_MOSAIC_TOPO_2x2_PASSIVE_STEREO = 29, - for (int screenIndex = 0; screenIndex < _screens.Count; screenIndex++) - { - // go through each screen, and check if it matches a mosaic screen - - } - }*/ - - - return _screens; - } - - - // The object specific Equals - public bool Equals(NVIDIAProfileItem other) - { - // Check references - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - - // Check the object fields - return base.Equals(other) && - NVIDIADisplayConfig == other.NVIDIADisplayConfig && - WindowsDisplayConfig == other.WindowsDisplayConfig; - } - - // The public override for the Object.Equals - public bool Equals(Object obj) - { - // Check references - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - // If different types then can't be true - if (obj.GetType() == this.GetType()) return false; - // Check the object fields as this must the same object as obj, and we need to test in more detail - return Equals((NVIDIAProfileItem)obj); - } - - // If Equals() returns true for this object compared to another - // then GetHashCode() must return the same value for these objects. - public override int GetHashCode() - { - // Calculate the hash code for the product, including the base properties - return (base.GetHashCode(), NVIDIADisplayConfig, WindowsDisplayConfig).GetHashCode(); - - } - - public static bool operator ==(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs) - { - return Equals(lhs, rhs); - } - - public static bool operator !=(NVIDIAProfileItem lhs, NVIDIAProfileItem rhs) - { - return !Equals(lhs, rhs); - } - } - -} \ No newline at end of file diff --git a/DisplayMagicianShared/ProfileItem.cs b/DisplayMagicianShared/ProfileItem.cs index 4d75d93..00bba14 100644 --- a/DisplayMagicianShared/ProfileItem.cs +++ b/DisplayMagicianShared/ProfileItem.cs @@ -12,7 +12,6 @@ using IWshRuntimeLibrary; using DisplayMagicianShared.AMD; using DisplayMagicianShared.NVIDIA; using DisplayMagicianShared.Windows; -//using WK.Libraries.HotkeyListenerNS; namespace DisplayMagicianShared { @@ -59,6 +58,9 @@ namespace DisplayMagicianShared private Bitmap _profileBitmap, _profileShortcutBitmap; private List _profileDisplayIdentifiers = new List(); private List _screens = new List(); + private NVIDIA_DISPLAY_CONFIG _nvidiaDisplayConfig = new NVIDIA_DISPLAY_CONFIG(); + private AMD_DISPLAY_CONFIG _amdDisplayConfig = new AMD_DISPLAY_CONFIG(); + private WINDOWS_DISPLAY_CONFIG _windowsDisplayConfig = new WINDOWS_DISPLAY_CONFIG(); internal static string AppDataPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DisplayMagician"); private static string AppWallpaperPath = Path.Combine(AppDataPath, $"Wallpaper"); @@ -183,7 +185,7 @@ namespace DisplayMagicianShared } } - public virtual VIDEO_MODE VideoMode { get; } = VIDEO_MODE.WINDOWS; + public virtual VIDEO_MODE VideoMode { get; set; } = VIDEO_MODE.WINDOWS; public Keys Hotkey { get @@ -198,7 +200,46 @@ namespace DisplayMagicianShared public virtual string Name { get; set; } - + [JsonRequired] + public NVIDIA_DISPLAY_CONFIG NVIDIADisplayConfig + { + get + { + return _nvidiaDisplayConfig; + } + set + { + _nvidiaDisplayConfig = value; + } + } + + [JsonRequired] + public AMD_DISPLAY_CONFIG AMDDisplayConfig + { + get + { + return _amdDisplayConfig; + } + set + { + _amdDisplayConfig = value; + } + } + + [JsonRequired] + public WINDOWS_DISPLAY_CONFIG WindowsDisplayConfig + { + get + { + return _windowsDisplayConfig; + } + set + { + _windowsDisplayConfig = value; + } + } + + [JsonIgnore] public virtual ProfileIcon ProfileIcon { @@ -334,23 +375,61 @@ namespace DisplayMagicianShared return false; } - public virtual bool IsValid() + public bool IsValid() { - return false; + if (VideoMode == VIDEO_MODE.NVIDIA) + { + if (!NVIDIALibrary.GetLibrary().IsValidConfig(_nvidiaDisplayConfig)) + { + SharedLogger.logger.Error($"ProfileRepository/IsValid: The profile {Name} has an invalid NVIDIA display config"); + return false; + } + } + else if (VideoMode == VIDEO_MODE.AMD) + { + if (!AMDLibrary.GetLibrary().IsValidConfig(_amdDisplayConfig)) + { + SharedLogger.logger.Error($"ProfileRepository/IsValid: The profile {Name} has an invalid AMD display config"); + return false; + } + } + else if (VideoMode == VIDEO_MODE.WINDOWS) + { + if (!WinLibrary.GetLibrary().IsValidConfig(_windowsDisplayConfig)) + { + SharedLogger.logger.Error($"ProfileRepository/IsValid: The profile {Name} has an invalid Windows CCD display config"); + return false; + } + } + else + { + SharedLogger.logger.Error($"ProfileRepository/IsValid: The profile {Name} has an unknown video mode!"); + } + + // The rest of the + if (ProfileIcon is ProfileIcon && + System.IO.File.Exists(SavedProfileIconCacheFilename) && + ProfileBitmap is Bitmap && + ProfileTightestBitmap is Bitmap && + ProfileDisplayIdentifiers.Count > 0) + return true; + else + return false; + } - + public virtual bool CopyTo(ProfileItem profile, bool overwriteId = true) { - if (!(profile is ProfileItem)) - return false; - if (overwriteId == true) profile.UUID = UUID; // Copy all our profile data over to the other profile profile.Name = Name; + profile.AMDDisplayConfig = AMDDisplayConfig; + profile.NVIDIADisplayConfig = NVIDIADisplayConfig; + profile.WindowsDisplayConfig = WindowsDisplayConfig; profile.ProfileIcon = ProfileIcon; profile.SavedProfileIconCacheFilename = SavedProfileIconCacheFilename; profile.ProfileBitmap = ProfileBitmap; @@ -375,9 +454,72 @@ namespace DisplayMagicianShared } - public virtual bool CreateProfileFromCurrentDisplaySettings() + public bool CreateProfileFromCurrentDisplaySettings() { - return false; + if (VideoMode == VIDEO_MODE.NVIDIA) + { + NVIDIALibrary nvidiaLibrary = NVIDIALibrary.GetLibrary(); + if (nvidiaLibrary.IsInstalled) + { + // Create the profile data from the current config + _nvidiaDisplayConfig = nvidiaLibrary.GetActiveConfig(); + _windowsDisplayConfig = WinLibrary.GetLibrary().GetActiveConfig(); + _profileDisplayIdentifiers = nvidiaLibrary.GetCurrentDisplayIdentifiers(); + + // Now, since the ActiveProfile has changed, we need to regenerate screen positions + _screens = GetScreenPositions(); + + return true; + } + else + { + return false; + } + } + else if(VideoMode == VIDEO_MODE.AMD) + { + AMDLibrary amdLibrary = AMDLibrary.GetLibrary(); + if (amdLibrary.IsInstalled) + { + // Create the profile data from the current config + _amdDisplayConfig = amdLibrary.GetActiveConfig(); + _windowsDisplayConfig = WinLibrary.GetLibrary().GetActiveConfig(); + _profileDisplayIdentifiers = amdLibrary.GetCurrentDisplayIdentifiers(); + + // Now, since the ActiveProfile has changed, we need to regenerate screen positions + _screens = GetScreenPositions(); + + return true; + } + else + { + return false; + } + } + else if (VideoMode == VIDEO_MODE.WINDOWS) + { + WinLibrary winLibrary = WinLibrary.GetLibrary(); + if (winLibrary.IsInstalled) + { + // Create the profile data from the current config + _windowsDisplayConfig = winLibrary.GetActiveConfig(); + _profileDisplayIdentifiers = winLibrary.GetCurrentDisplayIdentifiers(); + + // Now, since the ActiveProfile has changed, we need to regenerate screen positions + _screens = GetScreenPositions(); + + return true; + } + else + { + return false; + } + } + else + { + SharedLogger.logger.Error($"ProfileRepository/CreateProfileFromCurrentDisplaySettings: Tried to use an unknown video mode!"); + return false; + } } public virtual bool PerformPostLoadingTasks() @@ -456,59 +598,811 @@ namespace DisplayMagicianShared public virtual void RefreshPossbility() { - // Check each display in this profile and make sure it's currently available - int validDisplayCount = 0; - - //validDisplayCount = (from connectedDisplay in ProfileRepository.ConnectedDisplayIdentifiers select connectedDisplay == profileDisplayIdentifier).Count(); - - foreach (string profileDisplayIdentifier in ProfileDisplayIdentifiers) + // Check whether this profile is possible + if (ProfileRepository.CurrentVideoMode == VIDEO_MODE.NVIDIA && NVIDIALibrary.GetLibrary().IsInstalled) { - // If this profile has a display that isn't currently available then we need to say it's a no! - if (ProfileRepository.ConnectedDisplayIdentifiers.Any(s => profileDisplayIdentifier.Equals(s))) + if (NVIDIALibrary.GetLibrary().IsPossibleConfig(_nvidiaDisplayConfig)) { - SharedLogger.logger.Trace($"ProfileItem/RefreshPossbility: We found the display in the profile {Name} with profileDisplayIdentifier {profileDisplayIdentifier} is connected now."); - validDisplayCount++; + + SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The NVIDIA profile {Name} is possible!"); + _isPossible = true; + } else { - SharedLogger.logger.Warn($"ProfileItem/RefreshPossbility: We found the display in the profile {Name} with profileDisplayIdentifier {profileDisplayIdentifier} is NOT currently connected, so this profile cannot be used."); + SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The NVIDIA profile {Name} is NOT possible!"); + _isPossible = false; } - } - if (validDisplayCount == ProfileDisplayIdentifiers.Count) + else if (ProfileRepository.CurrentVideoMode == VIDEO_MODE.AMD && AMDLibrary.GetLibrary().IsInstalled) { - - SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The profile {Name} is possible!"); - _isPossible = true; + if (AMDLibrary.GetLibrary().IsPossibleConfig(_amdDisplayConfig)) + { + SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The AMD profile {Name} is possible!"); + _isPossible = true; + + } + else + { + SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The AMD profile {Name} is NOT possible!"); + _isPossible = false; + } + } + else if (ProfileRepository.CurrentVideoMode == VIDEO_MODE.WINDOWS && WinLibrary.GetLibrary().IsInstalled) + { + if (WinLibrary.GetLibrary().IsPossibleConfig(_windowsDisplayConfig)) + { + + SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The Windows CCD profile {Name} is possible!"); + _isPossible = true; + + } + else + { + SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The Windows CCD profile {Name} is NOT possible!"); + _isPossible = false; + } } else { - SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The profile {Name} is NOT possible!"); _isPossible = false; } - } // Actually set this profile active - public virtual bool SetActive() + public bool SetActive() { + if (VideoMode == VIDEO_MODE.NVIDIA) + { + NVIDIALibrary nvidiaLibrary = NVIDIALibrary.GetLibrary(); + WinLibrary winLibrary = WinLibrary.GetLibrary(); + if (nvidiaLibrary.IsInstalled) + { + if (!nvidiaLibrary.IsActiveConfig(_nvidiaDisplayConfig) && !winLibrary.IsActiveConfig(_windowsDisplayConfig)) + { + if (nvidiaLibrary.IsPossibleConfig(_nvidiaDisplayConfig)) + { + SharedLogger.logger.Trace($"ProfileRepository/SetActive: The NVIDIA display settings within profile {Name} are possible to use right now, so we'll use attempt to use them."); + bool itWorkedforNVIDIA = nvidiaLibrary.SetActiveConfig(_nvidiaDisplayConfig); + + if (itWorkedforNVIDIA) + { + SharedLogger.logger.Trace($"ProfileRepository/SetActive: The NVIDIA display settings within profile {Name} were successfully applied."); + // Then let's try to also apply the windows changes + // Note: we are unable to check if the Windows CCD display config is possible, as it won't match if either the current display config is a Mosaic config, + // or if the display config we want to change to is a Mosaic config. So we just have to assume that it will work! + bool itWorkedforWindows = winLibrary.SetActiveConfig(_windowsDisplayConfig); + if (itWorkedforWindows) + { + SharedLogger.logger.Trace($"ProfileRepository/SetActive: The Windows CCD display settings within profile {Name} were successfully applied."); + return true; + } + else + { + SharedLogger.logger.Trace($"ProfileRepository/SetActive: The Windows CCD display settings within profile {Name} were NOT applied correctly."); + } + + } + else + { + SharedLogger.logger.Trace($"ProfileRepository/SetActive: The NVIDIA display settings within profile {Name} were NOT applied correctly."); + } + + } + else + { + SharedLogger.logger.Error($"ProfileRepository/SetActive: ERROR - Cannot apply the NVIDIA display config in profile {Name} as it is not currently possible to use it."); + } + } + else + { + SharedLogger.logger.Info($"ProfileRepository/SetActive: The display settings in profile {Name} are already installed. No need to install them again. Exiting."); + } + } + } + else if (VideoMode == VIDEO_MODE.AMD) + { + AMDLibrary amdLibrary = AMDLibrary.GetLibrary(); + WinLibrary winLibrary = WinLibrary.GetLibrary(); + if (amdLibrary.IsInstalled) + { + if (!amdLibrary.IsActiveConfig(_amdDisplayConfig) && !winLibrary.IsActiveConfig(_windowsDisplayConfig)) + { + if (amdLibrary.IsPossibleConfig(_amdDisplayConfig)) + { + SharedLogger.logger.Trace($"ProfileRepository/SetActive: The AMD display settings within profile {Name} are possible to use right now, so we'll use attempt to use them."); + bool itWorkedforNVIDIA = amdLibrary.SetActiveConfig(_amdDisplayConfig); + + if (itWorkedforNVIDIA) + { + SharedLogger.logger.Trace($"ProfileRepository/SetActive: The AMD display settings within profile {Name} were successfully applied."); + // Then let's try to also apply the windows changes + // Note: we are unable to check if the Windows CCD display config is possible, as it won't match if either the current display config is a Mosaic config, + // or if the display config we want to change to is a Mosaic config. So we just have to assume that it will work! + bool itWorkedforWindows = winLibrary.SetActiveConfig(_windowsDisplayConfig); + if (itWorkedforWindows) + { + SharedLogger.logger.Trace($"ProfileRepository/SetActive: The Windows CCD display settings within profile {Name} were successfully applied."); + return true; + } + else + { + SharedLogger.logger.Trace($"ProfileRepository/SetActive: The Windows CCD display settings within profile {Name} were NOT applied correctly."); + } + + } + else + { + SharedLogger.logger.Trace($"ProfileRepository/SetActive: The AMD display settings within profile {Name} were NOT applied correctly."); + } + + } + else + { + SharedLogger.logger.Error($"ProfileRepository/SetActive: ERROR - Cannot apply the AMD display config in profile {Name} as it is not currently possible to use it."); + } + } + else + { + SharedLogger.logger.Info($"ProfileRepository/SetActive: The display settings in profile {Name} are already installed. No need to install them again. Exiting."); + } + } + } + else if (VideoMode == VIDEO_MODE.WINDOWS) + { + WinLibrary winLibrary = WinLibrary.GetLibrary(); + if (winLibrary.IsInstalled) + { + if (!winLibrary.IsActiveConfig(_windowsDisplayConfig)) + { + if (winLibrary.SetActiveConfig(_windowsDisplayConfig)) + { + SharedLogger.logger.Trace($"ProfileRepository/SetActive: The Windows CCD display settings within profile {Name} were successfully applied."); + return true; + } + else + { + SharedLogger.logger.Trace($"ProfileRepository/SetActive: The Windows CCD display settings within profile {Name} were NOT applied correctly."); + } + } + else + { + SharedLogger.logger.Info($"ProfileRepository/SetActive: The display settings in profile {Name} are already installed. No need to install them again. Exiting."); + } + } + + } return false; } - public virtual List GetScreenPositions() + public List GetScreenPositions() { + if (VideoMode == VIDEO_MODE.NVIDIA) + { + return GetNVIDIAScreenPositions(); + } + else if (VideoMode == VIDEO_MODE.AMD) + { + return GetAMDScreenPositions(); + } + else if (VideoMode == VIDEO_MODE.WINDOWS) + { + return GetWindowsScreenPositions(); + } return new List(); } - /*public virtual int CompareTo(object obj) - { - if (!(obj is ProfileItem)) throw new ArgumentException("Object to CompareTo is not a Shortcut"); ; - ProfileItem otherProfile = (ProfileItem)obj; - return this.Name.CompareTo(otherProfile.Name); - }*/ + private List GetNVIDIAScreenPositions() + { + // Set up some colours + Color primaryScreenColor = Color.FromArgb(0, 174, 241); // represents Primary screen blue + Color spannedScreenColor = Color.FromArgb(118, 185, 0); // represents NVIDIA Green + Color normalScreenColor = Color.FromArgb(155, 155, 155); // represents normal screen colour (gray) + + // Now we create the screens structure from the AMD profile information + _screens = new List(); + + int pathCount = _windowsDisplayConfig.DisplayConfigPaths.Length; + // First of all we need to figure out how many display paths we have. + if (pathCount < 1) + { + // Return an empty screen if we have no Display Config Paths to use! + return _screens; + } + // Now we need to check for Spanned screens + if (_nvidiaDisplayConfig.MosaicConfig.IsMosaicEnabled) + { + // TODO: Make the NVIDIA displays show the individual screens and overlap! + + + + // Create a dictionary of all the screen sizes we want + //Dictionary MosaicScreens = new Dictionary(); + for (int i = 0; i < _nvidiaDisplayConfig.MosaicConfig.MosaicGridCount; i++) + { + ScreenPosition screen = new ScreenPosition(); + screen.Library = "NVIDIA"; + screen.Colour = normalScreenColor; + if (_nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].DisplayCount > 1) + { + // Set some basics about the screen + screen.SpannedScreens = new List(); + screen.Name = "NVIDIA Surround/Mosaic"; + screen.IsSpanned = true; + screen.SpannedRows = (int)_nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].Rows; + screen.SpannedColumns = (int)_nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].Columns; + screen.Colour = spannedScreenColor; + + // This is a combined surround/mosaic screen + // We need to build the size of the screen to match it later so we check the MosaicViewports + uint minX = 0; + uint minY = 0; + uint maxX = 0; + uint maxY = 0; + uint overallX = 0; + uint overallY = 0; + int overallWidth = 0; + int overallHeight = 0; + for (int j = 0; j < _nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].DisplayCount; j++) + { + SpannedScreenPosition spannedScreen = new SpannedScreenPosition(); + spannedScreen.Name = _nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].Displays[j].DisplayId.ToString(); + spannedScreen.Colour = spannedScreenColor; + + // Calculate screen size + NV_RECT viewRect = _nvidiaDisplayConfig.MosaicConfig.MosaicViewports[i][j]; + if (viewRect.Left < minX) + { + minX = viewRect.Left; + } + if (viewRect.Top < minY) + { + minY = viewRect.Top; + } + if (viewRect.Right > maxX) + { + maxX = viewRect.Right; + } + if (viewRect.Bottom > maxY) + { + maxY = viewRect.Bottom; + } + uint width = viewRect.Right - viewRect.Left + 1; + uint height = viewRect.Bottom - viewRect.Top + 1; + spannedScreen.ScreenX = (int)viewRect.Left; + spannedScreen.ScreenY = (int)viewRect.Top; + spannedScreen.ScreenWidth = (int)width; + spannedScreen.ScreenHeight = (int)height; + + // Figure out the overall figures for the screen + if (viewRect.Left < overallX) + { + overallX = viewRect.Left; + } + if (viewRect.Top < overallY) + { + overallY = viewRect.Top; + } + + overallWidth = (int)maxX - (int)minX + 1; + overallHeight = (int)maxY - (int)minY + 1; + + spannedScreen.Row = i + 1; + spannedScreen.Column = j + 1; + + // Add the spanned screen to the screen + screen.SpannedScreens.Add(spannedScreen); + } + + //screen.Name = targetId.ToString(); + //screen.DisplayConnector = displayMode.DisplayConnector; + screen.ScreenX = (int)overallX; + screen.ScreenY = (int)overallY; + screen.ScreenWidth = (int)overallWidth; + screen.ScreenHeight = (int)overallHeight; + + // If we're at the 0,0 coordinate then we're the primary monitor + if (screen.ScreenX == 0 && screen.ScreenY == 0) + { + // Record we're primary screen + screen.IsPrimary = true; + // Change the colour to be the primary colour, but only if it isn't a surround screen + if (screen.Colour != spannedScreenColor) + { + screen.Colour = primaryScreenColor; + } + } + + } + else if (_nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].DisplayCount == 1) + { + // This is a single screen with an adjoining mosaic screen + // Set some basics about the screen + uint displayId = _nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].Displays[0].DisplayId; + string windowsDisplayName = _nvidiaDisplayConfig.DisplayNames[displayId]; + uint sourceIndex = _windowsDisplayConfig.DisplaySources[windowsDisplayName]; + for (int x = 0; x < _windowsDisplayConfig.DisplayConfigModes.Length; x++) + { + // Skip this if its not a source info config type + if (_windowsDisplayConfig.DisplayConfigModes[x].InfoType != DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE) + { + continue; + } + + // If the source index matches the index of the source info object we're looking at, then process it! + if (_windowsDisplayConfig.DisplayConfigModes[x].Id == sourceIndex) + { + screen.Name = displayId.ToString(); + + screen.ScreenX = (int)_windowsDisplayConfig.DisplayConfigModes[x].SourceMode.Position.X; + screen.ScreenY = (int)_windowsDisplayConfig.DisplayConfigModes[x].SourceMode.Position.Y; + screen.ScreenWidth = (int)_windowsDisplayConfig.DisplayConfigModes[x].SourceMode.Width; + screen.ScreenHeight = (int)_windowsDisplayConfig.DisplayConfigModes[x].SourceMode.Height; + break; + } + } + + + // If we're at the 0,0 coordinate then we're the primary monitor + if (screen.ScreenX == 0 && screen.ScreenY == 0) + { + screen.IsPrimary = true; + screen.Colour = primaryScreenColor; + } + + } + + _screens.Add(screen); + } + } + else + { + foreach (var path in _windowsDisplayConfig.DisplayConfigPaths) + { + // For each path we go through and get the relevant info we need. + if (_windowsDisplayConfig.DisplayConfigPaths.Length > 0) + { + // Set some basics about the screen + ScreenPosition screen = new ScreenPosition(); + screen.Library = "NVIDIA"; + screen.IsSpanned = false; + screen.Colour = normalScreenColor; // this is the default unless overridden by the primary screen + + UInt32 sourceId = path.SourceInfo.Id; + UInt32 targetId = path.TargetInfo.Id; + + + // Go through the screens as Windows knows them, and then enhance the info with Mosaic data if it applies + foreach (DISPLAYCONFIG_MODE_INFO displayMode in _windowsDisplayConfig.DisplayConfigModes) + { + // Find the matching Display Config Source Mode + if (displayMode.InfoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE && displayMode.Id == sourceId) + { + screen.Name = targetId.ToString(); + //screen.DisplayConnector = displayMode.DisplayConnector; + screen.ScreenX = displayMode.SourceMode.Position.X; + screen.ScreenY = displayMode.SourceMode.Position.Y; + screen.ScreenWidth = (int)displayMode.SourceMode.Width; + screen.ScreenHeight = (int)displayMode.SourceMode.Height; + + // If we're at the 0,0 coordinate then we're the primary monitor + if (screen.ScreenX == 0 && screen.ScreenY == 0) + { + screen.IsPrimary = true; + screen.Colour = primaryScreenColor; + } + break; + } + } + + foreach (ADVANCED_HDR_INFO_PER_PATH hdrInfo in _windowsDisplayConfig.DisplayHDRStates) + { + // Find the matching HDR information + if (hdrInfo.Id == targetId) + { + // HDR information + if (hdrInfo.AdvancedColorInfo.AdvancedColorSupported) + { + screen.HDRSupported = true; + if (hdrInfo.AdvancedColorInfo.AdvancedColorEnabled) + { + screen.HDREnabled = true; + } + else + { + screen.HDREnabled = false; + } + + } + else + { + screen.HDRSupported = false; + screen.HDREnabled = false; + } + break; + } + } + + _screens.Add(screen); + } + } + } + + + + + + /* + // Go through the screens, and update the Mosaic screens with their info (if there are any) + if (_nvidiaDisplayConfig.MosaicConfig.IsMosaicEnabled && _nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos.Length) + { + // *** Enum values for the mosaic topology type *** + // NV_MOSAIC_TOPO_1x2_BASIC = 1 + // NV_MOSAIC_TOPO_2x1_BASIC = 2, + // NV_MOSAIC_TOPO_1x3_BASIC = 3, + // NV_MOSAIC_TOPO_3x1_BASIC = 4, + // NV_MOSAIC_TOPO_1x4_BASIC = 5, + // NV_MOSAIC_TOPO_4x1_BASIC = 6, + // NV_MOSAIC_TOPO_2x2_BASIC = 7, + // NV_MOSAIC_TOPO_2x3_BASIC = 8, + // NV_MOSAIC_TOPO_2x4_BASIC = 9, + // NV_MOSAIC_TOPO_3x2_BASIC = 10, + // NV_MOSAIC_TOPO_4x2_BASIC = 11, + // NV_MOSAIC_TOPO_1x5_BASIC = 12, + // NV_MOSAIC_TOPO_1x6_BASIC = 13, + // NV_MOSAIC_TOPO_7x1_BASIC = 14, + + // *** Enum values for the mosaic topology type *** + // NV_MOSAIC_TOPO_1x2_PASSIVE_STEREO = 23, + // NV_MOSAIC_TOPO_2x1_PASSIVE_STEREO = 24, + // NV_MOSAIC_TOPO_1x3_PASSIVE_STEREO = 25, + // NV_MOSAIC_TOPO_3x1_PASSIVE_STEREO = 26, + // NV_MOSAIC_TOPO_1x4_PASSIVE_STEREO = 27, + // NV_MOSAIC_TOPO_4x1_PASSIVE_STEREO = 28, + // NV_MOSAIC_TOPO_2x2_PASSIVE_STEREO = 29, + for (int screenIndex = 0; screenIndex < _screens.Count; screenIndex++) + { + // go through each screen, and check if it matches a mosaic screen + + } + }*/ + + + return _screens; + } + + private List GetAMDScreenPositions() + { + // Set up some colours + Color primaryScreenColor = Color.FromArgb(0, 174, 241); // represents Primary screen blue + Color spannedScreenColor = Color.FromArgb(221, 0, 49); // represents AMD Red + Color normalScreenColor = Color.FromArgb(155, 155, 155); // represents normal screen colour (gray) + + // Now we create the screens structure from the AMD profile information + _screens = new List(); + + int pathCount = _windowsDisplayConfig.DisplayConfigPaths.Length; + // First of all we need to figure out how many display paths we have. + if (pathCount < 1) + { + // Return an empty screen if we have no Display Config Paths to use! + return _screens; + } + // Now we need to check for Spanned screens + if (_nvidiaDisplayConfig.MosaicConfig.IsMosaicEnabled) + { + // TODO: Make the NVIDIA displays show the individual screens and overlap! + + + + // Create a dictionary of all the screen sizes we want + //Dictionary MosaicScreens = new Dictionary(); + for (int i = 0; i < _nvidiaDisplayConfig.MosaicConfig.MosaicGridCount; i++) + { + ScreenPosition screen = new ScreenPosition(); + screen.Library = "NVIDIA"; + screen.Colour = normalScreenColor; + if (_nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].DisplayCount > 1) + { + // Set some basics about the screen + screen.SpannedScreens = new List(); + screen.Name = "NVIDIA Surround/Mosaic"; + screen.IsSpanned = true; + screen.SpannedRows = (int)_nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].Rows; + screen.SpannedColumns = (int)_nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].Columns; + screen.Colour = spannedScreenColor; + + // This is a combined surround/mosaic screen + // We need to build the size of the screen to match it later so we check the MosaicViewports + uint minX = 0; + uint minY = 0; + uint maxX = 0; + uint maxY = 0; + uint overallX = 0; + uint overallY = 0; + int overallWidth = 0; + int overallHeight = 0; + for (int j = 0; j < _nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].DisplayCount; j++) + { + SpannedScreenPosition spannedScreen = new SpannedScreenPosition(); + spannedScreen.Name = _nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].Displays[j].DisplayId.ToString(); + spannedScreen.Colour = spannedScreenColor; + + // Calculate screen size + NV_RECT viewRect = _nvidiaDisplayConfig.MosaicConfig.MosaicViewports[i][j]; + if (viewRect.Left < minX) + { + minX = viewRect.Left; + } + if (viewRect.Top < minY) + { + minY = viewRect.Top; + } + if (viewRect.Right > maxX) + { + maxX = viewRect.Right; + } + if (viewRect.Bottom > maxY) + { + maxY = viewRect.Bottom; + } + uint width = viewRect.Right - viewRect.Left + 1; + uint height = viewRect.Bottom - viewRect.Top + 1; + spannedScreen.ScreenX = (int)viewRect.Left; + spannedScreen.ScreenY = (int)viewRect.Top; + spannedScreen.ScreenWidth = (int)width; + spannedScreen.ScreenHeight = (int)height; + + // Figure out the overall figures for the screen + if (viewRect.Left < overallX) + { + overallX = viewRect.Left; + } + if (viewRect.Top < overallY) + { + overallY = viewRect.Top; + } + + overallWidth = (int)maxX - (int)minX + 1; + overallHeight = (int)maxY - (int)minY + 1; + + spannedScreen.Row = i + 1; + spannedScreen.Column = j + 1; + + // Add the spanned screen to the screen + screen.SpannedScreens.Add(spannedScreen); + } + + //screen.Name = targetId.ToString(); + //screen.DisplayConnector = displayMode.DisplayConnector; + screen.ScreenX = (int)overallX; + screen.ScreenY = (int)overallY; + screen.ScreenWidth = (int)overallWidth; + screen.ScreenHeight = (int)overallHeight; + + // If we're at the 0,0 coordinate then we're the primary monitor + if (screen.ScreenX == 0 && screen.ScreenY == 0) + { + // Record we're primary screen + screen.IsPrimary = true; + // Change the colour to be the primary colour, but only if it isn't a surround screen + if (screen.Colour != spannedScreenColor) + { + screen.Colour = primaryScreenColor; + } + } + + } + else if (_nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].DisplayCount == 1) + { + // This is a single screen with an adjoining mosaic screen + // Set some basics about the screen + uint displayId = _nvidiaDisplayConfig.MosaicConfig.MosaicGridTopos[i].Displays[0].DisplayId; + string windowsDisplayName = _nvidiaDisplayConfig.DisplayNames[displayId]; + uint sourceIndex = _windowsDisplayConfig.DisplaySources[windowsDisplayName]; + for (int x = 0; x < _windowsDisplayConfig.DisplayConfigModes.Length; x++) + { + // Skip this if its not a source info config type + if (_windowsDisplayConfig.DisplayConfigModes[x].InfoType != DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE) + { + continue; + } + + // If the source index matches the index of the source info object we're looking at, then process it! + if (_windowsDisplayConfig.DisplayConfigModes[x].Id == sourceIndex) + { + screen.Name = displayId.ToString(); + + screen.ScreenX = (int)_windowsDisplayConfig.DisplayConfigModes[x].SourceMode.Position.X; + screen.ScreenY = (int)_windowsDisplayConfig.DisplayConfigModes[x].SourceMode.Position.Y; + screen.ScreenWidth = (int)_windowsDisplayConfig.DisplayConfigModes[x].SourceMode.Width; + screen.ScreenHeight = (int)_windowsDisplayConfig.DisplayConfigModes[x].SourceMode.Height; + break; + } + } + + + // If we're at the 0,0 coordinate then we're the primary monitor + if (screen.ScreenX == 0 && screen.ScreenY == 0) + { + screen.IsPrimary = true; + screen.Colour = primaryScreenColor; + } + + } + + _screens.Add(screen); + } + } + else + { + foreach (var path in _windowsDisplayConfig.DisplayConfigPaths) + { + // For each path we go through and get the relevant info we need. + if (_windowsDisplayConfig.DisplayConfigPaths.Length > 0) + { + // Set some basics about the screen + ScreenPosition screen = new ScreenPosition(); + screen.Library = "NVIDIA"; + screen.IsSpanned = false; + screen.Colour = normalScreenColor; // this is the default unless overridden by the primary screen + + UInt32 sourceId = path.SourceInfo.Id; + UInt32 targetId = path.TargetInfo.Id; + + + // Go through the screens as Windows knows them, and then enhance the info with Mosaic data if it applies + foreach (DISPLAYCONFIG_MODE_INFO displayMode in _windowsDisplayConfig.DisplayConfigModes) + { + // Find the matching Display Config Source Mode + if (displayMode.InfoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE && displayMode.Id == sourceId) + { + screen.Name = targetId.ToString(); + //screen.DisplayConnector = displayMode.DisplayConnector; + screen.ScreenX = displayMode.SourceMode.Position.X; + screen.ScreenY = displayMode.SourceMode.Position.Y; + screen.ScreenWidth = (int)displayMode.SourceMode.Width; + screen.ScreenHeight = (int)displayMode.SourceMode.Height; + + // If we're at the 0,0 coordinate then we're the primary monitor + if (screen.ScreenX == 0 && screen.ScreenY == 0) + { + screen.IsPrimary = true; + screen.Colour = primaryScreenColor; + } + break; + } + } + + foreach (ADVANCED_HDR_INFO_PER_PATH hdrInfo in _windowsDisplayConfig.DisplayHDRStates) + { + // Find the matching HDR information + if (hdrInfo.Id == targetId) + { + // HDR information + if (hdrInfo.AdvancedColorInfo.AdvancedColorSupported) + { + screen.HDRSupported = true; + if (hdrInfo.AdvancedColorInfo.AdvancedColorEnabled) + { + screen.HDREnabled = true; + } + else + { + screen.HDREnabled = false; + } + + } + else + { + screen.HDRSupported = false; + screen.HDREnabled = false; + } + break; + } + } + + _screens.Add(screen); + } + } + } + + return _screens; + } + + private List GetWindowsScreenPositions() + { + // Set up some colours + Color primaryScreenColor = Color.FromArgb(0, 174, 241); // represents Primary screen blue + Color normalScreenColor = Color.FromArgb(155, 155, 155); // represents normal screen colour (gray) + + // Now we create the screens structure from the AMD profile information + _screens = new List(); + + int pathCount = _windowsDisplayConfig.DisplayConfigPaths.Length; + // First of all we need to figure out how many display paths we have. + if (pathCount < 1) + { + // Return an empty screen if we have no Display Config Paths to use! + return _screens; + } + + foreach (var path in _windowsDisplayConfig.DisplayConfigPaths) + { + // For each path we go through and get the relevant info we need. + if (_windowsDisplayConfig.DisplayConfigPaths.Length > 0) + { + // Set some basics about the screen + ScreenPosition screen = new ScreenPosition(); + screen.Library = "WINDOWS"; + screen.IsSpanned = false; + screen.Colour = normalScreenColor; // this is the default unless overridden by the primary screen + + UInt32 sourceId = path.SourceInfo.Id; + UInt32 targetId = path.TargetInfo.Id; + + + // Go through the screens as Windows knows them, and then enhance the info with Mosaic data if it applies + foreach (DISPLAYCONFIG_MODE_INFO displayMode in _windowsDisplayConfig.DisplayConfigModes) + { + // Find the matching Display Config Source Mode + if (displayMode.InfoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE && displayMode.Id == sourceId) + { + screen.Name = targetId.ToString(); + //screen.DisplayConnector = displayMode.DisplayConnector; + screen.ScreenX = displayMode.SourceMode.Position.X; + screen.ScreenY = displayMode.SourceMode.Position.Y; + screen.ScreenWidth = (int)displayMode.SourceMode.Width; + screen.ScreenHeight = (int)displayMode.SourceMode.Height; + + // If we're at the 0,0 coordinate then we're the primary monitor + if (screen.ScreenX == 0 && screen.ScreenY == 0) + { + screen.IsPrimary = true; + screen.Colour = primaryScreenColor; + } + break; + } + } + + foreach (ADVANCED_HDR_INFO_PER_PATH hdrInfo in _windowsDisplayConfig.DisplayHDRStates) + { + // Find the matching HDR information + if (hdrInfo.Id == targetId) + { + // HDR information + if (hdrInfo.AdvancedColorInfo.AdvancedColorSupported) + { + screen.HDRSupported = true; + if (hdrInfo.AdvancedColorInfo.AdvancedColorEnabled) + { + screen.HDREnabled = true; + } + else + { + screen.HDREnabled = false; + } + + } + else + { + screen.HDRSupported = false; + screen.HDREnabled = false; + } + break; + } + } + + _screens.Add(screen); + } + } + + return _screens; + } + public int CompareTo(ProfileItem other) { @@ -569,6 +1463,7 @@ namespace DisplayMagicianShared } + // The object specific Equals public bool Equals(ProfileItem other) { @@ -580,29 +1475,14 @@ namespace DisplayMagicianShared // ProfileDisplayIdentifiers may be the same but in different order within the array, so we need to handle // that fact. int ourMatchedIds = 0; - List otherDisplayIdentifiers = other.ProfileDisplayIdentifiers; - foreach (string ourDisplayIdentifier in ProfileDisplayIdentifiers) - { - if (otherDisplayIdentifiers.Contains(ourDisplayIdentifier)) - { - ourMatchedIds++; - } - } - int otherMatchedIds = 0; - foreach (string otherDisplayIdentifier in otherDisplayIdentifiers) - { - if (ProfileDisplayIdentifiers.Contains(otherDisplayIdentifier)) - { - otherMatchedIds++; - } - } - - return ProfileDisplayIdentifiers.Count == otherDisplayIdentifiers.Count && - ourMatchedIds == otherMatchedIds; + return NVIDIADisplayConfig.Equals(other.NVIDIADisplayConfig) && + //AMDDisplayConfig.Equals(other.AMDDisplayConfig) && + WindowsDisplayConfig.Equals(other.WindowsDisplayConfig) && + ProfileDisplayIdentifiers.SequenceEqual (other.ProfileDisplayIdentifiers); } // The public override for the Object.Equals - public bool Equals(Object obj) + public override bool Equals(Object obj) { // Check references if (ReferenceEquals(null, obj)) return false; @@ -618,7 +1498,7 @@ namespace DisplayMagicianShared public override int GetHashCode() { // Calculate the hash code for the product. - return (ProfileDisplayIdentifiers).GetHashCode(); + return (NVIDIADisplayConfig, AMDDisplayConfig, WindowsDisplayConfig, ProfileDisplayIdentifiers).GetHashCode(); } diff --git a/DisplayMagicianShared/ProfileRepository.cs b/DisplayMagicianShared/ProfileRepository.cs index fed8997..1601da3 100644 --- a/DisplayMagicianShared/ProfileRepository.cs +++ b/DisplayMagicianShared/ProfileRepository.cs @@ -424,33 +424,22 @@ namespace DisplayMagicianShared } - public static bool ContainsProfile(ProfileItem Profile) + public static bool ContainsProfile(ProfileItem profile) { - if (!(Profile is ProfileItem)) + if (!(profile is ProfileItem)) return false; - SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Checking if our profile repository contains a profile called {Profile.Name}"); + SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Checking if our profile repository contains a profile called {profile.Name}"); foreach (ProfileItem testProfile in _allProfiles) { - if (testProfile.VideoMode == VIDEO_MODE.NVIDIA && (testProfile as NVIDIAProfileItem).Equals(Profile)) + if (testProfile.Equals(profile)) { - SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {Profile.Name}"); + SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {profile.Name}"); return true; - } - else if (testProfile.VideoMode == VIDEO_MODE.AMD && (testProfile as AMDProfileItem).Equals(Profile)) - { - SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {Profile.Name}"); - return true; - } - else if (testProfile.VideoMode == VIDEO_MODE.WINDOWS && (testProfile as AMDProfileItem).Equals(Profile)) - { - SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {Profile.Name}"); - return true; - } - + } } - SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository doesn't contain a profile called {Profile.Name}"); + SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository doesn't contain a profile called {profile.Name}"); return false; } @@ -501,26 +490,12 @@ namespace DisplayMagicianShared foreach (ProfileItem testProfile in _allProfiles) { - // TODO - change for Equals - if (testProfile.VideoMode == VIDEO_MODE.NVIDIA && (testProfile as NVIDIAProfileItem).Equals(_currentProfile)) + if (testProfile.Equals(_currentProfile)) { SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {testProfile.Name}"); savedProfileName = testProfile.Name; return true; } - else if (testProfile.VideoMode == VIDEO_MODE.AMD && (testProfile as AMDProfileItem).Equals(_currentProfile)) - { - SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {testProfile.Name}"); - savedProfileName = testProfile.Name; - return true; - } - else if (testProfile.VideoMode == VIDEO_MODE.WINDOWS && (testProfile as AMDProfileItem).Equals(_currentProfile)) - { - SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {testProfile.Name}"); - savedProfileName = testProfile.Name; - return true; - } - } SharedLogger.logger.Debug($"ProfileRepository/ContainsCurrentProfile: Our profile repository doesn't contain the display profile currently in use"); @@ -609,79 +584,52 @@ namespace DisplayMagicianShared SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: Updating the profile currently active (in use now)."); - + ProfileItem profile; SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: Attempting to access configuration through NVIDIA, then AMD, then Windows CCD interfaces, in that order."); if (_currentVideoMode == VIDEO_MODE.NVIDIA) { SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: NVIDIA NVAPI Driver is installed, so using that for this display profile."); - NVIDIAProfileItem nvidiaProfile = new NVIDIAProfileItem + profile = new ProfileItem { Name = "Current NVIDIA Display Profile", - }; - nvidiaProfile.CreateProfileFromCurrentDisplaySettings(); - - if (_profilesLoaded && _allProfiles.Count > 0) - { - - foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles) - { - if (nvidiaProfile.Equals(loadedProfile)) - { - _currentProfile = loadedProfile; - SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The NVIDIA profile {loadedProfile.Name} is currently active (in use now)."); - return; - } - } - } - SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current NVIDIA profile is a new profile that doesn't already exist in the Profile Repository."); - _currentProfile = nvidiaProfile; + VideoMode = VIDEO_MODE.NVIDIA + }; } else if (_currentVideoMode == VIDEO_MODE.AMD) { SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: NVIDIA is not installed but the AMD ADL Driver IS installed, so using that for this display profile."); - AMDProfileItem amdProfile = new AMDProfileItem + profile = new ProfileItem { Name = "Current AMD Display Profile", + VideoMode = VIDEO_MODE.AMD }; - amdProfile.CreateProfileFromCurrentDisplaySettings(); - if (_profilesLoaded && _allProfiles.Count > 0) - { - foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles) - { - if (amdProfile.Equals(loadedProfile)) - { - _currentProfile = loadedProfile; - SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The AMD profile {loadedProfile.Name} is currently active (in use now)."); - return; - } - } - } - SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current AMD profile is a new profile that doesn't already exist in the Profile Repository."); - _currentProfile = amdProfile; } else { - SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: Neither NVIDIA NVAPI or AMD ADL Drivers are installed, so using the built in Windows CCD library interface for this display profile."); - WinProfileItem winProfile = new WinProfileItem + profile = new ProfileItem { Name = "Current Windows Display Profile", + VideoMode = VIDEO_MODE.WINDOWS }; - winProfile.CreateProfileFromCurrentDisplaySettings(); - if (_profilesLoaded && _allProfiles.Count > 0) + } + + profile.CreateProfileFromCurrentDisplaySettings(); + + if (_profilesLoaded && _allProfiles.Count > 0) + { + + foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles) { - foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles) + if (loadedProfile.Equals(profile)) { - if (winProfile.Equals(loadedProfile)) - { - _currentProfile = loadedProfile; - SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The Windows profile {loadedProfile.Name} is currently active (in use now)."); - return; - } + _currentProfile = loadedProfile; + SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The {loadedProfile.VideoMode.ToString("G")} profile '{loadedProfile.Name}' is currently active (in use now)."); + return; } } - SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current Windows profile is a new profile that doesn't already exist in the Profile Repository."); - _currentProfile = winProfile; } + SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current profile is a new profile that doesn't already exist in the Profile Repository."); + _currentProfile = profile; } public static ProfileItem GetActiveProfile() @@ -709,51 +657,14 @@ namespace DisplayMagicianShared return false; } - if (profile is NVIDIAProfileItem) + if (profile.Equals(_currentProfile)) { - NVIDIAProfileItem nvidiaCurrentProfile = (NVIDIAProfileItem)_currentProfile; - if (nvidiaCurrentProfile.Equals(profile)) - { - SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The NVIDIA profile {profile.Name} is the currently active profile."); - return true; - } - else - { - SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The NVIDIA profile {profile.Name} is the not currently active profile."); - return false; - } + SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The profile {profile.Name} is the currently active profile."); + return true; } - else if (_currentProfile is AMDProfileItem) - { - AMDProfileItem amdCurrentProfile = (AMDProfileItem)_currentProfile; - if (amdCurrentProfile.Equals(profile)) - { - SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The AMD profile {profile.Name} is the currently active profile."); - return true; - } - else - { - SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The AMD profile {profile.Name} is the not currently active profile."); - return false; - } - } - else if (_currentProfile is WinProfileItem) - { - WinProfileItem winCurrentProfile = (WinProfileItem)_currentProfile; - if (winCurrentProfile.Equals(profile)) - { - SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The Windows CCD profile {profile.Name} is the currently active profile."); - return true; - } - else - { - SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The Windows CCD profile {profile.Name} is the not currently active profile."); - return false; - } - } else { - SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The requested profile {profile.Name} is a different video library to the current profile, so can't possibly be the same one."); + SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The profile {profile.Name} is the not currently active profile."); return false; } } @@ -795,10 +706,7 @@ namespace DisplayMagicianShared SharedLogger.logger.Debug($"ProfileRepository/LoadProfiles: Finding the current profile in the Profile Repository"); - // Update the current active profile - UpdateActiveProfile(); - - // Go through all the profiles and set up the needed structures (such as the Screens list) + /*// Go through all the profiles and set up the needed structures (such as the Screens list) // and check if the current profile is used foreach (ProfileItem loadedProfile in _allProfiles) { @@ -833,7 +741,7 @@ namespace DisplayMagicianShared { SharedLogger.logger.Error($"ProfileRepository/LoadProfiles: ERROR - Profile {loadedProfile.Name} is not a recognised profile!"); } - } + }*/ // Sort the profiles alphabetically _allProfiles.Sort(); @@ -842,7 +750,7 @@ namespace DisplayMagicianShared else { SharedLogger.logger.Debug($"ProfileRepository/LoadProfiles: The {_profileStorageJsonFileName} profile JSON file exists but is empty! So we're going to treat it as if it didn't exist."); - UpdateActiveProfile(); + //UpdateActiveProfile(); } } else @@ -850,10 +758,12 @@ namespace DisplayMagicianShared // If we get here, then we don't have any profiles saved! // So we gotta start from scratch SharedLogger.logger.Debug($"ProfileRepository/LoadProfiles: Couldn't find the {_profileStorageJsonFileName} profile JSON file that contains the Profiles"); - UpdateActiveProfile(); + //UpdateActiveProfile(); } _profilesLoaded = true; + // Update the current active profile + UpdateActiveProfile(); IsPossibleRefresh(); return true; @@ -1021,9 +931,6 @@ namespace DisplayMagicianShared public static ApplyProfileResult ApplyProfile(ProfileItem profile) { SharedLogger.logger.Trace($"Program/ApplyProfile: Starting"); - NVIDIAProfileItem nvidiaProfile = null; - AMDProfileItem amdProfile = null; - WinProfileItem winProfile = null; // We try to time the profile display swap Stopwatch stopWatch = new Stopwatch(); bool wasDisplayChangeSuccessful = true; @@ -1040,39 +947,14 @@ namespace DisplayMagicianShared stopWatch.Start(); // We try to swap profiles. The profiles have checking logic in them - if (profile is NVIDIAProfileItem) + if (!(profile.SetActive())) { - SharedLogger.logger.Trace($"ProfileRepository/ApplyProfile: Profile is an NVIDIA Profile, so changing type to NVIDIAProfileItem"); - nvidiaProfile = (NVIDIAProfileItem)profile; - if (!nvidiaProfile.SetActive()) - { - SharedLogger.logger.Error($"ProfileRepository/ApplyProfile: Error applying the NVIDIA Profile!"); - return ApplyProfileResult.Error; - } - } - else if (profile is AMDProfileItem) - { - SharedLogger.logger.Trace($"ProfileRepository/ApplyProfile: Profile is an AMD Profile, so changing type to AMDProfileItem"); - amdProfile = (AMDProfileItem)profile; - if (!amdProfile.SetActive()) - { - SharedLogger.logger.Error($"ProfileRepository/ApplyProfile: Error applying the AMD Profile!"); - return ApplyProfileResult.Error; - } - } - else if (profile is WinProfileItem) - { - SharedLogger.logger.Trace($"ProfileRepository/ApplyProfile: Profile is a Windows CCD Profile, so changing type to WinProfileItem"); - winProfile = (WinProfileItem)profile; - if (!winProfile.SetActive()) - { - // Somehow return that this profile topology didn't apply - throw new ApplyTopologyException("ProfileRepository/ApplyProfile: amdApplyProfileTask: Error applying the AMD Profile!"); - } + SharedLogger.logger.Error($"ProfileRepository/ApplyProfile: Error applying the {profile.VideoMode.ToString("G")} Profile!"); + return ApplyProfileResult.Error; } else { - SharedLogger.logger.Trace($"ProfileRepository/ApplyProfile: Profile type is not one that is supported by DisplayMagician, so returning an ApplyProfileResult error"); + SharedLogger.logger.Trace($"ProfileRepository/ApplyProfile: Successfully applied the {profile.VideoMode.ToString("G")} Profile!"); return ApplyProfileResult.Error; } diff --git a/DisplayMagicianShared/Windows/WinProfileItem.cs b/DisplayMagicianShared/Windows/WinProfileItem.cs deleted file mode 100644 index 49e1d52..0000000 --- a/DisplayMagicianShared/Windows/WinProfileItem.cs +++ /dev/null @@ -1,366 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Windows.Forms; -using DisplayMagicianShared.Resources; -using Newtonsoft.Json; -using System.Drawing; -using System.Drawing.Imaging; - -namespace DisplayMagicianShared.Windows -{ - - public class WinProfileItem : ProfileItem, IEquatable - { - private static List _allSavedProfiles = new List(); - private ProfileIcon _profileIcon; - private Bitmap _profileBitmap, _profileShortcutBitmap; - private List _profileDisplayIdentifiers = new List(); - private List _screens; - private WINDOWS_DISPLAY_CONFIG _windowsDisplayConfig = new WINDOWS_DISPLAY_CONFIG(); - private static readonly string uuidV4Regex = @"(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$"; - - private string _uuid = ""; - private bool _isPossible = false; - private Keys _hotkey = Keys.None; - - public WinProfileItem() - { - } - - public new static Version Version = new Version(2, 1); - - #region Instance Properties - - [JsonIgnore] - public override bool IsPossible - { - get - { - // Return the cached answer - return _isPossible; - } - set - { - _isPossible = value; - } - } - - [JsonIgnore] - public override bool IsActive - { - get - { - - if (this.Equals(ProfileRepository.CurrentProfile)) - return true; - else - return false; - - } - } - - public override VIDEO_MODE VideoMode { get; } = VIDEO_MODE.WINDOWS; - - public override string Name { get; set; } - - [JsonRequired] - public WINDOWS_DISPLAY_CONFIG WindowsDisplayConfig - { - get - { - return _windowsDisplayConfig; - } - set - { - _windowsDisplayConfig = value; - } - } - - - public override List ProfileDisplayIdentifiers - { - get - { - if (_profileDisplayIdentifiers.Count == 0) - { - _profileDisplayIdentifiers = WinLibrary.GetLibrary().GetCurrentDisplayIdentifiers(); - } - return _profileDisplayIdentifiers; - } - set - { - if (value is List) - _profileDisplayIdentifiers = value; - } - } - - - #endregion - - public override bool IsValid() - { - - if (WinLibrary.GetLibrary().IsValidConfig(_windowsDisplayConfig) && - ProfileIcon is ProfileIcon && - System.IO.File.Exists(SavedProfileIconCacheFilename) && - ProfileBitmap is Bitmap && - ProfileTightestBitmap is Bitmap && - ProfileDisplayIdentifiers.Count > 0) - return true; - else - return false; - } - - - - public bool CopyTo(WinProfileItem profile, bool overwriteId = true) - { - if (!(profile is WinProfileItem)) - return false; - - if (overwriteId == true) - profile.UUID = UUID; - - // Copy all our profile data over to the other profile - profile.Name = Name; - profile.WindowsDisplayConfig = WindowsDisplayConfig; - profile.ProfileIcon = ProfileIcon; - profile.SavedProfileIconCacheFilename = SavedProfileIconCacheFilename; - profile.ProfileBitmap = ProfileBitmap; - profile.ProfileTightestBitmap = ProfileTightestBitmap; - profile.ProfileDisplayIdentifiers = ProfileDisplayIdentifiers; - profile.WallpaperMode = WallpaperMode; - profile.WallpaperBitmapFilename = WallpaperBitmapFilename; - profile.WallpaperStyle = WallpaperStyle; - return true; - } - - public override bool PreSave() - { - // Prepare our profile data for saving - if (_profileDisplayIdentifiers.Count == 0) - { - _profileDisplayIdentifiers = WinLibrary.GetLibrary().GetCurrentDisplayIdentifiers(); - } - - // Return if it is valid and we should continue - return IsValid(); - } - - public override void RefreshPossbility() - { - // Check whether this profile is possible - if (ProfileRepository.CurrentVideoMode == VIDEO_MODE.WINDOWS && WinLibrary.GetLibrary().IsInstalled) - { - if (WinLibrary.GetLibrary().IsPossibleConfig(_windowsDisplayConfig)) - { - - SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The Windows CCD profile {Name} is possible!"); - _isPossible = true; - - } - else - { - SharedLogger.logger.Debug($"ProfileRepository/IsPossibleRefresh: The Windows CCD profile {Name} is NOT possible!"); - _isPossible = false; - } - } - else - { - _isPossible = false; - } - } - - - // Actually set this profile active - public override bool SetActive() - { - WinLibrary winLibrary = WinLibrary.GetLibrary(); - if (winLibrary.IsInstalled) - { - if (!winLibrary.IsActiveConfig(_windowsDisplayConfig)) - { - if (winLibrary.SetActiveConfig(_windowsDisplayConfig)) - { - SharedLogger.logger.Trace($"ProfileRepository/SetActive: The Windows CCD display settings within profile {Name} were successfully applied."); - return true; - } - else - { - SharedLogger.logger.Trace($"ProfileRepository/SetActive: The Windows CCD display settings within profile {Name} were NOT applied correctly."); - } - } - else - { - SharedLogger.logger.Info($"ProfileRepository/SetActive: The display settings in profile {Name} are already installed. No need to install them again. Exiting."); - } - } - return false; - } - - public override bool CreateProfileFromCurrentDisplaySettings() - { - - WinLibrary winLibrary = WinLibrary.GetLibrary(); - if (winLibrary.IsInstalled) - { - // Create the profile data from the current config - _windowsDisplayConfig = winLibrary.GetActiveConfig(); - _profileDisplayIdentifiers = winLibrary.GetCurrentDisplayIdentifiers(); - - // Now, since the ActiveProfile has changed, we need to regenerate screen positions - _screens = GetScreenPositions(); - - return true; - } - else - { - return false; - } - } - - public override bool PerformPostLoadingTasks() - { - // First thing we do is to set up the Screens - _screens = GetScreenPositions(); - - return true; - } - - public override List GetScreenPositions() - { - // Set up some colours - Color primaryScreenColor = Color.FromArgb(0, 174, 241); // represents Primary screen blue - Color normalScreenColor = Color.FromArgb(155, 155, 155); // represents normal screen colour (gray) - - // Now we create the screens structure from the AMD profile information - _screens = new List(); - - int pathCount = _windowsDisplayConfig.DisplayConfigPaths.Length; - // First of all we need to figure out how many display paths we have. - if (pathCount < 1) - { - // Return an empty screen if we have no Display Config Paths to use! - return _screens; - } - - foreach (var path in _windowsDisplayConfig.DisplayConfigPaths) - { - // For each path we go through and get the relevant info we need. - if (_windowsDisplayConfig.DisplayConfigPaths.Length > 0) - { - // Set some basics about the screen - ScreenPosition screen = new ScreenPosition(); - screen.Library = "WINDOWS"; - screen.IsSpanned = false; - screen.Colour = normalScreenColor; // this is the default unless overridden by the primary screen - - UInt32 sourceId = path.SourceInfo.Id; - UInt32 targetId = path.TargetInfo.Id; - - - // Go through the screens as Windows knows them, and then enhance the info with Mosaic data if it applies - foreach (DISPLAYCONFIG_MODE_INFO displayMode in _windowsDisplayConfig.DisplayConfigModes) - { - // Find the matching Display Config Source Mode - if (displayMode.InfoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_SOURCE && displayMode.Id == sourceId) - { - screen.Name = targetId.ToString(); - //screen.DisplayConnector = displayMode.DisplayConnector; - screen.ScreenX = displayMode.SourceMode.Position.X; - screen.ScreenY = displayMode.SourceMode.Position.Y; - screen.ScreenWidth = (int)displayMode.SourceMode.Width; - screen.ScreenHeight = (int)displayMode.SourceMode.Height; - - // If we're at the 0,0 coordinate then we're the primary monitor - if (screen.ScreenX == 0 && screen.ScreenY == 0) - { - screen.IsPrimary = true; - screen.Colour = primaryScreenColor; - } - break; - } - } - - foreach (ADVANCED_HDR_INFO_PER_PATH hdrInfo in _windowsDisplayConfig.DisplayHDRStates) - { - // Find the matching HDR information - if (hdrInfo.Id == targetId) - { - // HDR information - if (hdrInfo.AdvancedColorInfo.AdvancedColorSupported) - { - screen.HDRSupported = true; - if (hdrInfo.AdvancedColorInfo.AdvancedColorEnabled) - { - screen.HDREnabled = true; - } - else - { - screen.HDREnabled = false; - } - - } - else - { - screen.HDRSupported = false; - screen.HDREnabled = false; - } - break; - } - } - - _screens.Add(screen); - } - } - - return _screens; - } - - // The object specific Equals - public bool Equals(WinProfileItem other) - { - // Check references - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - - // Check the object fields - return base.Equals(other) && - WindowsDisplayConfig == other.WindowsDisplayConfig; - } - - // The public override for the Object.Equals - public bool Equals(Object obj) - { - // Check references - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - // If different types then can't be true - if (obj.GetType() == this.GetType()) return false; - // Check the object fields as this must the same object as obj, and we need to test in more detail - return Equals((WinProfileItem)obj); - } - - // If Equals() returns true for this object compared to another - // then GetHashCode() must return the same value for these objects. - public override int GetHashCode() - { - // Calculate the hash code for the product. - return (base.GetHashCode(), WindowsDisplayConfig).GetHashCode(); - - } - - public static bool operator ==(WinProfileItem lhs, WinProfileItem rhs) - { - return Equals(lhs, rhs); - } - - public static bool operator !=(WinProfileItem lhs, WinProfileItem rhs) - { - return !Equals(lhs, rhs); - } - } - -} \ No newline at end of file diff --git a/displaymagician/uiforms/displayprofileform.cs b/displaymagician/uiforms/displayprofileform.cs index 8cdbaed..4792983 100644 --- a/displaymagician/uiforms/displayprofileform.cs +++ b/displaymagician/uiforms/displayprofileform.cs @@ -202,12 +202,9 @@ namespace DisplayMagician.UIForms // if the item was removed from the list during this // list refresh, then we select this profile only if it // is the currently used Profile - if (_selectedProfile is ProfileItem && _selectedProfile.Equals(profile)) + if (ProfileRepository.IsActiveProfile(_selectedProfile)) newItem.Selected = true; - - //ProfileRepository.ProfileValidityLookup[profile.Name] = profile.IsPossible; - // Add it to the list! ilv_saved_profiles.Items.Add(newItem, _profileAdaptor); @@ -351,14 +348,11 @@ namespace DisplayMagician.UIForms // We're in 'save' mode! // Check we're not already saving this profile - foreach (ProfileItem savedProfile in ProfileRepository.AllProfiles) - { - //if (String.Equals(txt_profile_save_name.Text, savedProfile.Name, StringComparison.InvariantCultureIgnoreCase)) - if (savedProfile.Equals(_selectedProfile)) - { - MessageBox.Show($"Sorry, this display profile was already saved as '{savedProfile.Name}'.", "Profile already saved", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } + string previouslySavedProfileName = ""; + if (ProfileRepository.ContainsCurrentProfile(out previouslySavedProfileName)) + { + MessageBox.Show($"Sorry, this display profile was already saved as '{previouslySavedProfileName}'.", "Profile already saved", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; } // So we've already passed the check that says this profile is unique @@ -434,10 +428,10 @@ namespace DisplayMagician.UIForms private void btn_view_current_Click(object sender, EventArgs e) { - // Reload the profiles in case we swapped to another program to change it - ProfileRepository.GetActiveProfile(); // Refresh the profiles to see whats valid ProfileRepository.IsPossibleRefresh(); + // Reload the profiles in case we swapped to another program to change it + ProfileRepository.UpdateActiveProfile(); // Change to the current selected Profile ChangeSelectedProfile(ProfileRepository.GetActiveProfile()); // Refresh the Profile UI @@ -460,7 +454,7 @@ namespace DisplayMagician.UIForms switch (m.Msg) { case WM_DISPLAYCHANGE: - ProfileRepository.UpdateActiveProfile(); + btn_view_current.PerformClick(); break; }