forked from jacklam718/myDesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyDesktopViewer.py
executable file
·191 lines (156 loc) · 6.59 KB
/
myDesktopViewer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/python
from twisted.internet.protocol import Protocol, Factory, ClientFactory
from twisted.python import log
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import myDesktopClientProtocol as clientProtocol
import qt4reactor
import sys
import os
log.startLogging(sys.stdout)
app = QApplication(sys.argv)
__applib__ = os.path.dirname(os.path.realpath(__file__))
__appicon__ = os.path.dirname(os.path.realpath(__file__))
qt4reactor.install( )
class RDCToGUI(clientProtocol.rdc):
def __init__(self):
clientProtocol.rdc.__init__(self)
self.num = 0
self.count = 0
def connectionMade(self):
self.factory.readyConnection(self)
def vncRequestPassword(self):
password = self.factory.password
if not password:
password = inputbox( )
self.sendPassword(password)
def commitFramebufferUpdate(self, framebuffer):
self.factory.display.updateFramebuffer(framebuffer)
self.framebufferUpdateRequest(width=self.factory.display.width, height=self.factory.display.height)
class RDCFactory(clientProtocol.RDCFactory):
def __init__(self, display=None, password=None, shared=0):
clientProtocol.RDCFactory.__init__(self, password, shared)
self.display = display
self.protocol = RDCToGUI
def buildProtocol(self, addr):
return clientProtocol.RDCFactory.buildProtocol(self, addr)
def readyConnection(self, client):
self.display.readyDisplay(client)
def clientConnectionFailed(self, connector, reason):
log.msg("Client connection failed!. (%s)" % reason.getErrorMessage( ))
reactor.stop( )
def clientConnectionLost(self, connector, reason):
log.msg("Client connection lost!. (%s)" % reason.getErrorMessage( ))
reactor.stop( )
class Display(QWidget):
"""
this class for display remoteframebuffer and get the client events
and then send the events to server, the include keyEvent, pointerEvent,
mouseMoveEvent, clipboardEvent.
"""
def __init__(self, parent=None):
super(Display, self).__init__(parent)
self.resize(1390, 780)
self._pixelmap = QPixmap( )
self._remoteframebuffer = ""
self._clipboard = QApplication.clipboard( )
self.setMouseTracking(True)
self.setFocusPolicy(Qt.StrongFocus)
self.clientProtocol = None
self.parent = parent
#-------------------------------------#
## Use QLabel or QPainter to display ##
#-------------------------------------#
#self.viewPort = QLabel(self)
#self.viewPort.setMaximumSize(self.size())
#self.viewPort.setMinimumSize(self.size())
def readyDisplay(self, protocol):
self.clientProtocol = protocol
def paintEvent(self, event):
"""
paint frame buffer in widget
"""
if self._remoteframebuffer:
self._pixelmap.loadFromData(self._remoteframebuffer)
painter = QPainter(self)
painter.drawPixmap(0, 0, self._pixelmap)
#painter.drawPixmap(0, 0, self._pixelmap.scaled(self.size( ), Qt.IgnoreAspectRatio))
self.update( )
def updateFramebuffer(self, pixelmap):
self._remoteframebuffer = pixelmap
#self._pixelmap.loadFromData(pixelmap)
#self.viewPort.setPixmap(self._pixelmap)
#self.update( )
def keyPressEvent(self, event):
key = event.key( )
print(key)
flag = event.type( )
if self.clientProtocol is None: return
self.clientProtocol.keyEvent(key, flag)
self.update( )
def mousePressEvent(self, event):
x, y = (event.pos( ).x( ), event.pos( ).y( ))
button = event.button( )
print(button)
flag = event.type( )
if self.clientProtocol is None: return #self.clientProtocol = self.parent.client.clientProto
self.clientProtocol.pointerEvent(x, y, button, flag)
print(self.clientProtocol.pointerEvent)
def mouseReleaseEvent(self, event):
x, y = (event.pos( ).x( ), event.pos( ).y( ))
button = event.button( )
flag = event.type( )
if self.clientProtocol is None: return #self.clientProtocol = self.parent.client.clientProto
self.clientProtocol.pointerEvent(x, y, button, flag)
def mouseMoveEvent(self, event):
x, y = (event.pos( ).x( ), event.pos( ).y( ))
button = event.button( )
flag = event.type( )
if self.clientProtocol is None: return #self.clientProtocol = self.parent.client.clientProto
self.clientProtocol.pointerEvent(x, y, button, flag)
def resizeEvent(self, event):
"""
the remote framebuffer's size is according the client viewer size
this may reduce the size of the images can be
"""
size = event.size( )
self.width, self.height = (size.width(), size.height())
class myDesktopViewer(QMainWindow):
def __init__(self, parent=None):
super(myDesktopViewer, self).__init__(parent)
self.display = Display(self)
self.setupUI( )
def setupUI(self):
self.setWindowTitle('myDesktop (viewer)')
self.resize(800, 600)
QApplication.setStyle(QStyleFactory.create('cleanlooks'))
QApplication.setPalette(QApplication.style( ).standardPalette())
# add adction on application
self.startAction = QAction(QIcon(os.path.join(__appicon__, 'icons', 'Start.png')), 'Start', self)
self.stopAction = QAction(QIcon(os.path.join(__appicon__, 'icons', 'Stop.png')), 'Stop', self)
self.startAction.setToolTip('Start connection')
self.stopAction.setToolTip('Stop connection')
self.startAction.triggered.connect(self.connectionStart)
self.stopAction.triggered.connect(self.connectionStop)
# add a toolbar
self.toolbar = self.addToolBar('')
self.toolbar.addAction(self.stopAction)
self.toolbar.addAction(self.startAction)
displayWidget = QWidget( )
vbox = QVBoxLayout(displayWidget)
vbox.addWidget(self.display)
vbox.setMargin(0)
self.setCentralWidget(displayWidget)
def connectionStart(self):
self.client = RDCFactory(display=self.display, password='1234')
reactor.connectTCP('192.168.1.103', 5000, self.client)
def connectionStop(self):
reactor.stop( )
def closeEvent(self, event):
self.connectionStop( )
exit( )
if __name__ == '__main__':
from twisted.internet import reactor
mydesktop = myDesktopViewer( )
mydesktop.show( )
reactor.run( ) # enter mainloop