From f85eae135465ac61957489581b8f888bb37f4e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Marchal?= Date: Mon, 16 Aug 2021 02:13:47 +0200 Subject: [PATCH] MPD moved to its own class --- App.xaml.cs | 6 +- Handlers/HotkeyHandler.cs | 18 +++-- Handlers/MPDHandler.cs | 121 +++++++++++++++++++++++++++- Views/MainWindow.xaml.cs | 165 +++++++++----------------------------- Views/Settings.xaml | 2 +- Views/Settings.xaml.cs | 16 +++- 6 files changed, 190 insertions(+), 138 deletions(-) diff --git a/App.xaml.cs b/App.xaml.cs index 9f23aff..3830b2e 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -8,6 +8,7 @@ namespace unison private TaskbarIcon Systray; private HotkeyHandler Hotkeys; private SnapcastHandler Snapcast; + private MPDHandler MPD; protected override void OnStartup(StartupEventArgs e) { @@ -19,10 +20,13 @@ namespace unison Snapcast = new SnapcastHandler(); Current.Properties["snapcast"] = Snapcast; + MPD = new MPDHandler(); + Current.Properties["mpd"] = MPD; + Current.MainWindow = new MainWindow(); Systray = (TaskbarIcon)FindResource("SystrayTaskbar"); - Current.Properties["notify"] = Systray; + Current.Properties["systray"] = Systray; } protected override void OnExit(ExitEventArgs e) diff --git a/Handlers/HotkeyHandler.cs b/Handlers/HotkeyHandler.cs index 8bdaf86..e00d99c 100644 --- a/Handlers/HotkeyHandler.cs +++ b/Handlers/HotkeyHandler.cs @@ -34,8 +34,11 @@ namespace unison private IntPtr _windowHandle; private HwndSource _source; + private readonly MPDHandler mpd; + public HotkeyHandler() { + mpd = (MPDHandler)Application.Current.Properties["mpd"]; } public void Activate(Window win) @@ -66,22 +69,21 @@ namespace unison switch (vkey) { case VK_MEDIA_NEXT_TRACK: - Trace.WriteLine("TEST super important"); - AppWindow.Next_Clicked(null, null); + mpd.Next(); break; case VK_MEDIA_PREV_TRACK: - AppWindow.Previous_Clicked(null, null); + mpd.Prev(); break; case VK_VOLUME_DOWN: - AppWindow._currentVolume -= 5; - AppWindow.ChangeVolume(AppWindow._currentVolume); + mpd._currentVolume -= 5; + mpd.SetVolume(mpd._currentVolume); break; case VK_VOLUME_UP: - AppWindow._currentVolume += 5; - AppWindow.ChangeVolume(AppWindow._currentVolume); + mpd._currentVolume += 5; + mpd.SetVolume(mpd._currentVolume); break; case VK_MEDIA_PLAY_PAUSE: - AppWindow.Pause_Clicked(null, null); + mpd.PlayPause(); break; case VK_ENTER: if (AppWindow.WindowState == WindowState.Minimized) diff --git a/Handlers/MPDHandler.cs b/Handlers/MPDHandler.cs index 314bd46..8b2cc87 100644 --- a/Handlers/MPDHandler.cs +++ b/Handlers/MPDHandler.cs @@ -1,9 +1,128 @@ -namespace unison +using System.Collections.Generic; +using System.Threading.Tasks; + +using MPDCtrl.Models; + +namespace unison { public class MPDHandler { + private readonly MPC _mpd = new(); + + public bool _connected; + public int _currentVolume; + public bool _currentRandom; + public bool _currentRepeat; + public bool _currentSingle; + public bool _currentConsume; + public double _currentElapsed; + + private Status _currentStatus = null; + private SongInfoEx _currentSong = null; + public MPDHandler() { } + + public async void Connect() + { + _connected = await _mpd.MpdCommandConnectionStart(Properties.Settings.Default.mpd_host, Properties.Settings.Default.mpd_port, Properties.Settings.Default.mpd_password); + if (_connected) + { + await _mpd.MpdQueryStatus(); + await Task.Delay(5); + _currentVolume = _mpd.MpdStatus.MpdVolume; + _currentRandom = _mpd.MpdStatus.MpdRandom; + _currentRepeat = _mpd.MpdStatus.MpdRepeat; + _currentSingle = _mpd.MpdStatus.MpdSingle; + _currentConsume = _mpd.MpdStatus.MpdConsume; + _currentElapsed = _mpd.MpdStatus.MpdSongElapsed; + } + } + + public void CheckStatus(ref T a, T b) + { + if (Comparer.Default.Compare(a, b) != 0) + a = b; + } + + public async void Loop() + { + if (!_connected) + return; + + CommandResult status = await _mpd.MpdQueryStatus(); + //Trace.WriteLine(status.ResultText); + await Task.Delay(5); + if (status != null) + { + _currentStatus = _mpd.MpdStatus; + + CheckStatus(ref _currentVolume, _mpd.MpdStatus.MpdVolume); + CheckStatus(ref _currentRandom, _mpd.MpdStatus.MpdRandom); + CheckStatus(ref _currentRepeat, _mpd.MpdStatus.MpdRepeat); + CheckStatus(ref _currentSingle, _mpd.MpdStatus.MpdSingle); + CheckStatus(ref _currentConsume, _mpd.MpdStatus.MpdConsume); + CheckStatus(ref _currentElapsed, _mpd.MpdStatus.MpdSongElapsed); + } + + CommandResult song = await _mpd.MpdQueryCurrentSong(); + await Task.Delay(5); + if (song != null) + _currentSong = _mpd.MpdCurrentSong; + } + + public SongInfoEx GetCurrentSong() => _currentSong; + public Status GetStatus() => _currentStatus; + + public async void Prev() + { + await _mpd.MpdPlaybackPrev(_currentVolume); + } + + public async void Next() + { + await _mpd.MpdPlaybackNext(_currentVolume); + } + + public async void PlayPause() + { + if (_mpd.MpdStatus.MpdState == Status.MpdPlayState.Play) + await _mpd.MpdPlaybackPause(); + else if (_mpd.MpdStatus.MpdState == Status.MpdPlayState.Pause) + await _mpd.MpdPlaybackPlay(_currentVolume); + } + + public async void Random() + { + await _mpd.MpdSetRandom(!_currentRandom); + + } + + public async void Repeat() + { + await _mpd.MpdSetRepeat(!_currentRepeat); + + } + + public async void Single() + { + await _mpd.MpdSetSingle(!_currentSingle); + + } + + public async void Consume() + { + await _mpd.MpdSetConsume(!_currentConsume); + + } + + public async void SetVolume(int value) + { + await _mpd.MpdSetVolume(value); + + } + + public bool IsPlaying() => _currentStatus?.MpdState == MPDCtrl.Models.Status.MpdPlayState.Play; } } diff --git a/Views/MainWindow.xaml.cs b/Views/MainWindow.xaml.cs index be60ede..6d86de1 100644 --- a/Views/MainWindow.xaml.cs +++ b/Views/MainWindow.xaml.cs @@ -2,31 +2,19 @@ using System.Collections.Generic; using System.Threading.Tasks; using System.ComponentModel; -using System.Diagnostics; using System.Windows; using System.Windows.Controls; using System.Windows.Threading; -using MPDCtrl.Models; using System.Windows.Interop; +using System.Diagnostics; namespace unison { public partial class MainWindow : Window { - private readonly MPC _mpd = new(); - private bool _connected; - public int _currentVolume; - private bool _currentRandom; - private bool _currentRepeat; - private bool _currentSingle; - private bool _currentConsume; - private double _currentElapsed; + private readonly Settings SettingsWindow = new Settings(); - private string _mpdHost = "192.168.1.13"; - private int _mpdPort = 6600; - private string _mpdPassword = null; - - Settings SettingsWindow = new Settings(); + private readonly MPDHandler mpd; Thickness SelectedThickness; Thickness BaseThickness; @@ -38,7 +26,9 @@ namespace unison WindowState = WindowState.Minimized; - ConnectToMPD(); + mpd = (MPDHandler)Application.Current.Properties["mpd"]; + mpd.Connect(); + DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(0.2); timer.Tick += Timer_Tick; @@ -49,57 +39,14 @@ namespace unison BaseThickness.Left = BaseThickness.Right = BaseThickness.Top = BaseThickness.Bottom = 0.0f; } - public async void ConnectToMPD() - { - _connected = await _mpd.MpdCommandConnectionStart(_mpdHost, _mpdPort, _mpdPassword); - if (_connected) - { - await _mpd.MpdQueryStatus(); - await Task.Delay(5); - _currentVolume = _mpd.MpdStatus.MpdVolume; - _currentRandom = _mpd.MpdStatus.MpdRandom; - _currentRepeat = _mpd.MpdStatus.MpdRepeat; - _currentSingle = _mpd.MpdStatus.MpdSingle; - _currentConsume = _mpd.MpdStatus.MpdConsume; - _currentElapsed = _mpd.MpdStatus.MpdSongElapsed; - } - } - private void Timer_Tick(object sender, EventArgs e) { - LoopMPD(); + MPDHandler mpd = (MPDHandler)Application.Current.Properties["mpd"]; + mpd.Loop(); + UpdateInterface(); } - public void CheckStatus(ref T a, T b) - { - if (Comparer.Default.Compare(a, b) != 0) - a = b; - } - - public async void LoopMPD() - { - if (!_connected) - return; - - var status = await _mpd.MpdQueryStatus(); - //Trace.WriteLine(status.ResultText); - await Task.Delay(5); - - if (status != null) - { - CheckStatus(ref _currentVolume, _mpd.MpdStatus.MpdVolume); - CheckStatus(ref _currentRandom, _mpd.MpdStatus.MpdRandom); - CheckStatus(ref _currentRepeat, _mpd.MpdStatus.MpdRepeat); - CheckStatus(ref _currentSingle, _mpd.MpdStatus.MpdSingle); - CheckStatus(ref _currentConsume, _mpd.MpdStatus.MpdConsume); - CheckStatus(ref _currentElapsed, _mpd.MpdStatus.MpdSongElapsed); - } - - await _mpd.MpdQueryCurrentSong(); - await Task.Delay(5); - } - public void UpdateButton(ref Border border, bool b) { if (b) @@ -116,31 +63,33 @@ namespace unison public void UpdateInterface() { - if (_mpd.MpdCurrentSong != null) + if (mpd.GetCurrentSong() != null) { - SongTitle.Text = _mpd.MpdCurrentSong.Title; - SongTitle.ToolTip = _mpd.MpdCurrentSong.File; - SongArtist.Text = _mpd.MpdCurrentSong.Artist; - SongAlbum.Text = _mpd.MpdCurrentSong.Album + " (" + _mpd.MpdCurrentSong.Date + ")"; - Bitrate.Text = _mpd.MpdCurrentSong.File.Substring(_mpd.MpdCurrentSong.File.LastIndexOf(".") + 1); - Bitrate.Text += " – "; - Bitrate.Text += _mpd.MpdStatus.MpdBitrate + "kbps"; + SongTitle.Text = mpd.GetCurrentSong().Title; + SongTitle.ToolTip = mpd.GetCurrentSong().File; + SongArtist.Text = mpd.GetCurrentSong().Artist; + SongAlbum.Text = mpd.GetCurrentSong().Album; + if (mpd.GetCurrentSong().Date.Length > 0) + SongAlbum.Text += $" ({ mpd.GetCurrentSong().Date})"; + Bitrate.Text = mpd.GetCurrentSong().File.Substring(mpd.GetCurrentSong().File.LastIndexOf(".") + 1) + " – "; + Bitrate.Text += mpd.GetStatus().MpdBitrate + "kbps"; - CurrentTime.Text = FormatSeconds(_currentElapsed); - EndTime.Text = FormatSeconds(_mpd.MpdStatus.MpdSongTime); + CurrentTime.Text = FormatSeconds(mpd._currentElapsed); + EndTime.Text = FormatSeconds(mpd.GetStatus().MpdSongTime); - TimeSlider.Value = _currentElapsed / _mpd.MpdCurrentSong.TimeSort * 100; + if (!System.Double.IsNaN(mpd.GetCurrentSong().TimeSort)) + TimeSlider.Value = mpd._currentElapsed / mpd.GetCurrentSong().TimeSort * 100; } - if (VolumeSlider.Value != _currentVolume) + if (VolumeSlider.Value != mpd._currentVolume) { - VolumeSlider.Value = _currentVolume; - VolumeSlider.ToolTip = _currentVolume; + VolumeSlider.Value = mpd._currentVolume; + VolumeSlider.ToolTip = mpd._currentVolume; } - if (_mpd.MpdStatus.MpdState == Status.MpdPlayState.Play) + if (mpd.IsPlaying()) PauseButtonEmoji.Text = "⏸️"; - else if (_mpd.MpdStatus.MpdState == Status.MpdPlayState.Pause) + else PauseButtonEmoji.Text = "▶️"; SnapcastHandler snapcast = (SnapcastHandler)Application.Current.Properties["snapcast"]; @@ -149,56 +98,22 @@ namespace unison else SnapcastText.Text = "Start Snapcast"; - Connection.Text = (_connected ? "✔️" : "❌") + _mpd.MpdHost + ":" + _mpd.MpdPort; + Connection.Text = (mpd._connected ? "✔️" : "❌") + $"{Properties.Settings.Default.mpd_host}:{Properties.Settings.Default.mpd_port}"; - UpdateButton(ref BorderRandom, _currentRandom); - UpdateButton(ref BorderRepeat, _currentRepeat); - UpdateButton(ref BorderSingle, _currentSingle); - UpdateButton(ref BorderConsume, _currentConsume); + UpdateButton(ref BorderRandom, mpd._currentRandom); + UpdateButton(ref BorderRepeat, mpd._currentRepeat); + UpdateButton(ref BorderSingle, mpd._currentSingle); + UpdateButton(ref BorderConsume, mpd._currentConsume); } - public async void Pause_Clicked(object sender, RoutedEventArgs e) - { - if (_mpd.MpdStatus.MpdState == Status.MpdPlayState.Play) - await _mpd.MpdPlaybackPause(); - else if (_mpd.MpdStatus.MpdState == Status.MpdPlayState.Pause) - await _mpd.MpdPlaybackPlay(_currentVolume); - } - - public async void Previous_Clicked(object sender, RoutedEventArgs e) - { - await _mpd.MpdPlaybackPrev(_currentVolume); - } - - public async void Next_Clicked(object sender, RoutedEventArgs e) - { - await _mpd.MpdPlaybackNext(_currentVolume); - } - - public async void Random_Clicked(object sender, RoutedEventArgs e) - { - await _mpd.MpdSetRandom(!_currentRandom); - } - - private async void Repeat_Clicked(object sender, RoutedEventArgs e) - { - await _mpd.MpdSetRepeat(!_currentRepeat); - } - - private async void Single_Clicked(object sender, RoutedEventArgs e) - { - await _mpd.MpdSetSingle(!_currentSingle); - } - - private async void Consume_Clicked(object sender, RoutedEventArgs e) - { - await _mpd.MpdSetConsume(!_currentConsume); - } - - public async void ChangeVolume(int value) - { - await _mpd.MpdSetVolume(value); - } + public void Pause_Clicked(object sender, RoutedEventArgs e) => mpd.PlayPause(); + public void Previous_Clicked(object sender, RoutedEventArgs e) => mpd.Prev(); + public void Next_Clicked(object sender, RoutedEventArgs e) => mpd.Next(); + public void Random_Clicked(object sender, RoutedEventArgs e) => mpd.Random(); + public void Repeat_Clicked(object sender, RoutedEventArgs e) => mpd.Repeat(); + public void Single_Clicked(object sender, RoutedEventArgs e) => mpd.Single(); + public void Consume_Clicked(object sender, RoutedEventArgs e) => mpd.Consume(); + public void ChangeVolume(int value) => mpd.SetVolume(value); public void Snapcast_Clicked(object sender, RoutedEventArgs e) { diff --git a/Views/Settings.xaml b/Views/Settings.xaml index 5324d3c..1bdfbae 100644 --- a/Views/Settings.xaml +++ b/Views/Settings.xaml @@ -34,7 +34,7 @@ -