ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Impl/Glib.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Impl/Glib.pm (file contents):
Revision 1.23 by root, Mon Jun 22 11:17:03 2009 UTC vs.
Revision 1.29 by root, Thu Aug 4 09:14:02 2011 UTC

32 32
33=cut 33=cut
34 34
35package AnyEvent::Impl::Glib; 35package AnyEvent::Impl::Glib;
36 36
37no warnings; 37use AnyEvent (); BEGIN { AnyEvent::common_sense }
38use strict; 38use Glib 1.210 (); # (stable 1.220 2009, also Glib 2.4+ required, 2004)
39
40use Glib ();
41 39
42our $mainloop = Glib::MainContext->default; 40our $mainloop = Glib::MainContext->default;
41
42my %io_cond = (
43 r => ["in" , "hup"],
44 w => ["out", "hup"],
45);
43 46
44sub io { 47sub io {
45 my ($class, %arg) = @_; 48 my ($class, %arg) = @_;
46 49
47 my $cb = $arg{cb}; 50 my $cb = $arg{cb};
51 my $fd = fileno $arg{fh};
52 defined $fd or $fd = $arg{fh};
48 53
49 my @cond; 54 my $source = add_watch Glib::IO
50 # some glibs need hup, others error with it, YMMV 55 $fd,
51 push @cond, "in", "hup" if $arg{poll} eq "r"; 56 $io_cond{$arg{poll}},
52 push @cond, "out", "hup" if $arg{poll} eq "w"; 57 sub { &$cb; 1 };
53 58
54 my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub { &$cb; 1 };
55 bless \\$source, $class 59 bless \\$source, $class
56} 60}
57 61
58sub timer { 62sub timer {
59 my ($class, %arg) = @_; 63 my ($class, %arg) = @_;
60 64
61 my $cb = $arg{cb}; 65 my $cb = $arg{cb};
62 my $ival = $arg{interval} * 1000; 66 my $ival = $arg{interval} * 1000;
63 67
64 my $source; $source = add Glib::Timeout $arg{after} * 1000, 68 my $source; $source = add Glib::Timeout $arg{after} < 0 ? 0 : $arg{after} * 1000,
65 $ival ? sub { 69 $ival ? sub {
66 remove Glib::Source $source; 70 remove Glib::Source $source;
67 $source = add Glib::Timeout $ival, sub { &$cb; 1 }; 71 $source = add Glib::Timeout $ival, sub { &$cb; 1 };
68 &$cb; 72 &$cb;
69 0 73 0
76sub idle { 80sub idle {
77 my ($class, %arg) = @_; 81 my ($class, %arg) = @_;
78 82
79 my $cb = $arg{cb}; 83 my $cb = $arg{cb};
80 my $source = add Glib::Idle sub { &$cb; 1 }; 84 my $source = add Glib::Idle sub { &$cb; 1 };
85
81 bless \\$source, $class 86 bless \\$source, $class
82} 87}
83 88
84sub DESTROY { 89sub DESTROY {
85 remove Glib::Source $${$_[0]}; 90 remove Glib::Source $${$_[0]};
86} 91}
87 92
88sub one_event { 93our %pid_w;
94our %pid_cb;
95
96sub child {
97 my ($class, %arg) = @_;
98
99 $arg{pid} > 0
100 or Carp::croak "Glib does not support watching for all pids (pid == 0) as attempted";
101
102 my $pid = $arg{pid};
103 my $cb = $arg{cb};
104
105 $pid_cb{$pid}{$cb+0} = $cb;
106
107 $pid_w{$pid} ||= Glib::Child->watch_add ($pid, sub {
108 $_->($_[0], $_[1])
109 for values %{ $pid_cb{$pid} };
110
111 1
112 });
113
114 bless [$pid, $cb+0], "AnyEvent::Impl::Glib::child"
115}
116
117sub AnyEvent::Impl::Glib::child::DESTROY {
118 my ($pid, $icb) = @{ $_[0] };
119
120 delete $pid_cb{$pid}{$icb};
121 unless (%{ $pid_cb{$pid} }) {
122 delete $pid_cb{$pid};
123 remove Glib::Source delete $pid_w{$pid};
124 }
125}
126
127#sub loop {
128# # hackish, but we do not have a mainloop, just a maincontext
129# $mainloop->iteration (1) while 1;
130#}
131
132sub _poll {
89 $mainloop->iteration (1); 133 $mainloop->iteration (1);
90} 134}
91 135
92sub loop { 136sub AnyEvent::CondVar::Base::_wait {
93 $mainloop->iteration (1) while 1; # hackish, but w ehave no mainloop 137 $mainloop->iteration (1) until $_[0]{_ae_sent};
94} 138}
95 139
961; 1401;
97 141
98=head1 SEE ALSO 142=head1 SEE ALSO

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines