home *** CD-ROM | disk | FTP | other *** search
- # event polling for toolbox users
-
- from sys import exit, argv
- import sys
- from swi import *
- import toolbox
- from toolbox import idblock
- import string
- from events import edict, mdict, wdict
- import traceback
-
- wblock=block(64)
- errorblock=block(64)
- mask=0xE3831
- wimpmask=0xFFFFF
-
- tbclasses={}
- winclasses={}
- usereport=8
-
- class Repout:
- def __init__(self):
- self.s=''
- def write(self,str):
- p=string.splitfields(self.s+str,'\n')
- self.s=p[-1]
- str=string.joinfields(p[:-1],'\n')
- if str:
- report(str,0)
- def flush(self):
- if self.s:
- report(self.s,0)
- self.s=''
-
- def report(s,n=16):
- if n<=usereport:
- errorblock[0]=256
- errorblock[3]=0
- errorblock[4]=0x4cd42
- errorblock.padstring(s[:200],'\0',20)
- swi(0x600e7,"ib0",17,errorblock)
-
- def errorbox(s):
- errorblock[0]=0
- errorblock.padstring(s[:200],'\0',4)
- swi(0x600df,"b1",errorblock)
-
-
- #make a block of wimp messages or toolbox events
- def makeblock(d):
- b=block(len(d)+1)
- i=0
- for s in d.keys():
- if s!=0:
- b[i]=s
- i=i+1
- b[i]=0
- return b
-
- #initialise toolbox
- #mask is passed to wimp poll.
- #wimpmask masks out other unmaskable events
- def initialise(dname):
- global wimpmask,mask
- toolbox.initialise(makeblock(mdict),makeblock(edict),dname)
- for s in wdict.keys():
- wimpmask=wimpmask&~(1<<s)
- mask=mask&~(1<<s)
-
- def dispatch(cdict,ndict,ccode,ncode):
- report('object='+`hex(ccode)`+' action='+`hex(ncode)`,32)
- if cdict.has_key(ccode):
- ci=cdict[ccode]
- cc=ci.__class__
- if cc.actions.has_key(ncode):
- cc.actions[ncode](ci)
- return
- else:
- if ndict.has_key(ncode):
- s=ndict[ncode]
- if cc.__dict__.has_key(s):
- cc.actions[ncode]=cc.__dict__[s]
- cc.actions[ncode](ci)
- return
- report("no action for ncode"+`hex(ncode)`,2)
- return
- report("no object for ccode"+`hex(ccode)`,2)
-
-
- #call wimp_poll, send result to event,message or wimp handler
- def poll():
- code=swi(0x600c7,"ib;i",mask,wblock)
- if code==512:
- dispatch(tbclasses,edict,idblock[0],wblock[2])
- elif code==17 or code==18:
- m=wblock[4]
- report("message "+`wblock[4]`,32)
- if mdict.has_key(m):
- try:
- msghandler.__class__.__dict__[mdict[m]](msghandler)
- except:
- if m!=0:
- report ("failed to handle message "+`m`+" : "+sys.exc_type,2)
- traceback.print_exc()
- exit(0)
- else:
- if wimpmask & 1<<code:
- return
- if code==6:
- handle=wblock[4]
- elif code==7 or code==9 or code==13:
- handle=0
- else:
- handle=wblock[0]
- dispatch(winclasses,wdict,handle,code)
-