|
|
1 | =head1 NAME |
|
|
2 | |
|
|
3 | AnyEvent::Impl::Glib - AnyEvent adaptor for Glib |
|
|
4 | |
|
|
5 | =head1 SYNOPSIS |
|
|
6 | |
|
|
7 | use AnyEvent; |
|
|
8 | use Glib; |
|
|
9 | |
|
|
10 | # this module gets loaded automatically as required |
|
|
11 | |
|
|
12 | =head1 DESCRIPTION |
|
|
13 | |
|
|
14 | This module provides transparent support for AnyEvent. You don't have to |
|
|
15 | do anything to make Glib work with AnyEvent except by loading Glib before |
|
|
16 | creating the first AnyEvent watcher. |
|
|
17 | |
|
|
18 | Glib is probably the most inefficient event loop that has ever seen the |
|
|
19 | light of the world: Glib not only scans all its watchers (really, ALL |
|
|
20 | of them, whether I/O-related, timer-related or not) during each loop |
|
|
21 | iteration, it also does so multiple times and rebuilds the poll list for |
|
|
22 | the kernel each time again, dynamically even. |
|
|
23 | |
|
|
24 | If you create many watchers (as in: more than two), you might consider one |
|
|
25 | of the L<Glib::EV>, L<EV::Glib> or L<Glib::Event> modules that map Glib to |
|
|
26 | other, more efficient, event loops. |
|
|
27 | |
|
|
28 | This module uses the default Glib main context for all it's watchers. |
|
|
29 | |
|
|
30 | =cut |
|
|
31 | |
1 | package AnyEvent::Impl::Glib; |
32 | package AnyEvent::Impl::Glib; |
2 | |
33 | |
3 | no warnings; |
34 | no warnings; |
4 | use strict; |
35 | use strict; |
5 | |
36 | |
6 | use Glib (); |
37 | use Glib (); |
7 | |
38 | |
8 | my $maincontext = Glib::MainContext->default; |
39 | our $maincontext = Glib::MainContext->default; |
9 | |
40 | |
10 | sub io { |
41 | sub io { |
11 | my ($class, %arg) = @_; |
42 | my ($class, %arg) = @_; |
12 | |
43 | |
13 | my $self = bless \%arg, $class; |
44 | my $fh = $arg{fh}; |
14 | my $rcb = \$self->{cb}; |
45 | my $cb = $arg{cb}; |
15 | |
46 | |
16 | my @cond; |
47 | my @cond; |
17 | # some glibs need hup, others error with it, YMMV |
48 | # some glibs need hup, others error with it, YMMV |
18 | push @cond, "in", "hup" if $self->{poll} eq "r"; |
49 | push @cond, "in", "hup" if $arg{poll} eq "r"; |
19 | push @cond, "out", "hup" if $self->{poll} eq "w"; |
50 | push @cond, "out", "hup" if $arg{poll} eq "w"; |
20 | |
51 | |
21 | $self->{source} = add_watch Glib::IO fileno $self->{fh}, \@cond, sub { |
52 | my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub { |
22 | $$rcb->(); |
53 | &$cb; |
23 | ! ! $$rcb |
54 | $fh; # mention it here to keep it from being destroyed |
|
|
55 | 1 |
24 | }; |
56 | }; |
25 | |
57 | |
26 | $self |
58 | bless \\$source, $class |
27 | } |
59 | } |
28 | |
60 | |
29 | sub timer { |
61 | sub timer { |
30 | my ($class, %arg) = @_; |
62 | my ($class, %arg) = @_; |
31 | |
63 | |
32 | my $self = bless \%arg, $class; |
|
|
33 | my $cb = $self->{cb}; |
64 | my $cb = $arg{cb}; |
|
|
65 | my $rp = $arg{repeat}; |
34 | |
66 | |
35 | $self->{source} = add Glib::Timeout $self->{after} * 1000, sub { |
67 | my $source = add Glib::Timeout 1000 * delete $arg{after}, sub { |
36 | $cb->(); |
68 | &$cb; |
37 | 0 |
69 | $rp |
38 | }; |
70 | }; |
39 | |
71 | |
40 | $self |
72 | bless \\$source, $class |
41 | } |
73 | } |
42 | |
74 | |
43 | sub DESTROY { |
75 | sub DESTROY { |
44 | my ($self) = @_; |
76 | remove Glib::Source $${$_[0]}; |
45 | |
|
|
46 | remove Glib::Source delete $self->{source} if $self->{source}; |
|
|
47 | # need to undef $cb because we hold references to it |
|
|
48 | $self->{cb} = undef; |
|
|
49 | %$self = (); |
|
|
50 | } |
77 | } |
51 | |
78 | |
52 | sub one_event { |
79 | sub one_event { |
53 | $maincontext->iteration (1); |
80 | $maincontext->iteration (1); |
54 | } |
81 | } |
55 | |
82 | |
56 | 1 |
83 | 1; |
57 | |
84 | |
|
|
85 | =head1 SEE ALSO |
|
|
86 | |
|
|
87 | L<AnyEvent>, L<Glib>. |
|
|
88 | |
|
|
89 | =head1 AUTHOR |
|
|
90 | |
|
|
91 | Marc Lehmann <schmorp@schmorp.de> |
|
|
92 | http://home.schmorp.de/ |
|
|
93 | |
|
|
94 | =cut |
|
|
95 | |