| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | package POE::Wheel::Curses; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | use vars qw($VERSION @ISA); | ||
| 8 | $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places) | ||
| 9 | |||
| 10 | use Carp qw(croak); | ||
| 11 | use Curses qw( | ||
| 12 | initscr start_color cbreak raw noecho nonl nodelay timeout keypad | ||
| 13 | intrflush meta typeahead clear refresh | ||
| 14 | endwin COLS | ||
| 15 | ); | ||
| 16 | use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK); | ||
| 17 | use POE qw( Wheel ); | ||
| 18 | push @ISA, qw(POE::Wheel); | ||
| 19 | |||
| 20 | sub SELF_STATE_READ () { 0 } | ||
| 21 | sub SELF_STATE_WRITE () { 1 } | ||
| 22 | sub SELF_EVENT_INPUT () { 2 } | ||
| 23 | sub SELF_ID () { 3 } | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my $type = shift; | ||
| 27 | my %params = @_; | ||
| 28 | |||
| 29 | croak "$type needs a working Kernel" unless defined $poe_kernel; | ||
| 30 | |||
| 31 | my $input_event = delete $params{InputEvent}; | ||
| 32 | croak "$type requires an InputEvent parameter" unless defined $input_event; | ||
| 33 | |||
| 34 | if (scalar keys %params) { | ||
| 35 | carp( "unknown parameters in $type constructor call: ", | ||
| 36 | join(', ', keys %params) | ||
| 37 | ); | ||
| 38 | } | ||
| 39 | |||
| 40 | # Create the object. | ||
| 41 | my $self = bless | ||
| 42 | [ undef, # SELF_STATE_READ | ||
| 43 | undef, # SELF_STATE_WRITE | ||
| 44 | $input_event, # SELF_EVENT_INPUT | ||
| 45 | &POE::Wheel::allocate_wheel_id(), # SELF_ID | ||
| 46 | ]; | ||
| 47 | |||
| 48 | # Set up the screen, and enable color, mangle the terminal and | ||
| 49 | # keyboard. | ||
| 50 | |||
| 51 | initscr(); | ||
| 52 | start_color(); | ||
| 53 | |||
| 54 | cbreak(); | ||
| 55 | raw(); | ||
| 56 | noecho(); | ||
| 57 | nonl(); | ||
| 58 | |||
| 59 | # Both of these achieve nonblocking input. | ||
| 60 | nodelay(1); | ||
| 61 | timeout(0); | ||
| 62 | |||
| 63 | keypad(1); | ||
| 64 | intrflush(0); | ||
| 65 | meta(1); | ||
| 66 | typeahead(-1); | ||
| 67 | |||
| 68 | clear(); | ||
| 69 | refresh(); | ||
| 70 | |||
| 71 | # Define the input event. | ||
| 72 | $self->_define_input_state(); | ||
| 73 | |||
| 74 | # Oop! Return ourself. I forgot to do this. | ||
| 75 | $self; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _define_input_state { | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | # Register the select-read handler. | ||
| 82 | if (defined $self->[SELF_EVENT_INPUT]) { | ||
| 83 | # Stupid closure tricks. | ||
| 84 | my $event_input = \$self->[SELF_EVENT_INPUT]; | ||
| 85 | my $unique_id = $self->[SELF_ID]; | ||
| 86 | |||
| 87 | $poe_kernel->state | ||
| 88 | ( $self->[SELF_STATE_READ] = ref($self) . "($unique_id) -> select read", | ||
| 89 | sub { | ||
| 90 | |||
| 91 | # Prevents SEGV in older Perls. | ||
| 92 | 0 && CRIMSON_SCOPE_HACK('<'); | ||
| 93 | |||
| 94 | my ($k, $me) = @_[KERNEL, SESSION]; | ||
| 95 | |||
| 96 | # Curses' getch() normally blocks, but we've already | ||
| 97 | # determined that STDIN has something for us. Be explicit | ||
| 98 | # about which getch() to use. | ||
| 99 | while ((my $keystroke = Curses::getch) ne '-1') { | ||
| 100 | $k->call( $me, $$event_input, $keystroke, $unique_id ); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | ); | ||
| 104 | |||
| 105 | # Now start reading from it. | ||
| 106 | $poe_kernel->select_read( \*STDIN, $self->[SELF_STATE_READ] ); | ||
| 107 | |||
| 108 | # Turn blocking back on for STDIN. Some Curses implementations | ||
| 109 | # don't deal well with non-blocking STDIN. | ||
| 110 | my $flags = fcntl(STDIN, F_GETFL, 0) or die $!; | ||
| 111 | fcntl(STDIN, F_SETFL, $flags & ~O_NONBLOCK) or die $!; | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | $poe_kernel->select_read( \*STDIN ); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | sub DESTROY { | ||
| 119 | my $self = shift; | ||
| 120 | |||
| 121 | # Turn off the select. | ||
| 122 | $poe_kernel->select( \*STDIN ); | ||
| 123 | |||
| 124 | # Remove states. | ||
| 125 | if ($self->[SELF_STATE_READ]) { | ||
| 126 | $poe_kernel->state($self->[SELF_STATE_READ]); | ||
| 127 | $self->[SELF_STATE_READ] = undef; | ||
| 128 | } | ||
| 129 | |||
| 130 | # Restore the terminal. | ||
| 131 | endwin if COLS; | ||
| 132 | |||
| 133 | &POE::Wheel::free_wheel_id($self->[SELF_ID]); | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; | ||
| 137 | |||
| 138 | __END__ |