| 1 | package XAS::Lib::Curses::Win32; | ||
| 2 | |||
| 3 | our $VERSION = '0.01'; | ||
| 4 | |||
| 5 | use POE; | ||
| 6 | use Curses; | ||
| 7 | |||
| 8 | use XAS::Class | ||
| 9 | debug => 0, | ||
| 10 | version => $VERSION, | ||
| 11 | base => 'XAS::Base', | ||
| 12 | mixins => 'startup keyin get_mouse_event handle_mouse_event', | ||
| 13 | ; | ||
| 14 | |||
| 15 | my @button_events = qw( | ||
| 16 | BUTTON1_PRESSED BUTTON1_RELEASED BUTTON1_CLICKED | ||
| 17 | BUTTON1_DOUBLE_CLICKED BUTTON1_TRIPLE_CLICKED | ||
| 18 | BUTTON2_PRESSED BUTTON2_RELEASED BUTTON2_CLICKED | ||
| 19 | BUTTON2_DOUBLE_CLICKED BUTTON2_TRIPLE_CLICKED | ||
| 20 | BUTTON3_PRESSED BUTTON3_RELEASED BUTTON3_CLICKED | ||
| 21 | BUTTON3_DOUBLE_CLICKED BUTTON3_TRIPLE_CLICKED | ||
| 22 | ); | ||
| 23 | |||
| 24 | # ---------------------------------------------------------------------- | ||
| 25 | # Public Methods | ||
| 26 | # ---------------------------------------------------------------------- | ||
| 27 | |||
| 28 | sub startup { | ||
| 29 | my ( $kernel, $session, $heap ) = @_[ KERNEL, SESSION, HEAP ]; | ||
| 30 | |||
| 31 | my $junk = 0; | ||
| 32 | my $event = ALL_MOUSE_EVENTS; | ||
| 33 | |||
| 34 | initscr(); | ||
| 35 | |||
| 36 | if (has_color()) { | ||
| 37 | |||
| 38 | start_color(); | ||
| 39 | |||
| 40 | } | ||
| 41 | |||
| 42 | cbreak(); | ||
| 43 | raw(); | ||
| 44 | noecho(); | ||
| 45 | nonl(); | ||
| 46 | |||
| 47 | keypad(1); | ||
| 48 | intrflush(0); | ||
| 49 | meta(1); | ||
| 50 | typeahead(-1); | ||
| 51 | curs_set(0); # these seems to be needed on PDCurses | ||
| 52 | |||
| 53 | nodelay(1); # set terminal input to non blocking | ||
| 54 | timeout(0); | ||
| 55 | |||
| 56 | mousemask($event, $junk); # turn the mouse on | ||
| 57 | mouseinterval(10); # this seems to give a good response | ||
| 58 | |||
| 59 | clear(); | ||
| 60 | refresh(); | ||
| 61 | |||
| 62 | $kernel->yield('keyin'); # Start keystroke polling | ||
| 63 | |||
| 64 | } | ||
| 65 | |||
| 66 | sub keyin { | ||
| 67 | my ($kernel) = $_[KERNEL]; | ||
| 68 | |||
| 69 | while ((my $keystroke = Curses::getch) ne '-1') { | ||
| 70 | |||
| 71 | $kernel->yield('key_handler', $keystroke); | ||
| 72 | |||
| 73 | } | ||
| 74 | |||
| 75 | $kernel->delay('keyin', 0.1); | ||
| 76 | |||
| 77 | } | ||
| 78 | |||
| 79 | sub get_mouse_event { | ||
| 80 | |||
| 81 | my $mouse_curses_event = 0; | ||
| 82 | |||
| 83 | getmouse($mouse_curses_event); | ||
| 84 | |||
| 85 | # $mouse_curses_event is a struct. From curses.h (note: this might change!): | ||
| 86 | # | ||
| 87 | # typedef struct | ||
| 88 | # { | ||
| 89 | # short id; /* ID to distinguish multiple devices */ | ||
| 90 | # int x, y, z; /* event coordinates (character-cell) */ | ||
| 91 | # mmask_t bstate; /* button state bits */ | ||
| 92 | # } MEVENT; | ||
| 93 | # | ||
| 94 | # --------------- | ||
| 95 | # s signed short | ||
| 96 | # x null byte | ||
| 97 | # x null byte | ||
| 98 | # --------------- | ||
| 99 | # i integer | ||
| 100 | # --------------- | ||
| 101 | # i integer | ||
| 102 | # --------------- | ||
| 103 | # i integer | ||
| 104 | # --------------- | ||
| 105 | # l long | ||
| 106 | # --------------- | ||
| 107 | |||
| 108 | my ($id, $x, $y, $z, $bstate) = unpack( "sx2i3l", $mouse_curses_event ); | ||
| 109 | |||
| 110 | return ($id, $x, $y, $z, $bstate); | ||
| 111 | |||
| 112 | } | ||
| 113 | |||
| 114 | sub handle_mouse_event { | ||
| 115 | my ($id, $x, $y, $z, $bstate, $heap) = @_; | ||
| 116 | |||
| 117 | foreach my $possible_event_name (@button_events) { | ||
| 118 | |||
| 119 | my $possible_event = eval($possible_event_name); | ||
| 120 | |||
| 121 | if ( !$@ && $bstate == $possible_event ) { | ||
| 122 | |||
| 123 | my ($button, $type2) = $possible_event_name =~ /^([^_]+)_(.+)$/; | ||
| 124 | |||
| 125 | # no matter what the mouseinterval() setting is, PDCurses | ||
| 126 | # always returned "BUTTON_PRESSED", so we are fudging here. | ||
| 127 | |||
| 128 | $type2 = 'CLICKED' if ($type2 eq 'PRESSED'); | ||
| 129 | |||
| 130 | $heap->{mainloop}->event_mouse( | ||
| 131 | type => 'click', | ||
| 132 | type2 => lc($type2), | ||
| 133 | button => lc($button), | ||
| 134 | x => $x, | ||
| 135 | y => $y, | ||
| 136 | z => $z, | ||
| 137 | ); | ||
| 138 | |||
| 139 | } | ||
| 140 | |||
| 141 | } | ||
| 142 | |||
| 143 | } | ||
| 144 | |||
| 145 | # ---------------------------------------------------------------------- | ||
| 146 | # Private Methods | ||
| 147 | # ---------------------------------------------------------------------- | ||
| 148 | |||
| 149 | 1; | ||
| 150 | |||
| 151 | __END__ | ||
| 152 | |||
| 153 | =head1 NAME | ||
| 154 | |||
| 155 | XAS::Lib::Curses::Win32 - A mixin for handling PDCurses issues on Win32 | ||
| 156 | |||
| 157 | =head1 DESCRIPTION | ||
| 158 | |||
| 159 | Who in there right minds would want to run Curses on Windows? I guess I did. | ||
| 160 | To start with you need to download and install PDCurses. You can get that | ||
| 161 | here: | ||
| 162 | |||
| 163 | http://sourceforge.net/projects/pdcurses/files/pdcurses/3.4/ | ||
| 164 | |||
| 165 | You want the pdc34dllw.zip file. This is compatible with current release of | ||
| 166 | Curses.pm. The package from the GnuWin32 site is not. To install, do the | ||
| 167 | following: | ||
| 168 | |||
| 169 | 1) Unpack the zip archive. | ||
| 170 | |||
| 171 | 2) Copy the .h files to \strawberry\c\include | ||
| 172 | |||
| 173 | 3) Copy the .lib file to \strawberry\c\lib | ||
| 174 | |||
| 175 | 4) copy the .dll file to \strawberry\c\bin | ||
| 176 | |||
| 177 | That's it, PDCurses is now installed. Now for the fun part. Actually making it | ||
| 178 | work correctly with Curses.pm. Start by downloading it from CPAN. Do | ||
| 179 | not do a default install. | ||
| 180 | |||
| 181 | Curses.pm is designed to be used with Ncurses. Which is a standard | ||
| 182 | install on most Linuxes and some Unixes. PDCurses is "compatible" with | ||
| 183 | Ncurses, with the exception of the mouse handling functions. | ||
| 184 | |||
| 185 | PDCurses supports the Ncurses mouse handling and an older set of | ||
| 186 | SYSV mouse handling routines. The problem lies with the getmouse() call. | ||
| 187 | This is a name clash with the Ncurses routine of the same name. PDCurses | ||
| 188 | solves this by providing a nc_getmouse() call. Curses.pm doesn't check for | ||
| 189 | this function, only getmouse() which exists, but has a different call | ||
| 190 | interface. So the mouse doesn't work. | ||
| 191 | |||
| 192 | To fix this problem you need to edit some files. Start with list.syms and add | ||
| 193 | the following line around the getmouse(0) line. | ||
| 194 | |||
| 195 | "E nc_getmouse(0)" | ||
| 196 | |||
| 197 | Next change some code in CursesFun.c. Replace the original code for the | ||
| 198 | XS_Curses_getmouse with this: | ||
| 199 | |||
| 200 | XS(XS_Curses_getmouse) | ||
| 201 | { | ||
| 202 | dXSARGS; | ||
| 203 | #ifdef C_GETMOUSE | ||
| 204 | c_exactargs("getmouse", items, 1); | ||
| 205 | { | ||
| 206 | MEVENT *event = (MEVENT *)sv_grow(ST(0), 2 * sizeof(MEVENT)); | ||
| 207 | int ret = getmouse(event); | ||
| 208 | c_setmevent(ST(0)); | ||
| 209 | ST(0) = sv_newmortal(); | ||
| 210 | sv_setiv(ST(0), (IV)ret); | ||
| 211 | } | ||
| 212 | XSRETURN(1); | ||
| 213 | #elif defined C_NC_GETMOUSE | ||
| 214 | c_exactargs("getmouse", items, 1); | ||
| 215 | { | ||
| 216 | MEVENT *event = (MEVENT *)sv_grow(ST(0), 2 * sizeof(MEVENT)); | ||
| 217 | int ret = nc_getmouse(event); | ||
| 218 | c_setmevent(ST(0)); | ||
| 219 | ST(0) = sv_newmortal(); | ||
| 220 | sv_setiv(ST(0), (IV)ret); | ||
| 221 | } | ||
| 222 | XSRETURN(1); | ||
| 223 | #else | ||
| 224 | c_fun_not_there("getmouse"); | ||
| 225 | XSRETURN(0); | ||
| 226 | #endif | ||
| 227 | } | ||
| 228 | |||
| 229 | This will allow Curses.pm to access the correct getmouse() function call. Now | ||
| 230 | you need to build Curses.pm. You do this in the usually fashion. | ||
| 231 | |||
| 232 | > perl Makefile.PL PANELS | ||
| 233 | > dmake | ||
| 234 | > dmake test | ||
| 235 | > dmake install | ||
| 236 | |||
| 237 | Note: You need the "PANELS" qualifier. Curses.pm will build without it, but it | ||
| 238 | will automatically find the panel routines in PDCurses. Doing so, without the | ||
| 239 | qualifier, will cause Perl to crash. | ||
| 240 | |||
| 241 | At this point, you have a fully functional curses on Windows. | ||
| 242 | |||
| 243 | =head1 METHODS | ||
| 244 | |||
| 245 | In the following methods: $kernel, $session, $heap are standard variables | ||
| 246 | from POE. | ||
| 247 | |||
| 248 | =head2 startup($kernel, $session, $heap) | ||
| 249 | |||
| 250 | This will initialize the screen, keyboard and turn on mouse handling. It | ||
| 251 | will also start the polling task for getch(). | ||
| 252 | |||
| 253 | =head2 keyin($kernel) | ||
| 254 | |||
| 255 | Read the keyboard with getch() and reschedule the poll. | ||
| 256 | |||
| 257 | =head2 get_mouse_event | ||
| 258 | |||
| 259 | The will parse the mouse event and return the following variables. | ||
| 260 | |||
| 261 | =over 4 | ||
| 262 | |||
| 263 | =item $id | ||
| 264 | |||
| 265 | This is the id of the event, this is always 0. | ||
| 266 | |||
| 267 | =item $x | ||
| 268 | |||
| 269 | The position of the mouse on the X plain of the screen. | ||
| 270 | |||
| 271 | =item $y | ||
| 272 | |||
| 273 | The position of the mouse on the Y plain of the screen. | ||
| 274 | |||
| 275 | =item $z | ||
| 276 | |||
| 277 | Not, sure, but it is always 0. | ||
| 278 | |||
| 279 | =item $bstate | ||
| 280 | |||
| 281 | The state of the mouse. | ||
| 282 | |||
| 283 | =back | ||
| 284 | |||
| 285 | =head2 handle_mounse_event($id, $x, $y, $z, $bstate, $heap) | ||
| 286 | |||
| 287 | This will take $id, $x, $y, $z, $bstate and $heap variable and create | ||
| 288 | the appropriate event to dispatch within Curses::Toolkit. | ||
| 289 | |||
| 290 | =head1 SEE ALSO | ||
| 291 | |||
| 292 | =over 4 | ||
| 293 | |||
| 294 | =item L<XAS|XAS> | ||
| 295 | |||
| 296 | =item L<Curses::Toolkit|https://metacpan.org/pod/Curses::Toolkit> | ||
| 297 | |||
| 298 | =back | ||
| 299 | |||
| 300 | =head1 AUTHOR | ||
| 301 | |||
| 302 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 303 | |||
| 304 | =head1 COPYRIGHT AND LICENSE | ||
| 305 | |||
| 306 | Copyright (C) 2014 Kevin L. Esteb | ||
| 307 | |||
| 308 | This library is free software; you can redistribute it and/or modify | ||
| 309 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 310 | at your option, any later version of Perl 5 you may have available. | ||
| 311 | |||
| 312 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 313 | |||
| 314 | =cut |