| 1 | =head1 NAME | ||
| 2 | |||
| 3 | AnyEvent::Impl::Prima - Prima event loop adapter for AnyEvent | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use Prima; | ||
| 8 | use AnyEvent::Impl::Prima; | ||
| 9 | |||
| 10 | my $mw = Prima::MainWindow->new(); | ||
| 11 | |||
| 12 | my $timer = AnyEvent->timer( | ||
| 13 | after => 10, | ||
| 14 | cb => sub { $mw->close; }, | ||
| 15 | ); | ||
| 16 | |||
| 17 | Prima->run; | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | { | ||
| 22 | |||
| 23 | package AnyEvent::Impl::Prima 0.03; | ||
| 24 | use strict; | ||
| 25 | |||
| 26 | use AnyEvent; | ||
| 27 | require Prima; | ||
| 28 | require Prima::Application; | ||
| 29 | |||
| 30 | |||
| 31 | sub io { my ($s,%r) = @_; | ||
| 32 | my $f = Prima::File->new( | ||
| 33 | mask => ("w" eq $r{poll} ? fe::Write() : fe::Read()), | ||
| 34 | onRead => $r{cb}, | ||
| 35 | onWrite => $r{cb}, | ||
| 36 | onException => $r{cb} | ||
| 37 | ); | ||
| 38 | if( ! ref $r{fh}) { | ||
| 39 | $f->fd( $r{fh} ) | ||
| 40 | } else { | ||
| 41 | $f->file( $r{fh} ) | ||
| 42 | }; | ||
| 43 | $f | ||
| 44 | } | ||
| 45 | |||
| 46 | sub AnyEvent::Impl::Prima::Timer::DESTROY { ${$_[0]}->destroy if $_[0] and ${$_[0]} } | ||
| 47 | |||
| 48 | sub timer { my ( $s, %r ) = @_; | ||
| 49 | my($c,$g) = $r{cb}; | ||
| 50 | |||
| 51 | my $next = $r{ after } || $r{ interval }; | ||
| 52 | my $repeat = delete $r{ interval }; | ||
| 53 | |||
| 54 | |||
| 55 | # Convert to miliseconds for Prima | ||
| 56 | $next *= 1000; | ||
| 57 | $repeat *= 1000 if $repeat; | ||
| 58 | |||
| 59 | my %timer_params = ( | ||
| 60 | timeout => $next, | ||
| 61 | ); | ||
| 62 | my $timer = Prima::Timer->new( | ||
| 63 | timeout => $next, | ||
| 64 | onTick => sub { | ||
| 65 | #warn "Timer $_[0] fired"; | ||
| 66 | if( $repeat ) { | ||
| 67 | $_[0]->timeout( $repeat ); | ||
| 68 | } else { | ||
| 69 | $_[0]->stop; | ||
| 70 | }; | ||
| 71 | &$c() | ||
| 72 | }, | ||
| 73 | onDestroy => sub { my ( $self ) = @_; | ||
| 74 | #warn "Discarding $self"; | ||
| 75 | $self->stop; | ||
| 76 | }, | ||
| 77 | ); | ||
| 78 | #warn "Starting new timer $res"; | ||
| 79 | $timer->start; | ||
| 80 | return bless \ $timer, "AnyEvent::Impl::Prima::Timer"; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub poll { | ||
| 84 | require Prima::Application; | ||
| 85 | $::application->yield; | ||
| 86 | } | ||
| 87 | |||
| 88 | { | ||
| 89 | no warnings 'redefine'; | ||
| 90 | sub AnyEvent::CondVar::Base::_wait { | ||
| 91 | require Prima::Application; | ||
| 92 | $::application->yield until exists $_[0]{_ae_sent}; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | push @AnyEvent::REGISTRY,["Prima",__PACKAGE__]; | ||
| 97 | |||
| 98 | } | ||
| 99 | __END__ |