From ca5ce66f31cb97f9096281018a62ec1a68f049f9 Mon Sep 17 00:00:00 2001 From: Chrys Date: Fri, 4 Oct 2019 17:08:27 +0200 Subject: [PATCH] retry grabbing --- src/fenrirscreenreader/core/inputDriver.py | 22 +++-- src/fenrirscreenreader/core/inputManager.py | 18 ++-- src/fenrirscreenreader/core/screenManager.py | 92 +++++++++---------- .../inputDriver/evdevDriver.py | 29 +++--- 4 files changed, 85 insertions(+), 76 deletions(-) diff --git a/src/fenrirscreenreader/core/inputDriver.py b/src/fenrirscreenreader/core/inputDriver.py index 5cacc925..5f2f65f5 100644 --- a/src/fenrirscreenreader/core/inputDriver.py +++ b/src/fenrirscreenreader/core/inputDriver.py @@ -11,48 +11,50 @@ class inputDriver(): self._initialized = False def initialize(self, environment): self.env = environment - self.env['runtime']['inputManager'].setShortcutType('KEY') + self.env['runtime']['inputManager'].setShortcutType('KEY') self._isInitialized = True def shutdown(self): if self._initialized: - self.removeAllDevices() + self.removeAllDevices() self._isInitialized = False def getInputEvent(self): time.sleep(0.1) return None def clearEventBuffer(self): if not self._initialized: - return + return del self.env['input']['eventBuffer'][:] def updateInputDevices(self, newDevices = None, init = False): if not self._initialized: - return + return def getLedState(self, led = 0): if not self._initialized: - return False + return False return False def toggleLedState(self, led = 0): if not self._initialized: return def grabAllDevices(self): if not self._initialized: - return + return True + return True def ungrabAllDevices(self): if not self._initialized: - return + return True + return True def hasIDevices(self): if not self._initialized: return False return True def removeAllDevices(self): if not self._initialized: - return + return def sendKey(self): if not self._initialized: - return + return def __del__(self): if not self._initialized: - return + return try: self.removeAllDevices() except: diff --git a/src/fenrirscreenreader/core/inputManager.py b/src/fenrirscreenreader/core/inputManager.py index 7758b766..0cd654d9 100644 --- a/src/fenrirscreenreader/core/inputManager.py +++ b/src/fenrirscreenreader/core/inputManager.py @@ -57,12 +57,14 @@ class inputManager(): if not self.noKeyPressed(): return if not self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'): + self.executeDeviceGrab = False return if self.env['runtime']['screenManager'].getCurrScreenIgnored(): - self.ungrabAllDevices() + if self.ungrabAllDevices(): + self.executeDeviceGrab = False else: - self.grabAllDevices() - self.executeDeviceGrab = False + if self.grabAllDevices(): + self.executeDeviceGrab = False def sendKeys(self, keyMacro): for e in keyMacro: key = '' @@ -143,15 +145,17 @@ class inputManager(): def grabAllDevices(self): if self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'): try: - self.env['runtime']['inputDriver'].grabAllDevices() + return self.env['runtime']['inputDriver'].grabAllDevices() except Exception as e: - pass + return False + return True def ungrabAllDevices(self): if self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'): try: - self.env['runtime']['inputDriver'].ungrabAllDevices() + return self.env['runtime']['inputDriver'].ungrabAllDevices() except Exception as e: - pass + return False + return True def handlePlugInputDevice(self, eventData): for deviceEntry in eventData: self.env['runtime']['inputManager'].updateInputDevices(deviceEntry['device']) diff --git a/src/fenrirscreenreader/core/screenManager.py b/src/fenrirscreenreader/core/screenManager.py index e3bca0de..7ea496d6 100644 --- a/src/fenrirscreenreader/core/screenManager.py +++ b/src/fenrirscreenreader/core/screenManager.py @@ -24,15 +24,15 @@ class screenManager(): def initialize(self, environment): self.env = environment self.env['runtime']['settingsManager'].loadDriver(\ - self.env['runtime']['settingsManager'].getSetting('screen', 'driver'), 'screenDriver') - self.getCurrScreen() + self.env['runtime']['settingsManager'].getSetting('screen', 'driver'), 'screenDriver') self.getCurrScreen() - self.getSessionInformation() + self.getCurrScreen() + self.getSessionInformation() + self.updateScreenIgnored() self.updateScreenIgnored() - self.updateScreenIgnored() def resetScreenText(self, screenText): self.prevScreenText = '' - self.currScreenText = screenText + self.currScreenText = screenText def setScreenText(self, screenText): self.prevScreenText = self.currScreenText self.currScreenText = screenText @@ -44,58 +44,54 @@ class screenManager(): except: pass def getSessionInformation(self): - try: + try: self.env['runtime']['screenDriver'].getSessionInformation() except: - pass - + pass def shutdown(self): self.env['runtime']['settingsManager'].shutdownDriver('screenDriver') def isCurrScreenIgnoredChanged(self): return self.getCurrScreenIgnored() != self.getPrevScreenIgnored() def hanldeScreenChange(self, eventData): self.getCurrScreen() - self.getSessionInformation() - self.updateScreenIgnored() + self.getSessionInformation() + self.updateScreenIgnored() if self.isCurrScreenIgnoredChanged(): self.env['runtime']['inputManager'].setExecuteDeviceGrab() - self.env['runtime']['inputManager'].handleDeviceGrab() - - if self.isScreenChange(): + self.env['runtime']['inputManager'].handleDeviceGrab() + if self.isScreenChange(): self.changeBrailleScreen() - - if not self.isSuspendingScreen(self.env['screen']['newTTY']): + if not self.isSuspendingScreen(self.env['screen']['newTTY']): self.update(eventData, 'onScreenChange') self.env['screen']['lastScreenUpdate'] = time.time() else: - self.env['runtime']['outputManager'].interruptOutput() - + self.env['runtime']['outputManager'].interruptOutput() def handleScreenUpdate(self, eventData): self.env['screen']['oldApplication'] = self.env['screen']['newApplication'] self.updateScreenIgnored() if self.isCurrScreenIgnoredChanged(): self.env['runtime']['inputManager'].setExecuteDeviceGrab() - self.env['runtime']['inputManager'].handleDeviceGrab() - if not self.getCurrScreenIgnored(): + self.env['runtime']['inputManager'].handleDeviceGrab() + if not self.getCurrScreenIgnored(): self.update(eventData, 'onScreenUpdate') #if trigger == 'onUpdate' or self.isScreenChange() \ # or len(self.env['screen']['newDelta']) > 6: - # self.env['runtime']['screenDriver'].getCurrApplication() + # self.env['runtime']['screenDriver'].getCurrApplication() self.env['screen']['lastScreenUpdate'] = time.time() - elif self.isCurrScreenIgnoredChanged(): - self.env['runtime']['outputManager'].interruptOutput() + elif self.isCurrScreenIgnoredChanged(): + self.env['runtime']['outputManager'].interruptOutput() def getCurrScreenIgnored(self): return self.currScreenIgnored def getPrevScreenIgnored(self): - return self.prevScreenIgnored + return self.prevScreenIgnored def updateScreenIgnored(self): - self.prevScreenIgnored = self.currScreenIgnored - self.currScreenIgnored = self.isSuspendingScreen(self.env['screen']['newTTY']) + self.prevScreenIgnored = self.currScreenIgnored + self.currScreenIgnored = self.isSuspendingScreen(self.env['screen']['newTTY']) def update(self, eventData, trigger='onUpdate'): # set new "old" values self.env['screen']['oldContentBytes'] = self.env['screen']['newContentBytes'] self.env['screen']['oldContentText'] = self.env['screen']['newContentText'] - self.env['screen']['oldCursor'] = self.env['screen']['newCursor'].copy() + self.env['screen']['oldCursor'] = self.env['screen']['newCursor'].copy() self.env['screen']['oldDelta'] = self.env['screen']['newDelta'] self.env['screen']['oldNegativeDelta'] = self.env['screen']['newNegativeDelta'] self.env['screen']['newContentBytes'] = eventData['bytes'] @@ -104,7 +100,7 @@ class screenManager(): self.env['screen']['lines'] = int( eventData['lines']) self.env['screen']['columns'] = int( eventData['columns']) self.colums = int( eventData['columns']) - self.rows = int( eventData['lines']) + self.rows = int( eventData['lines']) self.env['screen']['newCursor']['x'] = int( eventData['textCursor']['x']) self.env['screen']['newCursor']['y'] = int( eventData['textCursor']['y']) self.env['screen']['newTTY'] = eventData['screen'] @@ -113,16 +109,16 @@ class screenManager(): # screen change if self.isScreenChange(): self.env['screen']['oldContentBytes'] = b'' - self.resetScreenText(eventData['text']) + self.resetScreenText(eventData['text']) self.env['runtime']['attributeManager'].resetAttributes(eventData['attributes']) - self.env['runtime']['attributeManager'].resetAttributeCursor() + self.env['runtime']['attributeManager'].resetAttributeCursor() self.env['screen']['oldContentText'] = '' self.env['screen']['oldCursor']['x'] = 0 self.env['screen']['oldCursor']['y'] = 0 - self.env['screen']['oldDelta'] = '' - self.env['screen']['oldNegativeDelta'] = '' + self.env['screen']['oldDelta'] = '' + self.env['screen']['oldNegativeDelta'] = '' else: - self.setScreenText(eventData['text']) + self.setScreenText(eventData['text']) self.env['runtime']['attributeManager'].setAttributes(eventData['attributes']) # initialize current deltas self.env['screen']['newNegativeDelta'] = '' @@ -131,10 +127,10 @@ class screenManager(): # changes on the screen oldScreenText = re.sub(' +',' ',self.env['runtime']['screenManager'].getWindowAreaInText(self.env['screen']['oldContentText'])) - newScreenText = re.sub(' +',' ',self.env['runtime']['screenManager'].getWindowAreaInText(self.env['screen']['newContentText'])) + newScreenText = re.sub(' +',' ',self.env['runtime']['screenManager'].getWindowAreaInText(self.env['screen']['newContentText'])) typing = False - diffList = [] - + diffList = [] + if (self.env['screen']['oldContentText'] != self.env['screen']['newContentText']): if self.env['screen']['newContentText'] != '' and self.env['screen']['oldContentText'] == '': if oldScreenText == '' and\ @@ -142,7 +138,7 @@ class screenManager(): self.env['screen']['newDelta'] = newScreenText else: cursorLineStart = self.env['screen']['newCursor']['y'] * self.env['screen']['columns'] + self.env['screen']['newCursor']['y'] - cursorLineEnd = cursorLineStart + self.env['screen']['columns'] + cursorLineEnd = cursorLineStart + self.env['screen']['columns'] if abs(self.env['screen']['oldCursor']['x'] - self.env['screen']['newCursor']['x']) >= 1 and \ self.env['screen']['oldCursor']['y'] == self.env['screen']['newCursor']['y'] and \ self.env['screen']['newContentText'][:cursorLineStart] == self.env['screen']['oldContentText'][:cursorLineStart] and \ @@ -152,14 +148,14 @@ class screenManager(): #if cursorLineStart < cursorLineStart + self.env['screen']['newCursor']['x'] - 4: # cursorLineStartOffset = cursorLineStart + self.env['screen']['newCursor']['x'] - 4 if cursorLineEnd > cursorLineStart + self.env['screen']['newCursor']['x'] + 3: - cursorLineEndOffset = cursorLineStart + self.env['screen']['newCursor']['x'] + 3 + cursorLineEndOffset = cursorLineStart + self.env['screen']['newCursor']['x'] + 3 oldScreenText = self.env['screen']['oldContentText'][cursorLineStartOffset:cursorLineEndOffset] # oldScreenText = re.sub(' +',' ',oldScreenText) newScreenText = self.env['screen']['newContentText'][cursorLineStartOffset:cursorLineEndOffset] #newScreenText = re.sub(' +',' ',newScreenText) diff = self.differ.compare(oldScreenText, newScreenText) diffList = list(diff) - typing = True + typing = True tempNewDelta = ''.join(x[2:] for x in diffList if x[0] == '+') if tempNewDelta.strip() != '': if tempNewDelta != ''.join(newScreenText[self.env['screen']['oldCursor']['x']:self.env['screen']['newCursor']['x']].rstrip()): @@ -167,13 +163,13 @@ class screenManager(): typing = False else: diff = self.differ.compare(oldScreenText.split('\n'),\ - newScreenText.split('\n')) + newScreenText.split('\n')) diffList = list(diff) if not typing: self.env['screen']['newDelta'] = '\n'.join(x[2:] for x in diffList if x[0] == '+') else: - self.env['screen']['newDelta'] = ''.join(x[2:] for x in diffList if x[0] == '+') + self.env['screen']['newDelta'] = ''.join(x[2:] for x in diffList if x[0] == '+') self.env['screen']['newNegativeDelta'] = ''.join(x[2:] for x in diffList if x[0] == '-') # track highlighted @@ -181,9 +177,9 @@ class screenManager(): if self.env['runtime']['attributeManager'].isAttributeChange(): if self.env['runtime']['settingsManager'].getSettingAsBool('focus', 'highlight'): attributeDelta, attributeCursor = self.env['runtime']['attributeManager'].trackHighlights() - if attributeCursor: + if attributeCursor: self.env['runtime']['attributeManager'].setAttributeCursor(attributeCursor) - self.env['runtime']['attributeManager'].setAttributeDelta(attributeDelta) + self.env['runtime']['attributeManager'].setAttributeDelta(attributeDelta) except Exception as e: self.env['runtime']['debug'].writeDebugOut('screenManager:update:highlight: ' + str(e),debug.debugLevel.ERROR) @@ -193,10 +189,10 @@ class screenManager(): ignoreScreens = [] fixIgnoreScreens = self.env['runtime']['settingsManager'].getSetting('screen', 'suspendingScreen') if fixIgnoreScreens != '': - ignoreScreens.extend(fixIgnoreScreens.split(',')) + ignoreScreens.extend(fixIgnoreScreens.split(',')) if self.env['runtime']['settingsManager'].getSettingAsBool('screen', 'autodetectSuspendingScreen'): ignoreScreens.extend(self.env['screen']['autoIgnoreScreens']) - self.env['runtime']['debug'].writeDebugOut('screenManager:isSuspendingScreen ignore:' + str(ignoreScreens) + ' current:'+ str(screen ), debug.debugLevel.INFO) + self.env['runtime']['debug'].writeDebugOut('screenManager:isSuspendingScreen ignore:' + str(ignoreScreens) + ' current:'+ str(screen ), debug.debugLevel.INFO) return (screen in ignoreScreens) def isScreenChange(self): @@ -208,7 +204,7 @@ class screenManager(): if ignoreSpace: newDelta = newDelta.strip() return newDelta != '' - def isNegativeDelta(self): + def isNegativeDelta(self): return self.env['screen']['newNegativeDelta'] != '' def getWindowAreaInText(self, text): if not self.env['runtime']['cursorManager'].isApplicationWindowSet(): @@ -220,7 +216,7 @@ class screenManager(): for line in windowList: windowText += line[self.env['commandBuffer']['windowArea'][currApp]['1']['x']:self.env['commandBuffer']['windowArea'][currApp]['2']['x'] + 1] + '\n' return windowText - + def injectTextToScreen(self, text, screen = None): try: self.env['runtime']['screenDriver'].injectTextToScreen(text, screen) @@ -240,6 +236,6 @@ class screenManager(): self.env['runtime']['debug'].writeDebugOut('screenManager:changeBrailleScreen:leveScreen ' + str(e),debug.debugLevel.ERROR) if not self.isSuspendingScreen(): try: - self.env['runtime']['brailleDriver'].enterScreen(self.env['screen']['newTTY']) - except Exception as e: + self.env['runtime']['brailleDriver'].enterScreen(self.env['screen']['newTTY']) + except Exception as e: self.env['runtime']['debug'].writeDebugOut('screenManager:changeBrailleScreen:enterScreen ' + str(e),debug.debugLevel.ERROR) diff --git a/src/fenrirscreenreader/inputDriver/evdevDriver.py b/src/fenrirscreenreader/inputDriver/evdevDriver.py index e4a66695..371c7036 100644 --- a/src/fenrirscreenreader/inputDriver/evdevDriver.py +++ b/src/fenrirscreenreader/inputDriver/evdevDriver.py @@ -281,16 +281,18 @@ class driver(inputDriver): self.iDevices[i].set_led(led , 1) def grabAllDevices(self): if not self._initialized: - return + return True + ok = True for fd in self.iDevices: - self.grabDevice(fd) - + ok = ok and self.grabDevice(fd) + return ok def ungrabAllDevices(self): if not self._initialized: - return + return True + ok = True for fd in self.iDevices: - self.ungrabDevice(fd) - + ok = ok and self.ungrabDevice(fd) + return ok def createUInputDev(self, fd): if not self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'): self.uDevices[fd] = None @@ -324,7 +326,7 @@ class driver(inputDriver): self.createUInputDev(newDevice.fd) def grabDevice(self, fd): if not self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'): - return + return True try: self.iDevices[fd].grab() self.gDevices[fd] = True @@ -333,15 +335,20 @@ class driver(inputDriver): self.gDevices[fd] = True except Exception as e: self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: grabing not possible: ' + str(e),debug.debugLevel.ERROR) + return False + return True def ungrabDevice(self,fd): if not self.env['runtime']['settingsManager'].getSettingAsBool('keyboard', 'grabDevices'): - return + return True try: - self.gDevices[fd] = False self.iDevices[fd].ungrab() + self.gDevices[fd] = False self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: ungrab device ('+ str(self.iDevices[fd].name) + ')',debug.debugLevel.INFO) - except: - pass + except IOError: + self.gDevices[fd] = False + except Exception as e: + return False + return True def removeDevice(self,fd): self.env['runtime']['debug'].writeDebugOut('InputDriver evdev: device removed: ' + str(fd) + ' ' +str(self.iDevices[fd]),debug.debugLevel.INFO) self.clearEventBuffer()