From 35fb684627a0a4151da0c1a9e63dba66906fe78e Mon Sep 17 00:00:00 2001 From: chrys Date: Tue, 6 Mar 2018 22:14:05 +0000 Subject: [PATCH] WIP move key detection to watchdog --- config/settings/settings.conf | 2 +- src/fenrir/core/eventData.py | 0 src/fenrir/core/fenrirManager.py | 19 ++++-- src/fenrir/core/inputDriver.py | 5 +- src/fenrir/core/inputManager.py | 92 +++++++++++++-------------- src/fenrir/inputDriver/dummyDriver.py | 5 -- src/fenrir/inputDriver/evdevDriver.py | 65 +++++-------------- 7 files changed, 79 insertions(+), 109 deletions(-) mode change 100755 => 100644 src/fenrir/core/eventData.py diff --git a/config/settings/settings.conf b/config/settings/settings.conf index 29ea299f..16efab11 100644 --- a/config/settings/settings.conf +++ b/config/settings/settings.conf @@ -124,7 +124,7 @@ driver=vcsaDriver encoding=auto screenUpdateDelay=0.05 suspendingScreen= -autodetectSuspendingScreen=True +autodetectSuspendingScreen=False [keyboard] driver=evdevDriver diff --git a/src/fenrir/core/eventData.py b/src/fenrir/core/eventData.py old mode 100755 new mode 100644 diff --git a/src/fenrir/core/fenrirManager.py b/src/fenrir/core/fenrirManager.py index 4ac8ec97..6a4f0ee2 100644 --- a/src/fenrir/core/fenrirManager.py +++ b/src/fenrir/core/fenrirManager.py @@ -49,18 +49,21 @@ class fenrirManager(): self.environment['runtime']['eventManager'].startMainEventLoop() self.shutdown() def handleInput(self, event): - #startTime = time.time() - eventReceived = self.environment['runtime']['inputManager'].getInputEvent() + #startTime = time.time + if not event['Data']: + event['Data'] = self.environment['runtime']['inputManager'].getInputEvent() + if event['Data']: + event['Data']['EventName'] = self.environment['runtime']['inputManager'].convertEventName(event['Data']['EventName']) + self.environment['runtime']['inputManager'].handleInputEvent(event['Data']) if self.environment['runtime']['inputManager'].noKeyPressed(): self.environment['runtime']['inputManager'].clearLastDeepInput() - if eventReceived: - + if True: if self.environment['runtime']['screenManager'].isSuspendingScreen(): self.environment['runtime']['inputManager'].writeEventBuffer() else: if self.environment['runtime']['helpManager'].isTutorialMode(): self.environment['runtime']['inputManager'].clearEventBuffer() - + print('detect') self.detectCommand() if self.modifierInput: @@ -82,6 +85,7 @@ class fenrirManager(): if event['Data'] == '': return command = event['Data'] + print('fire',command) if self.environment['runtime']['helpManager'].isTutorialMode(): if self.environment['runtime']['commandManager'].commandExists( command, 'help'): self.environment['runtime']['commandManager'].executeCommand( command, 'help') @@ -143,10 +147,10 @@ class fenrirManager(): if self.environment['runtime']['inputManager'].isKeyPress(): if self.command != '': self.singleKeyCommand = True - + print(self.singleKeyCommand,self.modifierInput) if not (self.singleKeyCommand or self.modifierInput): return - + print(self.command,1) # fire event if self.command != '': if self.modifierInput: @@ -157,6 +161,7 @@ class fenrirManager(): if self.environment['runtime']['inputManager'].noKeyPressed(): self.environment['runtime']['eventManager'].putToEventQueue(fenrirEventType.ExecuteCommand, self.command) self.command = '' + print(self.command,2) def shutdownRequest(self): try: self.environment['runtime']['eventManager'].stopMainEventLoop() diff --git a/src/fenrir/core/inputDriver.py b/src/fenrir/core/inputDriver.py index b17a537c..f4d19e46 100644 --- a/src/fenrir/core/inputDriver.py +++ b/src/fenrir/core/inputDriver.py @@ -17,9 +17,12 @@ class inputDriver(): self.releaseDevices() self._isInitialized = False def getInputEvent(self): + time.sleep(0.05) + return None + def handleInputEvent(self, event): time.sleep(0.05) if not self._initialized: - return None + return def writeEventBuffer(self): if not self._initialized: return diff --git a/src/fenrir/core/inputManager.py b/src/fenrir/core/inputManager.py index 139ed4c6..bb2b8a82 100644 --- a/src/fenrir/core/inputManager.py +++ b/src/fenrir/core/inputManager.py @@ -27,52 +27,52 @@ class inputManager(): def shutdown(self): self.removeAllDevices() self.env['runtime']['settingsManager'].shutdownDriver('inputDriver') - - def getInputEvent(self): - eventReceived = False - mEvent = self.env['runtime']['inputDriver'].getInputEvent() - if mEvent: - mEvent['EventName'] = self.convertEventName(mEvent['EventName']) - eventReceived = True - self.env['input']['prevInput'] = self.env['input']['currInput'].copy() - if mEvent['EventState'] == 0: - if mEvent['EventName'] in self.env['input']['currInput']: - self.env['input']['currInput'].remove(mEvent['EventName']) - if len(self.env['input']['currInput']) > 1: - self.env['input']['currInput'] = sorted(self.env['input']['currInput']) - elif len(self.env['input']['currInput']) == 0: - self.env['input']['shortcutRepeat'] = 1 - self.setLedState = self.handleLedStates(mEvent) - self.lastInputTime = time.time() - elif mEvent['EventState'] == 1: - if not mEvent['EventName'] in self.env['input']['currInput']: - self.env['input']['currInput'].append(mEvent['EventName']) - if len(self.env['input']['currInput']) > 1: - self.env['input']['currInput'] = sorted(self.env['input']['currInput']) - if len(self.lastDeepestInput) < len(self.env['input']['currInput']): - self.setLastDeepestInput( self.env['input']['currInput'].copy()) - elif self.lastDeepestInput == self.env['input']['currInput']: - if time.time() - self.lastInputTime <= self.env['runtime']['settingsManager'].getSettingAsFloat('keyboard','doubleTapTimeout'): - self.env['input']['shortcutRepeat'] += 1 - else: - self.env['input']['shortcutRepeat'] = 1 - self.setLedState = self.handleLedStates(mEvent) - self.lastInputTime = time.time() - elif mEvent['EventState'] == 2: - self.lastInputTime = time.time() - else: - pass - self.env['input']['oldNumLock'] = self.env['input']['newNumLock'] - self.env['input']['newNumLock'] = self.env['runtime']['inputDriver'].getLedState() - self.env['input']['oldCapsLock'] = self.env['input']['newCapsLock'] - self.env['input']['newCapsLock'] = self.env['runtime']['inputDriver'].getLedState(1) - self.env['input']['oldScrollLock'] = self.env['input']['newScrollLock'] - self.env['input']['newScrollLock'] = self.env['runtime']['inputDriver'].getLedState(2) - self.env['runtime']['debug'].writeDebugOut("currInput " + str(self.env['input']['currInput'] ) ,debug.debugLevel.INFO) - if self.noKeyPressed(): - self.env['input']['prevInput'] = [] - self.setLedState = True - return eventReceived + def getInputEvent(self): + return self.env['runtime']['inputDriver'].getInputEvent() + def handleInputEvent(self, eventData): + if not eventData: + return + print(self.env['input']['currInput']) + print(eventData['EventState']) + self.env['input']['prevInput'] = self.env['input']['currInput'].copy() + if eventData['EventState'] == 0: + if eventData['EventName'] in self.env['input']['currInput']: + self.env['input']['currInput'].remove(eventData['EventName']) + if len(self.env['input']['currInput']) > 1: + self.env['input']['currInput'] = sorted(self.env['input']['currInput']) + elif len(self.env['input']['currInput']) == 0: + self.env['input']['shortcutRepeat'] = 1 + self.setLedState = self.handleLedStates(eventData) + self.lastInputTime = time.time() + elif eventData['EventState'] == 1: + if not eventData['EventName'] in self.env['input']['currInput']: + if not eventData['EventName'] == 'KEY_RESERVED': + self.env['input']['currInput'].append(eventData['EventName']) + if len(self.env['input']['currInput']) > 1: + self.env['input']['currInput'] = sorted(self.env['input']['currInput']) + if len(self.lastDeepestInput) < len(self.env['input']['currInput']): + self.setLastDeepestInput( self.env['input']['currInput'].copy()) + elif self.lastDeepestInput == self.env['input']['currInput']: + if time.time() - self.lastInputTime <= self.env['runtime']['settingsManager'].getSettingAsFloat('keyboard','doubleTapTimeout'): + self.env['input']['shortcutRepeat'] += 1 + else: + self.env['input']['shortcutRepeat'] = 1 + self.setLedState = self.handleLedStates(eventData) + self.lastInputTime = time.time() + elif eventData['EventState'] == 2: + self.lastInputTime = time.time() + else: + pass + self.env['input']['oldNumLock'] = self.env['input']['newNumLock'] + self.env['input']['newNumLock'] = self.env['runtime']['inputDriver'].getLedState() + self.env['input']['oldCapsLock'] = self.env['input']['newCapsLock'] + self.env['input']['newCapsLock'] = self.env['runtime']['inputDriver'].getLedState(1) + self.env['input']['oldScrollLock'] = self.env['input']['newScrollLock'] + self.env['input']['newScrollLock'] = self.env['runtime']['inputDriver'].getLedState(2) + self.env['runtime']['debug'].writeDebugOut("currInput " + str(self.env['input']['currInput'] ) ,debug.debugLevel.INFO) + if self.noKeyPressed(): + self.env['input']['prevInput'] = [] + self.setLedState = True def handleLedStates(self, mEvent): if not self.setLedState: diff --git a/src/fenrir/inputDriver/dummyDriver.py b/src/fenrir/inputDriver/dummyDriver.py index 55db7f83..917ee1f7 100644 --- a/src/fenrir/inputDriver/dummyDriver.py +++ b/src/fenrir/inputDriver/dummyDriver.py @@ -11,8 +11,3 @@ from core.inputDriver import inputDriver class driver(inputDriver): def __init__(self): inputDriver.__init__(self) - - def getInputEvent(self): - time.sleep(0.1) - if not self._initialized: - return None diff --git a/src/fenrir/inputDriver/evdevDriver.py b/src/fenrir/inputDriver/evdevDriver.py index 9241384b..e32c7d14 100644 --- a/src/fenrir/inputDriver/evdevDriver.py +++ b/src/fenrir/inputDriver/evdevDriver.py @@ -12,6 +12,7 @@ try: import evdev from evdev import InputDevice, UInput _evdevAvailable = True + except Exception as e: _evdevAvailableError = str(e) @@ -54,7 +55,7 @@ class driver(inputDriver): self.env['runtime']['processManager'].addCustomEventThread(self.plugInputDeviceWatchdogUdev) else: self.env['runtime']['processManager'].addSimpleEventThread(fenrirEventType.PlugInputDevice, self.plugInputDeviceWatchdogTimer) - self.env['runtime']['processManager'].addSimpleEventThread(fenrirEventType.KeyboardInput, self.inputWatchdog, {'dev':self.iDevicesFD}) + self.env['runtime']['processManager'].addCustomEventThread(self.inputWatchdog) def plugInputDeviceWatchdogUdev(self,active , eventQueue): context = pyudev.Context() monitor = pyudev.Monitor.from_netlink(context) @@ -65,66 +66,31 @@ class driver(inputDriver): if devices: while monitor.poll(0.5): time.sleep(0.08) - eventQueue.put({"Type":fenrirEventType.PlugInputDevice,"Data":None}) + #eventQueue.put({"Type":fenrirEventType.PlugInputDevice,"Data":None}) return time.time() def plugInputDeviceWatchdogTimer(self, active): time.sleep(2.5) return time.time() - def inputWatchdog(self,active , params): - try: - while self.watchDog.value == 0: - if active.value == 0: - return - time.sleep(0.01) - r = [] - while r == []: - if active.value == 0: - return - r, w, x = select(list(params['dev']), [], [], 0.5) - self.watchDog.value = 0 - except: - pass - def getInputEvent(self): - if not self.hasIDevices(): - self.watchDog.value = 1 - return None - event = None - r, w, x = select(self.iDevices, [], [], 0.00001) - if r != []: + def inputWatchdog(self,active , eventQueue): + while active.value: + r, w, x = select(self.iDevices, [], [], 0.5) for fd in r: + event = None try: event = self.iDevices[fd].read_one() + currMapEvent = self.mapEvent(event) + self.env['input']['eventBuffer'].append( [self.iDevices[fd], self.uDevices[fd], event]) + eventQueue.put({"Type":fenrirEventType.KeyboardInput,"Data":currMapEvent}) except: self.removeDevice(fd) - self.watchDog.value = 1 - return None - foreward = False - while(event): - self.env['input']['eventBuffer'].append( [self.iDevices[fd], self.uDevices[fd], event]) - if event.type == evdev.events.EV_KEY: - if event.code != 0: - currMapEvent = self.mapEvent(event) - if not currMapEvent: - foreward = True - event = self.iDevices[fd].read_one() - continue - if not isinstance(currMapEvent['EventName'], str): - foreward = True - event = self.iDevices[fd].read_one() - continue - if not foreward: - if currMapEvent['EventState'] in [0,1,2]: - self.watchDog.value = 1 - return currMapEvent - else: - if not event.type in [0,1,4]: - foreward = True - event = self.iDevices[fd].read_one() + + def handleInputEvent(self, event): + if not event: + print('skip') + return if foreward: self.writeEventBuffer() self.clearEventBuffer() - self.watchDog.value = 1 - return None def writeEventBuffer(self): if not self._initialized: @@ -224,6 +190,7 @@ class driver(inputDriver): mEvent['EventSec'] = event.sec mEvent['EventUsec'] = event.usec mEvent['EventState'] = event.value + mEvent['EventType'] = event.type return mEvent except Exception as e: return None