intial rewrite of command detection; move command execution to event

This commit is contained in:
chrys 2017-07-20 00:41:12 +02:00
parent 70156493ff
commit 7dcff82cf5
6 changed files with 93 additions and 80 deletions

View File

@ -199,7 +199,6 @@ class commandManager():
self.env['commands'][section][command].run() self.env['commands'][section][command].run()
except Exception as e: except Exception as e:
self.env['runtime']['debug'].writeDebugOut("Executing command:" + section + "." + command +' ' + str(e),debug.debugLevel.ERROR) self.env['runtime']['debug'].writeDebugOut("Executing command:" + section + "." + command +' ' + str(e),debug.debugLevel.ERROR)
self.clearCommandQueued()
self.env['commandInfo']['lastCommandExecutionTime'] = time.time() self.env['commandInfo']['lastCommandExecutionTime'] = time.time()
def getCommandDescription(self, command, section = 'commands'): def getCommandDescription(self, command, section = 'commands'):
@ -208,20 +207,8 @@ class commandManager():
return self.env['commands'][section][command].getDescription() return self.env['commands'][section][command].getDescription()
except Exception as e: except Exception as e:
self.env['runtime']['debug'].writeDebugOut('commandManager.getCommandDescription:' + str(e),debug.debugLevel.ERROR) self.env['runtime']['debug'].writeDebugOut('commandManager.getCommandDescription:' + str(e),debug.debugLevel.ERROR)
self.clearCommandQueued()
self.env['commandInfo']['lastCommandExecutionTime'] = time.time() self.env['commandInfo']['lastCommandExecutionTime'] = time.time()
def isCommandQueued(self):
return self.env['commandInfo']['currCommand'] != ''
def clearCommandQueued(self):
self.env['commandInfo']['currCommand'] = ''
def queueCommand(self, command):
if command == '':
return
self.env['commandInfo']['currCommand'] = command
def commandExists(self, command, section = 'commands'): def commandExists(self, command, section = 'commands'):
return( command in self.env['commands'][section]) return( command in self.env['commands'][section])
def getShortcutForCommand(self, command): def getShortcutForCommand(self, command):

View File

@ -17,6 +17,7 @@ class fenrirEventType(Enum):
BrailleFlush = 6 BrailleFlush = 6
ScreenChanged = 7 ScreenChanged = 7
HeartBeat = 8 # for time based scheduling HeartBeat = 8 # for time based scheduling
ExecuteCommand = 9
def __int__(self): def __int__(self):
return self.value return self.value
def __str__(self): def __str__(self):

View File

