1991-04-07 13:41:50 +00:00
|
|
|
# Combine a real-time scheduling queue and stdwin event handling.
|
1994-08-01 11:34:53 +00:00
|
|
|
# Keeps times in milliseconds.
|
1991-04-07 13:41:50 +00:00
|
|
|
|
1991-08-16 13:22:53 +00:00
|
|
|
import stdwin, stdwinq
|
1991-04-07 13:41:50 +00:00
|
|
|
from stdwinevents import WE_TIMER
|
1991-08-16 13:22:53 +00:00
|
|
|
import mainloop
|
1991-04-07 13:41:50 +00:00
|
|
|
import sched
|
|
|
|
import time
|
|
|
|
|
|
|
|
# Delay function called by the scheduler when it has nothing to do.
|
|
|
|
# Return immediately when something is done, or when the delay is up.
|
|
|
|
#
|
|
|
|
def delayfunc(msecs):
|
|
|
|
#
|
|
|
|
# Check for immediate stdwin event
|
|
|
|
#
|
1991-08-16 13:22:53 +00:00
|
|
|
event = stdwinq.pollevent()
|
1991-04-07 13:41:50 +00:00
|
|
|
if event:
|
1991-08-16 13:22:53 +00:00
|
|
|
mainloop.dispatch(event)
|
1991-04-07 13:41:50 +00:00
|
|
|
return
|
|
|
|
#
|
1994-08-01 11:34:53 +00:00
|
|
|
# Use sleep for very short delays or if there are no windows
|
1991-04-07 13:41:50 +00:00
|
|
|
#
|
1992-01-01 19:35:13 +00:00
|
|
|
if msecs < 100 or mainloop.countwindows() == 0:
|
1991-04-21 19:31:10 +00:00
|
|
|
if msecs > 0:
|
1994-08-01 11:34:53 +00:00
|
|
|
time.sleep(msecs * 0.001)
|
1991-04-07 13:41:50 +00:00
|
|
|
return
|
|
|
|
#
|
|
|
|
# Post a timer event on an arbitrary window and wait for it
|
|
|
|
#
|
1991-08-16 13:22:53 +00:00
|
|
|
window = mainloop.anywindow()
|
1991-04-07 13:41:50 +00:00
|
|
|
window.settimer(msecs/100)
|
1991-08-16 13:22:53 +00:00
|
|
|
event = stdwinq.getevent()
|
1991-04-07 13:41:50 +00:00
|
|
|
window.settimer(0)
|
|
|
|
if event[0] <> WE_TIMER:
|
1991-08-16 13:22:53 +00:00
|
|
|
mainloop.dispatch(event)
|
1991-04-07 13:41:50 +00:00
|
|
|
|
1994-08-01 11:34:53 +00:00
|
|
|
def millitimer():
|
|
|
|
return int(1000 * time.time())
|
|
|
|
|
|
|
|
q = sched.scheduler(millitimer, delayfunc)
|
1991-04-07 13:41:50 +00:00
|
|
|
|
|
|
|
# Export functions enter, enterabs and cancel just like a scheduler
|
|
|
|
#
|
|
|
|
enter = q.enter
|
|
|
|
enterabs = q.enterabs
|
|
|
|
cancel = q.cancel
|
|
|
|
|
|
|
|
# Emptiness check must check both queues
|
|
|
|
#
|
|
|
|
def empty():
|
1992-01-01 19:35:13 +00:00
|
|
|
return q.empty() and mainloop.countwindows() == 0
|
1991-04-07 13:41:50 +00:00
|
|
|
|
|
|
|
# Run until there is nothing left to do
|
|
|
|
#
|
|
|
|
def run():
|
|
|
|
while not empty():
|
|
|
|
if q.empty():
|
1991-08-16 13:22:53 +00:00
|
|
|
mainloop.dispatch(stdwinq.getevent())
|
1991-04-07 13:41:50 +00:00
|
|
|
else:
|
|
|
|
q.run()
|