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

QML: VNC Server state parsing and updating

This commit is contained in:
Bumsik Kim 2018-05-07 23:34:11 -04:00
parent d1775042b1
commit a07fecd574
No known key found for this signature in database
GPG key ID: E31041C8EC5B01C6
2 changed files with 53 additions and 26 deletions

View file

@ -1,6 +1,6 @@
import QtQuick 2.10 import QtQuick 2.10
import QtQuick.Controls 2.3 import QtQuick.Controls 2.3
// import QtQuick.Controls.Material 2.3 import QtQuick.Controls.Material 2.3
import QtQuick.Layouts 1.3 import QtQuick.Layouts 1.3
import QtQuick.Window 2.2 import QtQuick.Window 2.2
@ -15,8 +15,8 @@ ApplicationWindow {
flags: Qt.FramelessWindowHint flags: Qt.FramelessWindowHint
title: "Basic layouts" title: "Basic layouts"
// Material.theme: Material.Light Material.theme: Material.Light
// Material.accent: Material.Teal Material.accent: Material.Teal
property int margin: 11 property int margin: 11
width: 380 width: 380
@ -29,7 +29,7 @@ ApplicationWindow {
} }
} }
// virtscreen.py hackend. // virtscreen.py backend.
Backend { Backend {
id: backend id: backend
} }
@ -169,16 +169,16 @@ ApplicationWindow {
busyDialog.open(); busyDialog.open();
// Give a very short delay to show busyDialog. // Give a very short delay to show busyDialog.
timer.setTimeout (function() { timer.setTimeout (function() {
if (!backend.virtScreenCreated) { if (!backend.virtScreenCreated) {
backend.createVirtScreen(); backend.createVirtScreen();
} else { } else {
backend.deleteVirtScreen(); backend.deleteVirtScreen();
} }
}, 200); }, 200);
} }
Component.onCompleted: { Component.onCompleted: {
backend.virtScreenChanged.connect(function(created) { backend.onVirtScreenCreatedChanged.connect(function(created) {
busyDialog.close(); busyDialog.close();
virtScreenButton.enabled = true; virtScreenButton.enabled = true;
if (created) { if (created) {
@ -237,7 +237,9 @@ ApplicationWindow {
} }
Button { Button {
id: vncButton
text: "Start VNC Server" text: "Start VNC Server"
enabled: false
Layout.fillWidth: true Layout.fillWidth: true
// Material.background: Material.Teal // Material.background: Material.Teal
// Material.foreground: Material.Grey // Material.foreground: Material.Grey
@ -248,6 +250,23 @@ ApplicationWindow {
backend.stopVNC() backend.stopVNC()
} }
} }
Component.onCompleted: {
backend.onVncStateChanged.connect(function(state) {
if (state == "Off") {
vncButton.text = "Start VNC Server";
} else {
vncButton.text = "Stop VNC Server";
}
});
backend.onVirtScreenCreatedChanged.connect(function(created) {
if (created) {
vncButton.enabled = true;
} else {
vncButton.enabled = false;
}
});
}
} }
} }
} }
@ -256,6 +275,7 @@ ApplicationWindow {
RowLayout { RowLayout {
anchors.margins: spacing anchors.margins: spacing
Label { Label {
id: vncStateLabel
text: backend.vncState text: backend.vncState
} }
Item { Layout.fillWidth: true } Item { Layout.fillWidth: true }
@ -265,6 +285,12 @@ ApplicationWindow {
checked: true checked: true
} }
} }
Component.onCompleted: {
backend.onVncStateChanged.connect(function(state) {
vncStateLabel.text = state;
});
}
} }
// Sytray Icon // Sytray Icon
@ -293,9 +319,9 @@ ApplicationWindow {
var y_mid = height / 2; var y_mid = height / 2;
window.x = (backend.cursor_x > x_mid)? width - window.width : 0; window.x = (backend.cursor_x > x_mid)? width - window.width : 0;
window.y = (backend.cursor_y > y_mid)? height - window.height : 0; window.y = (backend.cursor_y > y_mid)? height - window.height : 0;
window.show() window.show();
window.raise() window.raise();
window.requestActivate() window.requestActivate();
} }
menu: Labs.Menu { menu: Labs.Menu {

View file

@ -229,8 +229,8 @@ class VNCState(Enum):
class Backend(QObject): class Backend(QObject):
""" Backend class for QML frontend """ """ Backend class for QML frontend """
# Signals # Signals
virtScreenChanged = pyqtSignal(bool) onVirtScreenCreatedChanged = pyqtSignal(bool)
vncStateChanged = pyqtSignal(str) onVncStateChanged = pyqtSignal(str)
def __init__(self, parent=None): def __init__(self, parent=None):
super(Backend, self).__init__(parent) super(Backend, self).__init__(parent)
@ -281,13 +281,13 @@ class Backend(QObject):
def hidpi(self, hidpi): def hidpi(self, hidpi):
self._hidpi = hidpi self._hidpi = hidpi
@pyqtProperty(bool) @pyqtProperty(bool, notify=onVirtScreenCreatedChanged)
def virtScreenCreated(self, notify=virtScreenChanged): def virtScreenCreated(self):
return self._virtScreenCreated return self._virtScreenCreated
@virtScreenCreated.setter @virtScreenCreated.setter
def virtScreenCreated(self, value): def virtScreenCreated(self, value):
self._virtScreenCreated = value self._virtScreenCreated = value
self.virtScreenChanged.emit(value) self.onVirtScreenCreatedChanged.emit(value)
@pyqtProperty(int) @pyqtProperty(int)
def vncPort(self): def vncPort(self):
@ -302,15 +302,14 @@ class Backend(QObject):
@vncPassword.setter @vncPassword.setter
def vncPassword(self, vncPassword): def vncPassword(self, vncPassword):
self._vncPassword = vncPassword self._vncPassword = vncPassword
print(self._vncPassword)
@pyqtProperty(str) @pyqtProperty(str, notify=onVncStateChanged)
def vncState(self, notify=vncStateChanged): def vncState(self):
return self._vncState.value return self._vncState.value
@vncState.setter @vncState.setter
def vncState(self, state): def vncState(self, state):
self._vncState = state self._vncState = state
self.vncStateChanged.emit(self._vncState.value) self.onVncStateChanged.emit(self._vncState.value)
@pyqtProperty(int) @pyqtProperty(int)
def cursor_x(self): def cursor_x(self):
@ -361,15 +360,17 @@ class Backend(QObject):
if not self.virtScreenCreated: if not self.virtScreenCreated:
print("Virtual Screen not crated.") print("Virtual Screen not crated.")
return return
# regex used in callbacks
re_connection = re.compile(r"^.*Got connection from client.*$", re.M)
# define callbacks # define callbacks
def _onConnected(): def _onConnected():
print("VNC started.") print("VNC started.")
self.vncState = VNCState.WAITING self.vncState = VNCState.WAITING
def _onReceived(data): def _onReceived(data):
data = data.decode("utf-8") data = data.decode("utf-8")
for line in data.splitlines(): if (self._vncState is not VNCState.CONNECTED) and re_connection.search(data):
# TODO: Update state of the server print("VNC connected.")
pass self.vncState = VNCState.CONNECTED
def _onEnded(exitCode): def _onEnded(exitCode):
print("VNC Exited.") print("VNC Exited.")
self.vncState = VNCState.OFF self.vncState = VNCState.OFF
@ -402,7 +403,7 @@ class Backend(QObject):
# Usually called from atexit(). # Usually called from atexit().
self.vncServer.kill() 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 (VNCState.WAITING.value, VNCState.CONNECTED.value): if self._vncState in (VNCState.WAITING, VNCState.CONNECTED):
self.vncServer.kill() self.vncServer.kill()
else: else:
print("stopVNC called while it is not running") print("stopVNC called while it is not running")