forked from jacklam718/myDesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyDesktopServer.py
executable file
·193 lines (161 loc) · 5.87 KB
/
myDesktopServer.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
192
193
#!/usr/bin/python
from twisted.internet.protocol import Factory, Protocol
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from getIPAddr import getIP
import qt4reactor
import myDesktopServerProtocol as serverProtocol
import sys
import os
import input_event as input
app = QApplication(sys.argv)
qt4reactor.install( )
class rdcProtocol(serverProtocol.RDCServerProtocol):
"""
this class is inheritance from RDCServerProtocol,
the class be responsible for achieve some of functions
include (
making screen pixel,
execute:
mouse event,
keyboard event,
copy text,
send cut text ) etc...
"""
def __init__(self):
serverProtocol.RDCServerProtocol.__init__(self)
self._array = QByteArray( )
self._buffer = QBuffer(self._array)
self._buffer.open(QIODevice.WriteOnly)
self._clipboard = QApplication.clipboard( )
self._maxWidth = QApplication.desktop( ).size( ).width( )
self._maxHeight = QApplication.desktop( ).size( ).height( )
self.keyboard = input.Keyboard( )
self.mouse = input.Mouse( )
def handleKeyEvent(self, key, flag=None):
'''
if flag == 6:
self.keyboard.press(key)
else flag == 7:
self.keyboard.release(key)
'''
self.keyboard.press(key)
self.keyboard.release(key)
def handleMouseEvent(self, x, y, buttonmask=0, flag=None):
print(x, y, buttonmask, flag)
if flag == 5: # move mouse event
self.mouse.move(x, y)
elif flag == 2: # mouse button down
self.mouse.press(x, y, buttonmask)
elif flag == 3: # mouse button up
self.mouse.release(x, y, buttonmask)
elif flag == 4: # mouse button duble clicked
self.mouse.press(x, y, buttonmask)
self.mouse.release(x, y, buttonmask)
def handleClientCopyText(self, text):
"""
copy text from client, and then set the text in clipboard
"""
self._clipboard.setText(text)
def cutTextToClient(self):
"""
cut text to client
"""
text = self._clipboard.text( )
self.sendCutTextToClient(text)
def _makeFramebuffer(self, width, height):
pix = QPixmap.grabWindow(QApplication.desktop( ).winId( ))
pix = pix.scaled(width, height)
if width >= self._maxWidth or height >= self._maxHeight:
width = self._maxWidth
height = self._maxHeight
pix.save(self._buffer, 'jpeg')
pixData = self._buffer.data( )
self._array.clear( )
self._buffer.close( )
return "%s" % pixData
class RDCFactory(serverProtocol.RDCFactory):
def __init__(self, password=None):
serverProtocol.RDCFactory.__init__(self, password)
self.protocol = rdcProtocol
def buildProtocol(self, addr):
return serverProtocol.RDCFactory.buildProtocol(self, addr)
def readyConnection(self, server):
self.server = server
#-----------------------#
## myDesktopServer GUI ##
#-----------------------#
class RDCServerGUI(QDialog):
"""
The PyRDCServerGUI responsible provide GUI interface, operate
the PyRDCServer.
"""
def __init__(self, reactor, parent=None):
super(RDCServerGUI, self).__init__(parent)
self.setupUI( )
mainLayout = QGridLayout( )
mainLayout.addWidget(self.groupbox, 0, 0)
mainLayout.addWidget(self.hostLab, 1, 0)
mainLayout.addLayout(self.butLayout, 2, 0)
mainLayout.setMargin(10)
self.setLayout(mainLayout)
self.reactor = reactor
self.running = False
QObject.connect(self.startStopBut, SIGNAL('clicked( )'), self.onStartStop)
QObject.connect(self.quitBut, SIGNAL('clicked( )'), self.quit)
def setupUI(self):
#self.resize(300, 200)
self.setFixedSize(300, 200)
self.setWindowTitle('PyRDCServer')
# Setting style
QApplication.setStyle(QStyleFactory.create('cleanlooks'))
QApplication.setPalette(QApplication.style().standardPalette())
self.setStyleSheet(open(os.path.dirname(__file__) + '/styleSheet.qss', 'r').read( ))
# Label
self.hostLab = QLabel('')
self.groupbox = QGroupBox( )
formLayout = QFormLayout( )
# LineEdit
self.portEdit = QLineEdit( )
self.portEdit.setText('5000')
self.addrEdit = QLineEdit( )
self.addrEdit.setText( getIP())
self.addrEdit.setEnabled(False)
self.passwdEdit = QLineEdit( )
formLayout.addRow(QLabel('Address'), self.addrEdit)
formLayout.addRow(QLabel('Port'), self.portEdit)
formLayout.addRow(QLabel('Password'), self.passwdEdit)
self.groupbox.setLayout(formLayout)
# Create Button
self.butLayout = QHBoxLayout( )
self.startStopBut = QPushButton('Start')
self.quitBut = QPushButton('Quit')
self.butLayout.addWidget(self.startStopBut)
self.butLayout.addWidget(self.quitBut)
def onStartStop(self):
if not self.running:
self._start( )
else:
self._stop( )
def _start(self):
port = int(self.portEdit.text( ))
pwd = str(self.passwdEdit.text( ))
self.startStopBut.setText('Close')
self.reactor.listenTCP(port, RDCFactory(password=pwd))
self.running = True
def _stop(self):
self.startStopBut.setText('Start')
self.reactor.stop( )
self.running = False
def quit(self):
# call reactor of stop method
self.reactor.stop( )
# call QDialog of method to close the gui window
self.close( )
def closeEvent(self, event):
self.quit( )
if __name__ == '__main__':
from twisted.internet import reactor
rdcServerGUI = RDCServerGUI(reactor)
rdcServerGUI.show( )
reactor.run( )