@ -47,15 +47,17 @@ class eventManager():
self.env['runtime']['debug'].writeDebugOut('eventManager:eventDispatcher:start: event:' + str(event['Type']) + ' QueueSize:' + str( self._eventQueue.qsize()),debug.debugLevel.INFO) self.env['runtime']['debug'].writeDebugOut('eventManager:eventDispatcher:start: event:' + str(event['Type']) + ' QueueSize:' + str( self._eventQueue.qsize()),debug.debugLevel.INFO)
if not event: if not event:
return return
if not event['Type']:
return
if event['Type'] == fenrirEventType.Ignore: if event['Type'] == fenrirEventType.Ignore:
return return
elif event['Type'] == fenrirEventType.StopMainLoop: elif event['Type'] == fenrirEventType.StopMainLoop:
self.handleStopMainLoop() self.handleStopMainLoop(event)
return return
elif event['Type'] == fenrirEventType.ScreenUpdate: elif event['Type'] == fenrirEventType.ScreenUpdate:
self.env['runtime']['fenrirManager'].handleScreenUpdate() self.env['runtime']['fenrirManager'].handleScreenUpdate(event)
elif event['Type'] == fenrirEventType.KeyboardInput: elif event['Type'] == fenrirEventType.KeyboardInput:
self.env['runtime']['fenrirManager'].handleInput() self.env['runtime']['fenrirManager'].handleInput(event)
elif event['Type'] == fenrirEventType.BrailleInput: elif event['Type'] == fenrirEventType.BrailleInput:
pass pass
elif event['Type'] == fenrirEventType.PlugInputDevice: elif event['Type'] == fenrirEventType.PlugInputDevice:
@ -63,18 +65,19 @@ class eventManager():
elif event['Type'] == fenrirEventType.BrailleFlush: elif event['Type'] == fenrirEventType.BrailleFlush:
pass pass
elif event['Type'] == fenrirEventType.ScreenChanged: elif event['Type'] == fenrirEventType.ScreenChanged:
self.env['runtime']['fenrirManager'].handleScreenChange() self.env['runtime']['fenrirManager'].handleScreenChange(event)
elif event['Type'] == fenrirEventType.HeartBeat: elif event['Type'] == fenrirEventType.HeartBeat:
self.env['runtime']['fenrirManager'].handleHeartBeat() self.env['runtime']['fenrirManager'].handleHeartBeat(event)
elif event['Type'] == fenrirEventType.ExecuteCommand:
self.env['runtime']['fenrirManager'].handleExecuteCommand(event)
def isMainEventLoopRunning(self): def isMainEventLoopRunning(self):
return self._mainLoopRunning.value == 1 return self._mainLoopRunning.value == 1
def startMainEventLoop(self): def startMainEventLoop(self):
self._mainLoopRunning.value = 1 self._mainLoopRunning.value = 1
while( self.isMainEventLoopRunning()): while( self.isMainEventLoopRunning()):
st = time.time()
self.proceedEventLoop() self.proceedEventLoop()
def handleStopMainLoop(self): def handleStopMainLoop(self, event):
self._mainLoopRunning.value = 0 self._mainLoopRunning.value = 0
time.sleep(0.1) time.sleep(0.1)
def stopMainEventLoop(self, Force = False): def stopMainEventLoop(self, Force = False):

View File

