fenrir/src/fenrirscreenreader/core/byteManager.py

110 lines
4.9 KiB
Python
Raw Normal View History

2018-03-25 18:47:00 -04:00
#!/bin/python
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
from fenrirscreenreader.core import debug
2018-05-29 16:47:10 -04:00
from fenrirscreenreader.core.eventData import fenrirEventType
import os, inspect, re, time
currentdir = os.path.dirname(os.path.realpath(os.path.abspath(inspect.getfile(inspect.currentframe()))))
fenrirPath = os.path.dirname(currentdir)
2018-03-25 18:47:00 -04:00
class byteManager():
def __init__(self):
2018-05-29 16:47:10 -04:00
self.switchCtrlModeOnce = 0
self.controlMode = True
self.repeat = 1
self.lastInputTime = time.time()
self.lastByteKey = b''
2018-03-25 18:47:00 -04:00
def initialize(self, environment):
self.env = environment
def shutdown(self):
pass
def unifyEscapeSeq(self, escapeSequence):
convertedEscapeSequence = escapeSequence
if convertedEscapeSequence[0] == 27:
2018-03-26 18:26:34 -04:00
convertedEscapeSequence = b'^[' + convertedEscapeSequence[1:]
if len(convertedEscapeSequence) > 1:
if convertedEscapeSequence[0] == 94 and convertedEscapeSequence[1] ==91:
convertedEscapeSequence = b'^[' + convertedEscapeSequence[2:]
2018-05-29 16:47:10 -04:00
return convertedEscapeSequence
def handleByteInput(self, eventData):
if not eventData:
return
if eventData == b'':
return
convertedEscapeSequence = self.unifyEscapeSeq(eventData)
if self.switchCtrlModeOnce > 0:
self.switchCtrlModeOnce -= 1
isControlMode = False
if self.controlMode and not self.switchCtrlModeOnce == 1 or\
not self.controlMode:
isControlMode = self.handleControlMode(eventData)
2018-05-29 16:54:36 -04:00
2018-05-29 16:47:10 -04:00
isCommand = False
if self.controlMode and not self.switchCtrlModeOnce == 1 or\
not self.controlMode and self.switchCtrlModeOnce == 1:
2018-05-29 16:54:36 -04:00
if self.lastByteKey == convertedEscapeSequence:
if time.time() - self.lastInputTime <= self.env['runtime']['settingsManager'].getSettingAsFloat('keyboard','doubleTapTimeout'):
self.repeat += 1
2018-05-30 05:59:45 -04:00
shortcutData = b''
2018-05-30 12:33:08 -04:00
for i in range(self.repeat):
2018-05-30 05:59:45 -04:00
shortcutData = shortcutData + convertedEscapeSequence
2018-05-29 16:47:10 -04:00
isCommand = self.detectByteCommand(shortcutData)
2018-05-30 05:59:45 -04:00
# fall back to single stroke - do we want this?
2018-05-29 16:47:10 -04:00
if not isCommand:
isCommand = self.detectByteCommand(convertedEscapeSequence)
2018-05-30 05:59:45 -04:00
self.repeat = 1
2018-05-29 16:47:10 -04:00
if not (isCommand or isControlMode):
self.env['runtime']['screenManager'].injectTextToScreen(eventData)
if not isCommand:
self.repeat = 1
self.lastByteKey = convertedEscapeSequence
self.lastInputTime = time.time()
def handleControlMode(self, escapeSequence):
convertedEscapeSequence = self.unifyEscapeSeq(escapeSequence)
if convertedEscapeSequence == b'^[R':
self.controlMode = not self.controlMode
self.switchCtrlModeOnce = 0
if self.controlMode:
self.env['runtime']['outputManager'].presentText(_('Sticky Mode On'), soundIcon='Accept', interrupt=True, flush=True)
else:
self.env['runtime']['outputManager'].presentText(_('Sticky Mode On'), soundIcon='Cancel', interrupt=True, flush=True)
return True
if convertedEscapeSequence == b'^[:':
self.switchCtrlModeOnce = 2
self.env['runtime']['outputManager'].presentText(_('bypass'), soundIcon='PTYBypass', interrupt=True, flush=True)
return True
return False
def detectByteCommand(self, escapeSequence):
convertedEscapeSequence = self.unifyEscapeSeq(escapeSequence)
2018-05-30 06:01:30 -04:00
command = self.env['runtime']['inputManager'].getCommandForShortcut(convertedEscapeSequence)
if command != '':
self.env['runtime']['eventManager'].putToEventQueue(fenrirEventType.ExecuteCommand, command)
command = ''
2018-05-29 16:47:10 -04:00
return True
return False
def loadByteShortcuts(self, kbConfigPath=fenrirPath + '/../../config/keyboard/pty.conf'):
kbConfig = open(kbConfigPath,"r")
while(True):
line = kbConfig.readline()
if not line:
break
line = line.replace('\n','')
if line.replace(" ","") == '':
continue
if line.replace(" ","").startswith("#"):
continue
if line.count("=") != 1:
continue
Values = line.split('=')
shortcut = bytes(Values[0],'UTF-8')
commandName = Values[1].upper()
self.env['bindings'][shortcut] = commandName
self.env['runtime']['debug'].writeDebugOut("Byte Shortcut: "+ str(shortcut) + ' command:' +commandName ,debug.debugLevel.INFO, onAnyLevel=True)
kbConfig.close()