blib/lib/XAS/Lib/Curses/Root.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 98 CC 10 SCAR 33.0
CC
10
Cov
0%
key_handler
72.0
BEGIN
6.0
init
6.0
CRAP 27.0
1 package XAS::Lib::Curses::Root;
2  
3 my $mixin;
4 BEGIN {
5 $mixin = 'XAS::Lib::Curses::Unix';
6 $mixin = 'XAS::Lib::Curses::Win32' if ($^O eq 'MSWin32');
7 }
8  
9 use POE;
10 use Curses;
11 use POE::Component::Curses::MainLoop;
12  
13 use XAS::Class
14 debug => 0,
15 version => '0.01',
16 base => 'XAS::Base',
17 mixin => $mixin,
18 ;
19  
20 # ----------------------------------------------------------------------
21 # Public Methods
22 # ----------------------------------------------------------------------
23  
24 sub key_handler {
25 my ( $kernel, $heap, $keystroke ) = @_[ KERNEL, HEAP, ARG0 ];
26
27 if ( $keystroke ne -1 ) {
28  
29 if ( $keystroke lt ' ' ) {
30  
31 $keystroke = '<' . uc( unctrl($keystroke) ) . '>';
32  
33 } elsif ( $keystroke =~ /^\d{2,}$/ ) {
34  
35 $keystroke = '<' . uc( keyname($keystroke) ) . '>';
36  
37 }
38  
39 if ( $keystroke eq '<KEY_RESIZE>' ) {
40
41 # don't handle this here, it's handled in window_resize
42  
43 return;
44  
45 } elsif ( $keystroke eq '<KEY_MOUSE>' ) {
46
47 # the mouse is handled differently depending on platform
48  
49 my ($id, $x, $y, $z, $bstate) = get_mouse_event();
50 handle_mouse_event($id, $x, $y, $z, $bstate, $heap);
51  
52 } else {
53
54 if ( $keystroke eq '<^L>' ) {
55  
56 $kernel->yield('window_resize');
57  
58 } elsif ( $keystroke eq '<^C>' ) {
59  
60 exit();
61  
62 } else {
63  
64 $heap->{mainloop}->event_key(
65 type => 'stroke',
66 key => $keystroke,
67 );
68  
69 }
70  
71 }
72  
73 }
74
75 }
76  
77 sub pre_window_resize {
78 my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
79
80 # This is a hack : it seems the window resize is one event
81 # late, so we issue an additional one a bit later
82  
83 $kernel->yield('window_resize');
84 $kernel->delay( window_resize => 1 / 10 );
85  
86 }
87  
88 sub window_resize {
89 my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
90  
91 $heap->{mainloop}->event_resize();
92  
93 }
94  
95 sub rebuild_all {
96 my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
97  
98 $heap->{mainloop}->event_rebuild_all();
99  
100 }
101
102 # Now the Mainloop signals
103  
104 sub redraw {
105 my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
106  
107 $heap->{mainloop}->event_redraw();
108  
109 }
110  
111 sub add_delay_handler {
112 my $seconds = $_[ARG0];
113 my $code = $_[ARG1];
114  
115 $_[KERNEL]->delay_set( 'delay_handler', $seconds, $code, @_[ ARG2 .. $#_ ] );
116  
117 }
118  
119 sub delay_handler {
120 my $code = $_[ARG0];
121
122 $code->( @_[ ARG1 .. $#_ ] );
123  
124 }
125  
126 sub stack_event {
127 my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
128  
129 $heap->{mainloop}->event_generic( @_[ ARG0 .. $#_ ] );
130  
131 }
132  
133 # ----------------------------------------------------------------------
134 # Private Methods
135 # ----------------------------------------------------------------------
136  
137 sub init {
138 my $self = shift;
139
140 my %params = $self->validate_params(@_, {
141 alias => { default => 'curses' },
142 args => { optional => 1, type => HASHREF }
143 });
144
145 # setup mainloop and root toolkit object
146  
147 my $mainloop = POE::Component::Curses::MainLoop->new(
148 session_name => $params{alias},
149 defined $params{args} ? ( args => $params{args} ) : ()
150 );
151  
152 POE::Session->create(
153 inline_states => {
154 _start => sub {
155 my ( $kernel, $session, $heap ) = @_[ KERNEL, SESSION, HEAP ];
156 # give a name to the session
157 $kernel->alias_set($params{alias});
158 # save the mainloop
159 $heap->{mainloop} = $mainloop;
160 # listen for window resize signals
161 $kernel->sig( WINCH => 'pre_window_resize' );
162 # start keyboard/mouse handler
163 $kernel->yield('startup');
164 # ask the mainloop to rebuild_all coordinates
165 $kernel->yield('rebuild_all');
166 },
167 startup => \&startup,
168 keyin => \&keyin,
169 key_handler => \&key_handler,
170 pre_window_resize => \&pre_window_resize,
171 window_resize => \&window_resize,
172 rebuild_all => \&rebuild_all,
173 redraw => \&redraw,
174 add_delay_handler => \&add_delay_handler,
175 delay_handler => \&delay_handler,
176 stack_event => \&stack_event,
177 }
178 );
179  
180 return $mainloop->get_toolkit_root();
181  
182 }
183  
184 1;
185  
186 __END__
187  
188 =head1 NAME
189  
190 XAS::Lib::Curses::Root - A class for the XAS environment
191  
192 =head1 SYNOPSIS
193  
194 use XAS::Lib::Curses::Root;
195 use Curses::Toolki::Widget::Window;
196 use Curses::Toolkit::Widget::Button;
197  
198 my $root = XAS::Lib::Curses::Root->new();
199 $root->add_window(
200 my $window = Curses::Toolkit::Widget::Window
201 ->new()
202 ->set_name('main_window')
203 ->add_widget(
204 my $button = Curses::Toolkit::Widget::Button
205 ->new()
206 ->set_name('my_button')
207 ->signal_connect(clicked => sub { exit(0); })
208 )
209 ->set_coordinates( x1 => 0, y1 => 0, x2 => '100%', y2 => '100%')
210 )
211  
212 POE:Kernel->run();
213  
214 =head1 DESCRIPTION
215  
216 This is an alternative event loop for the L<Curses::Toolkit|https://metacpan.org/pod/Curses::Toolkit>. I developed this
217 when I decided to write curses based programs that would also run on Windows.
218  
219 Curses::Toolkit has an external event loop that is based on POE, which uses
220 L<POE::Wheel::Curses|https://metacpan.org/pod/POE::Wheel::Curses>. This module uses select() to read STDIN. Windows
221 doesn't support this, so an alternative was needed. The alternative was a
222 polling POE task to read STDIN. While this will work on other platforms it
223 is not optimal. So this module loads mixins to handle those alternatives.
224  
225 You can read L<XAS::Lib::Curses::Win32|XAS::Lib::Curses::Win32> for the gory details on how to get
226 Curses.pm to work correctly on Windows.
227  
228 This module will allow all of the examples from Curses::Toolkit to run under
229 Windows. There are differences with color selection, which this module won't
230 address.
231  
232 =head1 SEE ALSO
233  
234 =over 4
235  
236 =item L<XAS|XAS>
237  
238 =item L<Curses::Toolkit|https://metacpan.org/pod/Curses::Toolkit>
239  
240 =item L<POE::Component::Curses|https://metacpan.org/pod/POE::Component::Curses>
241  
242 =back
243  
244 =head1 AUTHOR
245  
246 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
247  
248 =head1 COPYRIGHT AND LICENSE
249  
250 Copyright (C) 2014 Kevin L. Esteb
251  
252 This library is free software; you can redistribute it and/or modify
253 it under the same terms as Perl itself, either Perl version 5.8.8 or,
254 at your option, any later version of Perl 5 you may have available.
255  
256 See L<http://dev.perl.org/licenses/> for more information.
257  
258 =cut