2018-03-21 11:10:28 +01:00
|
|
|
#!/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Fenrir TTY screen reader
|
|
|
|
# By Chrys, Storm Dragon, and contributers.
|
|
|
|
|
2018-03-25 15:54:12 +02:00
|
|
|
import os, struct, sys, pty, tty, termios, shlex, signal, select, pyte, time, fcntl ,getpass
|
2018-03-21 11:10:28 +01:00
|
|
|
from fenrirscreenreader.core import debug
|
|
|
|
from fenrirscreenreader.core.eventData import fenrirEventType
|
|
|
|
from fenrirscreenreader.core.screenDriver import screenDriver
|
2018-03-26 07:31:04 +02:00
|
|
|
from fenrirscreenreader.utils import screen_utils
|
2018-03-21 11:10:28 +01:00
|
|
|
|
2018-03-23 23:09:24 +01:00
|
|
|
class Terminal:
|
|
|
|
def __init__(self, columns, lines, p_in):
|
|
|
|
self.screen = pyte.HistoryScreen(columns, lines)
|
|
|
|
self.screen.set_mode(pyte.modes.LNM)
|
|
|
|
self.screen.write_process_input = \
|
|
|
|
lambda data: p_in.write(data.encode())
|
|
|
|
self.stream = pyte.ByteStream()
|
|
|
|
self.stream.attach(self.screen)
|
|
|
|
def feed(self, data):
|
|
|
|
self.stream.feed(data)
|
2018-03-24 18:48:04 +01:00
|
|
|
def resize(self, lines, columns):
|
|
|
|
self.screen.resize(lines, columns)
|
2018-03-24 18:39:11 +01:00
|
|
|
self.setCursor()
|
|
|
|
def setCursor(self, x = -1, y = -1):
|
|
|
|
xPos = x
|
|
|
|
yPos = y
|
|
|
|
if xPos == -1:
|
|
|
|
xPos = self.screen.cursor.x
|
|
|
|
if yPos == -1:
|
|
|
|
yPos = self.screen.cursor.y
|
|
|
|
self.screen.cursor.x = min(self.screen.cursor.x, self.screen.columns - 1)
|
|
|
|
self.screen.cursor.y = min(self.screen.cursor.y, self.screen.lines - 1)
|
2018-03-23 23:09:24 +01:00
|
|
|
def dump(self):
|
|
|
|
cursor = self.screen.cursor
|
2018-03-24 17:34:28 +01:00
|
|
|
allAttributes = []
|
|
|
|
text = '\n'.join(self.screen.display)
|
|
|
|
for y in range(self.screen.lines):
|
|
|
|
line = self.screen.buffer[y]
|
|
|
|
attributes = [(char.reverse, char.fg, char.bg, char.bold, char.italics, char.underscore, char.strikethrough)
|
|
|
|
for char in (line[x] for x in range(self.screen.columns))]
|
|
|
|
allAttributes.append((attributes))
|
2018-03-23 23:09:24 +01:00
|
|
|
self.screen.dirty.clear()
|
2018-03-24 21:29:54 +01:00
|
|
|
return {"cursor": (cursor.x, cursor.y),
|
|
|
|
'lines': self.screen.lines,
|
|
|
|
'columns': self.screen.columns,
|
|
|
|
"text": text,
|
2018-03-26 07:31:04 +02:00
|
|
|
'attributes': allAttributes,
|
|
|
|
'screen': '1'
|
2018-03-24 21:29:54 +01:00
|
|
|
}.copy()
|
2018-03-23 23:09:24 +01:00
|
|
|
|
2018-03-21 11:10:28 +01:00
|
|
|
class driver(screenDriver):
|
|
|
|
def __init__(self):
|
|
|
|
screenDriver.__init__(self)
|
|
|
|
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')}
|
2018-03-24 18:39:11 +01:00
|
|
|
self.signalPipe = os.pipe()
|
2018-03-26 11:45:30 +02:00
|
|
|
self.p_out = None
|
2018-03-26 23:46:56 +02:00
|
|
|
self.
|
2018-03-25 23:03:53 +02:00
|
|
|
signal.signal(signal.SIGWINCH, self.handleSigwinch)
|
2018-03-21 11:10:28 +01:00
|
|
|
def initialize(self, environment):
|
|
|
|
self.env = environment
|
2018-03-26 23:46:56 +02:00
|
|
|
self.ShortcutType = self.env['runtime']['inputManager'].getShortcutType()
|
2018-03-25 23:03:53 +02:00
|
|
|
self.command = self.env['runtime']['settingsManager'].getSetting('general','shell')
|
2018-03-25 16:05:10 +02:00
|
|
|
self.env['runtime']['processManager'].addCustomEventThread(self.terminalEmulation)
|
2018-03-21 11:10:28 +01:00
|
|
|
def getCurrScreen(self):
|
2018-03-24 21:29:54 +01:00
|
|
|
self.env['screen']['oldTTY'] = '1'
|
|
|
|
self.env['screen']['newTTY'] = '1'
|
2018-03-23 21:05:47 +01:00
|
|
|
|
2018-03-25 15:54:12 +02:00
|
|
|
def injectTextToScreen(self, msgBytes, screen = None):
|
2018-03-26 11:45:30 +02:00
|
|
|
if not screen:
|
|
|
|
screen = self.p_out.fileno()
|
|
|
|
if isinstance(msgBytes, str):
|
|
|
|
msgBytes = bytes(msgBytes, 'UTF-8')
|
|
|
|
os.write(screen, msgBytes)
|
|
|
|
#print(str(msgBytes))
|
2018-03-26 07:31:04 +02:00
|
|
|
|
2018-03-21 11:10:28 +01:00
|
|
|
def getSessionInformation(self):
|
2018-03-23 21:05:47 +01:00
|
|
|
self.env['screen']['autoIgnoreScreens'] = []
|
2018-03-25 15:54:12 +02:00
|
|
|
self.env['general']['prevUser'] = getpass.getuser()
|
|
|
|
self.env['general']['currUser'] = getpass.getuser()
|
2018-03-26 17:23:48 +02:00
|
|
|
def readAll(self,fd, timeout = 9999999):
|
2018-03-23 23:09:24 +01:00
|
|
|
bytes = os.read(fd, 65536)
|
|
|
|
if bytes == b'':
|
|
|
|
raise EOFError
|
2018-03-26 17:23:48 +02:00
|
|
|
starttime = time.time()
|
|
|
|
# respect timeout but wait a little bit of time to see if something more is here
|
2018-03-26 23:46:56 +02:00
|
|
|
while screen_utils.hasMore(fd,0.0000001) and (time.time() - starttime) >= timeout:
|
2018-03-23 23:09:24 +01:00
|
|
|
data = os.read(fd, 65536)
|
|
|
|
if data == b'':
|
|
|
|
raise EOFError
|
|
|
|
bytes += data
|
2018-03-26 07:31:04 +02:00
|
|
|
return bytes
|
2018-03-25 23:03:53 +02:00
|
|
|
def openTerminal(self, columns, lines, command):
|
2018-03-23 23:09:24 +01:00
|
|
|
p_pid, master_fd = pty.fork()
|
|
|
|
if p_pid == 0: # Child.
|
|
|
|
argv = shlex.split(command)
|
|
|
|
env = os.environ.copy()
|
|
|
|
env["TERM"] = 'vt100'
|
|
|
|
os.execvpe(argv[0], argv, env)
|
|
|
|
# File-like object for I/O with the child process aka command.
|
|
|
|
p_out = os.fdopen(master_fd, "w+b", 0)
|
|
|
|
return Terminal(columns, lines, p_out), p_pid, p_out
|
2018-03-24 18:48:04 +01:00
|
|
|
def resizeTerminal(self,fd):
|
2018-03-24 18:39:11 +01:00
|
|
|
s = struct.pack('HHHH', 0, 0, 0, 0)
|
|
|
|
s = fcntl.ioctl(0, termios.TIOCGWINSZ, s)
|
|
|
|
fcntl.ioctl(fd, termios.TIOCSWINSZ, s)
|
2018-03-24 18:48:04 +01:00
|
|
|
lines, columns, _, _ = struct.unpack('hhhh', s)
|
|
|
|
return lines, columns
|
|
|
|
def getTerminalSize(self, fd):
|
2018-03-24 18:39:11 +01:00
|
|
|
s = struct.pack('HHHH', 0, 0, 0, 0)
|
2018-03-24 18:48:04 +01:00
|
|
|
lines, columns, _, _ = struct.unpack('HHHH', fcntl.ioctl(fd, termios.TIOCGWINSZ, s))
|
|
|
|
return lines, columns
|
|
|
|
def handleSigwinch(self, *args):
|
2018-03-24 18:39:11 +01:00
|
|
|
os.write(self.signalPipe[1], b'w')
|
2018-03-23 23:09:24 +01:00
|
|
|
def terminalEmulation(self,active , eventQueue):
|
|
|
|
try:
|
|
|
|
old_attr = termios.tcgetattr(sys.stdin)
|
|
|
|
tty.setraw(0)
|
2018-03-24 18:48:04 +01:00
|
|
|
lines, columns = self.getTerminalSize(0)
|
2018-03-26 07:31:04 +02:00
|
|
|
if self.command == '':
|
|
|
|
self.command = screen_utils.getShell()
|
2018-03-26 11:45:30 +02:00
|
|
|
terminal, p_pid, self.p_out = self.openTerminal(columns, lines, self.command)
|
|
|
|
lines, columns = self.resizeTerminal(self.p_out)
|
2018-03-26 07:31:04 +02:00
|
|
|
terminal.resize(lines, columns)
|
2018-03-25 15:54:12 +02:00
|
|
|
while active.value:
|
2018-03-26 11:45:30 +02:00
|
|
|
r, _, _ = select.select([sys.stdin, self.p_out, self.signalPipe[0]],[],[],1)
|
2018-03-23 23:09:24 +01:00
|
|
|
# none
|
|
|
|
if r == []:
|
|
|
|
continue
|
2018-03-24 18:39:11 +01:00
|
|
|
# signals
|
|
|
|
if self.signalPipe[0] in r:
|
|
|
|
os.read(self.signalPipe[0], 1)
|
2018-03-26 11:45:30 +02:00
|
|
|
lines, columns = self.resizeTerminal(self.p_out)
|
2018-03-26 13:22:29 +02:00
|
|
|
terminal.resize(lines, columns)
|
|
|
|
# input
|
|
|
|
if sys.stdin in r:
|
|
|
|
try:
|
|
|
|
msgBytes = self.readAll(sys.stdin.fileno())
|
|
|
|
except (EOFError, OSError):
|
|
|
|
active.value = False
|
2018-03-26 23:46:56 +02:00
|
|
|
break
|
|
|
|
if self.ShortcutType == 'KEY':
|
|
|
|
try:
|
|
|
|
self.injectTextToScreen(msgBytes)
|
|
|
|
except:
|
|
|
|
active.value = False
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
eventQueue.put({"Type":fenrirEventType.ByteInput,
|
|
|
|
"Data":msgBytes })
|
2018-03-23 23:09:24 +01:00
|
|
|
# output
|
2018-03-26 11:45:30 +02:00
|
|
|
if self.p_out in r:
|
2018-03-23 23:09:24 +01:00
|
|
|
try:
|
2018-03-26 22:29:47 +02:00
|
|
|
msgBytes = self.readAll(self.p_out.fileno(), timeout=0.01)
|
2018-03-23 23:09:24 +01:00
|
|
|
except (EOFError, OSError):
|
2018-03-26 07:31:04 +02:00
|
|
|
active.value = False
|
2018-03-23 23:09:24 +01:00
|
|
|
break
|
|
|
|
terminal.feed(msgBytes)
|
|
|
|
os.write(sys.stdout.fileno(), msgBytes)
|
|
|
|
eventQueue.put({"Type":fenrirEventType.ScreenUpdate,
|
2018-03-26 07:31:04 +02:00
|
|
|
"Data":screen_utils.createScreenEventData(terminal.dump())
|
2018-03-23 23:09:24 +01:00
|
|
|
})
|
|
|
|
except Exception as e: # Process died?
|
|
|
|
print(e)
|
2018-03-26 07:31:04 +02:00
|
|
|
active.value = False
|
2018-03-23 23:09:24 +01:00
|
|
|
finally:
|
|
|
|
os.kill(p_pid, signal.SIGTERM)
|
2018-03-26 11:45:30 +02:00
|
|
|
self.p_out.close()
|
2018-03-23 23:09:24 +01:00
|
|
|
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_attr)
|
|
|
|
eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None})
|
|
|
|
sys.exit(0)
|
2018-03-23 21:00:16 +01:00
|
|
|
|
2018-03-21 11:10:28 +01:00
|
|
|
def getFenrirBGColor(self, attribute):
|
|
|
|
try:
|
|
|
|
return self.bgColorNames[attribute[2]]
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
return ''
|
|
|
|
def getFenrirFGColor(self, attribute):
|
|
|
|
try:
|
|
|
|
return self.fgColorNames[attribute[1]]
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
return ''
|
|
|
|
def getFenrirUnderline(self, attribute):
|
|
|
|
if attribute[5] == 1:
|
|
|
|
return _('underlined')
|
|
|
|
return ''
|
|
|
|
def getFenrirBold(self, attribute):
|
|
|
|
if attribute[4] == 1:
|
|
|
|
return _('bold')
|
|
|
|
return ''
|
|
|
|
def getFenrirBlink(self, attribute):
|
|
|
|
if attribute[3] == 1:
|
|
|
|
return _('blink')
|
|
|
|
return ''
|
|
|
|
def getFenrirFont(self, attribute):
|
|
|
|
return _('Default')
|
|
|
|
def getFenrirFontSize(self, attribute):
|
2018-03-23 21:00:16 +01:00
|
|
|
return _('Default')
|
|
|
|
def getCurrApplication(self):
|
2018-03-23 21:05:47 +01:00
|
|
|
pass
|