@ -13,6 +13,7 @@ if not os.path.dirname(os.path.realpath(fenrirVersion.__file__)) in sys.path:
from core import i18n from core import i18n
from core import settingsManager from core import settingsManager
from core import debug from core import debug
from core.eventData import fenrirEventType
import argparse import argparse
class fenrirManager(): class fenrirManager():
@ -31,8 +32,9 @@ class fenrirManager():
signal.signal(signal.SIGINT, self.captureSignal) signal.signal(signal.SIGINT, self.captureSignal)
signal.signal(signal.SIGTERM, self.captureSignal) signal.signal(signal.SIGTERM, self.captureSignal)
self.initialized = True self.initialized = True
self.wasCommand = False self.modifierInput = False
self.singleKeyCommand = False
self.command = ''
def handleArgs(self): def handleArgs(self):
args = None args = None
parser = argparse.ArgumentParser(description="Fenrir Help") parser = argparse.ArgumentParser(description="Fenrir Help")
@ -49,31 +51,44 @@ class fenrirManager():
return return
self.environment['runtime']['eventManager'].startMainEventLoop() self.environment['runtime']['eventManager'].startMainEventLoop()
self.shutdown() self.shutdown()
def handleInput(self): def handleInput(self, event):
startTime = time.time() #startTime = time.time()
eventReceived = self.environment['runtime']['inputManager'].getInputEvent() eventReceived = self.environment['runtime']['inputManager'].getInputEvent()
if eventReceived: if eventReceived:
self.prepareCommand()
#if not self.environment['runtime']['screenManager'].isSuspendingScreen(): if self.environment['runtime']['screenManager'].isSuspendingScreen():
# if self.environment['runtime']['helpManager'].handleTutorialMode():
# self.wasCommand = True
if not (self.wasCommand or self.environment['runtime']['helpManager'].isTutorialMode()) or self.environment['runtime']['screenManager'].isSuspendingScreen():
self.environment['runtime']['inputManager'].writeEventBuffer() self.environment['runtime']['inputManager'].writeEventBuffer()
if self.environment['runtime']['inputManager'].noKeyPressed(): else:
if self.wasCommand:
self.wasCommand = False
self.environment['runtime']['inputManager'].clearEventBuffer()
self.environment['runtime']['commandManager'].clearCommandQueued()
if self.environment['runtime']['helpManager'].isTutorialMode(): if self.environment['runtime']['helpManager'].isTutorialMode():
self.environment['runtime']['inputManager'].clearEventBuffer() self.environment['runtime']['inputManager'].clearEventBuffer()
self.detectCommand()
if self.modifierInput:
self.environment['runtime']['inputManager'].clearEventBuffer()
if self.singleKeyCommand:
if self.environment['runtime']['inputManager'].noKeyPressed():
self.environment['runtime']['inputManager'].clearEventBuffer()
else:
self.environment['runtime']['inputManager'].writeEventBuffer()
if self.environment['runtime']['inputManager'].noKeyPressed():
self.modifierInput = False
self.singleKeyCommand = False
if self.environment['input']['keyForeward'] > 0: if self.environment['input']['keyForeward'] > 0:
self.environment['input']['keyForeward'] -=1 self.environment['input']['keyForeward'] -=1
self.environment['runtime']['screenManager'].update('onInput') self.environment['runtime']['screenManager'].update('onInput')
self.environment['runtime']['commandManager'].executeDefaultTrigger('onInput') self.environment['runtime']['commandManager'].executeDefaultTrigger('onInput')
self.handleCommands() #print('handleInput:',time.time() - startTime)
print('handleInput:',time.time() - startTime) def handleExecuteCommand(self, event):
if event['Data'] == '':
def handleScreenChange(self): return
command = event['Data']
if self.environment['runtime']['helpManager'].isTutorialMode():
if self.environment['runtime']['commandManager'].commandExists( command, 'help'):
self.environment['runtime']['commandManager'].executeCommand( command, 'help')
return
self.environment['runtime']['commandManager'].executeCommand( command, 'commands')
def handleScreenChange(self, event):
self.environment['runtime']['screenManager'].update('onScreenChange') self.environment['runtime']['screenManager'].update('onScreenChange')
''' '''
if self.environment['runtime']['applicationManager'].isApplicationChange(): if self.environment['runtime']['applicationManager'].isApplicationChange():
@ -84,8 +99,8 @@ class fenrirManager():
''' '''
self.environment['runtime']['commandManager'].executeDefaultTrigger('onScreenChanged') self.environment['runtime']['commandManager'].executeDefaultTrigger('onScreenChanged')
def handleScreenUpdate(self): def handleScreenUpdate(self, event):
startTime = time.time() #startTime = time.time()
self.environment['runtime']['screenManager'].update('onUpdate') self.environment['runtime']['screenManager'].update('onUpdate')
''' '''
@ -100,39 +115,46 @@ class fenrirManager():
self.environment['runtime']['cursorManager'].isCursorHorizontalMove(): self.environment['runtime']['cursorManager'].isCursorHorizontalMove():
self.environment['runtime']['commandManager'].executeDefaultTrigger('onCursorChange') self.environment['runtime']['commandManager'].executeDefaultTrigger('onCursorChange')
self.environment['runtime']['commandManager'].executeDefaultTrigger('onScreenUpdate') self.environment['runtime']['commandManager'].executeDefaultTrigger('onScreenUpdate')
print('handleScreenUpdate:',time.time() - startTime) #print('handleScreenUpdate:',time.time() - startTime)
def handlePlugInputDevice(self): def handlePlugInputDevice(self, event):
self.environment['runtime']['commandManager'].executeDefaultTrigger('PlugInputDevice') self.environment['runtime']['commandManager'].executeDefaultTrigger('PlugInputDevice')
def handleHeartBeat(self): def handleHeartBeat(self, event):
self.environment['runtime']['commandManager'].executeDefaultTrigger('onHeartBeat') self.environment['runtime']['commandManager'].executeDefaultTrigger('onHeartBeat')
#self.environment['runtime']['outputManager'].brailleText(flush=False) #self.environment['runtime']['outputManager'].brailleText(flush=False)
def prepareCommand(self): def detectCommand(self):
if self.environment['runtime']['screenManager'].isSuspendingScreen():
self.wasCommand = False
return
if self.environment['runtime']['inputManager'].noKeyPressed():
return
if self.environment['input']['keyForeward'] > 0: if self.environment['input']['keyForeward'] > 0:
return return
if self.environment['runtime']['inputManager'].isKeyPress():
self.modifierInput = self.environment['runtime']['inputManager'].currKeyIsModifier()
else:
if not self.environment['runtime']['inputManager'].noKeyPressed():
if self.singleKeyCommand:
self.singleKeyCommand = len(self.environment['input']['prevDeepestInput']) == 1
# key is already released. we need the old one
if not( self.singleKeyCommand and self.environment['runtime']['inputManager'].noKeyPressed()):
shortcut = self.environment['runtime']['inputManager'].getCurrShortcut() shortcut = self.environment['runtime']['inputManager'].getCurrShortcut()
command = self.environment['runtime']['inputManager'].getCommandForShortcut(shortcut) self.command = self.environment['runtime']['inputManager'].getCommandForShortcut(shortcut)
#if len(self.environment['input']['prevDeepestInput']) >= len(self.environment['input']['currInput']): if not self.modifierInput:
self.wasCommand = command != '' or self.environment['runtime']['inputManager'].isFenrirKeyPressed() or self.environment['runtime']['inputManager'].isScriptKeyPressed() if self.environment['runtime']['inputManager'].isKeyPress():
if command == '': if self.command != '':
return self.singleKeyCommand = True
self.environment['runtime']['commandManager'].queueCommand(command)
def handleCommands(self): if not (self.singleKeyCommand or self.modifierInput):
if not self.environment['runtime']['commandManager'].isCommandQueued():
return return
if len(self.environment['input']['prevDeepestInput']) > len(self.environment['input']['currInput']):
return # fire event
if self.environment['runtime']['helpManager'].isTutorialMode(): if self.command != '':
self.environment['runtime']['commandManager'].executeCommand( self.environment['commandInfo']['currCommand'], 'help') if self.modifierInput:
self.environment['runtime']['commandManager'].executeCommand( self.environment['commandInfo']['currCommand'], 'commands') self.environment['runtime']['eventManager'].putToEventQueue(fenrirEventType.ExecuteCommand, self.command)
self.command = ''
else:
if self.singleKeyCommand:
if self.environment['runtime']['inputManager'].noKeyPressed():
self.environment['runtime']['eventManager'].putToEventQueue(fenrirEventType.ExecuteCommand, self.command)
self.command = ''
def shutdownRequest(self): def shutdownRequest(self):
self.environment['runtime']['eventManager'].stopMainEventLoop() self.environment['runtime']['eventManager'].stopMainEventLoop()

