|
|
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; |
|
|
38 | use strict; |
4 | |
39 | |
5 | use Glib (); |
40 | use Glib (); |
6 | |
41 | |
7 | my $maincontext = Glib::MainContext->default; |
42 | our $mainloop = Glib::MainContext->default; |
|
|
43 | |
|
|
44 | my %io_cond = ( |
|
|
45 | r => ["in" , "hup"], |
|
|
46 | w => ["out", "hup"], |
|
|
47 | ); |
8 | |
48 | |
9 | sub io { |
49 | sub io { |
10 | my ($class, %arg) = @_; |
50 | my ($class, %arg) = @_; |
11 | |
51 | |
12 | my $self = bless \%arg, $class; |
|
|
13 | my $rcb = \$self->{cb}; |
52 | my $cb = $arg{cb}; |
|
|
53 | my $fd = fileno $arg{fh}; |
|
|
54 | defined $fd or $fd = $arg{fh}; |
14 | |
55 | |
15 | # some glibs need hup, others error with it, YMMV |
56 | my $source = add_watch Glib::IO |
16 | push @cond, "in", "hup" if $self->{poll} eq "r"; |
57 | $fd, |
17 | push @cond, "out", "hup" if $self->{poll} eq "w"; |
58 | $io_cond{$arg{poll}}, |
|
|
59 | sub { &$cb; 1 }; |
18 | |
60 | |
19 | $self->{source} = add_watch Glib::IO fileno $self->{fh}, \@cond, sub { |
61 | bless \\$source, $class |
20 | $$rcb->(); |
|
|
21 | ! ! $$rcb |
|
|
22 | }; |
|
|
23 | |
|
|
24 | $self |
|
|
25 | } |
62 | } |
26 | |
63 | |
27 | sub timer { |
64 | sub timer { |
28 | my ($class, %arg) = @_; |
65 | my ($class, %arg) = @_; |
29 | |
66 | |
30 | my $self = bless \%arg, $class; |
|
|
31 | my $cb = $self->{cb}; |
67 | my $cb = $arg{cb}; |
|
|
68 | my $ival = $arg{interval} * 1000; |
32 | |
69 | |
33 | $self->{source} = add Glib::Timeout $self->{after} * 1000, sub { |
70 | my $source; $source = add Glib::Timeout $arg{after} * 1000, |
34 | $cb->(); |
71 | $ival ? sub { |
35 | 0 |
72 | remove Glib::Source $source; |
36 | }; |
73 | $source = add Glib::Timeout $ival, sub { &$cb; 1 }; |
|
|
74 | &$cb; |
|
|
75 | 0 |
|
|
76 | } |
|
|
77 | : sub { &$cb; 0 }; |
37 | |
78 | |
38 | $self |
79 | bless \\$source, $class |
|
|
80 | } |
|
|
81 | |
|
|
82 | sub idle { |
|
|
83 | my ($class, %arg) = @_; |
|
|
84 | |
|
|
85 | my $cb = $arg{cb}; |
|
|
86 | my $source = add Glib::Idle sub { &$cb; 1 }; |
|
|
87 | bless \\$source, $class |
39 | } |
88 | } |
40 | |
89 | |
41 | sub DESTROY { |
90 | sub DESTROY { |
42 | my ($self) = @_; |
91 | remove Glib::Source $${$_[0]}; |
43 | |
|
|
44 | remove Glib::Source delete $self->{source} if $self->{source}; |
|
|
45 | # need to undef $cb because we hold references to it |
|
|
46 | $self->{cb} = undef; |
|
|
47 | %$self = (); |
|
|
48 | } |
92 | } |
49 | |
93 | |
50 | sub one_event { |
94 | sub one_event { |
51 | $maincontext->iteration (1); |
95 | $mainloop->iteration (1); |
52 | } |
96 | } |
53 | |
97 | |
54 | 1 |
98 | sub loop { |
|
|
99 | # hackish, but we do not have a mainloop, just a maincontext |
|
|
100 | $mainloop->iteration (1) while 1; |
|
|
101 | } |
55 | |
102 | |
|
|
103 | 1; |
|
|
104 | |
|
|
105 | =head1 SEE ALSO |
|
|
106 | |
|
|
107 | L<AnyEvent>, L<Glib>. |
|
|
108 | |
|
|
109 | =head1 AUTHOR |
|
|
110 | |
|
|
111 | Marc Lehmann <schmorp@schmorp.de> |
|
|
112 | http://home.schmorp.de/ |
|
|
113 | |
|
|
114 | =cut |
|
|
115 | |