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