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

filter: Statementstmt 0.0% 0 / 28 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 45 CC 2 SCAR 10.6
CC
2
Cov
0%
handle_mouse_event
6.0
startup
2.0
keyin
2.0
CRAP 2.9
1 package XAS::Lib::Curses::Unix;
2  
3 our $VERSION = '0.01';
4  
5 use POE;
6 use Curses;
7 use POE::Wheel::Curses;
8  
9 use XAS::Class
10 debug => 0,
11 version => $VERSION,
12 base => 'XAS::Base',
13 mixins => 'startup keyin get_mouse_event handle_mouse_event',
14 ;
15  
16 my @button_events = qw(
17 BUTTON1_PRESSED BUTTON1_RELEASED BUTTON1_CLICKED
18 BUTTON1_DOUBLE_CLICKED BUTTON1_TRIPLE_CLICKED
19 BUTTON2_PRESSED BUTTON2_RELEASED BUTTON2_CLICKED
20 BUTTON2_DOUBLE_CLICKED BUTTON2_TRIPLE_CLICKED
21 BUTTON3_PRESSED BUTTON3_RELEASED BUTTON3_CLICKED
22 BUTTON3_DOUBLE_CLICKED BUTTON3_TRIPLE_CLICKED
23 BUTTON4_PRESSED BUTTON4_RELEASED BUTTON4_CLICKED
24 BUTTON4_DOUBLE_CLICKED BUTTON4_TRIPLE_CLICKED
25 BUTTON5_PRESSED BUTTON5_RELEASED
26 BUTTON5_CLICKED BUTTON5_DOUBLE_CLICKED BUTTON5_TRIPLE_CLICKED
27 BUTTON_SHIFT BUTTON_CTRL BUTTON_ALT
28 );
29  
30 # ----------------------------------------------------------------------
31 # Public Methods
32 # ----------------------------------------------------------------------
33  
34 sub startup {
35 my ($kernel, $heap) = @_[KERNEL, HEAP];
36  
37 # now listen to the keys
38 $heap->{console} = POE::Wheel::Curses->new(
39 InputEvent => 'key_handler',
40 );
41  
42 }
43  
44 sub keyin {
45 my ($kernel) = $_[KERNEL];
46  
47 }
48  
49 sub get_mouse_event {
50  
51 my $mouse_curses_event = 0;
52  
53 getmouse($mouse_curses_event);
54  
55 # $mouse_curses_event is a struct. From curses.h (note: this might change!):
56 #
57 # typedef struct
58 # {
59 # short id; /* ID to distinguish multiple devices */
60 # int x, y, z; /* event coordinates (character-cell) */
61 # mmask_t bstate; /* button state bits */
62 # } MEVENT;
63 #
64 # ---------------
65 # s signed short
66 # x null byte
67 # x null byte
68 # ---------------
69 # i integer
70 # ---------------
71 # i integer
72 # ---------------
73 # i integer
74 # ---------------
75 # l long
76 # ---------------
77  
78 my ($id, $x, $y, $z, $bstate) = unpack( "sx2i3l", $mouse_curses_event );
79  
80 return ($id, $x, $y, $z, $bstate);
81  
82 }
83  
84 sub handle_mouse_event {
85 my ( $id, $x, $y, $z, $bstate, $heap ) = @_;
86  
87 foreach my $possible_event_name (@button_events) {
88  
89 my $possible_event = eval($possible_event_name);
90  
91 if ( !$@ && $bstate == $possible_event ) {
92  
93 my ($button, $type2) = $possible_event_name =~ /^([^_]+)_(.+)$/;
94  
95 $heap->{mainloop}->event_mouse(
96 type => 'click',
97 type2 => lc($type2),
98 button => lc($button),
99 x => $x,
100 y => $y,
101 z => $z,
102 );
103  
104 }
105  
106 }
107  
108 }
109  
110 # ----------------------------------------------------------------------
111 # Private Methods
112 # ----------------------------------------------------------------------
113  
114 1;
115  
116 __END__
117  
118 =head1 NAME
119  
120 XAS::Lib::Curses::Unix - A mixin class for Curses on Unix
121  
122 =head1 DESCRIPTION
123  
124 This is the mixin class for working with Curses on Linux\Unix.
125  
126 =head1 METHODS
127  
128 In the following methods: $kernel, $session and $heap are standard variables
129 from POE.
130  
131 =head2 startup($kernel, $session, $heap)
132  
133 This will initialize the screen and keyboard.
134  
135 =head2 keyin($kernel)
136  
137 This does nothing.
138  
139 =head2 get_mouse_event
140  
141 This will parse the mouse eventx and return the following variables.
142  
143 =over 4
144  
145 =item $id
146  
147 This is the id of the event, this is always 0.
148  
149 =item $x
150  
151 The position of the mouse on the X axis of the screen.
152  
153 =item $y
154  
155 The position of the mouse on the Y axis of the screen.
156  
157 =item $z
158  
159 Not, sure, but it is always 0.
160  
161 =item $bstate
162  
163 The state of the mouse.
164  
165 =back
166  
167 =head2 handle_mounse_event($id, $x, $y, $z, $bstate, $heap)
168  
169 This will take $id, $x, $y, $z, $bstate and $heap variable and create
170 the appropriate event to dispatch within Curses::Toolkit.
171  
172 =head1 SEE ALSO
173  
174 =over 4
175  
176 =item L<XAS|XAS>
177  
178 =item L<Curses::Toolkit|https://metacpan.org/pod/Curses::Toolkit>
179  
180 =back
181  
182 =head1 AUTHOR
183  
184 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
185  
186 =head1 COPYRIGHT AND LICENSE
187  
188 Copyright (C) 2014 Kevin L. Esteb
189  
190 This library is free software; you can redistribute it and/or modify
191 it under the same terms as Perl itself, either Perl version 5.8.8 or,
192 at your option, any later version of Perl 5 you may have available.
193  
194 See L<http://dev.perl.org/licenses/> for more information.
195  
196 =cut