MPD moved to its own class
This commit is contained in:
parent
a2f2af87cc
commit
f85eae1354
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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<T>(ref T a, T b)
|
||||
{
|
||||
if (Comparer<T>.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;
|
||||
}
|
||||
}
|
||||
|
@ -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<T>(ref T a, T b)
|
||||
{
|
||||
if (Comparer<T>.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)
|
||||
{
|
||||
|
@ -34,7 +34,7 @@
|
||||
<TextBlock Text="Password" TextWrapping="Wrap" Margin="5,0,0,0"/>
|
||||
<TextBox x:Name="MpdPassword" TextWrapping="Wrap" Width="250" Margin="10,2,0,0"/>
|
||||
</StackPanel>
|
||||
<Button Content="Connect" Margin="0,10,0,0" Width="120"/>
|
||||
<Button Content="Connect" Margin="0,10,0,0" Width="120" Click="MPDConnect_Clicked"/>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</GroupBox>
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
@ -65,6 +66,17 @@ namespace unison
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private void MPDConnect_Clicked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Properties.Settings.Default.mpd_host = MpdHost.Text;
|
||||
Properties.Settings.Default.mpd_port = int.Parse(MpdPort.Text, CultureInfo.InvariantCulture);
|
||||
Properties.Settings.Default.mpd_password = MpdPassword.Text;
|
||||
Properties.Settings.Default.Save();
|
||||
|
||||
var mpd = (MPDHandler)Application.Current.Properties["mpd"];
|
||||
mpd.Connect();
|
||||
}
|
||||
|
||||
private void SnapcastReset_Clicked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
SnapcastPath.Text = defaultSnapcastPath;
|
||||
@ -76,11 +88,11 @@ namespace unison
|
||||
e.Cancel = true;
|
||||
|
||||
Properties.Settings.Default.mpd_host = MpdHost.Text;
|
||||
Properties.Settings.Default.mpd_port = int.Parse(MpdPort.Text);
|
||||
Properties.Settings.Default.mpd_port = int.Parse(MpdPort.Text, CultureInfo.InvariantCulture);
|
||||
Properties.Settings.Default.mpd_password = MpdPassword.Text;
|
||||
Properties.Settings.Default.snapcast_startup = (bool)SnapcastStartup.IsChecked;
|
||||
Properties.Settings.Default.snapcast_path = SnapcastPath.Text;
|
||||
Properties.Settings.Default.snapcast_port = int.Parse(SnapcastPort.Text);
|
||||
Properties.Settings.Default.snapcast_port = int.Parse(SnapcastPort.Text, CultureInfo.InvariantCulture);
|
||||
Properties.Settings.Default.Save();
|
||||
|
||||
WindowState = WindowState.Minimized;
|
||||
|
Loading…
Reference in New Issue
Block a user