Lots of tweaks to make the display work and look better.
[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 my $delay = 15;
9
10 sub new {
11   my $proto = shift;
12   my $class = ref($proto) || $proto;
13
14   my $self = $class->SUPER::new(@_);
15
16   $self->{'mpd'} = Audio::MPD->new();
17   $self->{'file'} = '';
18   $self->{'active'} = 0;
19
20   bless ($self, $class);
21   $self->init();
22   return $self;
23 }
24
25 sub display {
26   my $self = shift;
27
28   my $current = $self->{'mpd'}->current();
29
30   if ($self->{'file'} ne $current->file()) {
31     my @lines = ();
32     push @lines, $current->title()  if defined $current->title();
33     push @lines, $current->album()  if defined $current->album();
34     push @lines, $current->artist() if defined $current->artist();
35
36     $self->{'status'}->set_text(join("\n", @lines));
37
38     $self->{'file'} = $current->file();
39
40     $self->{'bg'}->set_height($self->{'status'}->get_height() + 10);
41     $self->{'bg'}->set_anchor_point(1, $self->{'bg'}->get_height());
42     $self->{'bg'}->set_position(10, $self->{'stage'}->get_height() - 10);
43
44     $self->{'status'}->set_anchor_point(1, $self->{'status'}->get_height());
45     $self->{'status'}->set_position(20, $self->{'stage'}->get_height() - 20);
46
47     if (! $self->{'active'}) {
48       $self->{'block'}->set_opacity(0);
49       $self->{'stage'}->add($self->{'block'});
50       my $effect = Clutter::EffectTemplate->new_for_duration(1000, 'main::smoothstep_inc');
51       my $timeline = Clutter::Effect->fade($effect, $self->{'block'}, 255);
52       $timeline->start();
53       
54       $self->{'active'} = time() + $delay;
55     }
56
57     Glib::Timeout->add_seconds($delay, $self->can('hide_display'), $self);
58   }
59
60   return 1;
61 }
62
63 sub hide_display {
64   my $self = shift;
65
66   if ($self->{'active'} <= time()) {
67     $self->{'stage'}->remove($self->{'block'});
68
69     $self->{'active'} = 0;
70   }
71
72   return 0;
73 }
74
75 sub init {
76   my $self = shift;
77
78   $self->{'block'} = Clutter::Group->new();
79
80   $self->{'bg'} = Clutter::Rectangle->new(Clutter::Color->parse('Black'));
81   $self->{'bg'}->set_width($self->{'stage'}->get_width() - 20);
82   $self->{'bg'}->set_opacity(100);
83
84   $self->{'status'} = Clutter::Label->new('Sans 20', "Song\nAlbum");
85   $self->{'status'}->set_color(Clutter::Color->parse('White'));
86   $self->{'status'}->set_ellipsize('end');
87   $self->{'status'}->set_width($self->{'bg'}->get_width() - 20);
88
89   $self->{'block'}->add($self->{'bg'});
90   $self->{'block'}->add($self->{'status'});
91
92   Glib::Timeout->add_seconds(5, $self->can('display'), $self);
93 }
94
95 1;