line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# You may distribute under the terms of either the GNU General Public License |
2
|
|
|
|
|
|
|
# or the Artistic License (the same terms as Perl itself) |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# (C) Paul Evans, 2011-2013 -- leonerd@leonerd.org.uk |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Tickit::Async; |
7
|
|
|
|
|
|
|
|
8
|
5
|
|
|
5
|
|
260202
|
use strict; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
164
|
|
9
|
5
|
|
|
5
|
|
21
|
use warnings; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
182
|
|
10
|
5
|
|
|
5
|
|
29
|
use base qw( Tickit IO::Async::Notifier ); |
|
5
|
|
|
|
|
6
|
|
|
5
|
|
|
|
|
4864
|
|
11
|
|
|
|
|
|
|
Tickit->VERSION( '0.17' ); |
12
|
|
|
|
|
|
|
IO::Async::Notifier->VERSION( '0.43' ); # Need support for being a nonprinciple mixin |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.19'; |
15
|
|
|
|
|
|
|
|
16
|
5
|
|
|
5
|
|
64405
|
use IO::Async::Loop 0.47; # ->run and ->stop methods |
|
5
|
|
|
|
|
36230
|
|
|
5
|
|
|
|
|
160
|
|
17
|
5
|
|
|
5
|
|
2742
|
use IO::Async::Signal; |
|
5
|
|
|
|
|
2869
|
|
|
5
|
|
|
|
|
119
|
|
18
|
5
|
|
|
5
|
|
3181
|
use IO::Async::Stream; |
|
5
|
|
|
|
|
96027
|
|
|
5
|
|
|
|
|
217
|
|
19
|
5
|
|
|
5
|
|
39
|
use IO::Async::Handle; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
122
|
|
20
|
5
|
|
|
5
|
|
2692
|
use IO::Async::Timer::Countdown; |
|
5
|
|
|
|
|
7142
|
|
|
5
|
|
|
|
|
2152
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 NAME |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
C - use C with C |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
use IO::Async; |
29
|
|
|
|
|
|
|
use Tickit::Async; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $tickit = Tickit::Async->new; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Create some widgets |
34
|
|
|
|
|
|
|
# ... |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$tickit->set_root_widget( $rootwidget ); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $loop = IO::Async::Loop->new; |
39
|
|
|
|
|
|
|
$loop->add( $tickit ); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
$tickit->run; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 DESCRIPTION |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
This class allows a L user interface to run alongside other |
46
|
|
|
|
|
|
|
L-driven code, using C as a source of IO events. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
As a shortcut convenience, a containing L will be constructed |
49
|
|
|
|
|
|
|
using the default magic constructor the first time it is needed, if the object |
50
|
|
|
|
|
|
|
is not already a member of a loop. This will allow a C object |
51
|
|
|
|
|
|
|
to be used without being aware it is not a simple C object. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
To avoid accidentally creating multiple loops, callers should be careful to |
54
|
|
|
|
|
|
|
C the C object to the main application's loop if one |
55
|
|
|
|
|
|
|
already exists as soon as possible after construction. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=cut |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub new |
60
|
|
|
|
|
|
|
{ |
61
|
0
|
|
|
0
|
|
|
my $class = shift; |
62
|
0
|
|
|
|
|
|
my $self = $class->Tickit::new( @_ ); |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
$self->add_child( IO::Async::Signal->new( |
65
|
|
|
|
|
|
|
name => "WINCH", |
66
|
|
|
|
|
|
|
on_receipt => $self->_capture_weakself( "_SIGWINCH" ), |
67
|
|
|
|
|
|
|
) ); |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
$self->add_child( IO::Async::Handle->new( |
70
|
|
|
|
|
|
|
read_handle => $self->term->get_input_handle, |
71
|
|
|
|
|
|
|
on_read_ready => $self->_capture_weakself( "_input_readready" ), |
72
|
|
|
|
|
|
|
) ); |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
$self->add_child( $self->{timer} = IO::Async::Timer::Countdown->new( |
75
|
|
|
|
|
|
|
on_expire => $self->_capture_weakself( "_timeout" ), |
76
|
|
|
|
|
|
|
) ); |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
return $self; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub get_loop |
82
|
|
|
|
|
|
|
{ |
83
|
0
|
|
|
0
|
|
|
my $self = shift; |
84
|
0
|
|
0
|
|
|
|
return $self->SUPER::get_loop || do { |
85
|
|
|
|
|
|
|
my $newloop = IO::Async::Loop->new; |
86
|
|
|
|
|
|
|
$newloop->add( $self ); |
87
|
|
|
|
|
|
|
$newloop; |
88
|
|
|
|
|
|
|
}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub _make_writer |
92
|
|
|
|
|
|
|
{ |
93
|
0
|
|
|
0
|
|
|
my $self = shift; |
94
|
0
|
|
|
|
|
|
my ( $out ) = @_; |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
my $writer = IO::Async::Stream->new( |
97
|
|
|
|
|
|
|
write_handle => $out, |
98
|
|
|
|
|
|
|
autoflush => 1, |
99
|
|
|
|
|
|
|
); |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
$self->add_child( $writer ); |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
return $writer; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub _input_readready |
107
|
|
|
|
|
|
|
{ |
108
|
0
|
|
|
0
|
|
|
my $self = shift; |
109
|
0
|
|
|
|
|
|
my $term = $self->term; |
110
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
$self->{timer}->stop; |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
$term->input_readable; |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
$self->_timeout; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub _timeout |
119
|
|
|
|
|
|
|
{ |
120
|
0
|
|
|
0
|
|
|
my $self = shift; |
121
|
0
|
|
|
|
|
|
my $term = $self->term; |
122
|
|
|
|
|
|
|
|
123
|
0
|
0
|
|
|
|
|
if( defined( my $timeout = $term->check_timeout ) ) { |
124
|
0
|
|
|
|
|
|
$self->{timer}->configure( delay => $timeout / 1000 ); # msec |
125
|
0
|
|
|
|
|
|
$self->{timer}->start; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub later |
130
|
|
|
|
|
|
|
{ |
131
|
0
|
|
|
0
|
|
|
my $self = shift; |
132
|
0
|
|
|
|
|
|
my ( $code ) = @_; |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
$self->get_loop->later( $code ); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub timer |
138
|
|
|
|
|
|
|
{ |
139
|
0
|
|
|
0
|
|
|
my $self = shift; |
140
|
0
|
|
|
|
|
|
my ( $mode, $amount, $code ) = @_; |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
$self->get_loop->watch_time( $mode => $amount, code => $code ); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub tick |
146
|
|
|
|
|
|
|
{ |
147
|
0
|
|
|
0
|
|
|
my $self = shift; |
148
|
0
|
|
|
|
|
|
$self->get_loop->loop_once; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub stop |
152
|
|
|
|
|
|
|
{ |
153
|
0
|
|
|
0
|
|
|
my $self = shift; |
154
|
0
|
|
|
|
|
|
$self->get_loop->stop; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub run |
158
|
|
|
|
|
|
|
{ |
159
|
0
|
|
|
0
|
|
|
my $self = shift; |
160
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
my $loop = $self->get_loop; |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
$self->setup_term; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
$loop->add( my $sigint_notifier = IO::Async::Signal->new( |
166
|
|
|
|
|
|
|
name => "INT", |
167
|
|
|
|
|
|
|
on_receipt => $self->_capture_weakself( sub { |
168
|
0
|
|
|
0
|
|
|
my $self = shift; |
169
|
0
|
0
|
|
|
|
|
if( my $loop = $self->get_loop ) { |
170
|
0
|
|
|
|
|
|
$loop->stop |
171
|
|
|
|
|
|
|
} |
172
|
0
|
|
|
|
|
|
}), |
173
|
|
|
|
|
|
|
) ); |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
my $ret = eval { $loop->run }; |
|
0
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
my $e = $@; |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
{ |
179
|
0
|
|
|
|
|
|
local $@; |
|
0
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
|
181
|
0
|
|
|
|
|
|
$self->teardown_term; |
182
|
0
|
|
|
|
|
|
$loop->remove( $sigint_notifier ); |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
0
|
0
|
|
|
|
|
die $@ if $@; |
186
|
0
|
|
|
|
|
|
return $ret; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head1 AUTHOR |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Paul Evans |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=cut |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
0x55AA; |