diff --git a/HeliosPlus.Shared/ProfileRepository.cs b/HeliosPlus.Shared/ProfileRepository.cs index 5c21223..309c235 100644 --- a/HeliosPlus.Shared/ProfileRepository.cs +++ b/HeliosPlus.Shared/ProfileRepository.cs @@ -532,120 +532,6 @@ namespace HeliosPlus.Shared }; } - - public static bool ApplyProfile(ProfileItem profile) - { - try - { - - Debug.Print("Begin profile change"); - Thread.Sleep(2000); - ApplyTopos(profile); - - Debug.Print("Finished setting topologies"); - Debug.Print("Sleep"); - Thread.Sleep(18000); - Debug.Print("Awake"); - - ApplyPathInfos(profile); - - Debug.Print("Applying pathInfos"); - Debug.Print("Sleep"); - Thread.Sleep(10000); - Debug.Print("Awake"); - - UpdateCurrentProfile(); - - return true; - } - catch (Exception ex) - { - Console.WriteLine($"ProfileRepository/ApplyProfile exception: {ex.Message}: {ex.InnerException}"); - UpdateCurrentProfile(); - Console.WriteLine($"Profile: Problem applying the '{profile.Name}' Display Profile: {ex.Message}"); - MessageBox.Show($"Problem applying the '{profile.Name}' Display Profile! \n(ex.Message)", $"Problem applying '{profile.Name}' Profile", MessageBoxButtons.OK, MessageBoxIcon.Error); - - return false; - } - } - - public static void ApplyTopos(ProfileItem profile) - { - Debug.Print("_applyTopos()"); - try - { - var surroundTopologies = - profile.Viewports.SelectMany(viewport => viewport.TargetDisplays) - .Select(target => target.SurroundTopology) - .Where(topology => topology != null) - .Select(topology => topology.ToGridTopology()) - .ToArray(); - - if (surroundTopologies.Length == 0) - { - var currentTopologies = GridTopology.GetGridTopologies(); - - if (currentTopologies.Any(topology => topology.Rows * topology.Columns > 1)) - { - surroundTopologies = - GridTopology.GetGridTopologies() - .SelectMany(topology => topology.Displays) - .Select(displays => new GridTopology(1, 1, new[] { displays })) - .ToArray(); - } - } - - if (surroundTopologies.Length > 0) - { - GridTopology.SetGridTopologies(surroundTopologies, SetDisplayTopologyFlag.MaximizePerformance); - } - } - catch (Exception ex) - { - Console.WriteLine($"ProfileItem/ApplyTopos exception: {ex.Message}: {ex.InnerException}"); - // ignored - } - } - - public static void ApplyPathInfos(ProfileItem profile) - { - Debug.Print("_applyPathInfos()"); - if (!profile.IsPossible) - { - throw new InvalidOperationException( - $"Problem applying the '{profile.Name}' Display Profile! The display configuration changed since this profile is created. Please re-create this profile."); - } - - var pathInfos = profile.Viewports.Select(viewport => viewport.ToPathInfo()).Where(info => info != null).ToArray(); - PathInfo.ApplyPathInfos(pathInfos, true, true, true); - } - - /* public static IDictionary applyProfileActions(ProfileItem profile) - { - var dict = new Dictionary() - { - { "Applying_Topos", ApplyTopos(profile) }, - { "Applying_Paths", ApplyPathInfos(profile) } - }; - return dict; - } - - public static IDictionary applyProfileMsgs() - { - var dict = new Dictionary() - { - { "Applying_Topos", Language.Applying_First_Message }, - { "Applying_Paths", Language.Applying_Second_Message } - }; - return dict; - } - - public static List applyProfileSequence() - { - var list = new List() { "Applying_Topos", "Applying_Paths" }; - return list; - }*/ - public static bool IsValidFilename(string testName) { string strTheseAreInvalidFileNameChars = new string(Path.GetInvalidFileNameChars()); diff --git a/HeliosPlus/ShortcutItem.cs b/HeliosPlus/ShortcutItem.cs index 89ecd7e..e543b05 100644 --- a/HeliosPlus/ShortcutItem.cs +++ b/HeliosPlus/ShortcutItem.cs @@ -43,11 +43,13 @@ namespace HeliosPlus public class ShortcutItem { - private static List _allSavedShortcuts = new List(); - private MultiIcon _shortcutIcon, _originalIcon = null; + //private static List _allSavedShortcuts = new List(); + //private MultiIcon _shortcutIcon, _originalIcon = null; private Bitmap _shortcutBitmap, _originalBitmap = null; private ProfileItem _profileToUse = null; - private string _originalIconPath = "", _savedShortcutIconCacheFilename = "", _uuid = ""; + private string _originalIconPath = ""; + //private string _savedShortcutIconCacheFilename = ""; + private string _uuid = ""; private string _name = ""; //private uint _id = 0; private string _profileUuid = ""; @@ -62,7 +64,10 @@ namespace HeliosPlus ProfileToUse = profile; } - public static Version Version = new Version(1, 0); + public static Version Version + { + get => new Version(1, 0); + } public string UUID { @@ -147,7 +152,7 @@ namespace HeliosPlus // We try to find and set the ProfileTouse foreach (ProfileItem profileToTest in ProfileRepository.AllProfiles) { - if (profileToTest.UUID.Equals(_profileUuid)) + if (profileToTest.UUID.Equals(_profileUuid, StringComparison.InvariantCultureIgnoreCase)) _profileToUse = profileToTest; } } diff --git a/HeliosPlus/ShortcutRepository.cs b/HeliosPlus/ShortcutRepository.cs index b67cd82..6f5c941 100644 --- a/HeliosPlus/ShortcutRepository.cs +++ b/HeliosPlus/ShortcutRepository.cs @@ -2,16 +2,15 @@ using HeliosPlus.InterProcess; using HeliosPlus.Resources; using HeliosPlus.Shared; -using HeliosPlus.UIForms; using Newtonsoft.Json; +using NvAPIWrapper.Mosaic; +using NvAPIWrapper.Native.Mosaic; using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing.IconLib; -using System.Globalization; using System.IO; using System.Linq; -using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Threading; @@ -26,7 +25,6 @@ namespace HeliosPlus #region Class Variables // Common items to the class private static List _allShortcuts = null; - public static Version Version = new Version(1, 0, 0); // Other constants that are useful private static string _shortcutStorageJsonPath = Path.Combine(Program.AppDataPath, $"Shortcuts"); private static string _shortcutStorageJsonFileName = Path.Combine(_shortcutStorageJsonPath, $"Shortcuts_{Version.ToString(2)}.json"); @@ -72,6 +70,11 @@ namespace HeliosPlus } } + public static Version Version + { + get => new Version(1, 0, 0); + } + #endregion #region Class Methods @@ -116,7 +119,7 @@ namespace HeliosPlus return false; // Remove the Shortcut Icons from the Cache - List shortcutsToRemove = _allShortcuts.FindAll(item => item.UUID.Equals(shortcut.UUID)); + List shortcutsToRemove = _allShortcuts.FindAll(item => item.UUID.Equals(shortcut.UUID, StringComparison.InvariantCultureIgnoreCase)); foreach (ShortcutItem shortcutToRemove in shortcutsToRemove) { try @@ -132,7 +135,7 @@ namespace HeliosPlus } // Remove the shortcut from the list. - int numRemoved = _allShortcuts.RemoveAll(item => item.UUID.Equals(shortcut.UUID)); + int numRemoved = _allShortcuts.RemoveAll(item => item.UUID.Equals(shortcut.UUID, StringComparison.InvariantCultureIgnoreCase)); if (numRemoved == 1) { @@ -158,13 +161,13 @@ namespace HeliosPlus Match match = Regex.Match(shortcutNameOrUuid, uuidV4Regex, RegexOptions.IgnoreCase); if (match.Success) { - shortcutsToRemove = _allShortcuts.FindAll(item => item.UUID.Equals(shortcutNameOrUuid)); - numRemoved = _allShortcuts.RemoveAll(item => item.UUID.Equals(shortcutNameOrUuid)); + shortcutsToRemove = _allShortcuts.FindAll(item => item.UUID.Equals(shortcutNameOrUuid, StringComparison.InvariantCultureIgnoreCase)); + numRemoved = _allShortcuts.RemoveAll(item => item.UUID.Equals(shortcutNameOrUuid, StringComparison.InvariantCultureIgnoreCase)); } else { - shortcutsToRemove = _allShortcuts.FindAll(item => item.Name.Equals(shortcutNameOrUuid)); - numRemoved = _allShortcuts.RemoveAll(item => item.Name.Equals(shortcutNameOrUuid)); + shortcutsToRemove = _allShortcuts.FindAll(item => item.Name.Equals(shortcutNameOrUuid, StringComparison.InvariantCultureIgnoreCase)); + numRemoved = _allShortcuts.RemoveAll(item => item.Name.Equals(shortcutNameOrUuid, StringComparison.InvariantCultureIgnoreCase)); } // Remove the Shortcut Icons from the Cache foreach (ShortcutItem shortcutToRemove in shortcutsToRemove) @@ -201,7 +204,7 @@ namespace HeliosPlus foreach (ShortcutItem testShortcut in _allShortcuts) { - if (testShortcut.UUID.Equals(shortcut.UUID)) + if (testShortcut.UUID.Equals(shortcut.UUID,StringComparison.InvariantCultureIgnoreCase)) return true; } @@ -220,7 +223,7 @@ namespace HeliosPlus { foreach (ShortcutItem testShortcut in _allShortcuts) { - if (testShortcut.UUID.Equals(shortcutNameOrUuid)) + if (testShortcut.UUID.Equals(shortcutNameOrUuid, StringComparison.InvariantCultureIgnoreCase)) return true; } @@ -229,7 +232,7 @@ namespace HeliosPlus { foreach (ShortcutItem testShortcut in _allShortcuts) { - if (testShortcut.Name.Equals(shortcutNameOrUuid)) + if (testShortcut.Name.Equals(shortcutNameOrUuid, StringComparison.InvariantCultureIgnoreCase)) return true; } @@ -251,7 +254,7 @@ namespace HeliosPlus { foreach (ShortcutItem testShortcut in _allShortcuts) { - if (testShortcut.UUID.Equals(shortcutNameOrUuid)) + if (testShortcut.UUID.Equals(shortcutNameOrUuid, StringComparison.InvariantCultureIgnoreCase)) return testShortcut; } @@ -260,7 +263,7 @@ namespace HeliosPlus { foreach (ShortcutItem testShortcut in _allShortcuts) { - if (testShortcut.Name.Equals(shortcutNameOrUuid)) + if (testShortcut.Name.Equals(shortcutNameOrUuid, StringComparison.InvariantCultureIgnoreCase)) return testShortcut; } @@ -277,7 +280,7 @@ namespace HeliosPlus foreach (ShortcutItem testShortcut in ShortcutRepository.AllShortcuts) { - if (testShortcut.ProfileUUID.Equals(newProfile.UUID) && testShortcut.AutoName) + if (testShortcut.ProfileUUID.Equals(newProfile.UUID, StringComparison.InvariantCultureIgnoreCase) && testShortcut.AutoName) { testShortcut.ProfileToUse = newProfile; testShortcut.AutoSuggestShortcutName(); @@ -446,18 +449,22 @@ namespace HeliosPlus { // Do some validation to make sure the shortcut is sensible // And that we have enough to try and action within the shortcut + // including checking the Profile in the shortcut is possible // (in other words check everything in the shortcut is still valid) + if (!(shortcutToUse is ShortcutItem)) + return; + (bool valid, string reason) = shortcutToUse.IsValid(); if (!valid) { - throw new Exception(string.Format("Unable to run the shortcut '{0}': {1}", shortcutToUse.Name, reason)); + throw new Exception(string.Format("ShortcutRepository/SaveShortcutIconToCache exception: Unable to run the shortcut '{0}': {1}", shortcutToUse.Name, reason)); } // Remember the profile we are on now ProfileItem rollbackProfile = ProfileRepository.CurrentProfile; - // Try to change to the wanted profile - if (!ProfileRepository.ApplyProfile(shortcutToUse.ProfileToUse)) + // Apply the Profile! + if (!ApplyProfile(shortcutToUse.ProfileToUse)) { throw new Exception(Language.Cannot_change_active_profile); } @@ -664,7 +671,7 @@ namespace HeliosPlus // Change back to the original profile if it is different if (!ProfileRepository.IsActiveProfile(rollbackProfile)) { - if (!ProfileRepository.ApplyProfile(rollbackProfile)) + if (!ApplyProfile(rollbackProfile)) { throw new Exception(Language.Cannot_change_active_profile); } @@ -672,7 +679,7 @@ namespace HeliosPlus } - private static bool ChangeToProfile(ProfileItem profile) + private static bool ApplyProfile(ProfileItem profile) { // If we're already on the wanted profile then no need to change! if (ProfileRepository.IsActiveProfile(profile)) @@ -685,25 +692,21 @@ namespace HeliosPlus IPCService.GetInstance().Status = InstanceStatus.Busy; var failed = false; - if (new ApplyingChangesForm(() => + // Now lets start by changing the display topology + Task applyProfileTopologyTask = Task.Run(() => { - Task.Factory.StartNew(() => - { - if (!(ProfileRepository.ApplyProfile(profile))) - { - failed = true; - } - }, TaskCreationOptions.LongRunning); - }, 3, 30).ShowDialog() != - DialogResult.Cancel) - { - if (failed) - { - throw new Exception(Language.Profile_is_invalid_or_not_possible_to_apply); - } + Console.WriteLine("ShortcutRepository/SaveShortcutIconToCache : Applying Profile Topology" + profile.Name); + ApplyTopology(profile); + }); + applyProfileTopologyTask.Wait(); - return true; - } + // And then change the path information + Task applyProfilePathInfoTask = Task.Run(() => + { + Console.WriteLine("ShortcutRepository/SaveShortcutIconToCache : Applying Profile Topology" + profile.Name); + ApplyPathInfo(profile); + }); + applyProfilePathInfoTask.Wait(); return false; } @@ -712,6 +715,64 @@ namespace HeliosPlus IPCService.GetInstance().Status = instanceStatus; } } + + public static void ApplyTopology(ProfileItem profile) + { + Debug.Print("ShortcutRepository.ApplyTopology()"); + if (profile == null) + return; + + try + { + var surroundTopologies = + profile.Viewports.SelectMany(viewport => viewport.TargetDisplays) + .Select(target => target.SurroundTopology) + .Where(topology => topology != null) + .Select(topology => topology.ToGridTopology()) + .ToArray(); + + if (surroundTopologies.Length == 0) + { + var currentTopologies = GridTopology.GetGridTopologies(); + + if (currentTopologies.Any(topology => topology.Rows * topology.Columns > 1)) + { + surroundTopologies = + GridTopology.GetGridTopologies() + .SelectMany(topology => topology.Displays) + .Select(displays => new GridTopology(1, 1, new[] { displays })) + .ToArray(); + } + } + + if (surroundTopologies.Length > 0) + { + GridTopology.SetGridTopologies(surroundTopologies, SetDisplayTopologyFlag.MaximizePerformance); + } + } + catch (Exception ex) + { + Console.WriteLine($"ShortcutRepository/ApplyTopology exception: {ex.Message}: {ex.InnerException}"); + // ignored + } + } + + public static void ApplyPathInfo(ProfileItem profile) + { + Debug.Print("ShortcutRepository.ApplyPathInfo()"); + if (profile == null) + return; + + if (!profile.IsPossible) + { + throw new InvalidOperationException( + $"ShortcutRepository/ApplyPathInfo exception: Problem applying the '{profile.Name}' Display Profile! The display configuration changed since this profile is created. Please re-create this profile."); + } + + var pathInfos = profile.Viewports.Select(viewport => viewport.ToPathInfo()).Where(info => info != null).ToArray(); + WindowsDisplayAPI.DisplayConfig.PathInfo.ApplyPathInfos(pathInfos, true, true, true); + } + #endregion } diff --git a/HeliosPlus/UIForms/DisplayProfileForm.cs b/HeliosPlus/UIForms/DisplayProfileForm.cs index fbc88b5..0878b2f 100644 --- a/HeliosPlus/UIForms/DisplayProfileForm.cs +++ b/HeliosPlus/UIForms/DisplayProfileForm.cs @@ -42,6 +42,7 @@ namespace HeliosPlus.UIForms return; } + // Need to move this logic to the Shortcut Repository or the Profile Repository. /*IDictionary actions = dv_profile.Profile.applyProfileActions(); IDictionary messages = dv_profile.Profile.applyProfileMsgs(); List sequence = dv_profile.Profile.applyProfileSequence(); @@ -70,10 +71,9 @@ namespace HeliosPlus.UIForms Activate();*/ - - } + private void Exit_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; diff --git a/HeliosPlus/UIForms/ShortcutLibraryForm.cs b/HeliosPlus/UIForms/ShortcutLibraryForm.cs index a4f95d5..6f74d08 100644 --- a/HeliosPlus/UIForms/ShortcutLibraryForm.cs +++ b/HeliosPlus/UIForms/ShortcutLibraryForm.cs @@ -21,7 +21,7 @@ namespace HeliosPlus.UIForms { private ShortcutAdaptor _shortcutAdaptor = new ShortcutAdaptor(); - private ImageListViewItem _selectedShortcutILVItem = null; + //private ImageListViewItem _selectedShortcutILVItem = null; private ShortcutItem _selectedShortcut = null; public ShortcutLibraryForm()