Switch to using alarm instead of delay to stop skewing.
[picture-display.git] / lib / Display / Plugins / MPD.pm
1 package Display::Plugins::MPD;
2
3 use Clutter;
4 use Audio::MPD;
5 use POSIX qw/strftime/;
6 use POE::Session;
7 use base ('Display::Plugin');
8 use strict;
9
10 my $delay = 5;
11
12 sub new {
13   my $proto = shift;
14   my $class = ref($proto) || $proto;
15
16   my $self = $class->SUPER::new(@_);
17
18   $self->{'mpd'} = Audio::MPD->new();
19   $self->{'file'} = '';
20   $self->{'active'} = 0;
21
22   bless ($self, $class);
23   $self->init_delay($delay, 'mpd_display');
24   $self->init();
25   return $self;
26 }
27
28 sub display {
29   my ($self, $kernel) = @_[OBJECT, KERNEL];
30
31   my $current = $self->{'mpd'}->current();
32
33   if (defined $current && $self->{'file'} ne $current->file()) {
34     my @lines = ();
35     push @lines, $current->title()  if defined $current->title();
36     push @lines, $current->album()  if defined $current->album();
37     push @lines, $current->artist() if defined $current->artist();
38
39     $self->{'status'}->set_text(join("\n", @lines));
40
41     $self->{'file'} = $current->file();
42
43     $kernel->yield('notifications_add', $self->{'status'});
44   }
45
46   $self->delay();
47 }
48
49 sub init {
50   my $self = shift;
51
52   $self->{'kernel'}->state('mpd_display', $self, 'display');
53
54   $self->{'status'} = Clutter::Label->new('Sans 20', "Song\nAlbum");
55   $self->{'status'}->set_color(Clutter::Color->parse('White'));
56   $self->{'status'}->set_ellipsize('end');
57
58   $self->{'kernel'}->yield('mpd_display');
59 }
60
61 1;