20 #include "../../shared/util.h"
24 #include <netinet/tcp.h>
36 , _host(std::move(host))
38 , _passwd(std::move(passwd))
48 qDebug(
"VNC worker destructor called.");
49 Q_ASSERT(
_run ==
false);
73 qDebug(
"[%s] VNC client started.", metaObject()->className());
74 qDebug(
"[%s] Host: '%s' Port: %i Passwd: '%s' Quality: %i", metaObject()->className(), qPrintable(
_host),
_port,
78 for (
int retry = 0; retry < 5 &&
_run; ++retry) {
82 _client = rfbGetClient(8, 3, 4);
84 _client->canHandleNewFBSize =
true;
86 _client->serverHost = strdup(
_host.toUtf8().constData());
94 rfbClientSetClientData(
_client,
nullptr,
this);
97 if (rfbInitClient(
_client,
nullptr,
nullptr)) {
108 qDebug(
"[%s] Connection successful!", metaObject()->className());
110 setsockopt(
_client->sock, SOL_TCP, TCP_NODELAY, &one,
sizeof(one));
112 setsockopt(
_client->sock, SOL_TCP, TCP_QUICKACK, &one,
sizeof(one));
119 const int i = WaitForMessage(
_client, 100 * 1000);
122 if (i > 0 && !HandleRFBServerMessage(
_client))
131 qDebug(
"[%s] VNC client stopped.", metaObject()->className());
143 return QString(
_client->desktopName);
177 auto* t =
reinterpret_cast<VncThread*
>(rfbClientGetClientData(client,
nullptr));
178 return strdup(t->_passwd.toUtf8());
190 auto *t =
reinterpret_cast<VncThread*
>(rfbClientGetClientData(client,
nullptr));
191 const int width = client->width, height = client->height, depth = 32;
192 const int size = width * height * (depth / 8);
193 qDebug(
"[%s] Remote desktop: %ix%ix%i", t->metaObject()->className(), width, height, depth);
194 if (width < 0 || height < 0 || size < 0) {
195 qWarning() <<
"IGNORING INVALID FRAMEBUFFER SIZE";
196 client->frameBuffer =
nullptr;
200 t->
_img = QSharedPointer<QImage>(
new QImage(width, height, QImage::Format_RGB32));
201 if (size > t->_img->sizeInBytes()) {
202 qDebug() <<
"Fatal: Created image too small:" << t->_img->sizeInBytes() <<
"<" << size;
205 client->frameBuffer = t->_img->bits();
206 memset(client->frameBuffer,
'\0',
size_t(size));
207 client->format.trueColour = 1;
208 client->format.bitsPerPixel = depth;
209 client->format.redShift = 16;
210 client->format.greenShift = 8;
211 client->format.blueShift = 0;
212 client->format.redMax = 0xff;
213 client->format.greenMax = 0xff;
214 client->format.blueMax = 0xff;
216 const int quality = t->_quality;
219 client->appData.useBGR233 = 0;
220 client->appData.encodingsString =
"copyrect zlib hextile raw";
221 client->appData.compressLevel = 4;
222 client->appData.qualityLevel = 9;
223 client->appData.scaleSetting = 0;
226 client->appData.useBGR233 = 0;
227 client->appData.encodingsString =
"copyrect tight zrle ultra zlib hextile corre rre raw";
228 client->appData.compressLevel = 7;
229 client->appData.qualityLevel = 8;
230 client->appData.scaleSetting = 0;
234 client->appData.useBGR233 = 1;
235 client->appData.encodingsString =
"copyrect tight zrle ultra zlib hextile corre rre raw";
236 client->appData.compressLevel = 9;
237 client->appData.qualityLevel = 1;
238 client->appData.scaleSetting = 0;
241 SetFormatAndEncodings(client);
243 SendFramebufferUpdateRequest(client, 0, 0, width, height,
false);
265 auto* t = (
VncThread*)rfbClientGetClientData(client, 0);
266 t->processImageUpdate(x, y, w, h);
VncThread(QString host, int port, QString passwd, int quality)
Initialize this VNC client connection worker thread.
void run() override
Worker thread's mainloop, connecting to the VNC server and handling the connection.
QString getDesktopName() const
Get name of the VNC server's desktop.
void processImageUpdate(int x, int y, int w, int h)
Handle update of an area of the VNC framebuffer.
VncThread - communicate with VNC server, scale image if necessary.
static char * passwdHandler(rfbClient *client)
Callback for the vnc client lib: The VNC server is requesting a password.
void imageUpdated(const int x, const int y, const int w, const int h)
QSharedPointer< QImage > _img
static rfbBool frameBufferHandler(rfbClient *client)
Callback for the vnc client lib: The size of the remote screen has been changed, so we're supposed to...
static void updateImage(rfbClient *client, int x, int y, int w, int h)
Callback for the vnc client lib: A part of the frame buffer has changed.