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!
This commit is contained in:
Terry MacDonald 2021-09-07 21:26:42 +12:00
parent c59f236524
commit f8e9eecf81
7 changed files with 991 additions and 1611 deletions

View File

@ -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<AMDProfileItem>
{
private static List<AMDProfileItem> _allSavedProfiles = new List<AMDProfileItem>();
private ProfileIcon _profileIcon;
private Bitmap _profileBitmap, _profileShortcutBitmap;
private List<string> _profileDisplayIdentifiers = new List<string>();
private List<ScreenPosition> _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<string> ProfileDisplayIdentifiers
{
get
{
if (_profileDisplayIdentifiers.Count == 0)
{
_profileDisplayIdentifiers = AMDLibrary.GetLibrary().GetCurrentDisplayIdentifiers();
}
return _profileDisplayIdentifiers;
}
set
{
if (value is List<string>)
_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<ScreenPosition> GetScreenPositions()
{
// Now we create the screens structure from the AMD profile information
_screens = new List<ScreenPosition>();
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);
}
}
}

View File

@ -53,10 +53,8 @@
<ItemGroup> <ItemGroup>
<Compile Include="AMD\ADL.cs" /> <Compile Include="AMD\ADL.cs" />
<Compile Include="AMD\AMDLibrary.cs" /> <Compile Include="AMD\AMDLibrary.cs" />
<Compile Include="AMD\AMDProfileItem.cs" />
<Compile Include="NVIDIA\NVAPI.cs" /> <Compile Include="NVIDIA\NVAPI.cs" />
<Compile Include="NVIDIA\NVIDIALibrary.cs" /> <Compile Include="NVIDIA\NVIDIALibrary.cs" />
<Compile Include="NVIDIA\NVIDIAProfileItem.cs" />
<Compile Include="NVIDIA\PInvokeDelegateFactory.cs" /> <Compile Include="NVIDIA\PInvokeDelegateFactory.cs" />
<Compile Include="SharedLogger.cs" /> <Compile Include="SharedLogger.cs" />
<Compile Include="Properties\Resources.Designer.cs"> <Compile Include="Properties\Resources.Designer.cs">
@ -86,7 +84,6 @@
<Compile Include="UserControls\DisplayView.Designer.cs"> <Compile Include="UserControls\DisplayView.Designer.cs">
<DependentUpon>DisplayView.cs</DependentUpon> <DependentUpon>DisplayView.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Windows\WinProfileItem.cs" />
<Compile Include="Windows\CCD.cs" /> <Compile Include="Windows\CCD.cs" />
<Compile Include="Windows\WinLibrary.cs" /> <Compile Include="Windows\WinLibrary.cs" />
<Compile Include="Wallpaper.cs" /> <Compile Include="Wallpaper.cs" />

View File

