Skip to content

Commit

Permalink
Model ready for recording devices. See #19
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Aflalo committed Sep 2, 2015
1 parent 59db284 commit c50bf63
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public interface ISoundSwitchConfiguration : IConfiguration
string LastPlaybackActive { get; set; }
string LastRecordingActive { get; set; }
bool FirstRun { get; set; }
HotKeys HotKeysCombinaison { get; set; }
HotKeys PlaybackHotKeys { get; set; }
HotKeys RecordingHotKeys { get; set; }
bool ChangeCommunications { get; set; }
uint UpdateCheckInterval { get; set; }
}
Expand Down
16 changes: 14 additions & 2 deletions SoundSwitch/Framework/Configuration/SoundSwitchConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* GNU General Public License for more details.
********************************************************************/

using System;
using System.Collections.Generic;
using System.Windows.Forms;

Expand All @@ -25,7 +26,8 @@ public SoundSwitchConfiguration()
ChangeCommunications = false;
SelectedPlaybackDeviceList = new HashSet<string>();
SelectedRecordingDeviceList = new HashSet<string>();
HotKeysCombinaison = new HotKeys(Keys.F11, HotKeys.ModifierKeys.Alt | HotKeys.ModifierKeys.Control);
PlaybackHotKeys = new HotKeys(Keys.F11, HotKeys.ModifierKeys.Alt | HotKeys.ModifierKeys.Control);
RecordingHotKeys = new HotKeys(Keys.F7,HotKeys.ModifierKeys.Alt | HotKeys.ModifierKeys.Control);
//12 hours
UpdateCheckInterval = 3600*12;
}
Expand All @@ -35,7 +37,17 @@ public SoundSwitchConfiguration()
public string LastPlaybackActive { get; set; }
public string LastRecordingActive { get; set; }
public bool FirstRun { get; set; }
public HotKeys HotKeysCombinaison { get; set; }
public HotKeys PlaybackHotKeys { get; set; }

//TODO: Remove in a couple of version (introduced in 3.4)
[Obsolete("Replaced by PlaybackHotKeys")]
public HotKeys HotKeysCombinaison
{
get { return null; }
set { PlaybackHotKeys = value; }
}

public HotKeys RecordingHotKeys { get; set; }
public bool ChangeCommunications { get; set; }
public uint UpdateCheckInterval { get; set; }
//Needed by Interface
Expand Down
54 changes: 34 additions & 20 deletions SoundSwitch/Model/AppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public bool SetCommunications
}
}

public string PlaybackHotKeysString => AppConfigs.Configuration.HotKeysCombinaison.ToString();
public string PlaybackHotKeysString => AppConfigs.Configuration.PlaybackHotKeys.ToString();
public string RecordingHotKeysString { get; }

#region Misc settings
Expand Down Expand Up @@ -124,7 +124,8 @@ public void InitializeMain()
{
throw new InvalidOperationException("Already initialized");
}
SetPlaybackHotkeyCombination(AppConfigs.Configuration.HotKeysCombinaison);
SetHotkeyCombination(AppConfigs.Configuration.PlaybackHotKeys, AudioDeviceType.Playback);
SetHotkeyCombination(AppConfigs.Configuration.RecordingHotKeys, AudioDeviceType.Recording);
InitUpdateChecker();
_initialized = true;
}
Expand Down Expand Up @@ -253,42 +254,55 @@ public bool UnselectDevice(IAudioDevice device)

#region Hot keys

