fenrir/src/fenrirscreenreader/core/screenManager.py

256 lines
14 KiB
Python
Raw Normal View History

#!/bin/python
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
from fenrirscreenreader.core import debug
2018-03-23 16:57:39 -04:00
from fenrirscreenreader.utils import screen_utils
2018-03-23 15:44:31 -04:00
import time, os, re, difflib
class screenManager():
def __init__(self):
self.differ = difflib.Differ()
self.currScreenIgnored = False
self.prevScreenIgnored = False
2018-05-28 16:18:51 -04:00
self.prevScreenText = ''
self.currScreenText = ''
2018-06-08 14:43:25 -04:00
self.colums = None
self.rows = None
def getRows(self):
return self.rows
def getColumns(self):
return self.colums
2016-09-14 17:27:19 -04:00
def initialize(self, environment):
self.env = environment
self.env['runtime']['settingsManager'].loadDriver(\
self.env['runtime']['settingsManager'].getSetting('screen', 'driver'), 'screenDriver')
2018-05-11 16:25:57 -04:00
self.getCurrScreen()
self.getCurrScreen()
2018-05-22 19:12:31 -04:00
self.getSessionInformation()
self.updateScreenIgnored()
2018-05-28 16:18:51 -04:00
self.updateScreenIgnored()
def resetScreenText(self, screenText):
self.prevScreenText = ''
self.currScreenText = screenText
def setScreenText(self, screenText):
self.prevScreenText = self.currScreenText
self.currScreenText = screenText
def getScreenText(self):
return self.currScreenText
2018-05-11 16:25:57 -04:00
def getCurrScreen(self):
try:
self.env['runtime']['screenDriver'].getCurrScreen()
except:
pass
def getSessionInformation(self):
try:
self.env['runtime']['screenDriver'].getSessionInformation()
except:
pass
def shutdown(self):
self.env['runtime']['settingsManager'].shutdownDriver('screenDriver')
2018-05-22 19:12:31 -04:00
def isCurrScreenIgnoredChanged(self):
return self.getCurrScreenIgnored() != self.getPrevScreenIgnored()
def hanldeScreenChange(self, eventData):
2018-05-11 16:25:57 -04:00
self.getCurrScreen()
self.getSessionInformation()
2018-05-22 19:12:31 -04:00
self.updateScreenIgnored()
if self.isCurrScreenIgnoredChanged():
self.env['runtime']['inputManager'].setExecuteDeviceGrab()
self.env['runtime']['inputManager'].handleDeviceGrab()
if self.isScreenChange():
2018-05-24 05:38:05 -04:00
self.changeBrailleScreen()
if not self.isSuspendingScreen(self.env['screen']['newTTY']):
2018-03-23 15:44:31 -04:00
self.update(eventData, 'onScreenChange')
2018-05-22 19:12:31 -04:00
self.env['screen']['lastScreenUpdate'] = time.time()
2018-06-04 02:06:39 -04:00
else:
self.env['runtime']['outputManager'].interruptOutput()
2018-05-22 19:12:31 -04:00
def handleScreenUpdate(self, eventData):
self.env['screen']['oldApplication'] = self.env['screen']['newApplication']
self.updateScreenIgnored()
2018-05-22 19:12:31 -04:00
if self.isCurrScreenIgnoredChanged():
self.env['runtime']['inputManager'].setExecuteDeviceGrab()
2018-05-17 15:57:54 -04:00
self.env['runtime']['inputManager'].handleDeviceGrab()
if not self.getCurrScreenIgnored():
2018-03-23 15:44:31 -04:00
self.update(eventData, 'onScreenUpdate')
2017-06-26 19:35:12 -04:00
#if trigger == 'onUpdate' or self.isScreenChange() \
# or len(self.env['screen']['newDelta']) > 6:
# self.env['runtime']['screenDriver'].getCurrApplication()
2017-04-24 16:40:18 -04:00
self.env['screen']['lastScreenUpdate'] = time.time()
2018-06-04 02:06:39 -04:00
elif self.isCurrScreenIgnoredChanged():
self.env['runtime']['outputManager'].interruptOutput()
def getCurrScreenIgnored(self):
return self.currScreenIgnored
def getPrevScreenIgnored(self):
return self.prevScreenIgnored
def updateScreenIgnored(self):
self.prevScreenIgnored = self.currScreenIgnored
self.currScreenIgnored = self.isSuspendingScreen(self.env['screen']['newTTY'])
2018-03-23 15:44:31 -04:00
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']
2018-05-28 16:18:51 -04:00
self.env['screen']['oldCursor'] = self.env['screen']['newCursor'].copy()
2018-03-23 15:44:31 -04:00
self.env['screen']['oldDelta'] = self.env['screen']['newDelta']
self.env['screen']['oldNegativeDelta'] = self.env['screen']['newNegativeDelta']
self.env['screen']['newContentBytes'] = eventData['bytes']
2018-03-23 15:44:31 -04:00
# get metadata like cursor or screensize
self.env['screen']['lines'] = int( eventData['lines'])
self.env['screen']['columns'] = int( eventData['columns'])
2018-06-08 14:43:25 -04:00
self.colums = int( eventData['columns'])
self.rows = int( eventData['lines'])
2018-03-23 15:44:31 -04:00
self.env['screen']['newCursor']['x'] = int( eventData['textCursor']['x'])
self.env['screen']['newCursor']['y'] = int( eventData['textCursor']['y'])
self.env['screen']['newTTY'] = eventData['screen']
self.env['screen']['newContentText'] = eventData['text']
2018-05-28 16:18:51 -04:00
2018-03-23 21:11:55 -04:00
# screen change
2018-05-28 16:18:51 -04:00
if self.isScreenChange():
2018-03-23 15:44:31 -04:00
self.env['screen']['oldContentBytes'] = b''
2018-05-28 16:18:51 -04:00
self.resetScreenText(eventData['text'])
self.env['runtime']['attributeManager'].resetAttributes(eventData['attributes'])
self.env['runtime']['attributeManager'].resetAttributeCursor()
2018-03-23 15:44:31 -04:00
self.env['screen']['oldContentText'] = ''
self.env['screen']['oldCursor']['x'] = 0
self.env['screen']['oldCursor']['y'] = 0
2018-05-28 16:18:51 -04:00
self.env['screen']['oldDelta'] = ''
self.env['screen']['oldNegativeDelta'] = ''
else:
self.setScreenText(eventData['text'])
self.env['runtime']['attributeManager'].setAttributes(eventData['attributes'])
2018-03-23 15:44:31 -04:00
# initialize current deltas
self.env['screen']['newNegativeDelta'] = ''
self.env['screen']['newDelta'] = ''
2018-05-28 16:18:51 -04:00
self.env['runtime']['attributeManager'].resetAttributeDelta()
2018-03-23 15:44:31 -04:00
# 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']))
typing = False
diffList = []
2018-03-23 22:01:48 -04:00
2018-03-23 15:44:31 -04:00
if (self.env['screen']['oldContentText'] != self.env['screen']['newContentText']):
if self.env['screen']['newContentText'] != '' and self.env['screen']['oldContentText'] == '':
if oldScreenText == '' and\
newScreenText != '':
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']
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 \
self.env['screen']['newContentText'][cursorLineEnd:] == self.env['screen']['oldContentText'][cursorLineEnd:]:
cursorLineStartOffset = cursorLineStart
cursorLineEndOffset = cursorLineEnd
#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
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)
#diff = difflib.ndiff(oldScreenText, newScreenText)
2018-03-23 15:44:31 -04:00
diffList = list(diff)
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()):
diffList = ['+ ' + self.env['screen']['newContentText'].split('\n')[self.env['screen']['newCursor']['y']]]
typing = True
else:
diff = self.differ.compare(oldScreenText.split('\n'),\
newScreenText.split('\n'))
#diff = difflib.ndiff( oldScreenText.split('\n'),\
# newScreenText.split('\n'))
2018-03-23 15:44:31 -04:00
diffList = list(diff)
2018-05-18 04:05:32 -04:00
if not typing:
2018-03-23 15:44:31 -04:00
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']['newNegativeDelta'] = ''.join(x[2:] for x in diffList if x[0] == '-')
# track highlighted
2018-05-18 04:05:32 -04:00
try:
2018-05-28 16:18:51 -04:00
if self.env['runtime']['attributeManager'].isAttributeChange():
2018-05-18 04:05:32 -04:00
if self.env['runtime']['settingsManager'].getSettingAsBool('focus', 'highlight'):
2018-05-28 16:18:51 -04:00
attributeDelta, attributeCursor = self.env['runtime']['attributeManager'].trackHighlights()
if attributeCursor:
self.env['runtime']['attributeManager'].setAttributeCursor(attributeCursor)
self.env['runtime']['attributeManager'].setAttributeDelta(attributeDelta)
2018-05-18 04:05:32 -04:00
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('screenManager:update:highlight: ' + str(e),debug.debugLevel.ERROR)
2016-10-08 06:15:38 -04:00
def isSuspendingScreen(self, screen = None):
if screen == None:
2017-04-24 16:40:18 -04:00
screen = self.env['screen']['newTTY']
ignoreScreens = []
fixIgnoreScreens = self.env['runtime']['settingsManager'].getSetting('screen', 'suspendingScreen')
if fixIgnoreScreens != '':
2017-02-27 17:30:43 -05:00
ignoreScreens.extend(fixIgnoreScreens.split(','))
2017-01-16 17:12:27 -05:00
if self.env['runtime']['settingsManager'].getSettingAsBool('screen', 'autodetectSuspendingScreen'):
2017-04-24 16:40:18 -04:00
ignoreScreens.extend(self.env['screen']['autoIgnoreScreens'])
2018-03-13 17:45:52 -04:00
try:
ignoreFileName = self.env['runtime']['settingsManager'].getSetting('screen', 'suspendingScreenFile')
if ignoreFileName != '':
if os.access(ignoreFileName, os.R_OK):
with open(ignoreFileName) as fp:
ignoreScreens.extend(fp.read().replace('\n','').split(','))
except:
pass
2018-05-23 07:33:11 -04:00
self.env['runtime']['debug'].writeDebugOut('screenManager:isSuspendingScreen ignore:' + str(ignoreScreens) + ' current:'+ str(screen ), debug.debugLevel.INFO)
return (screen in ignoreScreens)
2016-09-23 03:51:47 -04:00
def isScreenChange(self):
2017-04-24 16:40:18 -04:00
if not self.env['screen']['oldTTY']:
2016-10-08 09:08:53 -04:00
return False
2017-04-24 16:40:18 -04:00
return self.env['screen']['newTTY'] != self.env['screen']['oldTTY']
2017-11-10 14:08:58 -05:00
def isDelta(self, ignoreSpace=False):
newDelta = self.env['screen']['newDelta']
if ignoreSpace:
newDelta = newDelta.strip()
return newDelta != ''
def isNegativeDelta(self):
2017-04-24 16:40:18 -04:00
return self.env['screen']['newNegativeDelta'] != ''
2016-09-24 16:56:18 -04:00
def getWindowAreaInText(self, text):
if not self.env['runtime']['cursorManager'].isApplicationWindowSet():
return text
windowText = ''
windowList = text.split('\n')
2016-10-02 19:34:46 -04:00
currApp = self.env['runtime']['applicationManager'].getCurrentApplication()
windowList = windowList[self.env['commandBuffer']['windowArea'][currApp]['1']['y']:self.env['commandBuffer']['windowArea'][currApp]['2']['y'] + 1]
2016-09-24 16:56:18 -04:00
for line in windowList:
2016-10-02 19:34:46 -04:00
windowText += line[self.env['commandBuffer']['windowArea'][currApp]['1']['x']:self.env['commandBuffer']['windowArea'][currApp]['2']['x'] + 1] + '\n'
2016-09-24 16:56:18 -04:00
return windowText
2016-12-15 04:23:29 -05:00
2016-12-15 04:33:52 -05:00
def injectTextToScreen(self, text, screen = None):
2016-12-15 04:23:29 -05:00
try:
2016-12-15 04:33:52 -05:00
self.env['runtime']['screenDriver'].injectTextToScreen(text, screen)
2016-12-15 04:23:29 -05:00
except Exception as e:
2016-12-15 04:37:31 -05:00
self.env['runtime']['debug'].writeDebugOut('screenManager:injectTextToScreen ' + str(e),debug.debugLevel.ERROR)
2016-12-15 04:23:29 -05:00
2016-10-08 06:15:38 -04:00
def changeBrailleScreen(self):
2017-02-27 16:13:39 -05:00
if not self.env['runtime']['settingsManager'].getSettingAsBool('braille', 'enabled'):
return
2016-10-08 06:15:38 -04:00
if not self.env['runtime']['brailleDriver']:
return
2017-04-24 16:40:18 -04:00
if self.env['screen']['oldTTY']:
if not self.isSuspendingScreen(self.env['screen']['oldTTY']):
2016-10-08 09:08:53 -04:00
try:
self.env['runtime']['brailleDriver'].leveScreen()
except Exception as e:
2016-12-17 06:49:54 -05:00
self.env['runtime']['debug'].writeDebugOut('screenManager:changeBrailleScreen:leveScreen ' + str(e),debug.debugLevel.ERROR)
2016-10-08 06:15:38 -04:00
if not self.isSuspendingScreen():
2016-10-08 08:58:48 -04:00
try:
2017-04-24 16:40:18 -04:00
self.env['runtime']['brailleDriver'].enterScreen(self.env['screen']['newTTY'])
2016-10-08 08:58:48 -04:00
except Exception as e:
2016-12-15 04:39:33 -05:00
self.env['runtime']['debug'].writeDebugOut('screenManager:changeBrailleScreen:enterScreen ' + str(e),debug.debugLevel.ERROR)