/[cvs]/joko/Scripts/psh/lib/POE/Component/Terminal.pm
ViewVC logotype

Diff of /joko/Scripts/psh/lib/POE/Component/Terminal.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by cvsjoko, Sat Jun 15 03:45:21 2002 UTC revision 1.6 by cvsjoko, Sat Jun 15 07:46:11 2002 UTC
# Line 5  Line 5 
5  ## $Id$  ## $Id$
6  ##  ##
7  ## $Log$  ## $Log$
8    ## Revision 1.6  2002/06/15 07:46:11  cvsjoko
9    ## + bugfixes for linux
10    ##
11    ## Revision 1.5  2002/06/15 05:19:36  cvsjoko
12    ## + modified order/way of key-handling-logic
13    ##
14    ## Revision 1.4  2002/06/15 04:28:10  cvsjoko
15    ## immediate polling, no delay
16    ##
17    ## Revision 1.3  2002/06/15 04:24:15  cvsjoko
18    ## + added modifier for running in linux or win32
19    ## + modified order of some commands in core key-polling
20    ##
21  ## Revision 1.2  2002/06/15 03:45:21  cvsjoko  ## Revision 1.2  2002/06/15 03:45:21  cvsjoko
22  ## + cvs id & log  ## + cvs id & log
23  ## + clearing inputbuffer just after getting the "enter"-key  ## + clearing inputbuffer just after getting the "enter"-key
# Line 48  my $states = { Line 61  my $states = {
61            recieveResponse => 'recieveResponse',            recieveResponse => 'recieveResponse',
62               };               };
63    
64    sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }
65    
66  sub new {  sub new {
67    my $class = shift;    my $class = shift;
68    $class = ref( $class ) || $class;    $class = ref( $class ) || $class;
# Line 66  sub new { Line 81  sub new {
81    # store entries from args to object ($self)    # store entries from args to object ($self)
82    map { $self->{$_} = $args->{$_}; } keys %{$args};    map { $self->{$_} = $args->{$_}; } keys %{$args};
83    
84      if ( RUNNING_IN_HELL () ) {
85        $self->{conf}{EnterKey} = "\r";
86      } else {
87        $self->{conf}{EnterKey} = "\n";
88      }
89    
90    ## Make our session.  See $states defined up above . . .    ## Make our session.  See $states defined up above . . .
91    POE::Session->create( object_states => [ $self => $states, ], );    POE::Session->create( object_states => [ $self => $states, ], );
92    
# Line 91  sub start { Line 112  sub start {
112    $heap->{state}{InputBuffer} = '';    $heap->{state}{InputBuffer} = '';
113    $heap->{state}{Startup} = 1;    $heap->{state}{Startup} = 1;
114    $heap->{state}{Caption} = $self->{Caption};    $heap->{state}{Caption} = $self->{Caption};
115      $heap->{state}{KeyPressed} = 0;
116      $heap->{state}{RequestPending} = 0;
117        
118    # initially call worker-state    # initially call worker-state
119      ReadMode 4;
120    $kernel->post($session, "pollForKey");    $kernel->post($session, "pollForKey");
121    
122    return;    return;
# Line 166  sub pollForKey { Line 190  sub pollForKey {
190    my $prompt = "#> ";    my $prompt = "#> ";
191    $prompt = $self->{PromptPrefix} . ' ' . $prompt_inlay . $self->{PromptPostfix};    $prompt = $self->{PromptPrefix} . ' ' . $prompt_inlay . $self->{PromptPostfix};
192    
193    if ( ($heap->{state}{KeyPressed} && !$heap->{state}{RequestPending}) || ($heap->{state}{Startup}) ) {    if ( (
194            ( $heap->{state}{KeyPressed} == 1 ) &&
195            ( $heap->{state}{RequestPending} == 0 )
196           ) ||
197            ( $heap->{state}{Startup} )
198          ) {
199      $heap->{state}{Startup} = 0;      $heap->{state}{Startup} = 0;
200      $heap->{state}{KeyPressed} = 0;      $heap->{state}{KeyPressed} = 0;
     #ReadMode 4; # Turn off controls keys  
     #ReadMode 1;  
     #print $prompt;  
201      my $outputline = $prompt . $heap->{state}{InputBuffer};      my $outputline = $prompt . $heap->{state}{InputBuffer};
202      my $s = ' ' x (length($outputline) + 1);      my $s = ' ' x (length($outputline) + 1);
203      print "\r", $s;      print "\r", $s;
204      print "\r", $outputline;      print "\r", $outputline;
205        #print $outputline;
206    }    }
207    
208    my $key = ReadKey(-1);    my $key = ReadKey(-1);
209    if ($key) {    if ( defined $key ) {
       
     $heap->{state}{KeyPressed} = 1;  
210    
211      # normal key?  #print "key: ", ord($key), "\n";
212      if ($key =~ m/[a-zA-Z? ]/) {      
213        $heap->{state}{InputBuffer} .= $key;      # CTRL+C pressed?
214        #print $key;      if (ord($key) == 3) {
215          ReadMode 1;
216          print "\n";
217          exit;
218      }      }
219    
220      # enter pressed?      # enter pressed?
221      if ($key eq "\r") {      if (ord($key) == 10) {
222        # send command  
223          # if enter was pressed while request was pending,
224          # we should stop waiting for output and come back to prompt again,
225          if ($heap->{state}{RequestPending}) {
226            $kernel->post( $self->{ResponseSession} => $self->{ResponseState} => '  < stopped waiting for response');
227          }
228    
229          # send command, if buffer is filled
230        my $buf = $heap->{state}{InputBuffer};        my $buf = $heap->{state}{InputBuffer};
231        if ($buf) {        if ($buf) {
232          $heap->{state}{InputBuffer} = '';          $heap->{state}{InputBuffer} = '';
233          $heap->{state}{RequestPending} = 1;          $heap->{state}{RequestPending} = 1;
         #print "ord: ", ord($buf), "\n";  
234          my $response_target = {          my $response_target = {
235            ResponseSession => $self->{ResponseSession},            ResponseSession => $self->{ResponseSession},
236            ResponseState => $self->{ResponseState},            ResponseState => $self->{ResponseState},
# Line 206  sub pollForKey { Line 240  sub pollForKey {
240    
241        # prepare new loop for fresh prompt        # prepare new loop for fresh prompt
242        print "\n";        print "\n";
243    
244      }      }
245    
246      # backspace pressed?      # backspace pressed?
247      if (ord($key) == 8) {      elsif (ord($key) == 127) {
248        $heap->{state}{InputBuffer} = substr($heap->{state}{InputBuffer}, 0, -1);        $heap->{state}{InputBuffer} = substr($heap->{state}{InputBuffer}, 0, -1);
249      }      }
250    
251        # normal key?
252        #elsif ($key =~ m/[a-zA-Z? ]/) {
253        else {
254          $heap->{state}{InputBuffer} .= $key;
255          #print $key;
256        }
257    
258        # mark "KeyPressed" for next poll
259        $heap->{state}{KeyPressed} = 1;
260    
261    }    }
262    
263        #$kernel->run_one_timeslice();
264    $kernel->post($session, "pollForKey");    $kernel->post($session, "pollForKey");
265    #$kernel->delay("term_work", 0.1);    #$kernel->yield($session, "pollForKey");
266      #$kernel->delay("pollForKey", 0.1);
267      #$kernel->delay("pollForKey", 0.01);
268    
269  }  }
270    
# Line 226  sub pollForKey { Line 273  sub pollForKey {
273    
274  sub recieveResponse {  sub recieveResponse {
275    my ($kernel, $heap, $session, $response) = @_[KERNEL, HEAP, SESSION, ARG0];    my ($kernel, $heap, $session, $response) = @_[KERNEL, HEAP, SESSION, ARG0];
276    if ($response) { print $response, "\n"; }    if ($response) {
277    $heap->{state}{RequestPending} = 0;      print $response, "\n";
278        $heap->{state}{RequestPending} = 0;
279      }
280  }  }
281  sub recievePrompt {  sub recievePrompt {
282    my ($kernel, $heap, $session, $prompt_inlay) = @_[KERNEL, HEAP, SESSION, ARG0];    my ($kernel, $heap, $session, $prompt_inlay) = @_[KERNEL, HEAP, SESSION, ARG0];

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.6

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed