From ce670c69d52d8522e1727cc0035dca5989da07c6 Mon Sep 17 00:00:00 2001 From: Andrew Ruthven Date: Mon, 22 Sep 2008 08:49:55 +1200 Subject: [PATCH] Only try and talk to the MPD daemon if we can actually comminucate with MPD. --- lib/Display/Plugins/MPD.pm | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/Display/Plugins/MPD.pm b/lib/Display/Plugins/MPD.pm index 6136ff3..46d5b51 100644 --- a/lib/Display/Plugins/MPD.pm +++ b/lib/Display/Plugins/MPD.pm @@ -1,9 +1,9 @@ package Display::Plugins::MPD; use Clutter; -use Audio::MPD; use POSIX qw/strftime/; use base ('Display::Plugin'); +use POE qw{ Component::Client::MPD }; sub new { my $proto = shift; @@ -11,7 +11,6 @@ sub new { my $self = $class->SUPER::new(@_); - $self->{'mpd'} = Audio::MPD->new(); $self->{'file'} = ''; $self->{'active'} = 0; @@ -21,9 +20,10 @@ sub new { } sub display { - my $self = shift; + my ($self, $msg, $answer) = @_[OBJECT, ARG0, ARG1]; - my $current = $self->{'mpd'}->current(); +warn "Got a response from MPD\n"; + my $current = $answer->current(); if ($self->{'file'} ne $current->file()) { my @lines = (); @@ -49,10 +49,10 @@ sub display { $self->{'active'} = time() + 30; } - Glib::Timeout->add_seconds(30, $self->can('hide_display'), $self); + $self->{'KERNEL'}->delay( hide_display, 30 ); } - return 1; + $self->{'KERNEL'}->delay_add( mpd, 5, current ); } sub hide_display { @@ -68,17 +68,41 @@ sub hide_display { return 0; } +sub error { + my ($kernel, $msg, $errstr) = @_[KERNEL, ARG0, ARG1]; + + warn "MPD: Error $errstr\n"; + + $kernel->delay_add( mpd, 5, current ); +} + sub init { my $self = shift; + $self->{'kernel'}->state('mpd_result', $self, 'display'); + $self->{'kernel'}->state('mpd_error', $self, 'error'); + $self->{'kernel'}->state('mpd_hide_display', $self, 'hide_display'); + POE::Component::Client::MPD->new( { + alias => 'mpd', + status_msgs_to => 'mpd_status_msgs', + } ); + $self->{'status'} = Clutter::Label->new('Sans 20', "Song\nAlbum"); $self->{'status'}->set_color(Clutter::Color->parse('White')); $self->{'bg'} = Clutter::Rectangle->new(Clutter::Color->parse('Black')); $self->{'bg'}->set_width($self->{'stage'}->get_width() - 40); $self->{'bg'}->set_opacity(100); +} - Glib::Timeout->add_seconds(5, $self->can('display'), $self); +sub mpd_status_msgs { + my ($kernel, $arg) = @_[KERNEL, ARG0]; + + if ($arg eq 'mpd_connected') { + $self->{'kernel'}->post( 'mpd', 'current' ); + } else { + warn "Failed to connected to MPD. What now?\n"; + } } 1; -- 2.30.2