/// <summary>
/// Sets the hotkey combination
/// </summary>
/// <param name="hotkeys"></param>
public void SetPlaybackHotkeyCombination(HotKeys hotkeys)
public bool SetHotkeyCombination(HotKeys hotkeys, AudioDeviceType deviceType)
{
using (AppLogger.Log.InfoCall())
{
AppLogger.Log.Info("Unregister previous hotkeys", AppConfigs.Configuration.HotKeysCombinaison);
WindowsAPIAdapter.UnRegisterHotKey(AppConfigs.Configuration.HotKeysCombinaison);
HotKeys confHotKeys = null;
switch (deviceType)
{
case AudioDeviceType.Playback:
confHotKeys = AppConfigs.Configuration.PlaybackHotKeys;
break;
case AudioDeviceType.Recording:
confHotKeys = AppConfigs.Configuration.RecordingHotKeys;
break;
default:
throw new ArgumentOutOfRangeException(nameof(deviceType), deviceType, null);
}
AppLogger.Log.Info("Unregister previous hotkeys", confHotKeys);
WindowsAPIAdapter.UnRegisterHotKey(confHotKeys);

if (!WindowsAPIAdapter.RegisterHotKey(hotkeys))
{
AppLogger.Log.Warn("Can't register new hotkeys", hotkeys);
ErrorTriggered?.Invoke(this, new ExceptionEvent(new Exception("Impossible to register HotKey: " + PlaybackHotKeysString)));
return false;
}
else

AppLogger.Log.Info("New Hotkeys registered", hotkeys);
switch (deviceType)
{
AppLogger.Log.Info("New Hotkeys registered", hotkeys);
AppConfigs.Configuration.HotKeysCombinaison = hotkeys;
AppConfigs.Configuration.Save();
case AudioDeviceType.Playback:
AppConfigs.Configuration.PlaybackHotKeys = hotkeys;
break;
case AudioDeviceType.Recording:
AppConfigs.Configuration.RecordingHotKeys = hotkeys;
break;
default:
throw new ArgumentOutOfRangeException(nameof(deviceType), deviceType, null);
}
AppConfigs.Configuration.Save();
return true;
}
}

public void SetRecordingHotkeyCombination(HotKeys hotkeys)
{
throw new NotImplementedException();
}


private void HandleHotkeyPress(object sender, WindowsAPIAdapter.KeyPressedEventArgs e)
{
using (AppLogger.Log.DebugCall())
{
if (e.HotKeys != AppConfigs.Configuration.HotKeysCombinaison)
if (e.HotKeys != AppConfigs.Configuration.PlaybackHotKeys)
{
AppLogger.Log.Debug("Not the registered Hotkeys", e.HotKeys);
return;
Expand Down Expand Up @@ -338,7 +352,7 @@ public bool SetActiveDevice(IAudioDevice device)
default:
throw new ArgumentOutOfRangeException();
}

AppConfigs.Configuration.Save();
return true;
}
Expand Down
10 changes: 3 additions & 7 deletions SoundSwitch/Model/IAppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,9 @@ public interface IAppModel
/// Sets the hotkey combination
/// </summary>
/// <param name="hotkeys"></param>
void SetPlaybackHotkeyCombination(HotKeys hotkeys);

/// <summary>
/// Sets the hotkey combination
/// </summary>
/// <param name="hotkeys"></param>
void SetRecordingHotkeyCombination(HotKeys hotkeys);
/// <param name="deviceType"></param>
/// <returns>if it's successfull</returns>
bool SetHotkeyCombination(HotKeys hotkeys, AudioDeviceType deviceType);

/// <summary>
/// Attempts to set active device to the specified name
Expand Down
5 changes: 3 additions & 2 deletions SoundSwitch/UI/Forms/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ private void TxtHotkey_KeyDown(object sender, KeyEventArgs e)
txtHotkey.Text = $"{displayString}{keyCode}";
if (!string.IsNullOrEmpty(keyCode))
{
txtHotkey.ForeColor = Color.Green;
AppModel.Instance.SetPlaybackHotkeyCombination(new HotKeys(e.KeyCode, modifierKeys));

txtHotkey.ForeColor = AppModel.Instance.SetHotkeyCombination(new HotKeys(e.KeyCode, modifierKeys),
AudioDeviceType.Playback) ? Color.Green : Color.Red;
}
}

Expand Down

0 comments on commit c50bf63

Please sign in to comment.