Changeset ecd4edf


Ignore:
Timestamp:
Aug 8, 2013, 2:38:50 PM (8 years ago)
Author:
Edward Z. Yang <ezyang@mit.edu>
Branches:
master
Children:
416a7e5, 5832433, d6bc343, c84c365, e0a5480
Parents:
120dac7
git-author:
Jason Gross <jgross@mit.edu> (07/20/11 03:37:08)
git-committer:
Edward Z. Yang <ezyang@mit.edu> (08/08/13 14:38:50)
Message:
Added perl methods to register and unregister idle watchers.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/lib/BarnOwl.pm

    r1ced34f recd4edf  
    66use base qw(Exporter);
    77our @EXPORT_OK = qw(command getcurmsg getnumcols getnumlines getidletime
     8                    register_idle_watcher unregister_idle_watcher
    89                    zephyr_getsender zephyr_getrealm zephyr_zwrite
    910                    zephyr_stylestrip zephyr_smartstrip_user zephyr_getsubs
     
    4647
    4748use List::Util qw(max);
     49use Tie::RefHash;
    4850
    4951=head1 NAME
     
    793795}
    794796
     797=head3 register_idle_watcher %ARGS
     798
     799Call a callback whenever the amount of time the user becomes idle or comes
     800back from being idle.
     801
     802You must include the following parameters:
     803
     804=over 4
     805
     806=item name
     807
     808The name given to the idle watcher
     809
     810=item after
     811
     812How long the user must be idle, in seconds, before the callback is called.
     813If the value is too small, you may have spurious or inaccurate calls.
     814(The current lower limit is about 1 second.)
     815
     816=item callback
     817
     818The Perl subroutine that gets called when the user has been idle for C<AFTER>
     819seconds, or comes back from being idle.  The subroutine is passed one parameter,
     820which is true if the user is currently idle, and false otherwise.
     821
     822=back
     823
     824This method returns a unique identifier which may be passed to
     825L<BarnOwl::unregister_idle_watcher>.
     826
     827=cut
     828
     829=head3 unregister_idle_watcher UNIQUE_ID [...]
     830
     831Removed and returns the idle watcher specified by C<UNIQUE_ID>.
     832You may specify multiple unique ids.
     833
     834=cut
     835
     836my %idle_watchers;
     837tie %idle_watchers, 'Tie::RefHash';
     838
     839$BarnOwl::Hooks::wakeup->add(sub {
     840        foreach my $idle_watcher (values %idle_watchers) {
     841            _wakeup_idle_watcher($idle_watcher);
     842        }
     843    });
     844
     845sub _wakeup_idle_watcher {
     846    my ($idle_watcher, $offset) = @_;
     847    $offset = 0 unless defined $offset;
     848    # go unidle
     849    $idle_watcher->{idle_timer}->stop if $idle_watcher->{idle_timer};
     850    undef $idle_watcher->{idle_timer};
     851    $idle_watcher->{callback}->(0) if $idle_watcher->{is_idle};
     852    $idle_watcher->{is_idle} = 0;
     853
     854    # queue going idle
     855    $idle_watcher->{idle_timer} = BarnOwl::Timer->new({
     856        name  => $idle_watcher->{name},
     857        after => $idle_watcher->{after} - $offset,
     858        cb    => sub {
     859            $idle_watcher->{is_idle} = 1;
     860            $idle_watcher->{callback}->(1);
     861        }
     862    });
     863}
     864
     865sub register_idle_watcher {
     866    my %args = (@_);
     867    $idle_watchers{\%args} = \%args;
     868    _wakeup_idle_watcher(\%args, BarnOwl::getidletime); # make sure to queue up the idle/unidle events from this idle watcher
     869    return \%args;
     870}
     871
     872sub unregister_idle_watcher {
     873    my ($id) = @_;
     874    $idle_watchers{$id}->{idle_timer}->stop if $idle_watchers{$id}->{idle_timer};
     875    return delete $idle_watchers{$id};
     876}
     877
    795878# Stub for owl::startup / BarnOwl::startup, so it isn't bound to the
    796879# startup command. This may be redefined in a user's configfile.
Note: See TracChangeset for help on using the changeset viewer.