From 8841a5610ec3c2cb697a7e007a47b4011659f59c Mon Sep 17 00:00:00 2001 From: Andrew Ruthven Date: Fri, 19 Sep 2008 13:35:19 +1200 Subject: [PATCH] Convert to using POE for the event management. --- lib/Display/Plugin.pm | 20 ++++++++++++++++ lib/Display/Plugins/Clock.pm | 15 ++++++------ lib/Display/Plugins/FSpot.pm | 21 +++++++++-------- lib/Display/Plugins/MPD.pm | 17 +++++++------- picture.pl | 44 ++++++++++++++++++++++-------------- 5 files changed, 76 insertions(+), 41 deletions(-) create mode 100644 lib/Display/Plugin.pm diff --git a/lib/Display/Plugin.pm b/lib/Display/Plugin.pm new file mode 100644 index 0000000..9df460e --- /dev/null +++ b/lib/Display/Plugin.pm @@ -0,0 +1,20 @@ +package Display::Plugin; + +# Provide a base class that does common things for all the plugins. +use Clutter; + +sub new { + my ($class,$kernel,$session,$stage) = @_; + + my $self = { + 'kernel' => $kernel, + 'session' => $session, + 'stage' => $stage, + }; + + bless ($self, $class); + $self->init(); + return $self; +} + +1; diff --git a/lib/Display/Plugins/Clock.pm b/lib/Display/Plugins/Clock.pm index 435d644..5f19c55 100644 --- a/lib/Display/Plugins/Clock.pm +++ b/lib/Display/Plugins/Clock.pm @@ -2,13 +2,13 @@ package Display::Plugins::Clock; use Clutter; use POSIX qw/strftime/; +use base ('Display::Plugin'); sub new { - my ($class,$stage) = @_; + my $proto = shift; + my $class = ref($proto) || $proto; - my $self = { - 'stage' => $stage, - }; + my $self = $class->SUPER::new(@_); bless ($self, $class); $self->init(); @@ -21,13 +21,14 @@ sub display { $self->{'clock'}->set_text(strftime("%H:%M %d/%m/%Y", localtime(time))); Glib::Timeout->add_seconds(60 - strftime(%s, localtime()), $self->can('display'), $self); - - return 0; + $self->{'kernel'}->delay_set(clock_display, 60 - strftime(%s, localtime())); } sub init { my $self = shift; + $self->{'kernel'}->state('clock_display', $self, 'display'); + $self->{'clock'} = Clutter::Label->new('Sans 20', strftime("%H:%M %d/%m/%Y", localtime(time))); $self->{'clock'}->set_anchor_point($self->{'clock'}->get_width(), 1); $self->{'clock'}->set_position($self->{'stage'}->get_width() - 20, 15); @@ -43,7 +44,7 @@ sub init { $self->{'stage'}->add($self->{'bg'}); $self->{'stage'}->add($self->{'clock'}); - Glib::Timeout->add_seconds(60 - strftime(%s, localtime()), $self->can('display'), $self); + $self->{'kernel'}->delay_set(clock_display, 60 - strftime(%s, localtime())); } 1; diff --git a/lib/Display/Plugins/FSpot.pm b/lib/Display/Plugins/FSpot.pm index 50357e8..766a9d6 100644 --- a/lib/Display/Plugins/FSpot.pm +++ b/lib/Display/Plugins/FSpot.pm @@ -1,17 +1,17 @@ package Display::Plugins::FSpot; -use DBI; use Clutter; +use base ('Display::Plugin'); my $f_spot_db = "/home/andrew/.gnome2/f-spot/photos.db"; sub new { - my ($class,$stage) = @_; + my $proto = shift; + my $class = ref($proto) || $proto; - my $self = { - 'stage' => $stage, - 'last_file' => '', - }; + my $self = $class->SUPER::new(@_); + + $self->{'last_file'} = ''; bless ($self, $class); $self->init(); @@ -33,7 +33,7 @@ sub display { fade_in($self->{'new'}); # } else { #warn "Fade out old\n"; -# my $old_effect = Clutter::EffectTemplate->new_for_duration(1000, 'smoothstep_dec' ); +# my $old_effect = Clutter::EffectTemplate->new_for_duration(1000, 'main::smoothstep_dec' ); # my $old = Clutter::Effect->fade($old_effect, $self->{'old'}, 0, $self->can('fade_in'), $self->{'new'}); # $old->start(); } @@ -43,13 +43,14 @@ sub display { # $self->{'new'} = $temp; $self->{'last_file'} = $file; + $self->{'kernel'}->delay_set(fspot_display => 30); return 1; } sub fade_in { my $new_pic = shift; - my $new_effect = Clutter::EffectTemplate->new_for_duration(1000, 'smoothstep_inc'); + my $new_effect = Clutter::EffectTemplate->new_for_duration(1000, 'main::smoothstep_inc'); my $new = Clutter::Effect->fade($new_effect, $new_pic, 255); $new->start(); } @@ -75,6 +76,8 @@ sub find_photo { sub init { my $self = shift; + $self->{'kernel'}->state('fspot_display', $self, 'display'); + $self->{'dbh'} = DBI->connect("dbi:SQLite:dbname=$f_spot_db","","") || die "Failed to connect to $f_spot_db: $DBI::errstr\n"; @@ -105,7 +108,7 @@ LIMIT 1 $self->{'stage'}->add($self->{$age}); } - Glib::Timeout->add_seconds(30, $self->can('display'), $self); + $self->{'kernel'}->delay_set(fspot_display => 30); } 1; diff --git a/lib/Display/Plugins/MPD.pm b/lib/Display/Plugins/MPD.pm index 4db3081..6136ff3 100644 --- a/lib/Display/Plugins/MPD.pm +++ b/lib/Display/Plugins/MPD.pm @@ -3,16 +3,17 @@ package Display::Plugins::MPD; use Clutter; use Audio::MPD; use POSIX qw/strftime/; +use base ('Display::Plugin'); sub new { - my ($class,$stage) = @_; - - my $self = { - 'stage' => $stage, - 'mpd' => Audio::MPD->new(), - 'file' => '', - 'active'=> 0, - }; + my $proto = shift; + my $class = ref($proto) || $proto; + + my $self = $class->SUPER::new(@_); + + $self->{'mpd'} = Audio::MPD->new(); + $self->{'file'} = ''; + $self->{'active'} = 0; bless ($self, $class); $self->init(); diff --git a/picture.pl b/picture.pl index aee5a09..a51ba80 100755 --- a/picture.pl +++ b/picture.pl @@ -4,6 +4,8 @@ use strict; use FindBin; use Module::Pluggable search_path => ['Display::Plugins']; use Clutter qw( :init ); +use Glib; +use POE qw(Loop::Glib); use Getopt::Long; use lib "$FindBin::Bin/lib"; @@ -14,39 +16,47 @@ GetOptions( 'full|f!' => \$full_screen, ); -my $stage = Clutter::Stage->get_default(); -$stage->set_color(Clutter::Color->parse('Black')); -$stage->signal_connect('key-press-event' => sub { Clutter->main_quit() }); +my $session = POE::Session->create ( + inline_states => { + _start => \&start, + }, +); -if ($full_screen) { - $stage->fullscreen; -} else { - $stage->set_size(800, 600); -} +$poe_kernel->run; -my @modules = loadModules(); +#Clutter->main(); -$stage->show_all(); +sub start { + my ( $kernel, $session ) = @_[ KERNEL, SESSION ]; -Clutter->main(); + my $stage = Clutter::Stage->get_default(); + $stage->set_color(Clutter::Color->parse('Black')); + $stage->signal_connect('key-press-event' => sub { Clutter->main_quit() }); -sub update_screen { - for my $module (@modules) { - $module->display(); + if ($full_screen) { + $stage->fullscreen; + } else { + $stage->set_size(800, 600); } + + my @modules = loadModules($kernel, $session, $stage); + + $stage->show_all(); } + sub loadModules { + my ($kernel, $session, $stage) = @_; my @modules; for my $module (plugins()) { warn "Considering $module\n"; eval "use $module"; if ($@) { - die "Failed to load plugin: $module ($@)\n"; + warn "Failed to load plugin: $module ($@)\n"; + } else { + push @modules, $module->new($kernel, $session, $stage); } - - push @modules, $module->new($stage); } return @modules; -- 2.30.2