|
|
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 of |
|
|
20 | them, whether I/O-related, timer-related or what 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 | On the positive side, and most importantly, Glib generally works |
|
|
25 | correctly, no quarrels there. |
|
|
26 | |
|
|
27 | If you create many watchers (as in: more than two), you might consider one |
|
|
28 | of the L<Glib::EV>, L<EV::Glib> or L<Glib::Event> modules that map Glib to |
|
|
29 | other, more efficient, event loops. |
|
|
30 | |
|
|
31 | This module uses the default Glib main context for all its watchers. |
|
|
32 | |
|
|
33 | =cut |
|
|
34 | |
1 | package AnyEvent::Impl::Glib; |
35 | package AnyEvent::Impl::Glib; |
2 | |
36 | |
3 | no warnings; |
37 | no warnings; |
4 | use strict; |
38 | use strict; |
5 | |
39 | |
6 | use Glib (); |
40 | use Glib (); |
7 | |
41 | |
8 | my $maincontext = Glib::MainContext->default; |
42 | our $mainloop = Glib::MainContext->default; |
9 | |
43 | |
10 | sub io { |
44 | sub io { |
11 | my ($class, %arg) = @_; |
45 | my ($class, %arg) = @_; |
12 | |
46 | |
13 | my $self = bless \%arg, $class; |
|
|
14 | my $rcb = \$self->{cb}; |
47 | my $cb = $arg{cb}; |
15 | |
48 | |
16 | my @cond; |
49 | my @cond; |
17 | # some glibs need hup, others error with it, YMMV |
50 | # some glibs need hup, others error with it, YMMV |
18 | push @cond, "in", "hup" if $self->{poll} eq "r"; |
51 | push @cond, "in", "hup" if $arg{poll} eq "r"; |
19 | push @cond, "out", "hup" if $self->{poll} eq "w"; |
52 | push @cond, "out", "hup" if $arg{poll} eq "w"; |
20 | |
53 | |
21 | $self->{source} = add_watch Glib::IO fileno $self->{fh}, \@cond, sub { |
54 | my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub { &$cb; 1 }; |
22 | $$rcb->(); |
55 | bless \\$source, $class |
23 | ! ! $$rcb |
|
|
24 | }; |
|
|
25 | |
|
|
26 | $self |
|
|
27 | } |
56 | } |
28 | |
57 | |
29 | sub timer { |
58 | sub timer { |
30 | my ($class, %arg) = @_; |
59 | my ($class, %arg) = @_; |
31 | |
60 | |
32 | my $self = bless \%arg, $class; |
|
|
33 | my $cb = $self->{cb}; |
61 | my $cb = $arg{cb}; |
|
|
62 | my $ival = $arg{interval} * 1000; |
34 | |
63 | |
35 | $self->{source} = add Glib::Timeout $self->{after} * 1000, sub { |
64 | my $source; $source = add Glib::Timeout $arg{after} * 1000, |
36 | $cb->(); |
65 | $ival ? sub { |
37 | 0 |
66 | remove Glib::Source $source; |
38 | }; |
67 | $source = add Glib::Timeout $ival, sub { &$cb; 1 }; |
|
|
68 | &$cb; |
|
|
69 | 0 |
|
|
70 | } |
|
|
71 | : sub { &$cb; 0 }; |
39 | |
72 | |
40 | $self |
73 | bless \\$source, $class |
|
|
74 | } |
|
|
75 | |
|
|
76 | sub idle { |
|
|
77 | my ($class, %arg) = @_; |
|
|
78 | |
|
|
79 | my $cb = $arg{cb}; |
|
|
80 | my $source = add Glib::Idle sub { &$cb; 1 }; |
|
|
81 | bless \\$source, $class |
41 | } |
82 | } |
42 | |
83 | |
43 | sub DESTROY { |
84 | sub DESTROY { |
44 | my ($self) = @_; |
85 | 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 | } |
86 | } |
51 | |
87 | |
52 | sub one_event { |
88 | sub one_event { |
53 | $maincontext->iteration (1); |
89 | $mainloop->iteration (1); |
54 | } |
90 | } |
55 | |
91 | |
56 | 1 |
92 | sub loop { |
|
|
93 | $mainloop->iteration (1) while 1; # hackish, but w ehave no mainloop |
|
|
94 | } |
57 | |
95 | |
|
|
96 | 1; |
|
|
97 | |
|
|
98 | =head1 SEE ALSO |
|
|
99 | |
|
|
100 | L<AnyEvent>, L<Glib>. |
|
|
101 | |
|
|
102 | =head1 AUTHOR |
|
|
103 | |
|
|
104 | Marc Lehmann <schmorp@schmorp.de> |
|
|
105 | http://home.schmorp.de/ |
|
|
106 | |
|
|
107 | =cut |
|
|
108 | |