remove poll from screen_utils; resize terminal at startup

This commit is contained in:
chrys 2018-03-26 07:31:04 +02:00
parent cdb2a40c85
commit 71bc7c6042
2 changed files with 69 additions and 89 deletions

View File

@ -8,6 +8,7 @@ import os, struct, sys, pty, tty, termios, shlex, signal, select, pyte, time, fc
from fenrirscreenreader.core import debug from fenrirscreenreader.core import debug
from fenrirscreenreader.core.eventData import fenrirEventType from fenrirscreenreader.core.eventData import fenrirEventType
from fenrirscreenreader.core.screenDriver import screenDriver from fenrirscreenreader.core.screenDriver import screenDriver
from fenrirscreenreader.utils import screen_utils
class Terminal: class Terminal:
def __init__(self, columns, lines, p_in): def __init__(self, columns, lines, p_in):
@ -45,7 +46,8 @@ class Terminal:
'lines': self.screen.lines, 'lines': self.screen.lines,
'columns': self.screen.columns, 'columns': self.screen.columns,
"text": text, "text": text,
'attributes': allAttributes 'attributes': allAttributes,
'screen': '1'
}.copy() }.copy()
class driver(screenDriver): class driver(screenDriver):
@ -66,47 +68,7 @@ class driver(screenDriver):
def injectTextToScreen(self, msgBytes, screen = None): def injectTextToScreen(self, msgBytes, screen = None):
#os.write(p_out.fileno(), msgBytes) #os.write(p_out.fileno(), msgBytes)
pass pass
def getShell(self):
shell = ''
try:
if self.command != '':
shell = self.command
if os.path.isfile(shell):
return shell
except:
pass
try:
shell = os.environ["FENRIRSHELL"]
if os.path.isfile(shell):
return shell
except:
pass
try:
shell = os.environ["SHELL"]
if os.path.isfile(shell):
return shell
except:
pass
try:
if os.acess('/etc/passwd'):
with open('/etc/passwd') as f:
users = f.readlines()
for user in users:
(username, encrypwd, uid, gid, gecos, homedir, shell) = user.split(':')
shell = shell.replace('\n','')
if username == getpass.getuser():
if shell != '':
if os.path.isfile(shell):
return shell
except:
pass
try:
if os.path.isfile('/bin/bash'):
shell = '/bin/bash'
return shell
except:
pass
return '/bin/sh'
def getSessionInformation(self): def getSessionInformation(self):
self.env['screen']['autoIgnoreScreens'] = [] self.env['screen']['autoIgnoreScreens'] = []
self.env['general']['prevUser'] = getpass.getuser() self.env['general']['prevUser'] = getpass.getuser()
@ -115,15 +77,12 @@ class driver(screenDriver):
bytes = os.read(fd, 65536) bytes = os.read(fd, 65536)
if bytes == b'': if bytes == b'':
raise EOFError raise EOFError
while self.hasMore(fd): while screen_utils.hasMore(fd,0.0002):
data = os.read(fd, 65536) data = os.read(fd, 65536)
if data == b'': if data == b'':
raise EOFError raise EOFError
bytes += data bytes += data
return bytes return bytes
def hasMore(self,fd):
r, w, e = select.select([fd], [], [], 0.02)
return (fd in r)
def openTerminal(self, columns, lines, command): def openTerminal(self, columns, lines, command):
p_pid, master_fd = pty.fork() p_pid, master_fd = pty.fork()
if p_pid == 0: # Child. if p_pid == 0: # Child.
@ -148,17 +107,17 @@ class driver(screenDriver):
os.write(self.signalPipe[1], b'w') os.write(self.signalPipe[1], b'w')
def terminalEmulation(self,active , eventQueue): def terminalEmulation(self,active , eventQueue):
debug = False debug = False
running = True
try: try:
old_attr = termios.tcgetattr(sys.stdin) old_attr = termios.tcgetattr(sys.stdin)
tty.setraw(0) tty.setraw(0)
lines, columns = self.getTerminalSize(0) lines, columns = self.getTerminalSize(0)
shell = self.getShell() if self.command == '':
terminal, p_pid, p_out = self.openTerminal(columns, lines, shell) self.command = screen_utils.getShell()
std_out = os.fdopen(sys.stdout.fileno(), "w+b", 0) terminal, p_pid, p_out = self.openTerminal(columns, lines, self.command)
lines, columns = self.resizeTerminal(p_out)
terminal.resize(lines, columns)
while active.value: while active.value:
r, w, x = select.select([sys.stdin, p_out, self.signalPipe[0]],[],[],1) r, _, _ = select.select([sys.stdin, p_out, self.signalPipe[0]],[],[],1)
# none # none
if r == []: if r == []:
continue continue
@ -174,7 +133,7 @@ class driver(screenDriver):
try: try:
msgBytes = self.readAll(p_out.fileno()) msgBytes = self.readAll(p_out.fileno())
except (EOFError, OSError): except (EOFError, OSError):
running = False active.value = False
break break
if debug: if debug:
print('after p_out read',msgBytes) print('after p_out read',msgBytes)
@ -183,7 +142,7 @@ class driver(screenDriver):
if debug: if debug:
print('after p_out write') print('after p_out write')
eventQueue.put({"Type":fenrirEventType.ScreenUpdate, eventQueue.put({"Type":fenrirEventType.ScreenUpdate,
"Data":self.createScreenEventData(terminal.dump()) "Data":screen_utils.createScreenEventData(terminal.dump())
}) })
if debug: if debug:
print('after p_out') print('after p_out')
@ -197,14 +156,14 @@ class driver(screenDriver):
"Data":msgBytes "Data":msgBytes
}) })
except (EOFError, OSError): except (EOFError, OSError):
running = False active.value = False
break break
os.write(p_out.fileno(), msgBytes) os.write(p_out.fileno(), msgBytes)
if debug: if debug:
print('after stdin') print('after stdin')
except Exception as e: # Process died? except Exception as e: # Process died?
print(e) print(e)
running = False active.value = False
finally: finally:
os.kill(p_pid, signal.SIGTERM) os.kill(p_pid, signal.SIGTERM)
p_out.close() p_out.close()
@ -212,23 +171,6 @@ class driver(screenDriver):
eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None}) eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None})
sys.exit(0) sys.exit(0)
def createScreenEventData(self, content):
eventData = {
'bytes': content,
'lines': content['lines'],
'columns': content['columns'],
'textCursor':
{
'x': int( content['cursor'][0]),
'y': int( content['cursor'][1])
},
'screen': '1',
'text': content['text'],
'attributes': content['attributes'],
'screenUpdateTime': time.time(),
}
return eventData.copy()
def getFenrirBGColor(self, attribute): def getFenrirBGColor(self, attribute):
try: try:
return self.bgColorNames[attribute[2]] return self.bgColorNames[attribute[2]]

