| 4 |
## |
## |
| 5 |
## $Id$ |
## $Id$ |
| 6 |
## |
## |
| 7 |
|
## $Log$ |
| 8 |
|
## Revision 1.7 2002/06/15 07:56:40 cvsjoko |
| 9 |
|
## + bugfixes for win32 (additional keys) |
| 10 |
|
## |
| 11 |
|
## Revision 1.6 2002/06/15 07:46:11 cvsjoko |
| 12 |
|
## + bugfixes for linux |
| 13 |
|
## |
| 14 |
|
## Revision 1.5 2002/06/15 05:19:36 cvsjoko |
| 15 |
|
## + modified order/way of key-handling-logic |
| 16 |
|
## |
| 17 |
|
## Revision 1.4 2002/06/15 04:28:10 cvsjoko |
| 18 |
|
## immediate polling, no delay |
| 19 |
|
## |
| 20 |
|
## Revision 1.3 2002/06/15 04:24:15 cvsjoko |
| 21 |
|
## + added modifier for running in linux or win32 |
| 22 |
|
## + modified order of some commands in core key-polling |
| 23 |
|
## |
| 24 |
|
## Revision 1.2 2002/06/15 03:45:21 cvsjoko |
| 25 |
|
## + cvs id & log |
| 26 |
|
## + clearing inputbuffer just after getting the "enter"-key |
| 27 |
|
## |
| 28 |
|
## |
| 29 |
package POE::Component::Terminal; |
package POE::Component::Terminal; |
| 30 |
|
|
| 31 |
use strict; |
use strict; |
| 64 |
recieveResponse => 'recieveResponse', |
recieveResponse => 'recieveResponse', |
| 65 |
}; |
}; |
| 66 |
|
|
| 67 |
|
sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } |
| 68 |
|
|
| 69 |
sub new { |
sub new { |
| 70 |
my $class = shift; |
my $class = shift; |
| 71 |
$class = ref( $class ) || $class; |
$class = ref( $class ) || $class; |
| 84 |
# store entries from args to object ($self) |
# store entries from args to object ($self) |
| 85 |
map { $self->{$_} = $args->{$_}; } keys %{$args}; |
map { $self->{$_} = $args->{$_}; } keys %{$args}; |
| 86 |
|
|
| 87 |
|
if ( RUNNING_IN_HELL () ) { |
| 88 |
|
$self->{conf}{KeyEnter} = 13; |
| 89 |
|
$self->{conf}{KeyBackspace} = 8; |
| 90 |
|
$self->{conf}{KeyCTRLC} = 3; |
| 91 |
|
} else { |
| 92 |
|
$self->{conf}{KeyEnter} = 10; |
| 93 |
|
$self->{conf}{KeyBackspace} = 127; |
| 94 |
|
$self->{conf}{KeyCTRLC} = 3; |
| 95 |
|
} |
| 96 |
|
|
| 97 |
## Make our session. See $states defined up above . . . |
## Make our session. See $states defined up above . . . |
| 98 |
POE::Session->create( object_states => [ $self => $states, ], ); |
POE::Session->create( object_states => [ $self => $states, ], ); |
| 99 |
|
|
| 119 |
$heap->{state}{InputBuffer} = ''; |
$heap->{state}{InputBuffer} = ''; |
| 120 |
$heap->{state}{Startup} = 1; |
$heap->{state}{Startup} = 1; |
| 121 |
$heap->{state}{Caption} = $self->{Caption}; |
$heap->{state}{Caption} = $self->{Caption}; |
| 122 |
|
$heap->{state}{KeyPressed} = 0; |
| 123 |
|
$heap->{state}{RequestPending} = 0; |
| 124 |
|
|
| 125 |
# initially call worker-state |
# initially call worker-state |
| 126 |
|
ReadMode 4; |
| 127 |
$kernel->post($session, "pollForKey"); |
$kernel->post($session, "pollForKey"); |
| 128 |
|
|
| 129 |
return; |
return; |
| 197 |
my $prompt = "#> "; |
my $prompt = "#> "; |
| 198 |
$prompt = $self->{PromptPrefix} . ' ' . $prompt_inlay . $self->{PromptPostfix}; |
$prompt = $self->{PromptPrefix} . ' ' . $prompt_inlay . $self->{PromptPostfix}; |
| 199 |
|
|
| 200 |
if ( ($heap->{state}{KeyPressed} && !$heap->{state}{RequestPending}) || ($heap->{state}{Startup}) ) { |
if ( ( |
| 201 |
|
( $heap->{state}{KeyPressed} == 1 ) && |
| 202 |
|
( $heap->{state}{RequestPending} == 0 ) |
| 203 |
|
) || |
| 204 |
|
( $heap->{state}{Startup} ) |
| 205 |
|
) { |
| 206 |
$heap->{state}{Startup} = 0; |
$heap->{state}{Startup} = 0; |
| 207 |
$heap->{state}{KeyPressed} = 0; |
$heap->{state}{KeyPressed} = 0; |
|
#ReadMode 4; # Turn off controls keys |
|
|
#ReadMode 1; |
|
|
#print $prompt; |
|
| 208 |
my $outputline = $prompt . $heap->{state}{InputBuffer}; |
my $outputline = $prompt . $heap->{state}{InputBuffer}; |
| 209 |
my $s = ' ' x (length($outputline) + 1); |
my $s = ' ' x (length($outputline) + 1); |
| 210 |
print "\r", $s; |
print "\r", $s; |
| 211 |
print "\r", $outputline; |
print "\r", $outputline; |
| 212 |
|
#print $outputline; |
| 213 |
} |
} |
| 214 |
|
|
| 215 |
my $key = ReadKey(-1); |
my $key = ReadKey(-1); |
| 216 |
if ($key) { |
if ( defined $key ) { |
|
|
|
|
$heap->{state}{KeyPressed} = 1; |
|
| 217 |
|
|
| 218 |
# normal key? |
#print "key: ", ord($key), "\n"; |
| 219 |
if ($key =~ m/[a-zA-Z? ]/) { |
|
| 220 |
$heap->{state}{InputBuffer} .= $key; |
# CTRL+C pressed? |
| 221 |
#print $key; |
if (ord($key) == $self->{conf}{KeyCTRLC}) { |
| 222 |
|
ReadMode 1; |
| 223 |
|
print "\n"; |
| 224 |
|
exit; |
| 225 |
} |
} |
| 226 |
|
|
| 227 |
# enter pressed? |
# enter pressed? |
| 228 |
if ($key eq "\r") { |
if (ord($key) == $self->{conf}{KeyEnter}) { |
| 229 |
# send command |
|
| 230 |
|
# if enter was pressed while request was pending, |
| 231 |
|
# we should stop waiting for output and come back to prompt again, |
| 232 |
|
if ($heap->{state}{RequestPending}) { |
| 233 |
|
$kernel->post( $self->{ResponseSession} => $self->{ResponseState} => ' < stopped waiting for response'); |
| 234 |
|
} |
| 235 |
|
|
| 236 |
|
# send command, if buffer is filled |
| 237 |
my $buf = $heap->{state}{InputBuffer}; |
my $buf = $heap->{state}{InputBuffer}; |
| 238 |
if ($buf) { |
if ($buf) { |
| 239 |
|
$heap->{state}{InputBuffer} = ''; |
| 240 |
$heap->{state}{RequestPending} = 1; |
$heap->{state}{RequestPending} = 1; |
|
#print "ord: ", ord($buf), "\n"; |
|
| 241 |
my $response_target = { |
my $response_target = { |
| 242 |
ResponseSession => $self->{ResponseSession}, |
ResponseSession => $self->{ResponseSession}, |
| 243 |
ResponseState => $self->{ResponseState}, |
ResponseState => $self->{ResponseState}, |
| 244 |
}; |
}; |
| 245 |
$kernel->post( $self->{RequestSession} => $self->{RequestState} => $buf, $response_target); |
$kernel->post( $self->{RequestSession} => $self->{RequestState} => $buf, $response_target); |
| 246 |
} |
} |
|
$heap->{state}{InputBuffer} = ''; |
|
| 247 |
|
|
| 248 |
# prepare new loop for fresh prompt |
# prepare new loop for fresh prompt |
| 249 |
print "\n"; |
print "\n"; |
| 250 |
|
|
| 251 |
} |
} |
| 252 |
|
|
| 253 |
# backspace pressed? |
# backspace pressed? |
| 254 |
if (ord($key) == 8) { |
elsif (ord($key) == $self->{conf}{KeyBackspace}) { |
| 255 |
$heap->{state}{InputBuffer} = substr($heap->{state}{InputBuffer}, 0, -1); |
$heap->{state}{InputBuffer} = substr($heap->{state}{InputBuffer}, 0, -1); |
| 256 |
} |
} |
| 257 |
|
|
| 258 |
|
# normal key? |
| 259 |
|
#elsif ($key =~ m/[a-zA-Z? ]/) { |
| 260 |
|
else { |
| 261 |
|
$heap->{state}{InputBuffer} .= $key; |
| 262 |
|
#print $key; |
| 263 |
|
} |
| 264 |
|
|
| 265 |
|
# mark "KeyPressed" for next poll |
| 266 |
|
$heap->{state}{KeyPressed} = 1; |
| 267 |
|
|
| 268 |
} |
} |
| 269 |
|
|
| 270 |
|
#$kernel->post($session, "pollForKey"); |
| 271 |
$kernel->post($session, "pollForKey"); |
#$kernel->run_one_timeslice(); |
| 272 |
#$kernel->delay("term_work", 0.1); |
#$kernel->post($session, "pollForKey"); |
| 273 |
|
#$kernel->yield($session, "pollForKey"); |
| 274 |
|
#$kernel->delay("pollForKey", 0.1); |
| 275 |
|
$kernel->delay("pollForKey", 0.001); |
| 276 |
|
|
| 277 |
} |
} |
| 278 |
|
|
| 281 |
|
|
| 282 |
sub recieveResponse { |
sub recieveResponse { |
| 283 |
my ($kernel, $heap, $session, $response) = @_[KERNEL, HEAP, SESSION, ARG0]; |
my ($kernel, $heap, $session, $response) = @_[KERNEL, HEAP, SESSION, ARG0]; |
| 284 |
if ($response) { print $response, "\n"; } |
if ($response) { |
| 285 |
$heap->{state}{RequestPending} = 0; |
print $response, "\n"; |
| 286 |
|
$heap->{state}{RequestPending} = 0; |
| 287 |
|
} |
| 288 |
} |
} |
| 289 |
sub recievePrompt { |
sub recievePrompt { |
| 290 |
my ($kernel, $heap, $session, $prompt_inlay) = @_[KERNEL, HEAP, SESSION, ARG0]; |
my ($kernel, $heap, $session, $prompt_inlay) = @_[KERNEL, HEAP, SESSION, ARG0]; |