Convert to using POE for the event management.
[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 base ('Display::Plugin');
7
8 sub new {
9   my $proto = shift;
10   my $class = ref($proto) || $proto;
11
12   my $self = $class->SUPER::new(@_);
13
14   $self->{'mpd'} = Audio::MPD->new();
15   $self->{'file'} = '';
16   $self->{'active'} = 0;
17
18   bless ($self, $class);
19   $self->init();
20   return $self;
21 }
22
23 sub display {
24   my $self = shift;
25
26   my $current = $self->{'mpd'}->current();
27
28   if ($self->{'file'} ne $current->file()) {
29     my @lines = ();
30     push @lines, $current->title()  if defined $current->title();
31     push @lines, $current->album()  if defined $current->album();
32     push @lines, $current->artist() if defined $current->artist();
33
34     $self->{'status'}->set_text(join("\n", @lines));
35
36     $self->{'file'} = $current->file();
37
38     $self->{'bg'}->set_height($self->{'status'}->get_height() + 10);
39     $self->{'bg'}->set_anchor_point(1, $self->{'bg'}->get_height());
40     $self->{'bg'}->set_position(10, $self->{'stage'}->get_height() - 10);
41
42     $self->{'status'}->set_anchor_point(1, $self->{'status'}->get_height());
43     $self->{'status'}->set_position(15, $self->{'stage'}->get_height() - 20);
44
45     if (! $self->{'active'}) {
46       $self->{'stage'}->add($self->{'bg'});
47       $self->{'stage'}->add($self->{'status'});
48       
49       $self->{'active'} = time() + 30;
50     }
51
52     Glib::Timeout->add_seconds(30, $self->can('hide_display'), $self);
53   }
54
55   return 1;
56 }
57
58 sub hide_display {
59   my $self = shift;
60
61   if ($self->{'active'} <= time()) {
62     $self->{'stage'}->remove($self->{'status'});
63     $self->{'stage'}->remove($self->{'bg'});
64
65     $self->{'active'} = 0;
66   }
67
68   return 0;
69 }
70
71 sub init {
72   my $self = shift;
73
74   $self->{'status'} = Clutter::Label->new('Sans 20', "Song\nAlbum");
75   $self->{'status'}->set_color(Clutter::Color->parse('White'));
76
77   $self->{'bg'} = Clutter::Rectangle->new(Clutter::Color->parse('Black'));
78   $self->{'bg'}->set_width($self->{'stage'}->get_width() - 40);
79   $self->{'bg'}->set_opacity(100);
80
81   Glib::Timeout->add_seconds(5, $self->can('display'), $self);
82 }
83
84 1;