fenrir/src/fenrir.py

64 lines
1.9 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-05 15:34:50 -04:00
import speech.es as es
runtime = {
2016-07-05 17:54:11 -04:00
'running':True,
2016-07-05 15:34:50 -04:00
'columns': 0,
2016-07-05 17:18:03 -04:00
'lines': 0,
2016-07-05 15:34:50 -04:00
'screenDriver': '/dev/vcsa3',
'delta': '',
2016-07-05 17:18:03 -04:00
'oldCursor':{'x':0,'y':0},
2016-07-05 15:34:50 -04:00
'oldContentBytes': b'',
'oldContentText': '',
'oldContentAttrib': b'',
2016-07-05 17:18:03 -04:00
'newCursor':{'x':0,'y':0},
2016-07-05 15:34:50 -04:00
'newContentBytes': b'',
'newContentText': '',
'newContentAttrib': b'',
'speechDriverString':'es',
'speechDriver': es.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 15:34:50 -04:00
vcsa = open(runtime['screenDriver'],'rb')
runtime['newContentBytes'] = vcsa.read()
vcsa.close()
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
runtime['newContentText'] = str(runtime['newContentBytes'][4:][::2].decode('cp1252').encode('utf-8'))
runtime['newContentAttrib'] = runtime['newContentBytes'][5:][::2]
2016-07-05 15:34:50 -04:00
runtime['newContentText'] = '\n'.join(textwrap.wrap(runtime['newContentText'], runtime['columns']))
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-05 18:03:26 -04:00
if len(runtime['delta']) < 3:
2016-07-05 18:01:59 -04:00
runtime['speechDriver'].stop()
2016-07-04 18:16:22 -04:00
print("tty3 changed")
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-04 18:16:22 -04:00