--- Plugin.pm 2008/02/18 16:40:51 1.1 +++ Plugin.pm 2008/02/26 02:50:35 @@ -101,7 +101,7 @@ 'autodisplay_brightness' => 0, # All the way dark. Supported everyplace ); -my $timer; +my %timer; our $brightness = (); my %functions = ( @@ -306,24 +306,25 @@ } } -sub setTimer { - my $now = shift; - Slim::Utils::Timers::killSpecific($timer) if (defined $timer); - - my $later = nextTime($now); - my $time = Time::HiRes::time() + ($later - $now); - $log->debug("Setting timer: Now: $now Later: $later Diff: " . ($later - $now) . " Time: $time"); - $timer = Slim::Utils::Timers::setTimer(0, $time, \&checkOnOff); +sub setTimer($$) { + my ($now,$client) = @_; + my $id = $client->id(); + my $later = nextTime($now,$client); + my $time = &getFuzzyTime($client) + ($later - $now); + $log->debug("Setting timer for $id: Now: $now Later: $later Diff: " . ($later - $now) . " Time: $time"); + if ( defined($timer{$id}) ) { &killFuzzyTimers($client, $client, \&checkOnOff); } + $timer{$id} = &setFuzzyTimer($client, $client, $time, \&checkOnOff); } -sub now { - my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); +sub now($) { + my $timeVal = shift; + my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($timeVal); my $time = $hour * 60 * 60 + $min * 60 + $sec; return $time; } -sub nextTime { - my $now = shift; +sub nextTime($$) { + my ($now,$client) = @_; my $day = 3600 * 24; ## Seconds in a day my $earliest = $now + $day * 7; ## Week away. Good number. foreach my $client (Slim::Player::Client::clients()) { @@ -343,11 +344,11 @@ } sub checkOnOff { - my $time = now(); - $log->debug("Checking timer Autodisplay plugin: " . $time); + $log->debug("Checking timer Autodisplay plugin: " . &now(time())); foreach my $client (Slim::Player::Client::clients()) { + my $time = now(&getFuzzyTime($client)); my $clientPrefs = $myPrefs->client($client); my $flag = $clientPrefs->get('autodisplay_flag'); if ($flag) { @@ -377,11 +378,52 @@ $client->brightness($x); } } + setTimer($time,$client); } } } - setTimer($time); } +sub getFuzzyTime { + my $client = shift; + if ( defined($Plugins::FuzzyTime::Plugin::apiVersion) ) { + return int(Plugins::FuzzyTime::Public::getClientTime($client)); + } + return time(); +} + +# like Slim::Utils::Timers::setTimer +# # *but* it takes 4-5 args: +# # required $client, +# # required $objRef (usually also $client), +# # required $displaytime (time to act, as displayed by FuzzyTime rather than real time), +# # required $subptr +# # optional @args +# # pass the $client object for the appropriate player +sub setFuzzyTimer { + my ($client, $objRef, $displaytime, $subptr, @args) = @_; + my $api = $Plugins::FuzzyTime::Plugin::apiVersion; + if ( defined($api) && ($api >= 1.1) ) { + return Plugins::FuzzyTime::Public::setTimer($client, $objRef, $displaytime, $subptr, @args); + } + return Slim::Utils::Timers::setTimer($objRef, $displaytime, $subptr, @args); +} + +# like Slim::Utils::Timers::killTimers +# *but* it takes 3 args +# required $client, +# required $objRef (usually also $client), +# required $coderef (same as $subptr in setFuzzyTimer) +# pass the $client object for the appropriate player +sub killFuzzyTimers { + my ($client, $obj, $coderef) = @_; + my $api = $Plugins::FuzzyTime::Plugin::apiVersion; + if ( defined($api) && ($api >= 1.1) ) { + return Plugins::FuzzyTime::Public::killTimers($client, $obj, $coderef); + } + return Slim::Utils::Timers::killTimers($obj, $coderef); +} + + 1;