changed some stuff of active/inactive detection
[pygdb.git] / DbgTerminal.py
index 89659d035628a9fe7a450bffc9e603ff905eceb7..ebe4e1a2f147ed16bb8c37474724d02f4811dded 100644 (file)
@@ -4,10 +4,13 @@
 __author__ = "shuber"
 
 
+import gobject
 import gtk
+import os
 import pango
 import pty
 import string
+import sys
 import time
 import threading
 import vte
@@ -18,15 +21,19 @@ import ClientIOTerminal
 
 class DbgTerminal (vte.Terminal):
 
-       isactive = True
-       lastrow = 0
-       history = []
-
-
        def __init__(self, clientCmd):
 
                vte.Terminal.__init__(self)
 
+               #Set members
+               self.childpid = None
+               self.history = [""]
+               self.isactive = True
+               self.lastc, self.lastr = 0,0
+               self.gotActiveCallback = []
+               self.gotInactiveCallback = []
+               self.activityChanged = None
+
                #Start debugger
                self.clientCmd = clientCmd
                #Open pseudo-terminal where to-be-debugged process reads/writes to
@@ -34,7 +41,8 @@ class DbgTerminal (vte.Terminal):
 
                #Set up terminal window and initialize debugger
                self.connect("cursor-moved", self.contents_changed)
-               self.connect("child-exited", lambda *w: gtk.main_quit())
+               self.connect("child-exited", quitHandler)
+               gobject.timeout_add(50, self.checkActivityChanged)
 
                #font description
                fontdesc = pango.FontDescription("monospace 9")
@@ -42,56 +50,106 @@ class DbgTerminal (vte.Terminal):
 
 
        def initialize(self):
-               self.fork_command( self.getCommand(), self.getArgv())
+               self.childpid = self.fork_command( self.getCommand(), self.getArgv())
+               self.waitForActivation(0)
                self.setPty(self.client_ptyslave)
-               self.waitForActivation()
+
+       def stopDbg(self):
+
+               if self.childpid != None:
+                       #9=KILL, 15=TERM
+                       os.kill(self.childpid, 15);
+                       self.childpid = None
+
+       def checkActivityChanged(self):
+
+               try:
+
+                       #There was activity
+                       if self.activityChanged != None:
+
+                               res = self.activityChanged
+                               self.activityChanged = None
+
+                               status, param = res
+                               if self.isActive():
+                                       print "got active: ", res
+                                       for cb in self.gotActiveCallback:
+                                               cb(status, param)
+                               else:
+                                       print "got inactive: ", res
+                                       for cb in self.gotInactiveCallback:
+                                               cb(status, param)
+               except:
+                       pass
+
+               return True
+
 
 
        def contents_changed(self, term):
+               assert( self.getHistoryLen()>0 )
+
                c,r = term.get_cursor_position()
+               text = self.get_text_range(self.lastr,self.lastc,r,c-1,lambda *w:True)
+               self.lastc, self.lastr = c,r
 
-               if self.lastrow <= r:
-                       text = self.get_text_range(self.lastrow,0,r,-1,lambda *w:True)
+               #Remove annoying \n at the end
+               assert(text[-1] == "\n")
+               text = text[:-1]
+               #Get the lines and remove empty lines
+               lines = string.split(text, "\n")
 
-                       #Remove the incomplete line
-                       if self.getHistoryLen()>0 and (len(self.history[-1])==0 or self.history[-1]!='\n') :
-                               del self.history[-1]
+               #Remove the incomplete line
+               len = max(0,self.getHistoryLen())
+               self.history[-1] += lines[0]
+               self.history += lines[1:]
 
-                       #Get the lines and remove empty lines
-                       lines = string.split(text, "\n")
 
-                       #Remove last empty line...
-                       if lines[-1] == "":
-                               del lines[-1]
+               #Check if activity status has been changed
+               for i in range(len, self.getHistoryLen()):
+                       line = self.history[i]
+                                       
+                       res = self.testForInactivity(i)
+                       if res != None:
+                               while self.activityChanged != None:
+                                       print "wait for pending activity"
+                                       gtk.main_iteration()
+
+                               self.setActive(False)
+                               self.activityChanged = res
+
+                       res = self.testForActivity(i)
+                       if res != None:
+                               while self.activityChanged != None:
+                                       print "wait for pending activity"
+                                       gtk.main_iteration()
+
+                               self.setActive(True)
+                               self.activityChanged = res
+
 
-                       #Add lines to history
-                       self.history += [l+"\n" for l in lines[:-1]]
-                       self.history += [lines[-1]]
-                       self.lastrow = r
 
 
        def waitForNewline(self):
-               r = self.lastrow
-               while not self.lastrow > r:
+               l = self.getHistoryLen()
+               while not self.getHistoryLen() > l:
                        gtk.main_iteration()
 
        def getHistoryLen(self):
                return len(self.history)
 
-       def waitForRx(self, rx, start=None):    
-
-               if start == None:
-                       start = self.getHistoryLen()
-               if start < 0:
-                       start = 0
-
+       def waitForRx(self, rx, start): 
+               curr = start
                while True:
-                       for no in range(start, self.getHistoryLen()):
+                       assert( curr>=start )
+                       for no in range(curr, self.getHistoryLen()):
                                line = self.history[no]
                                if rx.search(line):
                                        return no, line
 
-                       start = self.getHistoryLen()
+                       #Do not forget the last line
+                       curr = max(start,self.getHistoryLen()-1)
                        gtk.main_iteration()
 
 
@@ -131,6 +189,12 @@ class DbgTerminal (vte.Terminal):
        def waitForActivation(self, his):               
                raise NotImplementedError()
 
+       def testForActivity(self, his):
+               raise NotImplementedError()
+
+       def testForInactivity(self, his):
+               raise NotImplementedError()
+
        def setActive(self, isactive):
                self.isactive = isactive
 
@@ -138,7 +202,12 @@ class DbgTerminal (vte.Terminal):
                return self.isactive
 
        
-       
+
+def quitHandler(*w):
+       try:
+               gtk.main_quit()
+       except:
+               pass
 
 
 
@@ -154,7 +223,7 @@ class DbgWindow (gtk.Window):
 
                #Set up GTK stuff
                gtk.Window.__init__(self)
-               self.connect("destroy", lambda *w: gtk.main_quit())
+               self.connect("destroy", quitHandler)
 
                #Set title and add terminal
                self.set_title("Debugger I/O")