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

QML: graceful shutdown process

This commit is contained in:
Bumsik Kim 2018-05-07 16:53:43 -04:00
parent 0975e4e8ac
commit b8d9a7dc07
No known key found for this signature in database
GPG key ID: E31041C8EC5B01C6

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys, os, subprocess, signal, re, atexit import sys, os, subprocess, signal, re, atexit, time
from enum import Enum from enum import Enum
from PyQt5.QtWidgets import QApplication from PyQt5.QtWidgets import QApplication
@ -141,6 +141,7 @@ class XRandR(SubprocessWrapper):
return return
self.call(f"xrandr --output {self.virt.name} --off") self.call(f"xrandr --output {self.virt.name} --off")
self.call(f"xrandr --delmode {self.virt.name} {self.mode_name}") self.call(f"xrandr --delmode {self.virt.name} {self.mode_name}")
atexit.unregister(self.delete_virtual_screen)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Twisted class # Twisted class
@ -174,13 +175,13 @@ class ProcessProtocol(protocol.ProcessProtocol):
self.transport.closeStdin() # No more input self.transport.closeStdin() # No more input
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: if self.logfile is not None:
self.logfile.write(data) self.logfile.write(data)
def errReceived(self, data): def errReceived(self, data):
print("outReceived! with %d bytes!" % len(data)) # print("errReceived! with %d bytes!" % len(data))
self.onErrRecevied(data) self.onErrRecevied(data)
if self.logfile is not None: if self.logfile is not None:
self.logfile.write(data) self.logfile.write(data)
@ -347,6 +348,9 @@ class Backend(QObject):
@pyqtSlot() @pyqtSlot()
def deleteVirtScreen(self): def deleteVirtScreen(self):
print("Deleting the Virtual Screen...") print("Deleting the Virtual Screen...")
if self.vncState != VNCState.OFF.value:
print("Turn off the VNC server first")
return
self.xrandr.delete_virtual_screen() self.xrandr.delete_virtual_screen()
self.virtScreenCreated = False self.virtScreenCreated = False
@ -368,6 +372,7 @@ class Backend(QObject):
def _onEnded(exitCode): def _onEnded(exitCode):
print("VNC Exited.") print("VNC Exited.")
self.vncState = VNCState.OFF self.vncState = VNCState.OFF
atexit.unregister(self.stopVNC)
# Set password # Set password
password = False password = False
if self.vncPassword: if self.vncPassword:
@ -387,10 +392,15 @@ class Backend(QObject):
if password: if password:
arg += f" -rfbauth {X11VNC_PASSWORD_PATH}" arg += f" -rfbauth {X11VNC_PASSWORD_PATH}"
self.vncServer.run(arg) self.vncServer.run(arg)
# auto stop on exit
atexit.register(self.stopVNC, force=True)
@pyqtSlot() @pyqtSlot()
def stopVNC(self): def stopVNC(self, force=False):
print(self.vncState) if force:
# Usually called from atexit().
self.vncServer.kill()
time.sleep(2) # Make sure X11VNC shutdown before execute next atexit.
if self.vncState in (VNCState.WAITING.value, VNCState.CONNECTED.value): if self.vncState in (VNCState.WAITING.value, VNCState.CONNECTED.value):
self.vncServer.kill() self.vncServer.kill()
else: else: