2016-09-14 18:04:52 -04:00
|
|
|
#!/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-09-19 16:15:58 -04:00
|
|
|
# Fenrir TTY screen reader
|
|
|
|
# By Chrys, Storm Dragon, and contributers.
|
2017-07-09 17:27:54 -04:00
|
|
|
#attrib:
|
|
|
|
#http://rampex.ihep.su/Linux/linux_howto/html/tutorials/mini/Colour-ls-6.html
|
|
|
|
#0 = black, 1 = blue, 2 = green, 3 = cyan, 4 = red, 5 = purple, 6 = brown/yellow, 7 = white.
|
|
|
|
#https://github.com/jwilk/vcsapeek/blob/master/linuxvt.py
|
|
|
|
#blink = 5 if attr & 1 else 0
|
|
|
|
#bold = 1 if attr & 16 else 0
|
|
|
|
|
2016-09-16 19:04:03 -04:00
|
|
|
import subprocess
|
2017-07-07 20:44:07 -04:00
|
|
|
import glob, os
|
2016-12-15 04:31:03 -05:00
|
|
|
import termios
|
|
|
|
import time
|
2017-06-22 09:08:15 -04:00
|
|
|
import select
|
2017-02-22 17:48:37 -05:00
|
|
|
import dbus
|
2017-07-09 17:27:54 -04:00
|
|
|
import fcntl
|
|
|
|
from array import array
|
2017-07-17 18:58:54 -04:00
|
|
|
from fcntl import ioctl
|
|
|
|
from struct import unpack_from, unpack, pack
|
2018-03-21 06:10:28 -04:00
|
|
|
from fenrirscreenreader.core import debug
|
|
|
|
from fenrirscreenreader.core.eventData import fenrirEventType
|
|
|
|
from fenrirscreenreader.core.screenDriver import screenDriver
|
2017-07-09 17:27:54 -04:00
|
|
|
|
2017-10-24 01:42:06 -04:00
|
|
|
class driver(screenDriver):
|
2016-09-14 18:04:52 -04:00
|
|
|
def __init__(self):
|
2017-10-24 01:42:06 -04:00
|
|
|
screenDriver.__init__(self)
|
2017-02-26 09:17:08 -05:00
|
|
|
self.ListSessions = None
|
2017-07-17 18:58:54 -04:00
|
|
|
self.charmap = {}
|
2017-08-13 18:40:08 -04:00
|
|
|
self.bgColorNames = {0: _('black'), 1: _('blue'), 2: _('green'), 3: _('cyan'), 4: _('red'), 5: _('Magenta'), 6: _('brown/yellow'), 7: _('white')}
|
|
|
|
self.fgColorNames = {0: _('Black'), 1: _('Blue'), 2: _('Green'), 3: _('Cyan'), 4: _('Red'), 5: _('Magenta'), 6: _('brown/yellow'), 7: _('Light gray'), 8: _('Dark gray'), 9: _('Light blue'), 10: ('Light green'), 11: _('Light cyan'), 12: _('Light red'), 13: _('Light magenta'), 14: _('Light yellow'), 15: _('White')}
|
2017-07-17 18:58:54 -04:00
|
|
|
self.hichar = None
|
2016-09-14 18:04:52 -04:00
|
|
|
def initialize(self, environment):
|
2016-09-21 17:17:54 -04:00
|
|
|
self.env = environment
|
2017-08-03 16:04:47 -04:00
|
|
|
self.env['runtime']['processManager'].addCustomEventThread(self.updateWatchdog)
|
2016-09-14 18:04:52 -04:00
|
|
|
def getCurrScreen(self):
|
2017-04-24 16:40:18 -04:00
|
|
|
self.env['screen']['oldTTY'] = self.env['screen']['newTTY']
|
2016-09-14 18:04:52 -04:00
|
|
|
try:
|
|
|
|
currScreenFile = open('/sys/devices/virtual/tty/tty0/active','r')
|
2017-04-24 16:40:18 -04:00
|
|
|
self.env['screen']['newTTY'] = str(currScreenFile.read()[3:-1])
|
2016-09-14 18:04:52 -04:00
|
|
|
currScreenFile.close()
|
|
|
|
except Exception as e:
|
2016-09-21 17:17:54 -04:00
|
|
|
self.env['runtime']['debug'].writeDebugOut(str(e),debug.debugLevel.ERROR)
|
2016-12-15 04:36:10 -05:00
|
|
|
def injectTextToScreen(self, text, screen = None):
|
2017-04-24 16:40:18 -04:00
|
|
|
useScreen = "/dev/tty" + self.env['screen']['newTTY']
|
2016-12-15 04:36:10 -05:00
|
|
|
if screen != None:
|
|
|
|
useScreen = screen
|
|
|
|
with open(useScreen, 'w') as fd:
|
2016-12-15 04:31:03 -05:00
|
|
|
for c in text:
|
|
|
|
fcntl.ioctl(fd, termios.TIOCSTI, c)
|
|
|
|
|
2017-02-22 17:48:37 -05:00
|
|
|
def getSessionInformation(self):
|
2017-04-30 16:08:35 -04:00
|
|
|
try:
|
|
|
|
bus = dbus.SystemBus()
|
|
|
|
if not self.ListSessions:
|
|
|
|
obj = bus.get_object('org.freedesktop.login1', '/org/freedesktop/login1')
|
|
|
|
inf = dbus.Interface(obj, 'org.freedesktop.login1.Manager')
|
|
|
|
self.ListSessions = inf.get_dbus_method('ListSessions')
|
|
|
|
sessions = self.ListSessions()
|
2017-04-30 16:14:19 -04:00
|
|
|
self.env['screen']['autoIgnoreScreens'] = []
|
2017-04-30 16:08:35 -04:00
|
|
|
for session in sessions:
|
|
|
|
obj = bus.get_object('org.freedesktop.login1', session[4])
|
|
|
|
inf = dbus.Interface(obj, 'org.freedesktop.DBus.Properties')
|
|
|
|
sessionType = inf.Get('org.freedesktop.login1.Session', 'Type')
|
|
|
|
screen = str(inf.Get('org.freedesktop.login1.Session', 'VTNr'))
|
|
|
|
if screen == '':
|
|
|
|
screen = str(inf.Get('org.freedesktop.login1.Session', 'TTY'))
|
|
|
|
screen = screen[screen.upper().find('TTY') + 3:]
|
|
|
|
if screen == '':
|
|
|
|
self.env['runtime']['debug'].writeDebugOut('No TTY found for session:' + session[4],debug.debugLevel.ERROR)
|
|
|
|
return
|
2018-03-11 12:29:31 -04:00
|
|
|
if sessionType.upper() != 'TTY':
|
2017-04-30 16:19:31 -04:00
|
|
|
self.env['screen']['autoIgnoreScreens'].append(screen)
|
|
|
|
if screen == self.env['screen']['newTTY'] :
|
|
|
|
if self.env['general']['currUser'] != session[2]:
|
|
|
|
self.env['general']['prevUser'] = self.env['general']['currUser']
|
|
|
|
self.env['general']['currUser'] = session[2]
|
2017-04-30 16:08:35 -04:00
|
|
|
except Exception as e:
|
|
|
|
self.env['runtime']['debug'].writeDebugOut('getSessionInformation: Maybe no LoginD:' + str(e),debug.debugLevel.ERROR)
|
2017-07-07 20:58:11 -04:00
|
|
|
self.env['screen']['autoIgnoreScreens'] = []
|
2018-03-07 07:28:49 -05:00
|
|
|
#self.env['runtime']['debug'].writeDebugOut('getSessionInformation:' + str(self.env['screen']['autoIgnoreScreens']) + ' ' + str(self.env['general']) ,debug.debugLevel.INFO)
|
2018-03-23 15:44:31 -04:00
|
|
|
|
2017-06-25 09:32:47 -04:00
|
|
|
def updateWatchdog(self,active , eventQueue):
|
2017-07-01 17:10:48 -04:00
|
|
|
try:
|
|
|
|
vcsa = {}
|
2017-07-07 20:44:07 -04:00
|
|
|
vcsaDevices = glob.glob('/dev/vcsa*')
|
|
|
|
for vcsaDev in vcsaDevices:
|
|
|
|
index = vcsaDev[9:]
|
|
|
|
vcsa[str(index)] = open(vcsaDev,'rb')
|
2016-09-20 12:00:22 -04:00
|
|
|
|
2017-07-01 17:10:48 -04:00
|
|
|
tty = open('/sys/devices/virtual/tty/tty0/active','r')
|
|
|
|
currScreen = str(tty.read()[3:-1])
|
|
|
|
oldScreen = currScreen
|
|
|
|
watchdog = select.epoll()
|
2017-08-25 14:02:19 -04:00
|
|
|
watchdog.register(vcsa[currScreen], select.POLLPRI | select.POLLERR)
|
|
|
|
watchdog.register(tty, select.POLLPRI | select.POLLERR)
|
2017-07-01 17:10:48 -04:00
|
|
|
while active.value == 1:
|
|
|
|
changes = watchdog.poll(2)
|
|
|
|
for change in changes:
|
|
|
|
fileno = change[0]
|
|
|
|
event = change[1]
|
|
|
|
if fileno == tty.fileno():
|
2017-07-01 18:51:56 -04:00
|
|
|
self.env['runtime']['debug'].writeDebugOut('ScreenChange',debug.debugLevel.INFO)
|
2017-07-01 17:10:48 -04:00
|
|
|
tty.seek(0)
|
|
|
|
currScreen = str(tty.read()[3:-1])
|
|
|
|
if currScreen != oldScreen:
|
2017-07-07 20:44:07 -04:00
|
|
|
try:
|
|
|
|
watchdog.unregister(vcsa[ oldScreen ])
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try:
|
2017-08-25 14:02:19 -04:00
|
|
|
watchdog.register(vcsa[ currScreen ], select.POLLPRI | select.POLLERR)
|
2017-07-07 20:44:07 -04:00
|
|
|
except:
|
|
|
|
pass
|
2017-07-01 17:10:48 -04:00
|
|
|
oldScreen = currScreen
|
2017-07-07 20:44:07 -04:00
|
|
|
try:
|
|
|
|
vcsa[currScreen].seek(0)
|
|
|
|
lastScreenContent = vcsa[currScreen].read()
|
|
|
|
except:
|
2018-03-12 16:29:34 -04:00
|
|
|
pass
|
2018-03-23 15:44:31 -04:00
|
|
|
eventQueue.put({"Type":fenrirEventType.ScreenChanged,
|
|
|
|
"Data":self.createScreenEventData(currScreen,lastScreenContent)
|
|
|
|
})
|
2017-07-01 17:10:48 -04:00
|
|
|
else:
|
2017-07-01 18:41:26 -04:00
|
|
|
self.env['runtime']['debug'].writeDebugOut('ScreenUpdate',debug.debugLevel.INFO)
|
2017-08-25 14:02:19 -04:00
|
|
|
vcsa[currScreen].seek(0)
|
|
|
|
dirtyContent = vcsa[currScreen].read()
|
|
|
|
screenContent = b''
|
|
|
|
timeout = time.time()
|
2017-08-25 15:38:48 -04:00
|
|
|
while screenContent != dirtyContent:
|
|
|
|
screenContent = dirtyContent
|
|
|
|
if time.time() - timeout >= 0.4:
|
|
|
|
break
|
2018-03-12 16:54:57 -04:00
|
|
|
time.sleep(0.02)
|
2017-08-25 15:38:48 -04:00
|
|
|
vcsa[currScreen].seek(0)
|
|
|
|
dirtyContent = vcsa[currScreen].read()
|
2018-03-23 15:44:31 -04:00
|
|
|
eventQueue.put({"Type":fenrirEventType.ScreenUpdate,
|
|
|
|
"Data":self.createScreenEventData(currScreen,screenContent)
|
|
|
|
})
|
2017-07-01 17:10:48 -04:00
|
|
|
except Exception as e:
|
|
|
|
self.env['runtime']['debug'].writeDebugOut('VCSA:updateWatchdog:' + str(e),debug.debugLevel.ERROR)
|
2018-03-23 15:44:31 -04:00
|
|
|
|
2017-07-17 18:58:54 -04:00
|
|
|
|
2018-03-23 15:44:31 -04:00
|
|
|
def createScreenEventData(self, screen, content):
|
|
|
|
self.updateCharMap(screen)
|
|
|
|
eventData = {
|
|
|
|
'bytes': content,
|
|
|
|
'lines': int( content[0]),
|
|
|
|
'columns': int( content[1]),
|
|
|
|
'textCursor':
|
|
|
|
{
|
|
|
|
'x': int( content[2]),
|
|
|
|
'y': int( content[3])
|
|
|
|
},
|
|
|
|
'screen': screen,
|
|
|
|
'screenUpdateTime': time.time(),
|
|
|
|
}
|
|
|
|
encText, encAttr =\
|
|
|
|
self.autoDecodeVCSA(content[4:], eventData['lines'], eventData['columns'])
|
|
|
|
eventData['text'] = encText
|
|
|
|
eventData['attributes'] = encAttr
|
|
|
|
return eventData.copy()
|
2017-07-17 18:58:54 -04:00
|
|
|
def updateCharMap(self, screen):
|
2017-07-25 03:54:03 -04:00
|
|
|
self.charmap = {}
|
|
|
|
try:
|
|
|
|
tty = open('/dev/tty' + screen, 'rb')
|
|
|
|
except Exception as e:
|
|
|
|
self.env['runtime']['debug'].writeDebugOut('VCSA:updateCharMap:' + str(e),debug.debugLevel.ERROR)
|
|
|
|
return
|
2017-07-17 18:58:54 -04:00
|
|
|
GIO_UNIMAP = 0x4B66
|
|
|
|
VT_GETHIFONTMASK = 0x560D
|
|
|
|
himask = array("H", (0,))
|
|
|
|
ioctl(tty, VT_GETHIFONTMASK, himask)
|
|
|
|
self.hichar, = unpack_from("@H", himask)
|
|
|
|
sz = 512
|
|
|
|
line = ''
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
unipairs = array("H", [0]*(2*sz))
|
|
|
|
unimapdesc = array("B", pack("@HP", sz, unipairs.buffer_info()[0]))
|
|
|
|
ioctl(tty.fileno(), GIO_UNIMAP, unimapdesc)
|
|
|
|
break
|
2017-07-25 03:54:03 -04:00
|
|
|
except Exception as e:
|
2017-08-11 21:43:11 -04:00
|
|
|
self.env['runtime']['debug'].writeDebugOut('VCSA:updateCharMap:scaling up sz=' + str(sz) + ' ' + str(e),debug.debugLevel.WARNING)
|
2017-07-17 18:58:54 -04:00
|
|
|
sz *= 2
|
|
|
|
tty.close()
|
|
|
|
ncodes, = unpack_from("@H", unimapdesc)
|
|
|
|
utable = unpack_from("@%dH" % (2*ncodes), unipairs)
|
|
|
|
for u, b in zip(utable[::2], utable[1::2]):
|
|
|
|
if self.charmap.get(b) is None:
|
|
|
|
self.charmap[b] = chr(u)
|
|
|
|
|
|
|
|
def autoDecodeVCSA(self, allData, rows, cols):
|
|
|
|
allText = ''
|
2017-07-30 10:27:24 -04:00
|
|
|
allAttrib = []
|
2017-07-17 18:58:54 -04:00
|
|
|
i = 0
|
|
|
|
for y in range(rows):
|
|
|
|
lineText = ''
|
2017-07-30 10:27:24 -04:00
|
|
|
lineAttrib = []
|
2017-07-17 18:58:54 -04:00
|
|
|
for x in range(cols):
|
|
|
|
data = allData[i: i + 2]
|
|
|
|
i += 2
|
|
|
|
if data == b' \x07':
|
|
|
|
#attr = 7
|
|
|
|
#ink = 7
|
|
|
|
#paper = 0
|
|
|
|
#ch = ' '
|
2017-08-13 17:47:41 -04:00
|
|
|
lineAttrib.append((7,15,0,0,0,0)) # attribute, ink, paper, blink, bold, underline
|
2017-07-17 18:58:54 -04:00
|
|
|
lineText += ' '
|
|
|
|
continue
|
|
|
|
(sh,) = unpack("=H", data)
|
|
|
|
attr = (sh >> 8) & 0xFF
|
|
|
|
ch = sh & 0xFF
|
|
|
|
if self.hichar == 0x100:
|
|
|
|
attr >>= 1
|
|
|
|
ink = attr & 0x0F
|
|
|
|
paper = (attr>>4) & 0x0F
|
2017-08-13 17:06:45 -04:00
|
|
|
blink = 0
|
2017-08-13 18:47:15 -04:00
|
|
|
#if attr & 1:
|
|
|
|
# blink = 1
|
2017-08-13 17:06:45 -04:00
|
|
|
bold = 0
|
2017-08-13 18:47:15 -04:00
|
|
|
#if attr & 16:
|
|
|
|
# bold = 1
|
2017-07-17 18:58:54 -04:00
|
|
|
#if (ink != 7) or (paper != 0):
|
|
|
|
# print(ink,paper)
|
|
|
|
if sh & self.hichar:
|
|
|
|
ch |= 0x100
|
|
|
|
try:
|
|
|
|
lineText += self.charmap[ch]
|
2017-07-21 05:03:05 -04:00
|
|
|
except KeyError:
|
2017-08-11 20:53:00 -04:00
|
|
|
lineText += '?'
|
2017-08-13 17:06:45 -04:00
|
|
|
lineAttrib.append((attr,ink, paper,blink,bold,0)) # attribute, ink, paper, blink, bold, underline
|
2017-07-17 18:58:54 -04:00
|
|
|
allText += lineText + '\n'
|
|
|
|
allAttrib += lineAttrib
|
|
|
|
return str(allText), allAttrib
|
2017-08-13 17:06:45 -04:00
|
|
|
def getFenrirBGColor(self, attribute):
|
|
|
|
try:
|
2017-08-13 17:47:41 -04:00
|
|
|
return self.bgColorNames[attribute[2]]
|
|
|
|
except Exception as e:
|
2017-08-13 18:47:15 -04:00
|
|
|
print(e)
|
2017-08-13 17:06:45 -04:00
|
|
|
return ''
|
2017-08-13 17:15:23 -04:00
|
|
|
def getFenrirFGColor(self, attribute):
|
2017-08-13 17:06:45 -04:00
|
|
|
try:
|
2017-08-13 17:47:41 -04:00
|
|
|
return self.fgColorNames[attribute[1]]
|
2017-08-13 18:40:08 -04:00
|
|
|
except Exception as e:
|
2017-08-13 18:47:15 -04:00
|
|
|
print(e)
|
2017-08-13 17:06:45 -04:00
|
|
|
return ''
|
2017-08-13 17:15:23 -04:00
|
|
|
def getFenrirUnderline(self, attribute):
|
2017-08-13 17:47:41 -04:00
|
|
|
if attribute[5] == 1:
|
2017-08-13 17:06:45 -04:00
|
|
|
return _('underlined')
|
|
|
|
return ''
|
2017-08-13 17:15:23 -04:00
|
|
|
def getFenrirBold(self, attribute):
|
2017-08-13 17:47:41 -04:00
|
|
|
if attribute[4] == 1:
|
2017-08-13 17:06:45 -04:00
|
|
|
return _('bold')
|
|
|
|
return ''
|
2017-08-13 17:15:23 -04:00
|
|
|
def getFenrirBlink(self, attribute):
|
2017-08-13 17:47:41 -04:00
|
|
|
if attribute[3] == 1:
|
2017-08-13 17:06:45 -04:00
|
|
|
return _('blink')
|
|
|
|
return ''
|
2017-08-13 17:15:23 -04:00
|
|
|
def getFenrirFont(self, attribute):
|
2017-08-13 18:47:15 -04:00
|
|
|
return _('Default')
|
2017-08-13 17:15:23 -04:00
|
|
|
def getFenrirFontSize(self, attribute):
|
2018-03-23 15:44:31 -04:00
|
|
|
return _('Default')
|
2018-03-23 15:57:16 -04:00
|
|
|
def getCurrApplication(self):
|
|
|
|
apps = []
|
|
|
|
try:
|
|
|
|
currScreen = self.env['screen']['newTTY']
|
|
|
|
apps = subprocess.Popen('ps -t tty' + currScreen + ' -o comm,tty,stat', shell=True, stdout=subprocess.PIPE).stdout.read().decode()[:-1].split('\n')
|
|
|
|
except Exception as e:
|
|
|
|
self.env['runtime']['debug'].writeDebugOut(str(e),debug.debugLevel.ERROR)
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
for i in apps:
|
|
|
|
i = i.upper()
|
|
|
|
i = i.split()
|
|
|
|
i[0] = i[0]
|
|
|
|
i[1] = i[1]
|
|
|
|
if '+' in i[2]:
|
|
|
|
if i[0] != '':
|
|
|
|
if not "GREP" == i[0] and \
|
|
|
|
not "SH" == i[0] and \
|
|
|
|
not "PS" == i[0]:
|
|
|
|
if "TTY"+currScreen in i[1]:
|
|
|
|
if self.env['screen']['newApplication'] != i[0]:
|
|
|
|
self.env['screen']['newApplication'] = i[0]
|
|
|
|
return
|
|
|
|
except Exception as e:
|
|
|
|
self.env['runtime']['debug'].writeDebugOut(str(e),debug.debugLevel.ERROR)
|
|
|
|
|