fenrir/src/fenrirscreenreader/core/remoteManager.py

208 lines
8.2 KiB
Python
Raw Normal View History

2018-09-04 16:32:03 -04:00
#!/bin/python
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
2018-09-05 17:11:42 -04:00
'''
Remote controll:
2018-09-06 19:03:55 -04:00
section<space>command<space>parameters
sections:command,setting
setting commands:
- set section#setting=value[,section#setting=value]
2018-09-05 17:11:42 -04:00
- reset
2018-09-06 19:03:55 -04:00
command commands:
- say text to speech
- interrupt
examples
2018-09-05 17:11:42 -04:00
settings:
2018-09-06 19:03:55 -04:00
settings set section#setting=value[,section#setting=value]
setting set speech#voice=de
setting reset
command:
command say this is a test
command interrupt
2018-09-05 17:11:42 -04:00
'''
2018-09-04 16:32:03 -04:00
from fenrirscreenreader.core import debug
2018-09-05 13:13:20 -04:00
from fenrirscreenreader.core.eventData import fenrirEventType
2018-09-05 16:27:56 -04:00
import time
import select
2018-09-05 17:03:07 -04:00
import socket
2018-09-07 13:36:25 -04:00
import os, os.path, sys, stat
2018-09-04 16:32:03 -04:00
class remoteManager():
def __init__(self):
2018-09-07 11:09:11 -04:00
# command controll
self.commandConst = 'COMMAND '
self.sayConst = 'SAY '
self.interruptConst = 'INTERRUPT'
self.defineWindowConst = 'WINDOW '
self.resetWindowConst = 'RESETWINDOW'
# setting controll
self.settingConst = 'SETTING '
self.setSettingConst = 'SET '
2018-09-08 16:23:27 -04:00
self.saveSettingConst = 'SAVE '
2018-09-07 11:09:11 -04:00
self.resetSettingConst = 'RESET'
2018-09-04 16:32:03 -04:00
def initialize(self, environment):
2018-09-05 16:27:56 -04:00
self.env = environment
2018-09-06 18:56:29 -04:00
2018-09-05 16:27:56 -04:00
if self.env['runtime']['settingsManager'].getSettingAsBool('remote', 'enabled'):
2018-09-07 15:55:40 -04:00
if self.env['runtime']['settingsManager'].getSetting('remote', 'method').upper() == 'UNIX':
2018-09-05 16:27:56 -04:00
self.env['runtime']['processManager'].addCustomEventThread(self.unixSocketWatchDog, multiprocess=True)
2018-09-07 15:55:40 -04:00
elif self.env['runtime']['settingsManager'].getSetting('remote', 'method').upper() == 'TCP':
2018-09-05 16:27:56 -04:00
self.env['runtime']['processManager'].addCustomEventThread(self.tcpWatchDog, multiprocess=True)
2018-09-04 16:32:03 -04:00
def shutdown(self):
2018-09-05 16:27:56 -04:00
if self.sock:
self.sock.close()
self.sock = None
def unixSocketWatchDog(self, active, eventQueue):
2018-09-07 15:19:21 -04:00
# echo "command say this is a test" | socat - UNIX-CLIENT:/tmp/fenrirscreenreader-deamon.sock
2018-09-07 13:50:10 -04:00
if self.env['runtime']['settingsManager'].getSetting('screen', 'driver') =='vcsaDriver':
2018-09-07 15:19:21 -04:00
socketpath = self.env['runtime']['settingsManager'].getSettingAsInt('remote', 'socketpath') + 'fenrirscreenreader-deamon.sock'
2018-09-07 13:50:10 -04:00
else:
2018-09-07 15:19:21 -04:00
socketpath = self.env['runtime']['settingsManager'].getSettingAsInt('remote', 'socketpath') + 'fenrirscreenreader-' + str(os.getpid()) + '.sock'
2018-09-06 17:54:56 -04:00
if os.path.exists(socketpath):
os.remove(socketpath)
2018-09-06 18:56:29 -04:00
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
2018-09-06 17:54:56 -04:00
self.sock.bind(socketpath)
2018-09-05 17:03:07 -04:00
self.sock.listen(1)
2018-09-07 15:19:21 -04:00
if self.env['runtime']['settingsManager'].getSetting('screen', 'driver') =='vcsaDriver':
2018-09-07 13:52:56 -04:00
os.chmod(socketpath, 0o222)
2018-09-05 16:27:56 -04:00
while active.value == 1:
2018-09-05 17:03:07 -04:00
client_sock, client_addr = self.sock.accept()
if client_sock:
# Check if the client is still connected and if data is available:
try:
r, w, e = select.select([client_sock,], [], [])
except select.error:
return
if len(r) > 0:
rawdata = client_sock.recv(8129)
try:
2018-09-05 17:11:42 -04:00
data = rawdata.decode("utf-8").rstrip().lstrip()
2018-09-06 18:31:00 -04:00
eventQueue.put({"Type":fenrirEventType.RemoteIncomming,
"Data": data
})
2018-09-05 17:03:07 -04:00
except:
pass
client_sock.close()
2018-09-07 15:19:21 -04:00
2018-09-07 14:45:42 -04:00
if os.path.exists(socketpath):
os.remove(socketpath)
2018-09-05 17:03:07 -04:00
if self.sock:
self.sock.close()
self.sock = None
2018-09-05 16:27:56 -04:00
def tcpWatchDog(self, active, eventQueue):
2018-09-06 18:56:29 -04:00
# echo "command say this is a test" | nc localhost 22447
2018-09-05 16:27:56 -04:00
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
2018-09-07 13:39:12 -04:00
self.host = '127.0.0.1'
2018-09-07 15:19:21 -04:00
self.port = self.env['runtime']['settingsManager'].getSettingAsInt('remote', 'port')
2018-09-05 16:27:56 -04:00
self.sock.bind((self.host, self.port))
self.sock.listen(1)
while active.value == 1:
client_sock, client_addr = self.sock.accept()
if client_sock:
# Check if the client is still connected and if data is available:
try:
r, w, e = select.select([client_sock,], [], [])
except select.error:
return
if len(r) > 0:
rawdata = client_sock.recv(8129)
try:
2018-09-05 17:11:42 -04:00
data = rawdata.decode("utf-8").rstrip().lstrip()
2018-09-06 18:31:00 -04:00
eventQueue.put({"Type":fenrirEventType.RemoteIncomming,
"Data": data
})
2018-09-05 16:27:56 -04:00
except:
pass
client_sock.close()
if self.sock:
self.sock.close()
self.sock = None
def handleSettingsChange(self, settingsText):
if not self.env['runtime']['settingsManager'].getSettingAsBool('remote', 'enableSettingsRemote'):
return
2018-09-08 14:05:59 -04:00
2018-09-07 11:09:11 -04:00
upperSettingsText = settingsText.upper()
# set setting
if upperSettingsText.startswith(self.setSettingConst):
parameterText = settingsText[len(self.setSettingConst):]
2018-09-05 16:27:56 -04:00
self.setSettings(parameterText)
2018-09-08 16:23:27 -04:00
# save setting
if upperSettingsText.startswith(self.saveSettingConst):
parameterText = settingsText[len(self.saveSettingConst):]
self.saveSettings(parameterText)
2018-09-07 11:09:11 -04:00
# reset setting
if upperSettingsText.startswith(self.resetSettingConst):
2018-09-05 16:27:56 -04:00
self.resetSettings()
def handleCommandExecution(self, commandText):
if not self.env['runtime']['settingsManager'].getSettingAsBool('remote', 'enableCommandRemote'):
return
2018-09-08 14:05:59 -04:00
2018-09-07 11:09:11 -04:00
upperCommandText = commandText.upper()
2018-09-08 14:05:59 -04:00
2018-09-07 11:09:11 -04:00
# say
if upperCommandText.startswith(self.sayConst):
parameterText = commandText[len(self.sayConst):]
self.say(parameterText)
# interrupt
if upperCommandText.startswith(self.interruptConst):
self.interruptSpeech()
# define window
if upperCommandText.startswith(self.defineWindowConst):
parameterText = commandText[len(self.defineWindowConst):]
self.defineWindow(parameterText)
# reset window
if upperCommandText.startswith(self.resetWindowConst):
2018-09-07 13:50:10 -04:00
self.resetWindow()
2018-09-07 11:09:11 -04:00
def defineWindow(self, windowText):
start = {}
end = {}
try:
windowList = windowText.split(' ')
if len(windowList) < 4:
return
start['x'] = int(windowList[0])
start['y'] = int(windowList[1])
end['x'] = int(windowList[2])
end['y'] = int(windowList[3])
self.env['runtime']['cursorManager'].setWindowForApplication(start, end)
2018-09-07 11:13:48 -04:00
except Exception as e:
2018-09-07 11:09:11 -04:00
pass
def resetWindow(self):
self.env['runtime']['cursorManager'].clearWindowForApplication()
def say(self, text):
2018-09-08 16:23:27 -04:00
if not text:
return
if text == '':
return
2018-09-05 16:27:56 -04:00
self.env['runtime']['outputManager'].speakText(text)
2018-09-07 11:09:11 -04:00
def interruptSpeech(self):
2018-09-05 16:27:56 -04:00
self.env['runtime']['outputManager'].interruptOutput()
2018-09-08 16:23:27 -04:00
def saveSettings(self, settingConfigPath):
if not settingConfigPath:
return
if settingConfigPath == '':
return
self.env['runtime']['settingsManager'].saveSettings(settingConfigPath)
2018-09-05 16:27:56 -04:00
def resetSettings(self):
self.env['runtime']['settingsManager'].resetSettingArgDict()
def setSettings(self, settingsArgs):
2018-09-08 14:05:59 -04:00
self.env['runtime']['settingsManager'].parseSettingArgs(settingsArgs)
2018-09-05 13:13:20 -04:00
def handleRemoteIncomming(self, eventData):
if not eventData:
return
2018-09-07 11:09:11 -04:00
upperEventData = eventData.upper()
if upperEventData.startswith(self.settingConst):
settingsText = eventData[len(self.settingConst):]
2018-09-05 16:27:56 -04:00
self.handleSettingsChange(settingsText)
2018-09-07 11:09:11 -04:00
elif upperEventData.startswith(self.commandConst):
commandText = eventData[len(self.commandConst):]
2018-09-05 16:27:56 -04:00
self.handleCommandExecution(commandText)