| 49 | | my $self = bless &Curses::UI::new(@_), $type; |
| 50 | | |
| 51 | | $self->{modal_objects} = \@modal_objects; |
| 52 | | $self->{modal_callbacks} = \@modal_callbacks; |
| | 47 | my $self = &Curses::UI::new(@_); |
| | 48 | # my $self = bless Curses::UI->new, $type; |
| | 49 | # my $self = bless &Curses::UI::new(@_), $type; |
| | 50 | |
| | 51 | # I have to do this here, because if our first order of business is a |
| | 52 | # dialog then the _start event will be too late. This self reference is |
| | 53 | # just so we can stack and peel onto the list of modal objects, and get to |
| | 54 | # ourselves when we reach the top. |
| | 55 | push @modal_objects, $self; |
| | 56 | |
| 75 | | $self->{-no_output} = $options{no_output} || 0; |
| 76 | | |
| | 81 | $self->{-no_output} = $options{-no_output} || 0; |
| | 82 | |
| | 83 | return $self; |
| | 84 | } |
| | 85 | |
| | 86 | sub _start { |
| | 87 | my ($self, $kernel) = @_[ OBJECT, KERNEL ]; |
| | 88 | |
| | 89 | $kernel->select(\*STDIN, "keyin"); |
| | 90 | |
| | 91 | # Turn blocking back on for STDIN. Some Curses |
| | 92 | # implementations don't deal well with non-blocking STDIN. |
| | 93 | my $flags = fcntl STDIN, F_GETFL, 0 or die $!; |
| | 94 | fcntl STDIN, F_SETFL, $flags & ~O_NONBLOCK or die $!; |
| | 95 | |
| | 96 | # If we're in a dialog, then the TOP modal object is more appropriate than |
| | 97 | # $self, although if we're not in a dialog $self is what this actually is. |
| | 98 | set_read_timeout($modal_objects[TOP]); |
| | 99 | |
| | 100 | # Unmask... |
| | 101 | $self->{__start_callback}(@_) |
| | 102 | if defined $self->{__start_callback}; |
| | 103 | } |
| | 104 | |
| | 105 | sub _clear_modal_callback { |
| | 106 | my ($self) = @_; |
| | 107 | |
| | 108 | my $top = pop @modal_objects; |
| | 109 | |
| | 110 | # Reset focus |
| | 111 | $top->{-focus} = 0; |
| | 112 | |
| | 113 | # Dispatch callback. |
| | 114 | my $args = pop @modal_callbacks; |
| | 115 | my $sub = shift @$args; |
| | 116 | &{$sub}(@$args); |
| | 117 | } |
| | 118 | |
| | 119 | sub keyin { |
| | 120 | my ($self, $kernel) = @_[ OBJECT, KERNEL ]; |
| | 121 | |
| | 122 | unless ($#modal_objects) { |
| | 123 | $self->do_one_event; |
| | 124 | } |
| | 125 | else { |
| | 126 | # dispatch the event to the top-most modal object, or the root. |
| | 127 | $self->do_one_event($modal_objects[TOP]); |
| | 128 | |
| | 129 | # I didn't originally do this here, I'm not quite sure what I'm up to... |
| | 130 | # |
| | 131 | # # If this is a callback modal focus widget, and we lost modal focus, |
| | 132 | # # execute the callback an clear the level in the stack. |
| | 133 | # $self->_clear_modal_callback |
| | 134 | # unless $modal_objects[TOP]->{-has_modal_focus}; |
| | 135 | |
| | 136 | # This other wierdness seems unnecessary. |
| | 137 | # $top_object->root->do_one_event($top_object); |
| | 138 | } |
| | 139 | |
| | 140 | # This was a hack to make sure to pick up the extra events when things got out |
| | 141 | # of sync. I'm not sure if I need it. But let's try getting C::U::P working |
| | 142 | # first. |
| | 143 | # if (my $key = $self->get_key(0)) { |
| | 144 | # $self->feedkey($key) unless $key eq "-1"; |
| | 145 | # $self->do_one_event; |
| | 146 | # } |
| | 147 | |
| | 148 | # Set the root cursor mode |
| | 149 | unless ($self->{-no_output}) { |
| | 150 | Curses::curs_set($self->{-cursor_mode}); |
| | 151 | } |
| | 152 | } |
| | 153 | |
| | 154 | sub timer { |
| | 155 | my ($self) = @_; |
| | 156 | |
| | 157 | # dispatch the event to the top-most modal object, or the root. |
| | 158 | my $top_object = $modal_objects[TOP]; |
| | 159 | |
| | 160 | $top_object->do_timer; |
| | 161 | |
| | 162 | # Set the root cursor mode. |
| | 163 | unless ($self->{-no_output}) { |
| | 164 | Curses::curs_set($self->{-cursor_mode}); |
| | 165 | } |
| | 166 | |
| | 167 | set_read_timeout($top_object); |
| | 168 | |
| | 169 | # Looks like older versions didn't support callbackmodalfocus, whatever that |
| | 170 | # is. |
| | 171 | # I'm not sure what the deal is with the callbackmodalfocus shit... |
| | 172 | # $self->_clear_modal_callback unless $top_object->{-has_modal_focus}; |
| | 173 | } |
| | 174 | |
| | 175 | sub shutdown { |
| | 176 | my ($kernel) = $_[ KERNEL ]; |
| | 177 | |
| | 178 | # Unselect stdin |
| | 179 | $kernel->select(\*STDIN); |
| | 180 | } |
| | 181 | |
| | 182 | sub mainloop { |
| | 183 | my ($this) = @_; |
| | 184 | |
| | 185 | unless ($this->{-no_output}) { |
| | 186 | $this->focus(undef, 1); |
| | 187 | $this->draw; |
| | 188 | |
| | 189 | Curses::doupdate; |
| | 190 | } |
| | 191 | |
| | 192 | POE::Kernel->run; |
| | 193 | } |
| | 194 | |
| | 195 | sub set_read_timeout { |
| | 196 | my $this = shift; |
| | 197 | |
| | 198 | my $new_timeout = -1; |
| | 199 | |
| | 200 | while (my ($id, $config) = each %{$this->{-timers}}) { |
| | 201 | next unless $config->{-enabled}; |
| | 202 | |
| | 203 | $new_timeout = $config->{-time} |
| | 204 | unless $new_timeout != -1 and |
| | 205 | $new_timeout < $config->{-time}; |
| | 206 | } |
| | 207 | |
| | 208 | $poe_kernel->delay(timer => $new_timeout) if $new_timeout >= 0; |
| | 209 | |
| | 210 | # Force the read timeout to be 0, so Curses::UI polls. |
| | 211 | $this->{-read_timeout} = 0; |
| | 212 | |
| | 213 | return $this; |
| | 214 | } |
| | 215 | |
| | 216 | { |
| 130 | | |
| 131 | | return $self; |
| 132 | | } |
| 133 | | |
| 134 | | sub _start { |
| 135 | | my ($self, $kernel) = @_[ OBJECT, KERNEL ]; |
| 136 | | |
| 137 | | $kernel->select(\*STDIN, "-keyin"); |
| 138 | | |
| 139 | | # Turn blocking back on for STDIN. Some Curses |
| 140 | | # implementations don't deal well with non-blocking STDIN. |
| 141 | | my $flags = fcntl STDIN, F_GETFL, 0 or die $!; |
| 142 | | fcntl STDIN, F_SETFL, $flags & ~O_NONBLOCK or die $!; |
| 143 | | |
| 144 | | # This self reference is just so we can stack and peel onto the list of |
| 145 | | # modal objects, and get to ourselves when we reach the top. |
| 146 | | $self->{modal_objects} = [ $self ]; |
| 147 | | |
| 148 | | set_read_timeout($self); |
| 149 | | |
| 150 | | # Unmask... |
| 151 | | $self->{options}{inline_states}{_start}(@_) |
| 152 | | if (defined $self->{options}->{inline_states} && |
| 153 | | defined $self->{options}{inline_states}{_start}); |
| 154 | | } |
| 155 | | |
| 156 | | sub _clear_modal_callback { |
| 157 | | my ($self) = @_; |
| 158 | | |
| 159 | | my $top_object = pop @{ $self->{_modal_objects} }; |
| 160 | | |
| 161 | | # Reset focus |
| 162 | | $top_object->{-focus} = 0; |
| 163 | | |
| 164 | | # Dispatch callback. |
| 165 | | my @args = @{ pop @{ $self->{_modal_callbacks} } }; |
| 166 | | my ($sub) = shift @args; |
| 167 | | &{$sub}(@args); |
| 168 | | } |
| 169 | | |
| 170 | | sub keyin { |
| 171 | | my ($self, $kernel) = @_[ OBJECT, KERNEL ]; |
| 172 | | |
| 173 | | my $top_object = $self->{modal_objects}->[TOP]; |
| 174 | | # dispatch the event to the top-most modal object, or the root. |
| 175 | | $top_object->root->do_one_event($top_object); |
| 176 | | |
| 177 | | # Set the root cursor mode |
| 178 | | Curses::curs_set($self->{-cursor_mode}); |
| 179 | | |
| 180 | | _clear_modal_callback($self) unless $top_object->{-has_modal_focus}; |
| 181 | | } |
| 182 | | |
| 183 | | sub timer { |
| 184 | | my ($self) = @_; |
| 185 | | |
| 186 | | my $top_object = $self->{modal_objects}->[TOP]; |
| 187 | | # dispatch the event to the top-most modal object, or the root. |
| 188 | | $top_object->do_timer; |
| 189 | | |
| 190 | | # Set the root cursor mode. |
| 191 | | Curses::curs_set($self->{-cursor_mode}); |
| 192 | | |
| 193 | | set_read_timeout($top_object); |
| 194 | | |
| 195 | | _clear_modal_callback($self) unless $top_object->{-has_modal_focus}; |
| 196 | | } |
| 197 | | |
| 198 | | sub shutdown { |
| 199 | | my ($kernel) = $_[ KERNEL ]; |
| 200 | | |
| 201 | | # Unselect stdin |
| 202 | | $kernel->select(\*STDIN); |
| 203 | | } |
| 204 | | |
| 205 | | sub MainLoop { |
| 206 | | # When does this even happen!? |
| 207 | | # I need more comments! |
| 208 | | my $root = $Curses::UI::rootobject; |
| 209 | | die "MainLoop: Must create a curses UI object first." unless defined $root; |
| 210 | | |
| 211 | | # If some how we didn't get our object into the rootobject...redefine that |
| 212 | | # bad boy. |
| 213 | | if (ref $root ne __PACKAGE__) { |
| 214 | | $root = bless $root, __PACKAGE__; |
| 215 | | $Curses::UI::rootobject = $root; |
| 216 | | } |
| 217 | | |
| 218 | | $root->mainloop; |
| 219 | | } |
| 220 | | |
| 221 | | sub mainloop { |
| 222 | | my ($this) = @_; |
| 223 | | |
| 224 | | $this->focus(undef, 1); |
| 225 | | $this->draw; |
| 226 | | |
| 227 | | Curses::doupdate; |
| 228 | | |
| 229 | | run POE::Kernel; |
| 230 | | } |
| 231 | | |
| 232 | | sub set_read_timeout { |
| 233 | | my $this = shift; |
| 234 | | |
| 235 | | my $new_timeout = -1; |
| 236 | | |
| 237 | | while (my ($id, $config) = each %{$this->{-timers}}) { |
| 238 | | next unless $config->{-enabled}; |
| 239 | | |
| 240 | | $new_timeout = $config->{-time} |
| 241 | | unless $new_timeout != -1 and |
| 242 | | $new_timeout < $config->{-time}; |
| 243 | | } |
| 244 | | |
| 245 | | $poe_kernel->delay(-timer => $new_timeout) if $new_timeout >= 0; |
| 246 | | |
| 247 | | # Force the read timeout to be 0, so Curses::UI polls. |
| 248 | | $this->{-read_timeout} = 0; |
| 249 | | |
| 250 | | return $this; |