From fa3a15f5bc50698820cf02276a44262a9a313f7a Mon Sep 17 00:00:00 2001 From: chrys Date: Sun, 10 Jul 2016 23:02:17 +0200 Subject: [PATCH] threading test --- src/fenrir-package/core/commandManager.py | 10 +++--- src/fenrir-package/core/commands.py | 6 ++-- src/fenrir-package/core/inputManager.py | 20 +++++++----- src/fenrir-package/fenrir.py | 39 +++++++++++++---------- src/fenrir-package/screen/linux.py | 9 ++++-- 5 files changed, 49 insertions(+), 35 deletions(-) diff --git a/src/fenrir-package/core/commandManager.py b/src/fenrir-package/core/commandManager.py index 24f06aab..220850c0 100644 --- a/src/fenrir-package/core/commandManager.py +++ b/src/fenrir-package/core/commandManager.py @@ -27,11 +27,11 @@ class commandManager(): def executeCommand(self, environment, currCommand, section = 'commands'): if self.isCommandDefined(environment): - #try: - environ = environment['commands'][section][currCommand].run(environment) - if environ != None: - environment = environ - #except: + try: + environ = environment['commands'][section][currCommand].run(environment) + if environ != None: + environment = environ + except: pass environment['commandInfo']['currCommand'] = '' return environment diff --git a/src/fenrir-package/core/commands.py b/src/fenrir-package/core/commands.py index 0bddfb5b..6832457c 100644 --- a/src/fenrir-package/core/commands.py +++ b/src/fenrir-package/core/commands.py @@ -1,10 +1,10 @@ #!/bin/python -#from commands import curr_line -#from commands import shut_up +import time commandInfo = { 'currCommand': '', -'commandCueue':[] +'commandCueue':[], +'lastCommandTime': time.time() } commands = { diff --git a/src/fenrir-package/core/inputManager.py b/src/fenrir-package/core/inputManager.py index bce62549..da1ebf93 100644 --- a/src/fenrir-package/core/inputManager.py +++ b/src/fenrir-package/core/inputManager.py @@ -11,15 +11,19 @@ class inputManager(): #for dev in self.devices.values(): print(dev) def getKeyPressed(self, environment): - r, w, x = select(self.devices, [], []) + r, w, x = select(self.devices, [], [],0) currShortcut = environment['input']['currShortcut'] - for fd in r: - for event in self.devices[fd].read(): - if event.type == evdev.ecodes.EV_KEY: - if event.value != 0: - currShortcut[str(event.code)] = event.value - else: - del(currShortcut[str(event.code)]) + if r != []: + for fd in r: + for event in self.devices[fd].read(): + if event.type == evdev.ecodes.EV_KEY: + if event.value != 0: + currShortcut[str(event.code)] = event.value + else: + try: + del(currShortcut[str(event.code)]) + except: + pass environment['input']['currShortcut'] = currShortcut environment['input']['currShortcutString'] = self.getShortcutString(environment) return environment diff --git a/src/fenrir-package/fenrir.py b/src/fenrir-package/fenrir.py index a532df72..f1f1e71f 100755 --- a/src/fenrir-package/fenrir.py +++ b/src/fenrir-package/fenrir.py @@ -41,30 +41,37 @@ class fenrir(): self.threadUpdateScreen = Thread(target=self.updateScreen, args=()) self.threadHandleInput = Thread(target=self.handleInput, args=()) self.threadCommands = Thread(target=self.handleCommands, args=()) - self.threadUpdateScreen.start() - self.threadHandleInput.start() - self.threadCommands.start() + #self.threadUpdateScreen.start() + #self.threadHandleInput.start() + #self.threadCommands.start() while(self.environment['generalInformation']['running']): - time.sleep(0.2) + #starttime = time.time() + #time.sleep(0.2) + self.updateScreen() + self.handleInput() + self.handleCommands() + #print(time.time() -starttime) self.shutdown() def handleInput(self): - while(self.environment['generalInformation']['running']): - self.environment = self.environment['runtime']['inputManager'].getKeyPressed(self.environment) - if self.environment['input']['currShortcutString'] == '': - self.environment['commandInfo']['currCommand'] = '' + #while(self.environment['generalInformation']['running']): + self.environment = self.environment['runtime']['inputManager'].getKeyPressed(self.environment) + #if self.environment['input']['currShortcutString'] == '': + # self.environment['commandInfo']['currCommand'] = '' def updateScreen(self): - while(self.environment['generalInformation']['running']): - self.environment = self.environment['runtime']['screenDriver'].analyzeScreen(self.environment) + #while(self.environment['generalInformation']['running']): + self.environment = self.environment['runtime']['screenDriver'].analyzeScreen(self.environment) def handleCommands(self): - while(self.environment['generalInformation']['running']): - self.environment = self.environment['runtime']['commandManager'].getCommandForShortcut(self.environment) - #self.environment['input']['currShortcut'] = {} - if self.environment['commandInfo']['currCommand'] != '': - self.environment = self.environment['runtime']['commandManager'].executeCommand(self.environment, self.environment['commandInfo']['currCommand'], 'commands') - time.sleep(0.5) + #while(self.environment['generalInformation']['running']): + self.environment = self.environment['runtime']['commandManager'].getCommandForShortcut(self.environment) + #self.environment['input']['currShortcut'] = {} + print( self.environment['commandInfo']['currCommand'] ) + if (self.environment['commandInfo']['currCommand'] != '') and \ + (time.time() - self.environment['commandInfo']['lastCommandTime'] >= 0.4): + self.environment = self.environment['runtime']['commandManager'].executeCommand(self.environment, self.environment['commandInfo']['currCommand'], 'commands') + #time.sleep(0.5) def shutdown(self): self.environment['generalInformation']['running'] = False diff --git a/src/fenrir-package/screen/linux.py b/src/fenrir-package/screen/linux.py index 2bbbda4a..3c8ea9fd 100644 --- a/src/fenrir-package/screen/linux.py +++ b/src/fenrir-package/screen/linux.py @@ -10,7 +10,8 @@ import re class screenManager(): def __init__(self, device='/dev/vcsa'): self.vcsaDevicePath = device - + self.textWrapper = textwrap.TextWrapper() + self.textWrapper.drop_whitespace = False def analyzeScreen(self, environment): # read screen currTTY = open('/sys/devices/virtual/tty/tty0/active','r') @@ -36,10 +37,12 @@ class screenManager(): environment['screenData']['newContentText'] = str(environment['screenData']['newContentBytes'][4:][::2].decode('WINDOWS-1250')) #environment['screenData']['newContentText'] = str(environment['screenData']['newContentBytes'][4:][::2].decode('cp1252')).encode('utf-8')[2:] environment['screenData']['newContentAttrib'] = environment['screenData']['newContentBytes'][5:][::2] - #environment['screenData']['newContentText'] = '\n'.join(textwrap.wrap(environment['screenData']['newContentText'], environment['screenData']['columns']))[:-2] - environment['screenData']['newContentText'] = re.sub("(.{"+ str(environment['screenData']['columns'])+"})", "\\1\n", str(environment['screenData']['newContentText']), 0, re.DOTALL) +# environment['screenData']['newContentText'] = '\n'.join(textwrap.wrap(environment['screenData']['newContentText'], environment['screenData']['columns']))[:-2] + #environment['screenData']['newContentText'] = re.sub("(.{"+ str(environment['screenData']['columns'])+"})", "\\1\n", str(environment['screenData']['newContentText']), 0, re.DOTALL) + environment['screenData']['newContentText'] = '\n'.join(self.textWrapper.wrap(environment['screenData']['newContentText'], ))[:-2] if environment['screenData']['newTTY'] != environment['screenData']['oldTTY']: + self.textWrapper.width = environment['screenData']['columns'] environment['screenData']['oldContentBytes'] = b'' environment['screenData']['oldContentAttrib'] = b'' environment['screenData']['oldContentText'] = ''