1
0
Fork 0
mirror of https://github.com/kbumsik/VirtScreen.git synced 2025-03-09 15:40:18 +00:00

Creating ~/.virscreen dir and log x11vnc here

This commit is contained in:
Bumsik Kim 2018-04-26 03:49:00 -04:00
parent 6e93c9783d
commit b17c783f75
No known key found for this signature in database
GPG key ID: E31041C8EC5B01C6

View file

@ -7,11 +7,21 @@ from PyQt5.QtWidgets import (QAction, QApplication, QCheckBox, QComboBox,
QDialog, QGridLayout, QGroupBox, QHBoxLayout, QLabel, QLineEdit, QDialog, QGridLayout, QGroupBox, QHBoxLayout, QLabel, QLineEdit,
QMessageBox, QMenu, QPushButton, QSpinBox, QStyle, QSystemTrayIcon, QMessageBox, QMenu, QPushButton, QSpinBox, QStyle, QSystemTrayIcon,
QTextEdit, QVBoxLayout, QListWidget) QTextEdit, QVBoxLayout, QListWidget)
from twisted.internet import protocol from twisted.internet import protocol, error
from netifaces import interfaces, ifaddresses, AF_INET from netifaces import interfaces, ifaddresses, AF_INET
import subprocess import subprocess
import atexit, signal import atexit, signal
#-------------------------------------------------------------------------------
# PATH definitions
#-------------------------------------------------------------------------------
HOME_PATH = os.getenv('HOME', None)
if HOME_PATH is not None:
HOME_PATH = HOME_PATH + "/.virtscreen"
X11VNC_LOG_PATH = HOME_PATH + "/x11vnc_log.txt"
X11VNC_PASSWORD_PATH = HOME_PATH + "/x11vnc_passwd"
CONFIG_PATH = HOME_PATH + "/config"
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Display properties # Display properties
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
@ -122,10 +132,11 @@ class XRandR:
# Twisted class # Twisted class
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
class ProcessProtocol(protocol.ProcessProtocol): class ProcessProtocol(protocol.ProcessProtocol):
def __init__(self, onOutReceived, onErrRecevied, onProcessEnded): def __init__(self, onOutReceived, onErrRecevied, onProcessEnded, logfile=None):
self.onOutReceived = onOutReceived self.onOutReceived = onOutReceived
self.onErrRecevied = onErrRecevied self.onErrRecevied = onErrRecevied
self.onProcessEnded = onProcessEnded self.onProcessEnded = onProcessEnded
self.logfile = logfile
def run(self, arg: str): def run(self, arg: str):
"""Spawn a process """Spawn a process
@ -149,10 +160,14 @@ class ProcessProtocol(protocol.ProcessProtocol):
def outReceived(self, data): def outReceived(self, data):
print("outReceived! with %d bytes!" % len(data)) print("outReceived! with %d bytes!" % len(data))
self.onOutReceived(data) self.onOutReceived(data)
if self.logfile is not None:
self.logfile.write(data)
def errReceived(self, data): def errReceived(self, data):
print("outReceived! with %d bytes!" % len(data)) print("outReceived! with %d bytes!" % len(data))
self.onErrRecevied(data) self.onErrRecevied(data)
if self.logfile is not None:
self.logfile.write(data)
def inConnectionLost(self): def inConnectionLost(self):
print("inConnectionLost! stdin is closed! (we probably did it)") print("inConnectionLost! stdin is closed! (we probably did it)")
@ -174,6 +189,8 @@ class ProcessProtocol(protocol.ProcessProtocol):
print("processEnded, status", exitCode) print("processEnded, status", exitCode)
def processEnded(self, reason): def processEnded(self, reason):
if self.logfile is not None:
self.logfile.close()
exitCode = reason.value.exitCode exitCode = reason.value.exitCode
if exitCode is None: if exitCode is None:
print("Unknown exit") print("Unknown exit")
@ -212,8 +229,6 @@ class Window(QDialog):
self.trayIcon.activated.connect(self.iconActivated) self.trayIcon.activated.connect(self.iconActivated)
self.createDisplayButton.pressed.connect(self.createDisplayPressed) self.createDisplayButton.pressed.connect(self.createDisplayPressed)
self.startVNCButton.pressed.connect(self.startVNCPressed) self.startVNCButton.pressed.connect(self.startVNCPressed)
self.VNCMessageListWidget.model().rowsInserted.connect(
self.VNCMessageListWidget.scrollToBottom)
self.bottomQuitButton.pressed.connect(self.quitProgram) self.bottomQuitButton.pressed.connect(self.quitProgram)
# Show # Show
icon = QIcon("icon.png") icon = QIcon("icon.png")
@ -307,7 +322,7 @@ class Window(QDialog):
try: try:
# Rest of quit sequence will be handled in the callback. # Rest of quit sequence will be handled in the callback.
self.VNCServer.kill() self.VNCServer.kill()
except AttributeError: except (AttributeError, error.ProcessExitedAlready):
self.xrandr.delete_virtual_screen() self.xrandr.delete_virtual_screen()
QApplication.instance().quit() QApplication.instance().quit()
@ -389,10 +404,6 @@ class Window(QDialog):
self.startVNCButton.setDefault(False) self.startVNCButton.setDefault(False)
self.startVNCButton.setEnabled(False) self.startVNCButton.setEnabled(False)
messageLabel = QLabel("Server Messages")
self.VNCMessageListWidget = QListWidget()
self.VNCMessageListWidget.setEnabled(False)
# Set Overall layout # Set Overall layout
layout = QVBoxLayout() layout = QVBoxLayout()
portLayout = QHBoxLayout() portLayout = QHBoxLayout()
@ -403,9 +414,6 @@ class Window(QDialog):
layout.addWidget(self.startVNCButton) layout.addWidget(self.startVNCButton)
layout.addWidget(IPLabel) layout.addWidget(IPLabel)
layout.addWidget(self.VNCIPListWidget) layout.addWidget(self.VNCIPListWidget)
layout.addSpacing(15)
layout.addWidget(messageLabel)
layout.addWidget(self.VNCMessageListWidget)
self.VNCGroupBox.setLayout(layout) self.VNCGroupBox.setLayout(layout)
def createBottomLayout(self): def createBottomLayout(self):
@ -476,14 +484,14 @@ class Window(QDialog):
def _onReceived(data): def _onReceived(data):
data = data.decode("utf-8") data = data.decode("utf-8")
for line in data.splitlines(): for line in data.splitlines():
self.VNCMessageListWidget.addItem(line) # TODO: Update state of the server
pass
def _onEnded(exitCode): def _onEnded(exitCode):
self.startVNCButton.setEnabled(False) self.startVNCButton.setEnabled(False)
self.isVNCRunning = False self.isVNCRunning = False
if self.isQuitProgramPending: if self.isQuitProgramPending:
self.xrandr.delete_virtual_screen() self.xrandr.delete_virtual_screen()
QApplication.instance().quit() QApplication.instance().quit()
self.VNCMessageListWidget.setEnabled(False)
self.startVNCButton.setText("Start VNC Server") self.startVNCButton.setText("Start VNC Server")
self.startVNCButton.setEnabled(True) self.startVNCButton.setEnabled(True)
self.createDisplayButton.setEnabled(True) self.createDisplayButton.setEnabled(True)
@ -497,17 +505,17 @@ class Window(QDialog):
self.startVNCButton.setEnabled(False) self.startVNCButton.setEnabled(False)
self.startVNCButton.setText("Running...") self.startVNCButton.setText("Running...")
self.startVNCAction.setEnabled(False) self.startVNCAction.setEnabled(False)
self.VNCMessageListWidget.clear()
# Run VNC server # Run VNC server
self.isVNCRunning = True self.isVNCRunning = True
self.VNCServer = ProcessProtocol(_onReceived, _onReceived, _onEnded) logfile = open(X11VNC_LOG_PATH, "wb")
self.VNCServer = ProcessProtocol(_onReceived, _onReceived, _onEnded, logfile)
port = self.VNCPortSpinBox.value() port = self.VNCPortSpinBox.value()
virt = self.xrandr.get_virtual_screen() virt = self.xrandr.get_virtual_screen()
clip = f"{virt.width}x{virt.height}+{virt.x_offset}+{virt.y_offset}" clip = f"{virt.width}x{virt.height}+{virt.x_offset}+{virt.y_offset}"
arg = f"x11vnc -multiptr -repeat -rfbport {port} -clip {clip}" arg = f"x11vnc -multiptr -repeat -rfbport {port} -clip {clip}"
self.VNCServer.run(arg) self.VNCServer.run(arg)
self.update_ip_address()
# Change UI # Change UI
self.VNCMessageListWidget.setEnabled(True)
self.startVNCButton.setEnabled(True) self.startVNCButton.setEnabled(True)
self.startVNCButton.setText("Stop Sharing") self.startVNCButton.setText("Stop Sharing")
self.stopVNCAction.setEnabled(True) self.stopVNCAction.setEnabled(True)
@ -522,14 +530,25 @@ if __name__ == '__main__':
app = QApplication(sys.argv) app = QApplication(sys.argv)
if not QSystemTrayIcon.isSystemTrayAvailable(): if not QSystemTrayIcon.isSystemTrayAvailable():
QMessageBox.critical(None, "Systray", QMessageBox.critical(None, "VirtScreen",
"I couldn't detect any system tray on this system.") "I couldn't detect any system tray on this system.")
sys.exit(1) sys.exit(1)
if os.environ['XDG_SESSION_TYPE'] == 'wayland': if os.environ['XDG_SESSION_TYPE'] == 'wayland':
QMessageBox.critical(None, "Wayland Session", QMessageBox.critical(None, "VirtScreen",
"Currently Wayland is not supported") "Currently Wayland is not supported")
sys.exit(1) sys.exit(1)
if HOME_PATH is None:
QMessageBox.critical(None, "VirtScreen",
"VirtScreen cannot detect $HOME")
sys.exit(1)
if not os.path.exists(HOME_PATH):
try:
os.makedirs(HOME_PATH)
except:
QMessageBox.critical(None, "VirtScreen",
"VirtScreen cannot create ~/.virtscreen")
sys.exit(1)
import qt5reactor # pylint: disable=E0401 import qt5reactor # pylint: disable=E0401
qt5reactor.install() qt5reactor.install()