… | |
… | |
39 | |
39 | |
40 | use Glib (); |
40 | use Glib (); |
41 | |
41 | |
42 | our $mainloop = Glib::MainContext->default; |
42 | our $mainloop = Glib::MainContext->default; |
43 | |
43 | |
|
|
44 | my %io_cond = ( |
|
|
45 | r => ["in" , "hup"], |
|
|
46 | w => ["out", "hup"], |
|
|
47 | ); |
|
|
48 | |
44 | sub io { |
49 | sub io { |
45 | my ($class, %arg) = @_; |
50 | my ($class, %arg) = @_; |
46 | |
51 | |
47 | my $cb = $arg{cb}; |
52 | my $cb = $arg{cb}; |
|
|
53 | my $fd = fileno $arg{fh}; |
|
|
54 | defined $fd or $fd = $arg{fh}; |
48 | |
55 | |
49 | my @cond; |
56 | my $source = add_watch Glib::IO |
50 | # some glibs need hup, others error with it, YMMV |
57 | $fd, |
51 | push @cond, "in", "hup" if $arg{poll} eq "r"; |
58 | $io_cond{$arg{poll}}, |
52 | push @cond, "out", "hup" if $arg{poll} eq "w"; |
59 | sub { &$cb; 1 }; |
53 | |
60 | |
54 | my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub { &$cb; 1 }; |
|
|
55 | bless \\$source, $class |
61 | bless \\$source, $class |
56 | } |
62 | } |
57 | |
63 | |
58 | sub timer { |
64 | sub timer { |
59 | my ($class, %arg) = @_; |
65 | my ($class, %arg) = @_; |
… | |
… | |
88 | sub one_event { |
94 | sub one_event { |
89 | $mainloop->iteration (1); |
95 | $mainloop->iteration (1); |
90 | } |
96 | } |
91 | |
97 | |
92 | sub loop { |
98 | sub loop { |
93 | $mainloop->iteration (1) while 1; # hackish, but w ehave no mainloop |
99 | # hackish, but we do not have a mainloop, just a maincontext |
|
|
100 | $mainloop->iteration (1) while 1; |
94 | } |
101 | } |
95 | |
102 | |
96 | 1; |
103 | 1; |
97 | |
104 | |
98 | =head1 SEE ALSO |
105 | =head1 SEE ALSO |