@ -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<NVIDIAProfileItem>
{
private static List<NVIDIAProfileItem> _allSavedProfiles = new List<NVIDIAProfileItem>();
private ProfileIcon _profileIcon;
private Bitmap _profileBitmap, _profileShortcutBitmap;
private List<string> _profileDisplayIdentifiers = new List<string>();
private List<ScreenPosition> _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<string> ProfileDisplayIdentifiers
{
get
{
if (_profileDisplayIdentifiers.Count == 0)
{
_profileDisplayIdentifiers = NVIDIALibrary.GetLibrary().GetCurrentDisplayIdentifiers();
}
return _profileDisplayIdentifiers;
}
set
{
if (value is List<string>)
_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<ScreenPosition> 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<ScreenPosition>();
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<string,SpannedScreenPosition> MosaicScreens = new Dictionary<string,SpannedScreenPosition>();
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<SpannedScreenPosition>();
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);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -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; 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) 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; 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 doesn't contain a profile called {profile.Name}");
{
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}");
return false; return false;
} }
@ -501,26 +490,12 @@ namespace DisplayMagicianShared
foreach (ProfileItem testProfile in _allProfiles) foreach (ProfileItem testProfile in _allProfiles)
{ {
// TODO - change for Equals if (testProfile.Equals(_currentProfile))
if (testProfile.VideoMode == VIDEO_MODE.NVIDIA && (testProfile as NVIDIAProfileItem).Equals(_currentProfile))
{ {
SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {testProfile.Name}"); SharedLogger.logger.Debug($"ProfileRepository/ContainsProfile: Our profile repository does contain a profile called {testProfile.Name}");
savedProfileName = testProfile.Name; savedProfileName = testProfile.Name;
return true; 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"); 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)."); 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."); 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) if (_currentVideoMode == VIDEO_MODE.NVIDIA)
{ {
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: NVIDIA NVAPI Driver is installed, so using that for this display profile."); 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", Name = "Current NVIDIA Display Profile",
VideoMode = VIDEO_MODE.NVIDIA
}; };
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;
} }
else if (_currentVideoMode == VIDEO_MODE.AMD) 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."); 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", 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 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."); profile = new ProfileItem
WinProfileItem winProfile = new WinProfileItem
{ {
Name = "Current Windows Display Profile", Name = "Current Windows Display Profile",
VideoMode = VIDEO_MODE.WINDOWS
}; };
winProfile.CreateProfileFromCurrentDisplaySettings(); }
profile.CreateProfileFromCurrentDisplaySettings();
if (_profilesLoaded && _allProfiles.Count > 0) if (_profilesLoaded && _allProfiles.Count > 0)
{ {
foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles) foreach (ProfileItem loadedProfile in ProfileRepository.AllProfiles)
{ {
if (winProfile.Equals(loadedProfile)) if (loadedProfile.Equals(profile))
{ {
_currentProfile = loadedProfile; _currentProfile = loadedProfile;
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The Windows profile {loadedProfile.Name} is currently active (in use now)."); SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The {loadedProfile.VideoMode.ToString("G")} profile '{loadedProfile.Name}' is currently active (in use now).");
return; return;
} }
} }
} }
SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current Windows profile is a new profile that doesn't already exist in the Profile Repository."); SharedLogger.logger.Debug($"ProfileRepository/UpdateActiveProfile: The current profile is a new profile that doesn't already exist in the Profile Repository.");
_currentProfile = winProfile; _currentProfile = profile;
}
} }
public static ProfileItem GetActiveProfile() public static ProfileItem GetActiveProfile()
@ -709,51 +657,14 @@ namespace DisplayMagicianShared
return false; return false;
} }
if (profile is NVIDIAProfileItem) if (profile.Equals(_currentProfile))
{ {
NVIDIAProfileItem nvidiaCurrentProfile = (NVIDIAProfileItem)_currentProfile; SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The profile {profile.Name} is the currently active profile.");
if (nvidiaCurrentProfile.Equals(profile))
{
SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The NVIDIA profile {profile.Name} is the currently active profile.");
return true; return true;
} }
else else
{ {
SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The NVIDIA profile {profile.Name} is the not currently active profile."); SharedLogger.logger.Debug($"ProfileRepository/IsActiveProfile: The profile {profile.Name} is the not currently active profile.");
return false;
}
}
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.");
return false; return false;
} }
} }
@ -795,10 +706,7 @@ namespace DisplayMagicianShared
SharedLogger.logger.Debug($"ProfileRepository/LoadProfiles: Finding the current profile in the Profile Repository"); SharedLogger.logger.Debug($"ProfileRepository/LoadProfiles: Finding the current profile in the Profile Repository");
// Update the current active profile /*// Go through all the profiles and set up the needed structures (such as the Screens list)
UpdateActiveProfile();
// Go through all the profiles and set up the needed structures (such as the Screens list)
// and check if the current profile is used // and check if the current profile is used
foreach (ProfileItem loadedProfile in _allProfiles) 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!"); SharedLogger.logger.Error($"ProfileRepository/LoadProfiles: ERROR - Profile {loadedProfile.Name} is not a recognised profile!");
} }
} }*/
// Sort the profiles alphabetically // Sort the profiles alphabetically
_allProfiles.Sort(); _allProfiles.Sort();
@ -842,7 +750,7 @@ namespace DisplayMagicianShared
else 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."); 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 else
@ -850,10 +758,12 @@ namespace DisplayMagicianShared
// If we get here, then we don't have any profiles saved! // If we get here, then we don't have any profiles saved!
// So we gotta start from scratch // So we gotta start from scratch
SharedLogger.logger.Debug($"ProfileRepository/LoadProfiles: Couldn't find the {_profileStorageJsonFileName} profile JSON file that contains the Profiles"); SharedLogger.logger.Debug($"ProfileRepository/LoadProfiles: Couldn't find the {_profileStorageJsonFileName} profile JSON file that contains the Profiles");
UpdateActiveProfile(); //UpdateActiveProfile();
} }
_profilesLoaded = true; _profilesLoaded = true;
// Update the current active profile
UpdateActiveProfile();
IsPossibleRefresh(); IsPossibleRefresh();
return true; return true;
@ -1021,9 +931,6 @@ namespace DisplayMagicianShared
public static ApplyProfileResult ApplyProfile(ProfileItem profile) public static ApplyProfileResult ApplyProfile(ProfileItem profile)
{ {
SharedLogger.logger.Trace($"Program/ApplyProfile: Starting"); SharedLogger.logger.Trace($"Program/ApplyProfile: Starting");
NVIDIAProfileItem nvidiaProfile = null;
AMDProfileItem amdProfile = null;
WinProfileItem winProfile = null;
// We try to time the profile display swap // We try to time the profile display swap
Stopwatch stopWatch = new Stopwatch(); Stopwatch stopWatch = new Stopwatch();
bool wasDisplayChangeSuccessful = true; bool wasDisplayChangeSuccessful = true;
@ -1040,39 +947,14 @@ namespace DisplayMagicianShared
stopWatch.Start(); stopWatch.Start();
// We try to swap profiles. The profiles have checking logic in them // 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"); SharedLogger.logger.Error($"ProfileRepository/ApplyProfile: Error applying the {profile.VideoMode.ToString("G")} Profile!");
nvidiaProfile = (NVIDIAProfileItem)profile;
if (!nvidiaProfile.SetActive())
{
SharedLogger.logger.Error($"ProfileRepository/ApplyProfile: Error applying the NVIDIA Profile!");
return ApplyProfileResult.Error; 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!");
}
}
else 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; return ApplyProfileResult.Error;
} }

View File

@ -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<WinProfileItem>
{
private static List<WinProfileItem> _allSavedProfiles = new List<WinProfileItem>();
private ProfileIcon _profileIcon;
private Bitmap _profileBitmap, _profileShortcutBitmap;
private List<string> _profileDisplayIdentifiers = new List<string>();
private List<ScreenPosition> _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<string> ProfileDisplayIdentifiers
{
get
{
if (_profileDisplayIdentifiers.Count == 0)
{
_profileDisplayIdentifiers = WinLibrary.GetLibrary().GetCurrentDisplayIdentifiers();
}
return _profileDisplayIdentifiers;
}
set
{
if (value is List<string>)
_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<ScreenPosition> 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<ScreenPosition>();
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);
}
}
}

View File

@ -202,12 +202,9 @@ namespace DisplayMagician.UIForms
// if the item was removed from the list during this // if the item was removed from the list during this
// list refresh, then we select this profile only if it // list refresh, then we select this profile only if it
// is the currently used Profile // is the currently used Profile
if (_selectedProfile is ProfileItem && _selectedProfile.Equals(profile)) if (ProfileRepository.IsActiveProfile(_selectedProfile))
newItem.Selected = true; newItem.Selected = true;
//ProfileRepository.ProfileValidityLookup[profile.Name] = profile.IsPossible;
// Add it to the list! // Add it to the list!
ilv_saved_profiles.Items.Add(newItem, _profileAdaptor); ilv_saved_profiles.Items.Add(newItem, _profileAdaptor);
@ -351,15 +348,12 @@ namespace DisplayMagician.UIForms
// We're in 'save' mode! // We're in 'save' mode!
// Check we're not already saving this profile // Check we're not already saving this profile
foreach (ProfileItem savedProfile in ProfileRepository.AllProfiles) string previouslySavedProfileName = "";
if (ProfileRepository.ContainsCurrentProfile(out previouslySavedProfileName))
{ {
//if (String.Equals(txt_profile_save_name.Text, savedProfile.Name, StringComparison.InvariantCultureIgnoreCase)) MessageBox.Show($"Sorry, this display profile was already saved as '{previouslySavedProfileName}'.", "Profile already saved", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (savedProfile.Equals(_selectedProfile))
{
MessageBox.Show($"Sorry, this display profile was already saved as '{savedProfile.Name}'.", "Profile already saved", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
}
// So we've already passed the check that says this profile is unique // 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) 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 // Refresh the profiles to see whats valid
ProfileRepository.IsPossibleRefresh(); ProfileRepository.IsPossibleRefresh();
// Reload the profiles in case we swapped to another program to change it
ProfileRepository.UpdateActiveProfile();
// Change to the current selected Profile // Change to the current selected Profile
ChangeSelectedProfile(ProfileRepository.GetActiveProfile()); ChangeSelectedProfile(ProfileRepository.GetActiveProfile());
// Refresh the Profile UI // Refresh the Profile UI
@ -460,7 +454,7 @@ namespace DisplayMagician.UIForms
switch (m.Msg) switch (m.Msg)
{ {
case WM_DISPLAYCHANGE: case WM_DISPLAYCHANGE:
ProfileRepository.UpdateActiveProfile(); btn_view_current.PerformClick();
break; break;
} }