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 |
use AnyEvent (); BEGIN { AnyEvent::common_sense } |
38 |
use Glib (); |
39 |
|
40 |
our $mainloop = Glib::MainContext->default; |
41 |
|
42 |
my %io_cond = ( |
43 |
r => ["in" , "hup"], |
44 |
w => ["out", "hup"], |
45 |
); |
46 |
|
47 |
sub io { |
48 |
my ($class, %arg) = @_; |
49 |
|
50 |
my $cb = $arg{cb}; |
51 |
my $fd = fileno $arg{fh}; |
52 |
defined $fd or $fd = $arg{fh}; |
53 |
|
54 |
my $source = add_watch Glib::IO |
55 |
$fd, |
56 |
$io_cond{$arg{poll}}, |
57 |
sub { &$cb; 1 }; |
58 |
|
59 |
bless \\$source, $class |
60 |
} |
61 |
|
62 |
sub timer { |
63 |
my ($class, %arg) = @_; |
64 |
|
65 |
my $cb = $arg{cb}; |
66 |
my $ival = $arg{interval} * 1000; |
67 |
|
68 |
my $source; $source = add Glib::Timeout $arg{after} < 0 ? 0 : $arg{after} * 1000, |
69 |
$ival ? sub { |
70 |
remove Glib::Source $source; |
71 |
$source = add Glib::Timeout $ival, sub { &$cb; 1 }; |
72 |
&$cb; |
73 |
0 |
74 |
} |
75 |
: sub { &$cb; 0 }; |
76 |
|
77 |
bless \\$source, $class |
78 |
} |
79 |
|
80 |
sub idle { |
81 |
my ($class, %arg) = @_; |
82 |
|
83 |
my $cb = $arg{cb}; |
84 |
my $source = add Glib::Idle sub { &$cb; 1 }; |
85 |
bless \\$source, $class |
86 |
} |
87 |
|
88 |
sub DESTROY { |
89 |
remove Glib::Source $${$_[0]}; |
90 |
} |
91 |
|
92 |
sub one_event { |
93 |
$mainloop->iteration (1); |
94 |
} |
95 |
|
96 |
sub loop { |
97 |
# hackish, but we do not have a mainloop, just a maincontext |
98 |
$mainloop->iteration (1) while 1; |
99 |
} |
100 |
|
101 |
1; |
102 |
|
103 |
=head1 SEE ALSO |
104 |
|
105 |
L<AnyEvent>, L<Glib>. |
106 |
|
107 |
=head1 AUTHOR |
108 |
|
109 |
Marc Lehmann <schmorp@schmorp.de> |
110 |
http://home.schmorp.de/ |
111 |
|
112 |
=cut |
113 |
|