home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / !Python / Lib / RiscLib / py / tbevent < prev    next >
Encoding:
Text File  |  1996-08-19  |  2.8 KB  |  116 lines

  1. # event polling for toolbox users
  2.  
  3. from sys import exit, argv
  4. import sys
  5. from swi import *
  6. import toolbox
  7. from toolbox import idblock
  8. import string
  9. from events import edict, mdict, wdict
  10. import traceback
  11.  
  12. wblock=block(64)
  13. errorblock=block(64)
  14. mask=0xE3831
  15. wimpmask=0xFFFFF
  16.  
  17. tbclasses={}
  18. winclasses={}
  19. usereport=8
  20.  
  21. class Repout:
  22.       def __init__(self):
  23.           self.s=''
  24.       def write(self,str):
  25.           p=string.splitfields(self.s+str,'\n')
  26.           self.s=p[-1]
  27.           str=string.joinfields(p[:-1],'\n')
  28.           if str:
  29.              report(str,0)
  30.       def flush(self):
  31.           if self.s:
  32.              report(self.s,0)
  33.              self.s=''
  34.  
  35. def report(s,n=16):
  36.     if n<=usereport:
  37.        errorblock[0]=256
  38.        errorblock[3]=0
  39.        errorblock[4]=0x4cd42
  40.        errorblock.padstring(s[:200],'\0',20)
  41.        swi(0x600e7,"ib0",17,errorblock)
  42.  
  43. def errorbox(s):
  44.     errorblock[0]=0
  45.     errorblock.padstring(s[:200],'\0',4)
  46.     swi(0x600df,"b1",errorblock)
  47.  
  48.  
  49. #make a block of wimp messages or toolbox events
  50. def makeblock(d):
  51.     b=block(len(d)+1)
  52.     i=0
  53.     for s in d.keys():
  54.         if s!=0:
  55.            b[i]=s
  56.            i=i+1
  57.     b[i]=0
  58.     return b
  59.  
  60. #initialise toolbox
  61. #mask is passed to wimp poll.
  62. #wimpmask masks out other unmaskable events
  63. def initialise(dname):
  64.     global wimpmask,mask
  65.     toolbox.initialise(makeblock(mdict),makeblock(edict),dname)
  66.     for s in wdict.keys():
  67.         wimpmask=wimpmask&~(1<<s)
  68.         mask=mask&~(1<<s)
  69.  
  70. def dispatch(cdict,ndict,ccode,ncode):
  71.     report('object='+`hex(ccode)`+' action='+`hex(ncode)`,32)
  72.     if cdict.has_key(ccode):
  73.        ci=cdict[ccode]
  74.        cc=ci.__class__
  75.        if cc.actions.has_key(ncode):
  76.           cc.actions[ncode](ci)
  77.           return
  78.        else:
  79.           if ndict.has_key(ncode):
  80.              s=ndict[ncode]
  81.              if cc.__dict__.has_key(s):
  82.                 cc.actions[ncode]=cc.__dict__[s]
  83.                 cc.actions[ncode](ci)
  84.                 return
  85.        report("no action for ncode"+`hex(ncode)`,2)
  86.        return
  87.     report("no object for ccode"+`hex(ccode)`,2)
  88.  
  89.  
  90. #call wimp_poll, send result to event,message or wimp handler
  91. def poll():
  92.     code=swi(0x600c7,"ib;i",mask,wblock)
  93.     if code==512:
  94.        dispatch(tbclasses,edict,idblock[0],wblock[2])
  95.     elif code==17 or code==18:
  96.        m=wblock[4]
  97.        report("message "+`wblock[4]`,32)
  98.        if mdict.has_key(m):
  99.           try:
  100.              msghandler.__class__.__dict__[mdict[m]](msghandler)
  101.           except:
  102.              if m!=0:
  103.                 report ("failed to handle message "+`m`+" : "+sys.exc_type,2)
  104.                 traceback.print_exc()
  105.              exit(0)
  106.     else:
  107.        if wimpmask & 1<<code:
  108.           return
  109.        if code==6:
  110.           handle=wblock[4]
  111.        elif code==7 or code==9 or code==13:
  112.             handle=0
  113.        else:
  114.             handle=wblock[0]
  115.        dispatch(winclasses,wdict,handle,code)
  116.