fenrir/src/fenrir-package/screen/linux.py

65 lines
3.7 KiB
Python
Raw Normal View History

#!/bin/python
2016-07-08 19:01:00 -04:00
# -*- coding: utf-8 -*-
import difflib
import textwrap
import time
2016-07-08 19:01:00 -04:00
import re
#import fenrir.utils.debug
class screenManager():
def __init__(self, device='/dev/vcsa'):
2016-07-08 05:34:56 -04:00
self.vcsaDevicePath = device
2016-07-08 05:34:56 -04:00
def analyzeScreen(self, environment):
# read screen
currTTY = open('/sys/devices/virtual/tty/tty0/active','r')
2016-07-08 08:29:35 -04:00
environment['screenData']['newTTY'] = currTTY.read()[3:-1]
currTTY.close()
try:
2016-07-08 05:34:56 -04:00
vcsa = open(self.vcsaDevicePath + environment['screenData']['newTTY'] ,'rb',0)
environment['screenData']['newContentBytes'] = vcsa.read()
vcsa.close()
2016-07-10 12:34:44 -04:00
if len(environment['screenData']['newContentBytes']) < 5:
return environment
except:
2016-07-08 12:33:32 -04:00
return environment
# get metadata like cursor or screensize
2016-07-08 05:34:56 -04:00
environment['screenData']['lines'] = int( environment['screenData']['newContentBytes'][0])
environment['screenData']['columns'] = int( environment['screenData']['newContentBytes'][1])
environment['screenData']['newCursor']['x'] = int( environment['screenData']['newContentBytes'][2])
environment['screenData']['newCursor']['y'] = int( environment['screenData']['newContentBytes'][3])
# analyze content
2016-07-08 19:01:00 -04:00
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:]
2016-07-08 05:34:56 -04:00
environment['screenData']['newContentAttrib'] = environment['screenData']['newContentBytes'][5:][::2]
2016-07-08 19:01:00 -04:00
#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)
2016-07-08 05:34:56 -04:00
if environment['screenData']['newTTY'] != environment['screenData']['oldTTY']:
environment['screenData']['oldContentBytes'] = b''
environment['screenData']['oldContentAttrib'] = b''
environment['screenData']['oldContentText'] = ''
environment['screenData']['oldCursor']['x'] = 0
environment['screenData']['oldCursor']['y'] = 0
2016-07-10 12:34:44 -04:00
environment['runtime']['speechDriver'].cancel()
# changes on the screen
2016-07-10 12:34:44 -04:00
if (environment['screenData']['oldContentText'] != environment['screenData']['newContentText']) and \
(len(environment['screenData']['newContentText']) > 0):
2016-07-08 08:29:35 -04:00
diff = difflib.ndiff(environment['screenData']['oldContentText'], environment['screenData']['newContentText'])
2016-07-08 05:34:56 -04:00
environment['screenData']['delta'] = ''.join(x[2:] for x in diff if x.startswith('+ '))
2016-07-10 09:43:15 -04:00
if ((len(environment['screenData']['delta']) < 3)):
environment['runtime']['speechDriver'].cancel()
2016-07-08 05:34:56 -04:00
environment['runtime']['speechDriver'].speak(environment['screenData']['delta'])
# set new "old" values
2016-07-08 05:34:56 -04:00
environment['screenData']['oldContentBytes'] = environment['screenData']['newContentBytes']
environment['screenData']['oldContentText'] = environment['screenData']['newContentText']
environment['screenData']['oldContentTextAttrib'] = environment['screenData']['newContentAttrib']
environment['screenData']['oldCursor']['x'] = environment['screenData']['newCursor']['x']
environment['screenData']['oldCursor']['y'] = environment['screenData']['newCursor']['y']
environment['screenData']['oldTTY'] = environment['screenData']['newTTY']
return environment