View File

@ -6,11 +6,7 @@
from fenrirscreenreader.core import debug from fenrirscreenreader.core import debug
from collections import Counter from collections import Counter
import string import getpass, time, re, string, select, os
from select import select
from select import epoll
import select
import re
def removeNonprintable(text): def removeNonprintable(text):
# Get the difference of all ASCII characters from the set of printable characters # Get the difference of all ASCII characters from the set of printable characters
@ -23,17 +19,59 @@ def insertNewlines(string, every=64):
def splitEvery(toSplit, every=64): def splitEvery(toSplit, every=64):
return list(toSplit[i:i+every] for i in range(0, len(toSplit), every)) return list(toSplit[i:i+every] for i in range(0, len(toSplit), every))
def createScreenEventData(content):
eventData = {
'bytes': content,
'lines': content['lines'],
'columns': content['columns'],
'textCursor':
{
'x': int( content['cursor'][0]),
'y': int( content['cursor'][1])
},
'screen': content['screen'],
'text': content['text'],
'attributes': content['attributes'],
'screenUpdateTime': time.time(),
}
return eventData.copy()
def hasMoreRead(fd): def hasMore(fd, timetout=0.2):
r, w, e = select([fd], [], [], 0) r, _, _ = select.select([fd], [], [], timetout)
return (fd in r) return (fd in r)
def getShell():
def hasMorePollPri(fd): try:
p = epoll() shell = os.environ["FENRIRSHELL"]
p.register(fd, select.POLLPRI | select.POLLERR) if os.path.isfile(shell):
r = p.poll(0) return shell
return (fd in r) except:
pass
try:
shell = os.environ["SHELL"]
if os.path.isfile(shell):
return shell
except:
pass
try:
if os.acess('/etc/passwd'):
with open('/etc/passwd') as f:
users = f.readlines()
for user in users:
(username, encrypwd, uid, gid, gecos, homedir, shell) = user.split(':')
shell = shell.replace('\n','')
if username == getpass.getuser():
if shell != '':
if os.path.isfile(shell):
return shell
except:
pass
try:
if os.path.isfile('/bin/bash'):
shell = '/bin/bash'
return shell
except:
pass
return '/bin/sh'
def trackHighlights(oldAttr, newAttr, text, lenght): def trackHighlights(oldAttr, newAttr, text, lenght):
result = '' result = ''
currCursor = None currCursor = None