mirror of
https://github.com/kbumsik/VirtScreen.git
synced 2025-03-09 15:40:18 +00:00
Conformance to PEP 8
This commit is contained in:
parent
6ccdd556f2
commit
9001fee975
1 changed files with 79 additions and 51 deletions
|
@ -13,13 +13,13 @@ from PyQt5.QtQml import qmlRegisterType, QQmlApplicationEngine, QQmlListProperty
|
|||
from twisted.internet import protocol, error
|
||||
from netifaces import interfaces, ifaddresses, AF_INET
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------------------
|
||||
# file path definitions
|
||||
#-------------------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------------------
|
||||
# Sanitize environment variables
|
||||
# https://wiki.sei.cmu.edu/confluence/display/c/ENV03-C.+Sanitize+the+environment+when+invoking+external+programs
|
||||
del os.environ['HOME'] # Delete $HOME env for security reason. This will make
|
||||
# Path.home() to look up in the password directory (pwd module)
|
||||
# Path.home() to look up in the password directory (pwd module)
|
||||
os.environ['PATH'] = os.confstr("CS_PATH") # Sanitize $PATH
|
||||
|
||||
HOME_PATH = str(Path.home())
|
||||
|
@ -35,9 +35,10 @@ ICON_PATH = PROGRAM_PATH + "/icon/icon.png"
|
|||
ICON_TABLET_OFF_PATH = PROGRAM_PATH + "/icon/icon_tablet_off.png"
|
||||
ICON_TABLET_ON_PATH = PROGRAM_PATH + "/icon/icon_tablet_on.png"
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
# -------------------------------------------------------------------------------
|
||||
# Subprocess wrapper
|
||||
#-------------------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------------------
|
||||
class SubprocessWrapper:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
@ -51,9 +52,10 @@ class SubprocessWrapper:
|
|||
return subprocess.run(arg.split(), input=input, stdout=subprocess.PIPE,
|
||||
check=check, stderr=subprocess.STDOUT).stdout.decode('utf-8')
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
# -------------------------------------------------------------------------------
|
||||
# Twisted class
|
||||
#-------------------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------------------
|
||||
class ProcessProtocol(protocol.ProcessProtocol):
|
||||
def __init__(self, onConnected, onOutReceived, onErrRecevied, onProcessEnded, logfile=None):
|
||||
self.onConnected = onConnected
|
||||
|
@ -125,12 +127,14 @@ class ProcessProtocol(protocol.ProcessProtocol):
|
|||
print("quitting")
|
||||
self.onProcessEnded(exitCode)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
# -------------------------------------------------------------------------------
|
||||
# Display properties
|
||||
#-------------------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------------------
|
||||
class Display(object):
|
||||
__slots__ = ['name', 'primary', 'connected', 'active', 'width', 'height', 'x_offset', 'y_offset']
|
||||
def __init__(self, parent=None):
|
||||
|
||||
def __init__(self):
|
||||
self.name: str = None
|
||||
self.primary: bool = False
|
||||
self.connected: bool = False
|
||||
|
@ -154,9 +158,8 @@ class Display(object):
|
|||
ret += f" not active {self.width}x{self.height}"
|
||||
return ret
|
||||
|
||||
class DisplayProperty(QObject):
|
||||
_display: Display
|
||||
|
||||
class DisplayProperty(QObject):
|
||||
def __init__(self, display: Display, parent=None):
|
||||
super(DisplayProperty, self).__init__(parent)
|
||||
self._display = display
|
||||
|
@ -168,6 +171,7 @@ class DisplayProperty(QObject):
|
|||
@pyqtProperty(str, constant=True)
|
||||
def name(self):
|
||||
return self._display.name
|
||||
|
||||
@name.setter
|
||||
def name(self, name):
|
||||
self._display.name = name
|
||||
|
@ -175,6 +179,7 @@ class DisplayProperty(QObject):
|
|||
@pyqtProperty(bool, constant=True)
|
||||
def primary(self):
|
||||
return self._display.primary
|
||||
|
||||
@primary.setter
|
||||
def primary(self, primary):
|
||||
self._display.primary = primary
|
||||
|
@ -182,6 +187,7 @@ class DisplayProperty(QObject):
|
|||
@pyqtProperty(bool, constant=True)
|
||||
def connected(self):
|
||||
return self._display.connected
|
||||
|
||||
@connected.setter
|
||||
def connected(self, connected):
|
||||
self._display.connected = connected
|
||||
|
@ -189,6 +195,7 @@ class DisplayProperty(QObject):
|
|||
@pyqtProperty(bool, constant=True)
|
||||
def active(self):
|
||||
return self._display.active
|
||||
|
||||
@active.setter
|
||||
def active(self, active):
|
||||
self._display.active = active
|
||||
|
@ -196,6 +203,7 @@ class DisplayProperty(QObject):
|
|||
@pyqtProperty(int, constant=True)
|
||||
def width(self):
|
||||
return self._display.width
|
||||
|
||||
@width.setter
|
||||
def width(self, width):
|
||||
self._display.width = width
|
||||
|
@ -203,6 +211,7 @@ class DisplayProperty(QObject):
|
|||
@pyqtProperty(int, constant=True)
|
||||
def height(self):
|
||||
return self._display.height
|
||||
|
||||
@height.setter
|
||||
def height(self, height):
|
||||
self._display.height = height
|
||||
|
@ -210,6 +219,7 @@ class DisplayProperty(QObject):
|
|||
@pyqtProperty(int, constant=True)
|
||||
def x_offset(self):
|
||||
return self._display.x_offset
|
||||
|
||||
@x_offset.setter
|
||||
def x_offset(self, x_offset):
|
||||
self._display.x_offset = x_offset
|
||||
|
@ -217,13 +227,15 @@ class DisplayProperty(QObject):
|
|||
@pyqtProperty(int, constant=True)
|
||||
def y_offset(self):
|
||||
return self._display.y_offset
|
||||
|
||||
@y_offset.setter
|
||||
def y_offset(self, y_offset):
|
||||
self._display.y_offset = y_offset
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
# -------------------------------------------------------------------------------
|
||||
# Screen adjustment class
|
||||
#-------------------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------------------
|
||||
class XRandR(SubprocessWrapper):
|
||||
DEFAULT_VIRT_SCREEN = "VIRTUAL1"
|
||||
VIRT_SCREEN_SUFFIX = "_virt"
|
||||
|
@ -268,7 +280,7 @@ class XRandR(SubprocessWrapper):
|
|||
for s in self.screens:
|
||||
print("\t", s)
|
||||
if self.virt_idx == self.primary_idx:
|
||||
raise RuntimeError("VIrtual screen must be selected other than the primary screen")
|
||||
raise RuntimeError("Virtual screen must be selected other than the primary screen")
|
||||
if self.virt_idx is None:
|
||||
for idx, screen in enumerate(self.screens):
|
||||
if not screen.connected and not screen.active:
|
||||
|
@ -338,11 +350,13 @@ class XRandR(SubprocessWrapper):
|
|||
atexit.unregister(self.delete_virtual_screen)
|
||||
self._update_screens()
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
# -------------------------------------------------------------------------------
|
||||
# QML Backend class
|
||||
#-------------------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------------------
|
||||
class Backend(QObject):
|
||||
""" Backend class for QML frontend """
|
||||
|
||||
class VNCState:
|
||||
""" Enum to indicate a state of the VNC server """
|
||||
OFF = 0
|
||||
|
@ -383,6 +397,7 @@ class Backend(QObject):
|
|||
except FileNotFoundError:
|
||||
with open(DEFAULT_CONFIG_PATH, "r") as f:
|
||||
return f.read()
|
||||
|
||||
@settings.setter
|
||||
def settings(self, json_str):
|
||||
with open(CONFIG_PATH, "w") as f:
|
||||
|
@ -391,6 +406,7 @@ class Backend(QObject):
|
|||
@pyqtProperty(bool, notify=onVirtScreenCreatedChanged)
|
||||
def virtScreenCreated(self):
|
||||
return self._virtScreenCreated
|
||||
|
||||
@virtScreenCreated.setter
|
||||
def virtScreenCreated(self, value):
|
||||
self._virtScreenCreated = value
|
||||
|
@ -403,6 +419,7 @@ class Backend(QObject):
|
|||
@pyqtProperty(int, notify=onVirtScreenIndexChanged)
|
||||
def virtScreenIndex(self):
|
||||
return self._virtScreenIndex
|
||||
|
||||
@virtScreenIndex.setter
|
||||
def virtScreenIndex(self, virtScreenIndex):
|
||||
print("Changing virt to ", virtScreenIndex)
|
||||
|
@ -418,6 +435,7 @@ class Backend(QObject):
|
|||
if self._vncUsePassword:
|
||||
self.vncUsePassword = False
|
||||
return self._vncUsePassword
|
||||
|
||||
@vncUsePassword.setter
|
||||
def vncUsePassword(self, use):
|
||||
self._vncUsePassword = use
|
||||
|
@ -426,6 +444,7 @@ class Backend(QObject):
|
|||
@pyqtProperty(VNCState, notify=onVncStateChanged)
|
||||
def vncState(self):
|
||||
return self._vncState
|
||||
|
||||
@vncState.setter
|
||||
def vncState(self, state):
|
||||
self._vncState = state
|
||||
|
@ -513,10 +532,12 @@ class Backend(QObject):
|
|||
# regex used in callbacks
|
||||
patter_connected = re.compile(r"^.*Got connection from client.*$", re.M)
|
||||
patter_disconnected = re.compile(r"^.*client_count: 0*$", re.M)
|
||||
|
||||
# define callbacks
|
||||
def _onConnected():
|
||||
print("VNC started.")
|
||||
self.vncState = self.VNCState.WAITING
|
||||
|
||||
def _onReceived(data):
|
||||
data = data.decode("utf-8")
|
||||
if (self._vncState is not self.VNCState.CONNECTED) and patter_connected.search(data):
|
||||
|
@ -525,6 +546,7 @@ class Backend(QObject):
|
|||
if (self._vncState is self.VNCState.CONNECTED) and patter_disconnected.search(data):
|
||||
print("VNC disconnected.")
|
||||
self.vncState = self.VNCState.WAITING
|
||||
|
||||
def _onEnded(exitCode):
|
||||
if exitCode is not 0:
|
||||
self.vncState = self.VNCState.ERROR
|
||||
|
@ -534,6 +556,7 @@ class Backend(QObject):
|
|||
self.vncState = self.VNCState.OFF
|
||||
print("VNC Exited.")
|
||||
atexit.unregister(self.stopVNC)
|
||||
|
||||
logfile = open(X11VNC_LOG_PATH, "wb")
|
||||
self.vncServer = ProcessProtocol(_onConnected, _onReceived, _onReceived, _onEnded, logfile)
|
||||
virt = self.xrandr.get_virtual_screen()
|
||||
|
@ -550,13 +573,16 @@ class Backend(QObject):
|
|||
# define callbacks
|
||||
def _onConnected():
|
||||
print("External Display Setting opened.")
|
||||
|
||||
def _onReceived(data):
|
||||
pass
|
||||
|
||||
def _onEnded(exitCode):
|
||||
print("External Display Setting closed.")
|
||||
self.onDisplaySettingClosed.emit()
|
||||
if exitCode is not 0:
|
||||
self.onError.emit(f'Error opening "{running_program}".')
|
||||
|
||||
program_list = ["gnome-control-center display", "arandr"]
|
||||
program = ProcessProtocol(_onConnected, _onReceived, _onReceived, _onEnded, None)
|
||||
running_program = ''
|
||||
|
@ -578,7 +604,7 @@ class Backend(QObject):
|
|||
if force:
|
||||
# Usually called from atexit().
|
||||
self.vncServer.kill()
|
||||
time.sleep(2) # Make sure X11VNC shutdown before execute next atexit.
|
||||
time.sleep(2) # Make sure X11VNC shutdown before execute next atexit().
|
||||
if self._vncState in (self.VNCState.WAITING, self.VNCState.CONNECTED):
|
||||
self.vncServer.kill()
|
||||
else:
|
||||
|
@ -593,9 +619,10 @@ class Backend(QObject):
|
|||
self.blockSignals(True) # This will prevent invoking auto-restart or etc.
|
||||
QApplication.instance().quit()
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
# -------------------------------------------------------------------------------
|
||||
# Main Code
|
||||
#-------------------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------------------
|
||||
if __name__ == '__main__':
|
||||
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
|
||||
app = QApplication(sys.argv)
|
||||
|
@ -624,6 +651,7 @@ if __name__ == '__main__':
|
|||
sys.exit(1)
|
||||
|
||||
import qt5reactor # pylint: disable=E0401
|
||||
|
||||
qt5reactor.install()
|
||||
from twisted.internet import utils, reactor # pylint: disable=E0401
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue