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 |
|
35 |
package AnyEvent::Impl::Glib; |
36 |
|
37 |
no warnings; |
38 |
use strict; |
39 |
|
40 |
use Glib (); |
41 |
|
42 |
our $maincontext = Glib::MainContext->default; |
43 |
|
44 |
sub io { |
45 |
my ($class, %arg) = @_; |
46 |
|
47 |
my $cb = $arg{cb}; |
48 |
|
49 |
my @cond; |
50 |
# some glibs need hup, others error with it, YMMV |
51 |
push @cond, "in", "hup" if $arg{poll} eq "r"; |
52 |
push @cond, "out", "hup" if $arg{poll} eq "w"; |
53 |
|
54 |
my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub { &$cb; 1 }; |
55 |
bless \\$source, $class |
56 |
} |
57 |
|
58 |
sub timer { |
59 |
my ($class, %arg) = @_; |
60 |
|
61 |
my $cb = $arg{cb}; |
62 |
my $ival = $arg{interval} * 1000; |
63 |
|
64 |
my $source; $source = add Glib::Timeout $arg{after} * 1000, |
65 |
$ival ? sub { |
66 |
remove Glib::Source $source; |
67 |
$source = add Glib::Timeout $ival, sub { &$cb; 1 }; |
68 |
&$cb; |
69 |
0 |
70 |
} |
71 |
: sub { &$cb; 0 }; |
72 |
|
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 |
82 |
} |
83 |
|
84 |
sub DESTROY { |
85 |
remove Glib::Source $${$_[0]}; |
86 |
} |
87 |
|
88 |
sub one_event { |
89 |
$maincontext->iteration (1); |
90 |
} |
91 |
|
92 |
1; |
93 |
|
94 |
=head1 SEE ALSO |
95 |
|
96 |
L<AnyEvent>, L<Glib>. |
97 |
|
98 |
=head1 AUTHOR |
99 |
|
100 |
Marc Lehmann <schmorp@schmorp.de> |
101 |
http://home.schmorp.de/ |
102 |
|
103 |
=cut |
104 |
|