View File

@ -141,13 +141,6 @@ class inputManager():
eventName = 'KEY_SCRIPT' eventName = 'KEY_SCRIPT'
return eventName return eventName
def isConsumeInput(self):
return self.env['runtime']['commandManager'].isCommandQueued() and \
not self.env['input']['keyForeward']
#and
# not (self.env['input']['keyForeward'] or \
# self.env['runtime']['settingsManager'].getSettingAsBool(, 'keyboard', 'grabDevices'))
def clearEventBuffer(self): def clearEventBuffer(self):
self.env['runtime']['inputDriver'].clearEventBuffer() self.env['runtime']['inputDriver'].clearEventBuffer()
@ -155,7 +148,6 @@ class inputManager():
try: try:
if self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'): if self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'):
self.env['runtime']['inputDriver'].writeEventBuffer() self.env['runtime']['inputDriver'].writeEventBuffer()
time.sleep(0.008)
self.clearEventBuffer() self.clearEventBuffer()
if len(self.env['input']['currInput']) == 1: if len(self.env['input']['currInput']) == 1:
if self.env['input']['currInput'][0] in ['KEY_UP','KEY_DOWN']: if self.env['input']['currInput'][0] in ['KEY_UP','KEY_DOWN']:
@ -172,7 +164,8 @@ class inputManager():
def noKeyPressed(self): def noKeyPressed(self):
return self.env['input']['currInput'] == [] return self.env['input']['currInput'] == []
def isKeyPress(self):
return (self.env['input']['prevInput'] == []) and (self.env['input']['currInput'] != [])
def getPrevDeepestInput(self): def getPrevDeepestInput(self):
shortcut = [] shortcut = []
shortcut.append(self.env['input']['shortcutRepeat']) shortcut.append(self.env['input']['shortcutRepeat'])
@ -185,9 +178,12 @@ class inputManager():
shortcut.append(self.env['input']['prevInput']) shortcut.append(self.env['input']['prevInput'])
return str(shortcut) return str(shortcut)
def getCurrShortcut(self): def getCurrShortcut(self, inputSequence = None):
shortcut = [] shortcut = []
shortcut.append(self.env['input']['shortcutRepeat']) shortcut.append(self.env['input']['shortcutRepeat'])
if inputSequence:
shortcut.append(inputSequence)
else:
shortcut.append(self.env['input']['currInput']) shortcut.append(self.env['input']['currInput'])
if len(self.env['input']['prevInput']) < len(self.env['input']['currInput']): if len(self.env['input']['prevInput']) < len(self.env['input']['currInput']):
if self.env['input']['shortcutRepeat'] > 1 and not self.shortcutExists(str(shortcut)): if self.env['input']['shortcutRepeat'] > 1 and not self.shortcutExists(str(shortcut)):
@ -198,6 +194,10 @@ class inputManager():
self.env['runtime']['debug'].writeDebugOut("currShortcut " + str(shortcut) ,debug.debugLevel.INFO) self.env['runtime']['debug'].writeDebugOut("currShortcut " + str(shortcut) ,debug.debugLevel.INFO)
return str(shortcut) return str(shortcut)
def currKeyIsModifier(self):
if len(self.env['input']['prevDeepestInput']) != 1:
return False
return (self.env['input']['currInput'][0] =='KEY_FENRIR') or (self.env['input']['currInput'][0] == 'KEY_SCRIPT')
def isFenrirKey(self, eventName): def isFenrirKey(self, eventName):
return eventName in self.env['input']['fenrirKey'] return eventName in self.env['input']['fenrirKey']
def isScriptKey(self, eventName): def isScriptKey(self, eventName):

