fenrir/src/fenrir.py

70 lines
2.4 KiB
Python
Raw Normal View History

2016-07-04 18:16:22 -04:00
#!/bin/python
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
2016-07-04 18:16:22 -04:00
import hashlib
import difflib
import textwrap
2016-07-07 11:22:30 -04:00
import time
2016-07-06 19:40:07 -04:00
from subprocess import Popen, PIPE
import utils.debug
2016-07-07 11:22:30 -04:00
import core.environment as environment
2016-07-07 08:23:43 -04:00
import speech.espeak as es
import speech.speechd as sd
2016-07-05 15:34:50 -04:00
2016-07-07 11:22:30 -04:00
runtime = environment.runtime
runtime['screenDriver'] = '/dev/vcsa'
runtime['speechDriverString'] = 'speechd'
runtime['speechDriver'] = sd.speech()
2016-07-04 18:16:22 -04:00
2016-07-05 17:54:11 -04:00
while(runtime['running']):
2016-07-05 17:18:03 -04:00
# read screen
2016-07-05 19:13:28 -04:00
currTTY = open('/sys/devices/virtual/tty/tty0/active','r')
runtime['newTTY'] = currTTY.read()[3:-1]
2016-07-07 11:22:30 -04:00
currTTY.close()
2016-07-05 19:13:28 -04:00
try:
2016-07-07 11:22:30 -04:00
vcsa = open(runtime['screenDriver'] + runtime['newTTY'] ,'rb',0)
2016-07-05 19:13:28 -04:00
runtime['newContentBytes'] = vcsa.read()
vcsa.close()
except:
continue
2016-07-05 17:18:03 -04:00
# get metadata like cursor or screensize
runtime['lines'] = int( runtime['newContentBytes'][0])
2016-07-05 15:34:50 -04:00
runtime['columns'] = int( runtime['newContentBytes'][1])
2016-07-05 17:18:03 -04:00
runtime['newCursor']['x'] = int( runtime['newContentBytes'][2])
runtime['newCursor']['y'] = int( runtime['newContentBytes'][3])
# analyze content
2016-07-07 11:22:30 -04:00
runtime['newContentText'] = str(runtime['newContentBytes'][4:][::2].decode('cp1252').encode('utf-8'))[2:-1]
2016-07-05 17:18:03 -04:00
runtime['newContentAttrib'] = runtime['newContentBytes'][5:][::2]
2016-07-07 11:22:30 -04:00
runtime['newContentText'] = '\n'.join(textwrap.wrap(runtime['newContentText'], runtime['columns']))[:-1]
print("|"+runtime['newContentText'] +"|")
print(runtime['newTTY'])
2016-07-05 19:13:28 -04:00
if runtime['newTTY'] != runtime['oldTTY']:
runtime['oldContentBytes'] = b''
runtime['oldContentAttrib'] = b''
runtime['oldContentText'] = ''
runtime['oldCursor']['x'] = 0
runtime['oldCursor']['y'] = 0
2016-07-05 17:18:03 -04:00
# changes on the screen
2016-07-05 15:34:50 -04:00
if runtime['oldContentBytes'] != runtime['newContentBytes']:
2016-07-06 20:16:53 -04:00
if ((len(runtime['delta']) < 3) or runtime['oldTTY'] != runtime['newTTY']):
2016-07-05 18:32:06 -04:00
runtime['speechDriver'].cancel()
2016-07-05 15:34:50 -04:00
diff = difflib.ndiff(runtime['oldContentText'], runtime['newContentText'])
runtime['delta'] = ''.join(x[2:] for x in diff if x.startswith('+ '))
2016-07-05 17:18:03 -04:00
runtime['speechDriver'].speak(runtime['delta'])
2016-07-04 18:16:22 -04:00
2016-07-05 17:18:03 -04:00
# set new "old" values
2016-07-05 15:34:50 -04:00
runtime['oldContentBytes'] = runtime['newContentBytes']
runtime['oldContentText'] = runtime['newContentText']
runtime['oldContentTextAttrib'] = runtime['newContentAttrib']
2016-07-05 17:18:03 -04:00
runtime['oldCursor']['x'] = runtime['newCursor']['x']
runtime['oldCursor']['y'] = runtime['newCursor']['y']
2016-07-05 19:13:28 -04:00
runtime['oldTTY'] = runtime['newTTY']