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