From eb93e84e221019c076a0c39245bd487a4462f9ef Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Thu, 3 Dec 2020 21:45:24 +1300 Subject: [PATCH] Added audiodevice logic to save it Added the ability to store the audio device information within the ShortcutItem, and also added the logic to the ShortcutForm to correctly load and save the audio device settings. Also coped with the edge case of when an audio device is turned off or unplugged when the user edits the shortcut... in that case we want to still keep the old audio device settings as it is likely to be plugged back in when the shortcut is actually run. --- DisplayMagician/ShortcutItem.cs | 148 ++++++++++++++++++++++-- DisplayMagician/UIForms/MainForm.resx | 2 +- DisplayMagician/UIForms/ShortcutForm.cs | 129 +++++++++++---------- 3 files changed, 207 insertions(+), 72 deletions(-) diff --git a/DisplayMagician/ShortcutItem.cs b/DisplayMagician/ShortcutItem.cs index 243f365..1b53ba5 100644 --- a/DisplayMagician/ShortcutItem.cs +++ b/DisplayMagician/ShortcutItem.cs @@ -80,6 +80,8 @@ namespace DisplayMagician private uint _startTimeout; private string _gameArguments; private bool _gameArgumentsRequired; + private string _audioDevice; + private bool _changeAudioDevice; private ShortcutPermanence _permanence = ShortcutPermanence.Temporary; private bool _autoName = true; private bool _isPossible; @@ -114,6 +116,7 @@ namespace DisplayMagician ProfileItem profile, ShortcutPermanence permanence, string originalIconPath, + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "" @@ -124,6 +127,12 @@ namespace DisplayMagician _name = name; _category = ShortcutCategory.NoGame; _profileToUse = profile; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -144,13 +153,19 @@ namespace DisplayMagician } public ShortcutItem(string name, string profileUuid, ShortcutPermanence permanence, string originalIconPath, - List startPrograms = null, bool autoName = true, string uuid = "") : this() + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "") : this() { if (!String.IsNullOrWhiteSpace(uuid)) _uuid = uuid; _name = name; _profileUuid = profileUuid; _category = ShortcutCategory.NoGame; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -194,6 +209,7 @@ namespace DisplayMagician bool gameArgumentsRequired, ShortcutPermanence permanence, string originalIconPath, + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "" @@ -210,6 +226,12 @@ namespace DisplayMagician _startTimeout = gameTimeout; _gameArguments = gameArguments; _gameArgumentsRequired = gameArgumentsRequired; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -230,7 +252,7 @@ namespace DisplayMagician } public ShortcutItem(string name, ProfileItem profile, GameStruct game, ShortcutPermanence permanence, string originalIconPath, - List startPrograms = null, bool autoName = true, string uuid = "") : this() + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "") : this() { // Create a new UUID for the shortcut if one wasn't created already if (!String.IsNullOrWhiteSpace(uuid)) @@ -244,6 +266,11 @@ namespace DisplayMagician _startTimeout = game.StartTimeout; _gameArguments = game.GameArguments; _gameArgumentsRequired = game.GameArgumentsRequired; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -264,7 +291,7 @@ namespace DisplayMagician public ShortcutItem(string name, string profileUuid, GameStruct game, ShortcutPermanence permanence, string originalIconPath, - List startPrograms = null, bool autoName = true, string uuid = "") : this() + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "") : this() { if (!String.IsNullOrWhiteSpace(uuid)) _uuid = uuid; @@ -278,6 +305,11 @@ namespace DisplayMagician _gameArguments = game.GameArguments; _gameArgumentsRequired = game.GameArgumentsRequired; _gameArgumentsRequired = false; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -319,6 +351,7 @@ namespace DisplayMagician bool processNameToMonitorUsesExecutable, ShortcutPermanence permanence, string originalIconPath, + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "" @@ -335,6 +368,11 @@ namespace DisplayMagician _executableArguments = executableArguments; _executableArgumentsRequired = executableArgumentsRequired; _processNameToMonitorUsesExecutable = processNameToMonitorUsesExecutable; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -355,7 +393,7 @@ namespace DisplayMagician } public ShortcutItem(string name, ProfileItem profile, Executable executable, ShortcutPermanence permanence, string originalIconPath, - List startPrograms = null, bool autoName = true, string uuid = "") : this() + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "") : this() { if (!String.IsNullOrWhiteSpace(uuid)) _uuid = uuid; @@ -368,6 +406,11 @@ namespace DisplayMagician _executableArguments = executable.ExecutableArguments; _executableArgumentsRequired = executable.ExecutableArgumentsRequired; _processNameToMonitorUsesExecutable = executable.ProcessNameToMonitorUsesExecutable; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -387,7 +430,7 @@ namespace DisplayMagician } public ShortcutItem(string name, string profileUuid, Executable executable, ShortcutPermanence permanence, string originalIconPath, - List startPrograms = null, bool autoName = true, string uuid = "") : this() + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "") : this() { if (!String.IsNullOrWhiteSpace(uuid)) _uuid = uuid; @@ -400,6 +443,11 @@ namespace DisplayMagician _executableArguments = executable.ExecutableArguments; _executableArgumentsRequired = executable.ExecutableArgumentsRequired; _processNameToMonitorUsesExecutable = executable.ProcessNameToMonitorUsesExecutable; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -701,6 +749,33 @@ namespace DisplayMagician } } + public string AudioDevice + { + get + { + return _audioDevice; + } + + set + { + _audioDevice = value; + } + } + + public bool ChangeAudioDevice + { + get + { + return _changeAudioDevice; + } + + set + { + _changeAudioDevice = value; + } + } + + public List StartPrograms { get @@ -792,6 +867,7 @@ namespace DisplayMagician ProfileItem profile, ShortcutPermanence permanence, string originalIconPath, + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "" @@ -802,6 +878,12 @@ namespace DisplayMagician _name = name; _category = ShortcutCategory.NoGame; _profileToUse = profile; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -823,13 +905,19 @@ namespace DisplayMagician } public void UpdateNoGameShortcut(string name, string profileUuid, ShortcutPermanence permanence, string originalIconPath, - List startPrograms = null, bool autoName = true, string uuid = "") + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "") { if (!String.IsNullOrWhiteSpace(uuid)) _uuid = uuid; _name = name; _profileUuid = profileUuid; _category = ShortcutCategory.NoGame; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -875,6 +963,7 @@ namespace DisplayMagician bool gameArgumentsRequired, ShortcutPermanence permanence, string originalIconPath, + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "" @@ -891,6 +980,12 @@ namespace DisplayMagician _startTimeout = gameTimeout; _gameArguments = gameArguments; _gameArgumentsRequired = gameArgumentsRequired; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -912,7 +1007,7 @@ namespace DisplayMagician } public void UpdateGameShortcut(string name, ProfileItem profile, GameStruct game, ShortcutPermanence permanence, string originalIconPath, - List startPrograms = null, bool autoName = true, string uuid = "") + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "") { // Create a new UUID for the shortcut if one wasn't created already if (!String.IsNullOrWhiteSpace(uuid)) @@ -926,6 +1021,12 @@ namespace DisplayMagician _startTimeout = game.StartTimeout; _gameArguments = game.GameArguments; _gameArgumentsRequired = game.GameArgumentsRequired; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -948,7 +1049,7 @@ namespace DisplayMagician public void UpdateGameShortcut(string name, string profileUuid, GameStruct game, ShortcutPermanence permanence, string originalIconPath, - List startPrograms = null, bool autoName = true, string uuid = "") + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "") { if (!String.IsNullOrWhiteSpace(uuid)) _uuid = uuid; @@ -962,6 +1063,12 @@ namespace DisplayMagician _gameArguments = game.GameArguments; _gameArgumentsRequired = game.GameArgumentsRequired; _gameArgumentsRequired = false; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -1005,6 +1112,7 @@ namespace DisplayMagician bool processNameToMonitorUsesExecutable, ShortcutPermanence permanence, string originalIconPath, + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "" @@ -1021,6 +1129,12 @@ namespace DisplayMagician _executableArguments = executableArguments; _executableArgumentsRequired = executableArgumentsRequired; _processNameToMonitorUsesExecutable = processNameToMonitorUsesExecutable; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -1042,7 +1156,7 @@ namespace DisplayMagician } public void UpdateExecutableShortcut(string name, ProfileItem profile, Executable executable, ShortcutPermanence permanence, string originalIconPath, - List startPrograms = null, bool autoName = true, string uuid = "") + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "") { if (!String.IsNullOrWhiteSpace(uuid)) _uuid = uuid; @@ -1055,6 +1169,12 @@ namespace DisplayMagician _executableArguments = executable.ExecutableArguments; _executableArgumentsRequired = executable.ExecutableArgumentsRequired; _processNameToMonitorUsesExecutable = executable.ProcessNameToMonitorUsesExecutable; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -1076,7 +1196,7 @@ namespace DisplayMagician } public void UpdateExecutableShortcut(string name, string profileUuid, Executable executable, ShortcutPermanence permanence, string originalIconPath, - List startPrograms = null, bool autoName = true, string uuid = "") + string audioDevice = "", List startPrograms = null, bool autoName = true, string uuid = "") { if (!String.IsNullOrWhiteSpace(uuid)) _uuid = uuid; @@ -1089,6 +1209,12 @@ namespace DisplayMagician _executableArguments = executable.ExecutableArguments; _executableArgumentsRequired = executable.ExecutableArgumentsRequired; _processNameToMonitorUsesExecutable = executable.ProcessNameToMonitorUsesExecutable; + if (String.IsNullOrEmpty(audioDevice)) + _changeAudioDevice = false; + else + _changeAudioDevice = true; + + _audioDevice = audioDevice; _permanence = permanence; _autoName = autoName; _startPrograms = startPrograms; @@ -1165,6 +1291,8 @@ namespace DisplayMagician shortcut.SavedShortcutIconCacheFilename = SavedShortcutIconCacheFilename; shortcut.IsPossible = IsPossible; shortcut.StartPrograms = StartPrograms; + shortcut.ChangeAudioDevice = ChangeAudioDevice; + shortcut.AudioDevice = AudioDevice; // Save the shortcut incon to the icon cache shortcut.ReplaceShortcutIconInCache(); diff --git a/DisplayMagician/UIForms/MainForm.resx b/DisplayMagician/UIForms/MainForm.resx index 3456ebe..8a921a3 100644 --- a/DisplayMagician/UIForms/MainForm.resx +++ b/DisplayMagician/UIForms/MainForm.resx @@ -10840,7 +10840,7 @@ NoControl - 698, 355 + 700, 352 75, 23 diff --git a/DisplayMagician/UIForms/ShortcutForm.cs b/DisplayMagician/UIForms/ShortcutForm.cs index 465e3ea..12da21e 100644 --- a/DisplayMagician/UIForms/ShortcutForm.cs +++ b/DisplayMagician/UIForms/ShortcutForm.cs @@ -27,6 +27,8 @@ namespace DisplayMagician.UIForms private Executable _executableToUse; private ShortcutPermanence _permanence = ShortcutPermanence.Temporary; List _startPrograms = new List(); + private string _audioDevice = ""; + private bool _changeAudioDevice = false; private ShortcutItem _shortcutToEdit = null; List allGames = new List(); private bool _isUnsaved = true; @@ -256,17 +258,6 @@ namespace DisplayMagician.UIForms return; } - /*// Please use a plain name that can be - if (ShortcutRepository.ContainsShortcut(txt_shortcut_save_name.Text)) - { - MessageBox.Show( - @"A shortcut has already been created with this name. Please enter a different name for this shortcut.", - @"Please rename this Shortcut.", - MessageBoxButtons.OK, - MessageBoxIcon.Exclamation); - return; - }*/ - // Check the profile is set and that it's still valid if (!(_profileToUse is ProfileItem)) { @@ -365,6 +356,15 @@ namespace DisplayMagician.UIForms } + // Save the Audio features + if (rb_change_audio.Checked) + { + _changeAudioDevice = true; + _audioDevice = cb_audio_device.Text; + } + else + _changeAudioDevice = false; + // Check the permanence requirements @@ -425,22 +425,13 @@ namespace DisplayMagician.UIForms _gameToUse.GameArguments = txt_args_game.Text; _gameToUse.GameArgumentsRequired = cb_args_game.Checked; - /* _shortcutToEdit = new ShortcutItem( - txt_shortcut_save_name.Text, - _profileToUse, - _gameToUse, - _permanence, - _gameToUse.GameToPlay.IconPath, - _startPrograms, - _autoName, - _uuid - );*/ _shortcutToEdit.UpdateGameShortcut( txt_shortcut_save_name.Text, _profileToUse, _gameToUse, _permanence, _gameToUse.GameToPlay.IconPath, + _audioDevice, _startPrograms, _autoName, _uuid @@ -457,22 +448,13 @@ namespace DisplayMagician.UIForms _gameToUse.GameArguments = txt_args_game.Text; _gameToUse.GameArgumentsRequired = cb_args_game.Checked; - /*_shortcutToEdit = new ShortcutItem( - txt_shortcut_save_name.Text, - _profileToUse, - _gameToUse, - _permanence, - _gameToUse.GameToPlay.IconPath, - _startPrograms, - _autoName, - _uuid - );*/ _shortcutToEdit.UpdateGameShortcut( txt_shortcut_save_name.Text, _profileToUse, _gameToUse, _permanence, _gameToUse.GameToPlay.IconPath, + _audioDevice, _startPrograms, _autoName, _uuid @@ -498,21 +480,13 @@ namespace DisplayMagician.UIForms _executableToUse.ProcessNameToMonitorUsesExecutable = true; } - /*_shortcutToEdit = new ShortcutItem( - txt_shortcut_save_name.Text, - _profileToUse, - _executableToUse, - _permanence, - _executableToUse.ExecutableNameAndPath, - _startPrograms, - _autoName - );*/ _shortcutToEdit.UpdateExecutableShortcut( txt_shortcut_save_name.Text, _profileToUse, _executableToUse, _permanence, _executableToUse.ExecutableNameAndPath, + _audioDevice, _startPrograms, _autoName ); @@ -521,20 +495,12 @@ namespace DisplayMagician.UIForms else { -/* _shortcutToEdit = new ShortcutItem( - txt_shortcut_save_name.Text, - _profileToUse, - _permanence, - _executableToUse.ExecutableNameAndPath, - _startPrograms, - _autoName - - ); -*/ _shortcutToEdit.UpdateNoGameShortcut( + _shortcutToEdit.UpdateNoGameShortcut( txt_shortcut_save_name.Text, _profileToUse, _permanence, _executableToUse.ExecutableNameAndPath, + _audioDevice, _startPrograms, _autoName ); @@ -714,6 +680,7 @@ namespace DisplayMagician.UIForms if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Active) { int index = cb_audio_device.Items.Add(audioDevice.FullName); + // Set the audio device to the default device by default if (audioDevice.IsDefaultDevice) cb_audio_device.SelectedIndex = index; } @@ -821,17 +788,6 @@ namespace DisplayMagician.UIForms } } - - // If it is a new Shortcut then we don't have to load anything! - //if (_isNewShortcut) - //{ - // RefreshShortcutUI(); - // ChangeSelectedProfile(ProfileRepository.CurrentProfile); - // _isUnsaved = true; - // return; - //} - - // Now start populating the other fields if they need it _uuid = _shortcutToEdit.UUID; @@ -859,6 +815,35 @@ namespace DisplayMagician.UIForms break; } + // Set the Audio device to the shortcut audio device only if + // the Change Audio radiobutton is set + rb_change_audio.Checked = _shortcutToEdit.ChangeAudioDevice; + if (_shortcutToEdit.ChangeAudioDevice) + { + bool foundAudioDevice = false; + for (int i = 0; i < cb_audio_device.Items.Count; i++) + { + if (cb_audio_device.Items[i].Equals(_shortcutToEdit.AudioDevice)) + { + cb_audio_device.SelectedIndex = i; + foundAudioDevice = true; + break; + } + + } + // If we have a saved Audio device which isn't plugged in + // or isn't turned on right now, we don't want to lose the + // information, as the user has specifically set that audio device + // and they may plug it in when it comes time to use the shortcut + // So we add the audiodevice as an extra selection in this case. + if (!foundAudioDevice) + { + int index = cb_audio_device.Items.Add(_shortcutToEdit.AudioDevice); + cb_audio_device.SelectedIndex = index; + } + } + + // Set the launcher items if we have them txt_game_launcher.Text = _shortcutToEdit.GameLibrary.ToString(); txt_game_name.Text = _shortcutToEdit.GameName; @@ -1502,6 +1487,28 @@ namespace DisplayMagician.UIForms } } + bool foundAudioDevice = false; + for (int i = 0; i < cb_audio_device.Items.Count; i++) + { + if (cb_audio_device.Items[i].Equals(_shortcutToEdit.AudioDevice)) + { + cb_audio_device.SelectedIndex = i; + foundAudioDevice = true; + break; + } + + } + // If we have a saved Audio device which isn't plugged in + // or isn't turned on right now, we don't want to lose the + // information, as the user has specifically set that audio device + // and they may plug it in when it comes time to use the shortcut + // So we add the audiodevice as an extra selection in this case. + if (!foundAudioDevice) + { + int index = cb_audio_device.Items.Add(_shortcutToEdit.AudioDevice); + cb_audio_device.SelectedIndex = index; + } + } } } \ No newline at end of file