1
0
Fork 0
mirror of https://github.com/kbumsik/VirtScreen.git synced 2025-02-12 19:31:50 +00:00

Enable status printing in CLI mode #8

This commit is contained in:
Bumsik Kim 2018-11-07 05:19:37 +09:00
parent ebbbf97cdf
commit 00388dcb0a
No known key found for this signature in database
GPG key ID: E31041C8EC5B01C6
2 changed files with 17 additions and 12 deletions

View file

@ -70,7 +70,7 @@ def main() -> None:
parser.add_argument('--hidpi', action='store_true', parser.add_argument('--hidpi', action='store_true',
help='HiDPI mode. Width and height are doubled') help='HiDPI mode. Width and height are doubled')
parser.add_argument('--log', type=str, parser.add_argument('--log', type=str,
help='Python logging level, For example, --log=DEBUG.\n' help='Python logging level, For example, --log=INFO.\n'
'Only used for reporting bugs and debugging') 'Only used for reporting bugs and debugging')
# Add signal handler # Add signal handler
def on_exit(self, signum=None, frame=None): def on_exit(self, signum=None, frame=None):
@ -177,7 +177,7 @@ def main_cli(args: argparse.Namespace):
sys.exit(1) sys.exit(1)
# By instantiating the backend, additional verifications of config # By instantiating the backend, additional verifications of config
# file will be done. # file will be done.
backend = Backend() backend = Backend(logger=print)
# Get settings # Get settings
with open(CONFIG_PATH, 'r') as f: with open(CONFIG_PATH, 'r') as f:
config = json.load(f) config = json.load(f)

View file

@ -8,6 +8,7 @@ import shutil
import atexit import atexit
import time import time
import logging import logging
from typing import Callable
from PyQt5.QtCore import QObject, pyqtProperty, pyqtSlot, pyqtSignal, Q_ENUMS from PyQt5.QtCore import QObject, pyqtProperty, pyqtSlot, pyqtSignal, Q_ENUMS
from PyQt5.QtGui import QCursor from PyQt5.QtGui import QCursor
@ -41,7 +42,7 @@ class Backend(QObject):
onDisplaySettingClosed = pyqtSignal() onDisplaySettingClosed = pyqtSignal()
onError = pyqtSignal(str) onError = pyqtSignal(str)
def __init__(self, parent=None): def __init__(self, parent=None, logger=logging.info, error_logger=logging.error):
super(Backend, self).__init__(parent) super(Backend, self).__init__(parent)
# Virtual screen properties # Virtual screen properties
self.xrandr: XRandR = XRandR() self.xrandr: XRandR = XRandR()
@ -51,6 +52,9 @@ class Backend(QObject):
self._vncState: self.VNCState = self.VNCState.OFF self._vncState: self.VNCState = self.VNCState.OFF
# Primary screen and mouse posistion # Primary screen and mouse posistion
self.vncServer: AsyncSubprocess self.vncServer: AsyncSubprocess
# Info/error logger
self.log: Callable[[str], None] = logger
self.log_error: Callable[[str], None] = error_logger
# Check config file # Check config file
# and initialize if needed # and initialize if needed
need_init = False need_init = False
@ -95,7 +99,7 @@ class Backend(QObject):
f.write(json.dumps(config, indent=4, sort_keys=True)) f.write(json.dumps(config, indent=4, sort_keys=True))
def promptError(self, msg): def promptError(self, msg):
logging.error(msg) self.log_error(msg)
self.onError.emit(msg) self.onError.emit(msg)
# Qt properties # Qt properties
@ -153,7 +157,7 @@ class Backend(QObject):
@pyqtSlot(str, int, int, bool, bool) @pyqtSlot(str, int, int, bool, bool)
def createVirtScreen(self, device, width, height, portrait, hidpi, pos=''): def createVirtScreen(self, device, width, height, portrait, hidpi, pos=''):
self.xrandr.virt_name = device self.xrandr.virt_name = device
logging.info("Creating a Virtual Screen...") self.log("Creating a Virtual Screen...")
try: try:
self.xrandr.create_virtual_screen(width, height, portrait, hidpi, pos) self.xrandr.create_virtual_screen(width, height, portrait, hidpi, pos)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
@ -163,10 +167,11 @@ class Backend(QObject):
self.promptError(str(e)) self.promptError(str(e))
return return
self.virtScreenCreated = True self.virtScreenCreated = True
self.log("The Virtual Screen successfully created.")
@pyqtSlot() @pyqtSlot()
def deleteVirtScreen(self): def deleteVirtScreen(self):
logging.info("Deleting the Virtual Screen...") self.log("Deleting the Virtual Screen...")
if self.vncState is not self.VNCState.OFF: if self.vncState is not self.VNCState.OFF:
self.promptError("Turn off the VNC server first") self.promptError("Turn off the VNC server first")
self.virtScreenCreated = True self.virtScreenCreated = True
@ -215,16 +220,16 @@ class Backend(QObject):
# define callbacks # define callbacks
def _connected(): def _connected():
logging.info("VNC started.") self.log(f"VNC started. Now connect a VNC client to port {port}.")
self.vncState = self.VNCState.WAITING self.vncState = self.VNCState.WAITING
def _received(data): def _received(data):
data = data.decode("utf-8") data = data.decode("utf-8")
if (self._vncState is not self.VNCState.CONNECTED) and patter_connected.search(data): if (self._vncState is not self.VNCState.CONNECTED) and patter_connected.search(data):
logging.info("VNC connected.") self.log("VNC connected.")
self.vncState = self.VNCState.CONNECTED self.vncState = self.VNCState.CONNECTED
if (self._vncState is self.VNCState.CONNECTED) and patter_disconnected.search(data): if (self._vncState is self.VNCState.CONNECTED) and patter_disconnected.search(data):
logging.info("VNC disconnected.") self.log("VNC disconnected.")
self.vncState = self.VNCState.WAITING self.vncState = self.VNCState.WAITING
def _ended(exitCode): def _ended(exitCode):
@ -235,7 +240,7 @@ class Backend(QObject):
self.vncState = self.VNCState.OFF # TODO: better handling error state self.vncState = self.VNCState.OFF # TODO: better handling error state
else: else:
self.vncState = self.VNCState.OFF self.vncState = self.VNCState.OFF
logging.info("VNC Exited.") self.log("VNC Exited.")
atexit.unregister(self.stopVNC) atexit.unregister(self.stopVNC)
# load settings # load settings
with open(CONFIG_PATH, 'r') as f: with open(CONFIG_PATH, 'r') as f:
@ -269,13 +274,13 @@ class Backend(QObject):
def openDisplaySetting(self, app: str = "arandr"): def openDisplaySetting(self, app: str = "arandr"):
# define callbacks # define callbacks
def _connected(): def _connected():
logging.info("External Display Setting opened.") self.log("External Display Setting opened.")
def _received(data): def _received(data):
pass pass
def _ended(exitCode): def _ended(exitCode):
logging.info("External Display Setting closed.") self.log("External Display Setting closed.")
self.onDisplaySettingClosed.emit() self.onDisplaySettingClosed.emit()
if exitCode is not 0: if exitCode is not 0:
self.promptError(f'Error opening "{running_program}".') self.promptError(f'Error opening "{running_program}".')