… | |
… | |
39 | our $maincontext = Glib::MainContext->default; |
39 | our $maincontext = Glib::MainContext->default; |
40 | |
40 | |
41 | sub io { |
41 | sub io { |
42 | my ($class, %arg) = @_; |
42 | my ($class, %arg) = @_; |
43 | |
43 | |
44 | my $self = bless \%arg, $class; |
44 | my $fh = $arg{fh}; |
45 | my $rcb = \$self->{cb}; |
45 | my $cb = $arg{cb}; |
46 | |
46 | |
47 | my @cond; |
47 | my @cond; |
48 | # some glibs need hup, others error with it, YMMV |
48 | # some glibs need hup, others error with it, YMMV |
49 | push @cond, "in", "hup" if $self->{poll} eq "r"; |
49 | push @cond, "in", "hup" if $arg{poll} eq "r"; |
50 | push @cond, "out", "hup" if $self->{poll} eq "w"; |
50 | push @cond, "out", "hup" if $arg{poll} eq "w"; |
51 | |
51 | |
52 | $self->{source} = add_watch Glib::IO fileno $self->{fh}, \@cond, sub { |
52 | my $source = add_watch Glib::IO fileno $arg{fh}, \@cond, sub { |
53 | $$rcb->(); |
53 | &$cb; |
54 | ! ! $$rcb |
54 | $fh; # mention it here to keep it from being destroyed |
|
|
55 | 1 |
55 | }; |
56 | }; |
56 | |
57 | |
57 | $self |
58 | bless \\$source, $class |
58 | } |
59 | } |
59 | |
60 | |
60 | sub timer { |
61 | sub timer { |
61 | my ($class, %arg) = @_; |
62 | my ($class, %arg) = @_; |
62 | |
63 | |
63 | my $self = bless \%arg, $class; |
|
|
64 | my $cb = $self->{cb}; |
64 | my $cb = $arg{cb}; |
|
|
65 | my $rp = $arg{repeat}; |
65 | |
66 | |
66 | $self->{source} = add Glib::Timeout $self->{after} * 1000, sub { |
67 | my $source = add Glib::Timeout 1000 * delete $arg{after}, sub { |
67 | $cb->(); |
68 | &$cb; |
68 | 0 |
69 | $rp |
69 | }; |
70 | }; |
70 | |
71 | |
71 | $self |
72 | bless \\$source, $class |
72 | } |
73 | } |
73 | |
74 | |
74 | sub DESTROY { |
75 | sub DESTROY { |
75 | my ($self) = @_; |
76 | remove Glib::Source $${$_[0]}; |
76 | |
|
|
77 | remove Glib::Source delete $self->{source} if $self->{source}; |
|
|
78 | # need to undef $cb because we hold references to it |
|
|
79 | $self->{cb} = undef; |
|
|
80 | } |
77 | } |
81 | |
78 | |
82 | sub one_event { |
79 | sub one_event { |
83 | $maincontext->iteration (1); |
80 | $maincontext->iteration (1); |
84 | } |
81 | } |