From 71bc7c6042e06d3b0eb030cf6841f3c005151a48 Mon Sep 17 00:00:00 2001 From: chrys Date: Mon, 26 Mar 2018 07:31:04 +0200 Subject: [PATCH] remove poll from screen_utils; resize terminal at startup --- .../screenDriver/ptyDriver.py | 90 ++++--------------- src/fenrirscreenreader/utils/screen_utils.py | 68 ++++++++++---- 2 files changed, 69 insertions(+), 89 deletions(-) diff --git a/src/fenrirscreenreader/screenDriver/ptyDriver.py b/src/fenrirscreenreader/screenDriver/ptyDriver.py index 32796211..42be45aa 100644 --- a/src/fenrirscreenreader/screenDriver/ptyDriver.py +++ b/src/fenrirscreenreader/screenDriver/ptyDriver.py @@ -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.eventData import fenrirEventType from fenrirscreenreader.core.screenDriver import screenDriver +from fenrirscreenreader.utils import screen_utils class Terminal: def __init__(self, columns, lines, p_in): @@ -45,7 +46,8 @@ class Terminal: 'lines': self.screen.lines, 'columns': self.screen.columns, "text": text, - 'attributes': allAttributes + 'attributes': allAttributes, + 'screen': '1' }.copy() class driver(screenDriver): @@ -66,47 +68,7 @@ class driver(screenDriver): def injectTextToScreen(self, msgBytes, screen = None): #os.write(p_out.fileno(), msgBytes) 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): self.env['screen']['autoIgnoreScreens'] = [] self.env['general']['prevUser'] = getpass.getuser() @@ -115,15 +77,12 @@ class driver(screenDriver): bytes = os.read(fd, 65536) if bytes == b'': raise EOFError - while self.hasMore(fd): + while screen_utils.hasMore(fd,0.0002): data = os.read(fd, 65536) if data == b'': raise EOFError bytes += data - return bytes - def hasMore(self,fd): - r, w, e = select.select([fd], [], [], 0.02) - return (fd in r) + return bytes def openTerminal(self, columns, lines, command): p_pid, master_fd = pty.fork() if p_pid == 0: # Child. @@ -148,17 +107,17 @@ class driver(screenDriver): os.write(self.signalPipe[1], b'w') def terminalEmulation(self,active , eventQueue): debug = False - running = True try: old_attr = termios.tcgetattr(sys.stdin) tty.setraw(0) lines, columns = self.getTerminalSize(0) - shell = self.getShell() - terminal, p_pid, p_out = self.openTerminal(columns, lines, shell) - std_out = os.fdopen(sys.stdout.fileno(), "w+b", 0) - + if self.command == '': + self.command = screen_utils.getShell() + terminal, p_pid, p_out = self.openTerminal(columns, lines, self.command) + lines, columns = self.resizeTerminal(p_out) + terminal.resize(lines, columns) 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 if r == []: continue @@ -174,7 +133,7 @@ class driver(screenDriver): try: msgBytes = self.readAll(p_out.fileno()) except (EOFError, OSError): - running = False + active.value = False break if debug: print('after p_out read',msgBytes) @@ -183,7 +142,7 @@ class driver(screenDriver): if debug: print('after p_out write') eventQueue.put({"Type":fenrirEventType.ScreenUpdate, - "Data":self.createScreenEventData(terminal.dump()) + "Data":screen_utils.createScreenEventData(terminal.dump()) }) if debug: print('after p_out') @@ -197,14 +156,14 @@ class driver(screenDriver): "Data":msgBytes }) except (EOFError, OSError): - running = False + active.value = False break os.write(p_out.fileno(), msgBytes) if debug: print('after stdin') except Exception as e: # Process died? print(e) - running = False + active.value = False finally: os.kill(p_pid, signal.SIGTERM) p_out.close() @@ -212,23 +171,6 @@ class driver(screenDriver): eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None}) 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): try: return self.bgColorNames[attribute[2]] diff --git a/src/fenrirscreenreader/utils/screen_utils.py b/src/fenrirscreenreader/utils/screen_utils.py index f8c87b38..e8b32731 100644 --- a/src/fenrirscreenreader/utils/screen_utils.py +++ b/src/fenrirscreenreader/utils/screen_utils.py @@ -6,11 +6,7 @@ from fenrirscreenreader.core import debug from collections import Counter -import string -from select import select -from select import epoll -import select -import re +import getpass, time, re, string, select, os def removeNonprintable(text): # 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): 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): - r, w, e = select([fd], [], [], 0) - return (fd in r) - -def hasMorePollPri(fd): - p = epoll() - p.register(fd, select.POLLPRI | select.POLLERR) - r = p.poll(0) - return (fd in r) - +def hasMore(fd, timetout=0.2): + r, _, _ = select.select([fd], [], [], timetout) + return (fd in r) +def getShell(): + 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 trackHighlights(oldAttr, newAttr, text, lenght): result = '' currCursor = None