View File

@ -260,7 +260,7 @@ class driver():
self.env['screen']['newCursor']['x'] = int( self.env['screen']['newContentBytes'][2]) self.env['screen']['newCursor']['x'] = int( self.env['screen']['newContentBytes'][2])
self.env['screen']['newCursor']['y'] = int( self.env['screen']['newContentBytes'][3]) self.env['screen']['newCursor']['y'] = int( self.env['screen']['newContentBytes'][3])
# analyze content # analyze content
s = time.time()
if screenEncoding.upper() == 'AUTO': if screenEncoding.upper() == 'AUTO':
self.updateCharMap(str(self.env['screen']['newTTY'])) self.updateCharMap(str(self.env['screen']['newTTY']))
self.env['screen']['newContentText'], \ self.env['screen']['newContentText'], \
@ -271,7 +271,7 @@ class driver():
self.env['screen']['newContentText'] = screen_utils.removeNonprintable(self.env['screen']['newContentText']) self.env['screen']['newContentText'] = screen_utils.removeNonprintable(self.env['screen']['newContentText'])
self.env['screen']['newContentAttrib'] = self.env['screen']['newContentBytes'][5:][::2] self.env['screen']['newContentAttrib'] = self.env['screen']['newContentBytes'][5:][::2]
self.env['screen']['newContentText'] = screen_utils.insertNewlines(self.env['screen']['newContentText'], self.env['screen']['columns']) self.env['screen']['newContentText'] = screen_utils.insertNewlines(self.env['screen']['newContentText'], self.env['screen']['columns'])
print(time.time() -s, self.env['screen']['newTTY'] )
if self.env['screen']['newTTY'] != self.env['screen']['oldTTY']: if self.env['screen']['newTTY'] != self.env['screen']['oldTTY']:
self.env['screen']['oldContentBytes'] = b'' self.env['screen']['oldContentBytes'] = b''
self.env['screen']['oldContentAttrib'] = b'' self.env['screen']['oldContentAttrib'] = b''