Rabbit Remote Control 0.0.33
Loading...
Searching...
No Matches
ConnectLibVNCServer.cpp
1
2#include <QApplication>
3#include <QImage>
4#include <QClipboard>
5#include <QNetworkProxy>
6#include <QInputDialog>
7#include <QLoggingCategory>
8
9#include "ConnectLibVNCServer.h"
10
11static Q_LOGGING_CATEGORY(log, "LibVNCServer.Connect")
12static Q_LOGGING_CATEGORY(logger, "LibVNCServer.Connect.log")
13static Q_LOGGING_CATEGORY(logKey, "LibVNCServer.Connect.Key")
14static Q_LOGGING_CATEGORY(logMouse, "LibVNCServer.Connect.Mouse")
15
16const char* gThis = "This pointer";
17#define LOG_BUFFER_LENGTH 1024
18static void rfbQtClientLog(const char *format, ...)
19{
20 int nRet = 0;
21 va_list args;
22 char buf[LOG_BUFFER_LENGTH];
23
24 if(!rfbEnableClientLogging)
25 return;
26
27 va_start(args, format);
28 nRet = vsnprintf(buf, LOG_BUFFER_LENGTH, format, args);
29 va_end(args);
30 if(nRet < 0)
31 {
32 qCritical(log) << "vsnprintf fail";
33 return;
34 }
35 if(nRet >= LOG_BUFFER_LENGTH)
36 {
37 qCritical(log) << "vsprintf buf is short,"
38 << nRet << ">" << LOG_BUFFER_LENGTH
39 << ". Truncated it:" << nRet - LOG_BUFFER_LENGTH;
40 buf[LOG_BUFFER_LENGTH - 1] = 0;
41 }
42 qDebug(logger) << buf;
43}
44
45CConnectLibVNCServer::CConnectLibVNCServer(CConnecterLibVNCServer *pConnecter)
46 : CConnectDesktop(pConnecter),
47 m_pClient(nullptr),
48 m_pParameter(dynamic_cast<CParameterLibVNCServer*>(pConnecter->GetParameter()))
49#ifdef HAVE_LIBSSH
50 ,m_pThread(nullptr)
51#endif
52{
53 rfbClientLog = rfbQtClientLog;
54
55 if(!m_pParameter->GetLocalCursor())
56 {
57 emit sigUpdateCursor(QCursor(Qt::BlankCursor));
58 }
59}
60
61CConnectLibVNCServer::~CConnectLibVNCServer()
62{
63 qDebug(log) << "CConnectLibVNCServer::~CConnectLibVNCServer()";
64}
65
66/*
67 * \return
68 * \li OnInitReturnValue::Fail: error
69 * \li OnInitReturnValue::UseOnProcess: Use OnProcess (non-Qt event loop)
70 * \li OnInitReturnValue::NotUseOnProcess: Don't use OnProcess (qt event loop)
71 */
72CConnect::OnInitReturnValue CConnectLibVNCServer::OnInit()
73{
74 qDebug(log) << "CConnectLibVNCServer::OnInit()";
75 if(m_pClient) Q_ASSERT(false);
76
77 m_pClient = rfbGetClient(8, 3, 4);
78 if(!m_pClient)
79 {
80 QString szErr;
81 szErr = tr("Protocol version error");
82 emit sigShowMessageBox(tr("Error"), szErr, QMessageBox::Critical);
83 qCritical(log) << "rfbGetClient fail";
84 return OnInitReturnValue::Fail;
85 }
86
87 // Set parameters
88 m_pClient->programName = strdup(qApp->applicationName().toStdString().c_str());
89 m_pClient->appData.shareDesktop = m_pParameter->GetShared();
90 m_pClient->appData.viewOnly = m_pParameter->GetOnlyView();
91 m_pClient->appData.useRemoteCursor = m_pParameter->GetLocalCursor();
92
93 //Qt is support QImage::Format_RGB32, so we use default format QImage::Format_RGB32 in OnSize()
94 // m_pClient->appData.requestedDepth = m_pPara->nColorLevel;
95 // m_pClient->format.depth = m_pPara->nColorLevel;
96 // switch (m_pClient->format.depth) {
97 // case 8:
98 // m_pClient->format.depth = 8;
99 // m_pClient->format.bitsPerPixel = 8;
100 // m_pClient->format.blueMax = 3;
101 // m_pClient->format.blueShift = 6;
102 // m_pClient->format.greenMax = 7;
103 // m_pClient->format.greenShift = 3;
104 // m_pClient->format.redMax = 7;
105 // m_pClient->format.redShift = 0;
106 // break;
107 // case 16:
108 // m_pClient->format.depth = 15;
109 // m_pClient->format.bitsPerPixel = 16;
110 // m_pClient->format.redShift = 11;
111 // m_pClient->format.greenShift = 6;
112 // m_pClient->format.blueShift = 1;
113 // m_pClient->format.redMax = 31;
114 // m_pClient->format.greenMax = 31;
115 // m_pClient->format.blueMax = 31;
116 // break;
117 // case 32:
118 // default:
119 // m_pClient->format.depth = 24;
120 // m_pClient->format.bitsPerPixel = 32;
121 // m_pClient->format.blueShift = 0;
122 // m_pClient->format.redShift = 16;
123 // m_pClient->format.greenShift = 8;
124 // m_pClient->format.blueMax = 0xff;
125 // m_pClient->format.redMax = 0xff;
126 // m_pClient->format.greenMax = 0xff;
127 // break;
128 // }
129
130 m_pClient->appData.enableJPEG = m_pParameter->GetJpeg();
131 if(m_pClient->appData.enableJPEG)
132 m_pClient->appData.qualityLevel = m_pParameter->GetQualityLevel();
133 if(m_pParameter->GetEnableCompressLevel())
134 m_pClient->appData.compressLevel = m_pParameter->GetCompressLevel();
135
136 // Set callback function
137 m_pClient->MallocFrameBuffer = cb_resize;
138 m_pClient->GotFrameBufferUpdate = cb_update;
139 m_pClient->HandleKeyboardLedState = cb_kbd_leds;
140 m_pClient->Bell = cb_bell;
141 m_pClient->HandleTextChat = cb_text_chat;
142 m_pClient->GotXCutText = cb_got_selection;
143 m_pClient->GetCredential = cb_get_credential;
144 m_pClient->GetPassword = cb_get_password;
145 m_pClient->HandleCursorPos = cb_cursor_pos;
146 m_pClient->GotCursorShape = cb_got_cursor_shape;
147
148 m_pClient->canHandleNewFBSize = TRUE;
149 rfbClientSetClientData(m_pClient, (void*)gThis, this);
150
151 // Set sock
152 switch(m_pParameter->m_Proxy.GetUsedType())
153 {
154 case CParameterProxy::TYPE::None:
155 {
156 // Set server ip and port
157 if(m_pParameter->m_Net.GetHost().isEmpty())
158 {
159 QString szErr;
160 szErr = tr("The server is empty, please input it");
161 qCritical(log) << szErr;
162 emit sigShowMessageBox(tr("Error"), szErr, QMessageBox::Critical);
163 return OnInitReturnValue::Fail;
164 }
165 auto &net = m_pParameter->m_Net;
166 m_pClient->serverHost = strdup(net.GetHost().toStdString().c_str());
167 m_pClient->serverPort = net.GetPort();
168
169 if(!rfbInitClient(m_pClient, nullptr, nullptr))
170 {
171 QString szErr;
172 szErr = tr("Connect to %1:%2 fail").arg(m_pParameter->m_Net.GetHost(),
173 QString::number(m_pParameter->m_Net.GetPort()));
174 qCritical(log) << szErr;
175 emit sigShowMessageBox(tr("Error"), szErr, QMessageBox::Critical);
176 return OnInitReturnValue::Fail;
177 }
178
179 QString szInfo = QString("Connect to ") + m_pClient->desktopName;
180 qInfo(log) << szInfo;
181
182 emit sigSetDesktopSize(m_pClient->width, m_pClient->height);
183 emit sigServerName(m_pClient->desktopName);
184 emit sigInformation(szInfo);
185 emit sigConnected();
186 return OnInitReturnValue::UseOnProcess;
187 }
188 /*case CParameterProxy::TYPE::SockesV5:
189 {
190 QNetworkProxy proxy;
191 proxy.setType(QNetworkProxy::Socks5Proxy);
192 auto &net = m_pParameter->m_Proxy.m_SockesV5;
193 proxy.setHostName(net.GetHost());
194 if(net.GetHost().isEmpty())
195 {
196 QString szErr;
197 szErr = tr("The proxy server is empty, please input it");
198 qCritical(log) << szErr;
199 emit sigShowMessageBox(tr("Error"), szErr, QMessageBox::Critical);
200 return OnInitReturnValue::Fail;
201 }
202 proxy.setPort(net.GetPort());
203 auto &user = net.m_User;
204 proxy.setUser(user.GetUser());
205 proxy.setPassword(user.GetPassword());
206 m_tcpSocket.setProxy(proxy);
207 m_tcpSocket.connectToHost(m_pClient->serverHost, m_pClient->serverPort);
208 if (!m_tcpSocket.waitForConnected(3000))
209 return OnInitReturnValue::Fail;
210 //TODO: The is fail
211 m_pClient->sock = m_tcpSocket.socketDescriptor();
212 if(!rfbInitClient(m_pClient, nullptr, nullptr))
213 {
214 QString szErr;
215 szErr = tr("Connect to %1:%2 fail").arg(m_pParameter->m_Net.GetHost(),
216 QString::number(m_pParameter->m_Net.GetPort()));
217 qCritical(log) << szErr;
218 emit sigShowMessageBox(tr("Error"), szErr, QMessageBox::Critical);
219 return OnInitReturnValue::Fail;
220 }
221 break;
222 }//*/
223 /*TODO: add UltraVncRepeater
224 case (CParameterConnecter::emProxy) CParameterLibVNCServer::emVncProxy::UltraVncRepeater:
225 m_pClient->destHost = strdup(m_pPara->GetProxyHost().toStdString().c_str());
226 m_pClient->destPort = m_pPara->GetProxyPort();//*/
227
229#ifdef HAVE_LIBSSH
230 case CParameterProxy::TYPE::SSHTunnel:
231 {
232 // Set SSH parameters
233 QSharedPointer<CParameterChannelSSH> parameter(new CParameterChannelSSH());
234 auto &ssh = m_pParameter->m_Proxy.m_SSH;
235 parameter->setServer(ssh.GetHost());
236 parameter->setPort(ssh.GetPort());
237 auto &user = ssh.m_User;
238 parameter->SetUser(user.GetUser());
239 parameter->SetUseSystemFile(user.GetUseSystemFile());
240 if(CParameterUser::TYPE::UserPassword == user.GetUsedType()) {
241 parameter->SetAuthenticationMethod(SSH_AUTH_METHOD_PASSWORD);
242 parameter->SetPassword(user.GetPassword());
243 }
244 if(CParameterUser::TYPE::PublicKey == user.GetUsedType()) {
245 parameter->SetAuthenticationMethod(SSH_AUTH_METHOD_PUBLICKEY);
246 parameter->SetPublicKeyFile(user.GetPublicKeyFile());
247 parameter->SetPrivateKeyFile(user.GetPrivateKeyFile());
248 parameter->SetPassphrase(user.GetPassphrase());
249 }
250 auto &net = m_pParameter->m_Net;
251 parameter->SetRemoteHost(net.GetHost());
252 parameter->SetRemotePort(net.GetPort());
253
254 // Start ssh thread
255 if(!m_pThread)
256 m_pThread = new CSSHTunnelThread(parameter);
257 if(!m_pThread)
258 return OnInitReturnValue::Fail;
259 bool check = connect(m_pThread, SIGNAL(sigServer(QString, quint16)),
260 this, SLOT(slotConnectProxyServer(QString, quint16)));
261 Q_ASSERT(check);
262#if defined(HAVE_UNIX_DOMAIN_SOCKET) && defined(Q_OS_UNIX)
263 check = connect(m_pThread, SIGNAL(sigServer(QString)),
264 this, SLOT(slotConnectProxyServer(QString)));
265 Q_ASSERT(check);
266#endif
267 check = connect(m_pThread, SIGNAL(sigError(int,QString)),
268 this, SIGNAL(sigError(int,QString)));
269 Q_ASSERT(check);
270 check = connect(m_pThread, SIGNAL(sigDisconnect()),
271 this, SIGNAL(sigDisconnect()));
272 Q_ASSERT(check);
273 m_pThread->start();
274
275 return OnInitReturnValue::UseOnProcess;
276 }
277#endif // #ifdef HAVE_LIBSSH
279 default:
280 break;
281 }
282
283 return OnInitReturnValue::UseOnProcess;
284}
285
287{
288 qDebug(log) << "CConnectLibVNCServer::OnClean()";
289#ifdef HAVE_LIBSSH
290 if(m_pThread)
291 {
292 m_pThread->Exit();
293 m_pThread = nullptr;
294 }
295#endif
296 if(m_pClient)
297 {
298 m_tcpSocket.close();
299 //m_pClient->sock = -1; //RFB_INVALID_SOCKET;
300 rfbClientCleanup(m_pClient);
301 m_pClient = nullptr;
302 }
303 return 0;
304}
305
320{
321 //LOG_MODEL_DEBUG("CConnectLibVNCServer", "CConnectLibVNCServer::Process()");
322 int nRet = 0;
323 if(!m_pClient)
324 {
325 Q_ASSERT(m_pClient);
326 return -1;
327 }
328
329 struct timeval timeout = {0, 50000};
330 fd_set set;
331 FD_ZERO(&set);
332 if(RFB_INVALID_SOCKET != m_pClient->sock)
333 {
334 FD_SET(m_pClient->sock, &set);
335 }
336 rfbSocket eventFd = m_Event.GetFd();
337 if(RFB_INVALID_SOCKET != eventFd)
338 {
339 FD_SET(eventFd, &set);
340 }
341 rfbSocket fd = RFB_INVALID_SOCKET;
342 fd = std::max(eventFd, m_pClient->sock);
343 //qDebug(log) << "fd:" << fd << "select m_pClient->sock:" << (int)m_pClient->sock << "event:" << eventFd;
344 nRet = select(fd + 1, &set, NULL, NULL, &timeout);
345 //qDebug(log) << "select end";
346 if (nRet < 0) {
347 qCritical(log) << "select fail:" << nRet << "errno" << errno << strerror(errno);
348 return nRet;
349 }
350
351 /*if(0 == nRet)
352 {
353 //qDebug(log) << "Time out";
354 return 0;
355 }//*/
356
357 if(RFB_INVALID_SOCKET != eventFd && FD_ISSET(eventFd, &set))
358 {
359 //qDebug(log) << "fires event";
360 m_Event.Reset();
361 }
362
363 if(RFB_INVALID_SOCKET != m_pClient->sock
364 && FD_ISSET(m_pClient->sock, &set)) {
365 //qDebug(log) << "HandleRFBServerMessage";
366 if(!HandleRFBServerMessage(m_pClient))
367 {
368 qCritical(log) << "HandleRFBServerMessage fail";
369 return -2;
370 }
371 }
372
373 return 0;
374}
375
377{
378 return m_Event.WakeUp();
379}
380
381void CConnectLibVNCServer::slotClipBoardChanged()
382{
383 if(!m_pParameter || !m_pParameter->GetClipboard() || !m_pClient) return;
384 QClipboard* pClipboard = QApplication::clipboard();
385 if(pClipboard)
386 {
387 QString szText = pClipboard->text();
388 if(!szText.isEmpty() && m_pClient)
389 SendClientCutText(m_pClient,
390 (char*)szText.toStdString().c_str(),
391 szText.toStdString().length());
392 }
393}
394
395rfbBool CConnectLibVNCServer::cb_resize(rfbClient* client)
396{
397 //qDebug(log) << "CConnectLibVnc::cb_resize:" << client->width << client->height;
398 CConnectLibVNCServer* pThis = (CConnectLibVNCServer*)rfbClientGetClientData(client, (void*)gThis);
399 if(pThis->OnSize()) return FALSE;
400 return TRUE;
401}
402
403void CConnectLibVNCServer::cb_update(rfbClient *client, int x, int y, int w, int h)
404{
405 if(0 == w || 0 == h)
406 return;
407 //qDebug(LibVNCServer, "CConnectLibVnc::cb_update:(%d, %d, %d, %d)", x, y, w, h);
408 CConnectLibVNCServer* pThis = (CConnectLibVNCServer*)rfbClientGetClientData(client, (void*)gThis);
409 QRect rect(x, y, w, h);
410 QImage img = pThis->m_Image.copy(rect);
411 emit pThis->sigUpdateRect(rect, img);
412}
413
414void CConnectLibVNCServer::cb_got_selection(rfbClient *client, const char *text, int len)
415{
416 //qDebug(LibVNCServer, "CConnectLibVnc::cb_got_selection:%s", text);
417 CConnectLibVNCServer* pThis = (CConnectLibVNCServer*)rfbClientGetClientData(client, (void*)gThis);
418 if(!pThis->m_pParameter->GetClipboard()) return;
419 QClipboard* pClipboard = QApplication::clipboard();
420 if(pClipboard)
421 pClipboard->setText(text);
422}
423
424void CConnectLibVNCServer::cb_kbd_leds(rfbClient *client, int value, int pad)
425{
426 qDebug(log) << "CConnectLibVnc::cb_kbd_leds";
427}
428
429void CConnectLibVNCServer::cb_bell(struct _rfbClient *client)
430{
431 qApp->beep();
432}
433
434void CConnectLibVNCServer::cb_text_chat(rfbClient *client, int value, char *text)
435{
436 qDebug(log) << "CConnectLibVnc::cb_text_chat";
437}
438
439rfbCredential* CConnectLibVNCServer::cb_get_credential(rfbClient *cl, int credentialType)
440{
441 qDebug(log) <<"CConnectLibVnc::cb_get_credential";
442 CConnectLibVNCServer* pThis = (CConnectLibVNCServer*)rfbClientGetClientData(cl, (void*)gThis);
443 rfbCredential *c = (rfbCredential*)malloc(sizeof(rfbCredential));
444 c->userCredential.username = (char*)malloc(RFB_BUF_SIZE);
445 memset(c->userCredential.username, 0, RFB_BUF_SIZE);
446 c->userCredential.password = (char*)malloc(RFB_BUF_SIZE);
447 memset(c->userCredential.password, 0, RFB_BUF_SIZE);
448
449 if(credentialType != rfbCredentialTypeUser) {
450 qCritical(log) << "something else than username and password required for authentication";
451 return NULL;
452 }
453
454 qDebug(log) << "Username and password required for authentication!";
455
456 memcpy(c->userCredential.username,
457 pThis->m_pParameter->m_Net.m_User.GetUser().toStdString().c_str(),
458 pThis->m_pParameter->m_Net.m_User.GetUser().toStdString().length());
459 memcpy(c->userCredential.password,
460 pThis->m_pParameter->m_Net.m_User.GetPassword().toStdString().c_str(),
461 pThis->m_pParameter->m_Net.m_User.GetPassword().toStdString().length());
462
463 return c;
464}
465
466char* CConnectLibVNCServer::cb_get_password(rfbClient *client)
467{
468 //LOG_MODEL_ERROR("LibVNCServer", "CConnectLibVnc::cb_get_password");
469 CConnectLibVNCServer* pThis = (CConnectLibVNCServer*)rfbClientGetClientData(client, (void*)gThis);
470 QString szPassword = pThis->m_pParameter->m_Net.m_User.GetPassword();
471 if(szPassword.isEmpty())
472 {
473 int nRet = QDialog::Rejected;
474 emit pThis->sigBlockShowWidget("CDlgLibVNCServerPassword", nRet, pThis->m_pParameter);
475 if(QDialog::Accepted == nRet)
476 {
477 szPassword = pThis->m_pParameter->m_Net.m_User.GetPassword();
478 }
479 if(szPassword.isEmpty())
480 return nullptr;
481 }
482 return strdup(szPassword.toStdString().c_str());
483}
484
485int CConnectLibVNCServer::OnSize()
486{
487 int nRet = 0;
488 int nWidth = m_pClient->width;
489 int nHeight = m_pClient->height;
490
491 m_Image = QImage(nWidth, nHeight, QImage::Format_RGB32);
492
493 m_pClient->frameBuffer = m_Image.bits();
494
495 m_pClient->format.bitsPerPixel = 32;
496 m_pClient->format.depth = m_Image.depth();
497 m_pClient->format.redShift = 16;
498 m_pClient->format.greenShift = 8;
499 m_pClient->format.blueShift = 0;
500 m_pClient->format.redMax = 255;
501 m_pClient->format.greenMax = 255;
502 m_pClient->format.blueMax = 255;
503 SetFormatAndEncodings(m_pClient);
504
505 emit sigSetDesktopSize(m_pClient->width, m_pClient->height);
506 return nRet;
507}
508
509rfbBool CConnectLibVNCServer::cb_cursor_pos(rfbClient *client, int x, int y)
510{
511 //qDebug(LibVNCServer, "CConnectLibVnc::cb_cursor_pos:%d,%d", x, y);
512 rfbBool bRet = true;
513
514 return bRet;
515}
516
517void CConnectLibVNCServer::cb_got_cursor_shape(rfbClient *client,
518 int xhot, int yhot,
519 int width, int height,
520 int bytesPerPixel)
521{
522 /*
523 qDebug(LibVNCServer, "CConnectLibVnc::cb_got_cursor_shape:x:%d, y:%d, width:%d, height:%d, bytesPerPixel:%d",
524 xhot, yhot, width, height, bytesPerPixel);//*/
525 if(!client->rcSource)
526 {
527 qCritical(log) << "client->rcSource is null";
528 return;
529 }
530 CConnectLibVNCServer* pThis = (CConnectLibVNCServer*)rfbClientGetClientData(client, (void*)gThis);
531 if ((width == 0) || (height == 0)) {
532 QImage cursor(1, 1, QImage::Format_ARGB32);
533 uchar* buffer = cursor.bits();
534 memset(buffer, 0, 4);
535 emit pThis->sigUpdateCursor(QCursor(QPixmap::fromImage(cursor), xhot, yhot));
536 } else {
537 switch (bytesPerPixel) {
538 case 4:
539 {
540 QImage cursor(width, height, QImage::Format_ARGB32);
541 uchar *buffer = cursor.bits();
542 memcpy(buffer, client->rcSource, width * height * 4);
543 uchar* pMask = client->rcMask;
544 if(pMask)
545 {
546 uchar* pDest = buffer;
547 for(int y = 0; y < height; y++)
548 {
549 for(int w = 0; w < width; w++)
550 {
551 if(*pMask++)
552 {
553 // NOTE: Do not manipulate the memory,
554 // because the host byte order may be different
555 QColor c = cursor.pixel(w, y);
556 c.setAlpha(0xff);
557 cursor.setPixelColor(w,y,c);
558 }
559 else
560 *((uint32_t*)pDest) = 0;
561
562 pDest += 4;
563 }
564 }
565 }
566 emit pThis->sigUpdateCursor(QCursor(QPixmap::fromImage(cursor), xhot, yhot));
567 break;
568 }
569 default:
570 break;
571 }
572 }
573}
574
575void CConnectLibVNCServer::mousePressEvent(QMouseEvent *event)
576{
577 if(!m_pClient) return;
578 if(m_pParameter && m_pParameter->GetOnlyView()) return;
579
580 unsigned char mask = 0;
581 if(event->button() & Qt::MouseButton::LeftButton)
582 mask |= 0x1;
583 if(event->button() & Qt::MouseButton::MiddleButton)
584 mask |= 0x2;
585 if(event->button() & Qt::MouseButton::RightButton)
586 mask |= 0x4;
587 if(event->button() & Qt::MouseButton::BackButton)
588 mask |= 0x80;
589 QPoint pos = event->pos();
590
591 qDebug(logMouse) << Q_FUNC_INFO << event->buttons() << event->button() << pos << mask;
592
593 SendPointerEvent(m_pClient, pos.x(), pos.y(), mask);
594}
595
596void CConnectLibVNCServer::mouseReleaseEvent(QMouseEvent *event)
597{
598 if(!m_pClient) return;
599 if(m_pParameter && m_pParameter->GetOnlyView()) return;
600 int mask = 0;
601 QPoint pos = event->pos();
602 qDebug(logMouse) << Q_FUNC_INFO << event->buttons() << event->button() << pos << mask;
603 SendPointerEvent(m_pClient, pos.x(), pos.y(), mask);
604}
605
606void CConnectLibVNCServer::mouseMoveEvent(QMouseEvent *event)
607{
608 if(!m_pClient) return;
609 if(m_pParameter && m_pParameter->GetOnlyView()) return;
610 int mask = 0;
611 if(event->buttons() & Qt::MouseButton::LeftButton)
612 mask |= 0x1;
613 if(event->buttons() & Qt::MouseButton::MiddleButton)
614 mask |= 0x2;
615 if(event->buttons() & Qt::MouseButton::RightButton)
616 mask |= 0x4;
617 if(event->buttons() & Qt::MouseButton::BackButton)
618 mask |= 0x80;
619 QPoint pos = event->pos();
620 qDebug(logMouse) << Q_FUNC_INFO << event->buttons() << event->button() << pos << mask;
621 SendPointerEvent(m_pClient, pos.x(), pos.y(), mask);
622}
623
624// https://github.com/rfbproto/rfbproto/blob/master/rfbproto.rst#pointerevent
625void CConnectLibVNCServer::wheelEvent(QWheelEvent *event)
626{
627 if(!m_pClient) return;
628 if(m_pParameter && m_pParameter->GetOnlyView()) return;
629 int mask = 0;
630 if(event->buttons() & Qt::MouseButton::LeftButton)
631 mask |= 0x1;
632 if(event->buttons() & Qt::MouseButton::MiddleButton)
633 mask |= 0x2;
634 if(event->buttons() & Qt::MouseButton::RightButton)
635 mask |= 0x4;
636 if(event->buttons() & Qt::MouseButton::BackButton)
637 mask |= 0x80;
638
639 QPoint d = event->angleDelta();
640 if(d.y() > 0)
641 mask |= 0x8;
642 if(d.y() < 0)
643 mask |= 0x10;
644 if(d.x() < 0)
645 mask |= 0x20;
646 if(d.x() > 0)
647 mask |= 0x40;
648
649 QPointF pos;
650#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
651 pos = event->position();
652#else
653 pos = event->pos();
654#endif
655
656 qDebug(logMouse) << Q_FUNC_INFO << event->buttons() << event->angleDelta() << pos;
657
658 SendPointerEvent(m_pClient, pos.x(), pos.y(), mask);
659}
660
668uint32_t TranslateRfbKey(quint32 inkey, bool modifier)
669{
670 quint32 k = 5000;
671
672 switch (inkey)
673 {
674 case Qt::Key_Backspace: k = XK_BackSpace; break;
675 case Qt::Key_Tab: k = XK_Tab; break;
676 case Qt::Key_Clear: k = XK_Clear; break;
677 case Qt::Key_Return: k = XK_Return; break;
678 case Qt::Key_Pause: k = XK_Pause; break;
679 case Qt::Key_Escape: k = XK_Escape; break;
680 case Qt::Key_Space: k = XK_space; break;
681 case Qt::Key_Delete: k = XK_Delete; break;
682 case Qt::Key_Period: k = XK_period; break;
683
684 /* International & multi-key character composition */
685 case Qt::Key_Multi_key: k = XK_Multi_key; break;
686 case Qt::Key_SingleCandidate: k = XK_SingleCandidate; break;
687 case Qt::Key_MultipleCandidate: k = XK_MultipleCandidate; break;
688 case Qt::Key_PreviousCandidate: k = XK_PreviousCandidate; break;
689
690 /* Japanese keyboard support */
691 case Qt::Key_Kanji: k = XK_Kanji; break;
692 case Qt::Key_Muhenkan: k = XK_Muhenkan; break;
693 case Qt::Key_Henkan: k = XK_Henkan; break;
694 case Qt::Key_Romaji: k = XK_Romaji; break;
695 case Qt::Key_Hiragana: k = XK_Hiragana; break;
696 case Qt::Key_Katakana: k = XK_Katakana; break;
697 case Qt::Key_Hiragana_Katakana: k = XK_Hiragana_Katakana;break;
698 case Qt::Key_Zenkaku: k = XK_Zenkaku; break;
699 case Qt::Key_Hankaku: k = XK_Hankaku; break;
700 case Qt::Key_Zenkaku_Hankaku: k = XK_Zenkaku_Hankaku; break;
701 case Qt::Key_Touroku: k = XK_Touroku; break;
702 case Qt::Key_Massyo: k = XK_Massyo; break;
703 case Qt::Key_Kana_Lock: k = XK_Kana_Lock; break;
704 case Qt::Key_Kana_Shift: k = XK_Kana_Shift; break;
705 case Qt::Key_Eisu_Shift: k = XK_Eisu_Shift; break;
706 case Qt::Key_Eisu_toggle: k = XK_Eisu_toggle; break;
707
708 //special keyboard char
709 case Qt::Key_Exclam: k = XK_exclam; break;
710 case Qt::Key_QuoteDbl: k = XK_quotedbl; break; //"
711 case Qt::Key_NumberSign: k = XK_numbersign; break; //#
712 case Qt::Key_Percent: k = XK_percent; break; //%
713 case Qt::Key_Dollar: k = XK_dollar; break; //$
714 case Qt::Key_Ampersand: k = XK_ampersand; break; //&
715 case Qt::Key_Apostrophe: k = XK_apostrophe; break;
716 case Qt::Key_ParenLeft: k = XK_parenleft; break; // (
717 case Qt::Key_ParenRight: k = XK_parenright; break; // )
718
719 case Qt::Key_Slash: k = XK_slash; break; // /
720 case Qt::Key_Asterisk: k = XK_asterisk; break; //*
721 case Qt::Key_Minus: k = XK_minus; break; //-
722 case Qt::Key_Plus: k = XK_plus; break; //+
723 case Qt::Key_Enter: k = XK_Return; break; //
724 case Qt::Key_Equal: k = XK_equal; break; //=
725 case Qt::Key_Comma: return XK_comma; //,
726
727 case Qt::Key_Colon: k = XK_colon;break; // :
728 case Qt::Key_Semicolon: k = XK_semicolon; break; //;
729 case Qt::Key_Less: k = XK_less; break; // <
730 case Qt::Key_Greater: k = XK_greater; break; // >
731 case Qt::Key_Question: k = XK_question; break; //?
732 case Qt::Key_At: k = XK_at; break; //@
733
734 case Qt::Key_BracketLeft: k = XK_bracketleft; break;
735 case Qt::Key_Backslash: k = XK_backslash;break;
736 case Qt::Key_BracketRight: k = XK_bracketright;break;
737 case Qt::Key_AsciiCircum: k = XK_asciicircum;break;
738 case Qt::Key_Underscore: k = XK_underscore;break;
739 case Qt::Key_QuoteLeft: k = XK_quoteleft;break;
740 case Qt::Key_BraceLeft: k = XK_braceleft;break;
741 case Qt::Key_Bar: k = XK_bar; break;
742 case Qt::Key_BraceRight: k = XK_braceright;break;
743 case Qt::Key_AsciiTilde: k = XK_asciitilde;break;
744 case Qt::Key_nobreakspace: k = XK_nobreakspace;break;
745 case Qt::Key_exclamdown: k = XK_exclamdown;break;
746 case Qt::Key_cent: k = XK_cent;break;
747 case Qt::Key_sterling: k = XK_sterling;break;
748 case Qt::Key_currency: k = XK_currency;break;
749 case Qt::Key_yen: k = XK_yen;break;
750 case Qt::Key_brokenbar: k = XK_brokenbar;break;
751 case Qt::Key_section: k = XK_section;break;
752 case Qt::Key_diaeresis: k = XK_diaeresis;break;
753 case Qt::Key_copyright: k = XK_copyright; break;
754 case Qt::Key_ordfeminine: k = XK_ordfeminine; break;
755 case Qt::Key_guillemotleft: k = XK_guillemotleft; break;
756 case Qt::Key_guillemotright: k = XK_guillemotright; break;
757 case Qt::Key_notsign: k = XK_notsign; break;
758 case Qt::Key_hyphen: k = XK_hyphen; break;
759 case Qt::Key_registered: k = XK_registered; break;
760
761 case Qt::Key_Up: k = XK_Up; break;
762 case Qt::Key_Down: k = XK_Down; break;
763 case Qt::Key_Right: k = XK_Right; break;
764 case Qt::Key_Left: k = XK_Left; break;
765 case Qt::Key_Insert: k = XK_Insert; break;
766 case Qt::Key_Home: k = XK_Home; break;
767 case Qt::Key_End: k = XK_End; break;
768 case Qt::Key_PageUp: k = XK_Page_Up; break;
769 case Qt::Key_PageDown: k = XK_Page_Down; break;
770 case Qt::Key_MediaPrevious: k = XK_Prior; break;
771 case Qt::Key_MediaNext: k = XK_Next; break;
772 case Qt::Key_MediaPlay: k = XK_Begin; break;
773
774 /* Misc Functions */
775 case Qt::Key_Select: k = XK_Select; break;
776 case Qt::Key_Printer: k = XK_Print; break;
777 case Qt::Key_Execute: k = XK_Execute; break;
778 case Qt::Key_Undo: k = XK_Undo; break;
779 case Qt::Key_Redo: k = XK_Redo; break;
780 case Qt::Key_Menu: k = XK_Menu;break;
781 case Qt::Key_Find: k = XK_Find; break;
782 case Qt::Key_Exit:
783 case Qt::Key_Cancel:
784 case Qt::Key_Stop:
785 k = XK_Cancel;
786 break;
787 case Qt::Key_Mode_switch: k = XK_Mode_switch; break;
788
789 case Qt::Key_F1: k = XK_F1; break;
790 case Qt::Key_F2: k = XK_F2; break;
791 case Qt::Key_F3: k = XK_F3; break;
792 case Qt::Key_F4: k = XK_F4; break;
793 case Qt::Key_F5: k = XK_F5; break;
794 case Qt::Key_F6: k = XK_F6; break;
795 case Qt::Key_F7: k = XK_F7; break;
796 case Qt::Key_F8: k = XK_F8; break;
797 case Qt::Key_F9: k = XK_F9; break;
798 case Qt::Key_F10: k = XK_F10; break;
799 case Qt::Key_F11: k = XK_F11; break;
800 case Qt::Key_F12: k = XK_F12; break;
801 case Qt::Key_F13: k = XK_F13; break;
802 case Qt::Key_F14: k = XK_F14; break;
803 case Qt::Key_F15: k = XK_F15; break;
804 case Qt::Key_F16: k = XK_F16; break;
805 case Qt::Key_F17: k = XK_F17; break;
806 case Qt::Key_F18: k = XK_F18; break;
807 case Qt::Key_F19: k = XK_F19; break;
808 case Qt::Key_F20: k = XK_F20; break;
809 case Qt::Key_F21: k = XK_F21; break;
810 case Qt::Key_F22: k = XK_F22; break;
811 case Qt::Key_F23: k = XK_F23; break;
812 case Qt::Key_F24: k = XK_F24; break;
813 case Qt::Key_F25: k = XK_F25; break;
814 case Qt::Key_F26: k = XK_F26; break;
815 case Qt::Key_F27: k = XK_F27; break;
816 case Qt::Key_F28: k = XK_F28; break;
817 case Qt::Key_F29: k = XK_F29; break;
818 case Qt::Key_F30: k = XK_F30; break;
819 case Qt::Key_F31: k = XK_F31; break;
820 case Qt::Key_F32: k = XK_F32; break;
821 case Qt::Key_F33: k = XK_F33; break;
822 case Qt::Key_F34: k = XK_F34; break;
823 case Qt::Key_F35: k = XK_F35; break;
824
825 case Qt::Key_NumLock: k = XK_Num_Lock; break;
826 case Qt::Key_CapsLock: k = XK_Caps_Lock; break;
827 case Qt::Key_ScrollLock: k = XK_Scroll_Lock; break;
828
829 case Qt::Key_Shift: k = XK_Shift_R; break; //k = XK_Shift_L; break;
830 case Qt::Key_Control: k = XK_Control_R; break;// k = XK_Control_L; break;
831 case Qt::Key_Alt: k = XK_Alt_R; break;//k = XK_Alt_L; break;
832 case Qt::Key_Meta: k = XK_Meta_R; break;//k = XK_Meta_L; break;*/
833
834 case Qt::Key_Super_L: k = XK_Super_L; break; /* left "windows" key */
835 case Qt::Key_Super_R: k = XK_Super_R; break; /* right "windows" key */
836
837 case Qt::Key_Help: k = XK_Help; break;
838 case Qt::Key_Print: k = XK_Print; break;
839 case Qt::Key_SysReq: k = XK_Sys_Req; break;
840 case Qt::Key_0: k = XK_0;break;
841 case Qt::Key_1: k = XK_1;break;
842 case Qt::Key_2: k = XK_2;break;
843 case Qt::Key_3: k = XK_3;break;
844 case Qt::Key_4: k = XK_4;break;
845 case Qt::Key_5: k = XK_5;break;
846 case Qt::Key_6: k = XK_6;break;
847 case Qt::Key_7: k = XK_7;break;
848 case Qt::Key_8: k = XK_8;break;
849 case Qt::Key_9: k = XK_9;break;
850 }
851
852 if (k == 5000)
853 {
854
855 if (!modifier)
856 {
857 switch (inkey)
858 {
859 case Qt::Key_A: k = XK_a;break;
860 case Qt::Key_B: k = XK_b;break;
861 case Qt::Key_C: k = XK_c;break;
862 case Qt::Key_D: k = XK_d;break;
863 case Qt::Key_E: k = XK_e;break;
864 case Qt::Key_F: k = XK_f;break;
865 case Qt::Key_G: k = XK_g;break;
866 case Qt::Key_H: k = XK_h;break;
867 case Qt::Key_I: k = XK_i;break;
868 case Qt::Key_J: k = XK_j;break;
869 case Qt::Key_K: k = XK_k;break;
870 case Qt::Key_L: k = XK_l;break;
871 case Qt::Key_M: k = XK_m;break;
872 case Qt::Key_N: k = XK_n;break;
873 case Qt::Key_O: k = XK_o;break;
874 case Qt::Key_P: k = XK_p;break;
875 case Qt::Key_Q: k = XK_q;break;
876 case Qt::Key_R: k = XK_r;break;
877 case Qt::Key_S: k = XK_s;break;
878 case Qt::Key_T: k = XK_t;break;
879 case Qt::Key_U: k = XK_u;break;
880 case Qt::Key_V: k = XK_v;break;
881 case Qt::Key_W: k = XK_w;break;
882 case Qt::Key_X: k = XK_x;break;
883 case Qt::Key_Y: k = XK_y;break;
884 case Qt::Key_Z: k = XK_z;break;
885 }
886 }
887 else
888 {
889 switch (inkey)
890 {
891 case Qt::Key_A: k = XK_A;break;
892 case Qt::Key_B: k = XK_B;break;
893 case Qt::Key_C: k = XK_C;break;
894 case Qt::Key_D: k = XK_D;break;
895 case Qt::Key_E: k = XK_E;break;
896 case Qt::Key_F: k = XK_F;break;
897 case Qt::Key_G: k = XK_G;break;
898 case Qt::Key_H: k = XK_H;break;
899 case Qt::Key_I: k = XK_I;break;
900 case Qt::Key_J: k = XK_J;break;
901 case Qt::Key_K: k = XK_K;break;
902 case Qt::Key_L: k = XK_L;break;
903 case Qt::Key_M: k = XK_M;break;
904 case Qt::Key_N: k = XK_N;break;
905 case Qt::Key_O: k = XK_O;break;
906 case Qt::Key_P: k = XK_P;break;
907 case Qt::Key_Q: k = XK_Q;break;
908 case Qt::Key_R: k = XK_R;break;
909 case Qt::Key_S: k = XK_S;break;
910 case Qt::Key_T: k = XK_T;break;
911 case Qt::Key_U: k = XK_U;break;
912 case Qt::Key_V: k = XK_V;break;
913 case Qt::Key_W: k = XK_W;break;
914 case Qt::Key_X: k = XK_X;break;
915 case Qt::Key_Y: k = XK_Y;break;
916 case Qt::Key_Z: k = XK_Z;break;
917 }
918 }
919 }
920
921 return k;
922}
923
924void CConnectLibVNCServer::keyPressEvent(QKeyEvent *event)
925{
926 if(!m_pClient) return;
927 if(m_pParameter && m_pParameter->GetOnlyView()) return;
928 bool modifier = false;
929 if (event->modifiers() & Qt::ShiftModifier)
930 modifier = true;
931 uint32_t k = TranslateRfbKey(event->key(), modifier);
932 qDebug(logKey) << Q_FUNC_INFO << event << k << modifier;
933 SendKeyEvent(m_pClient, k, TRUE);
934}
935
936void CConnectLibVNCServer::keyReleaseEvent(QKeyEvent *event)
937{
938 if(!m_pClient) return;
939 if(m_pParameter && m_pParameter->GetOnlyView()) return;
940 bool modifier = false;
941 if (event->modifiers() & Qt::ShiftModifier)
942 modifier = true;
943 uint32_t k = TranslateRfbKey(event->key(), modifier);
944 qDebug(logKey) << Q_FUNC_INFO << event << k << modifier;
945 SendKeyEvent(m_pClient, k, FALSE);
946}
947
949void CConnectLibVNCServer::slotConnectProxyServer(QString szHost, quint16 nPort)
950{
951 QString szErr;
952 qDebug(log) << "CConnectLibVNCServer::slotConnectServer" << nPort;
953 // Set server ip and port
954 m_pClient->serverHost = strdup(szHost.toStdString().c_str());
955 m_pClient->serverPort = nPort;
956 szErr = tr("Connect to local socket server %1:%2")
957 .arg(m_pClient->serverHost,
958 QString::number(m_pClient->serverPort));
959 qDebug(log) << szErr;
960 if(!rfbInitClient(m_pClient, nullptr, nullptr))
961 {
962 szErr += tr(" fail");
963 qCritical(log) << szErr;
964 //emit sigShowMessageBox(tr("Error"), szErr, QMessageBox::Critical);
965 return;
966 }
967
968 QString szInfo = QString("Connect to ") + m_pClient->desktopName;
969 qInfo(log) << szInfo;
970
971 emit sigSetDesktopSize(m_pClient->width, m_pClient->height);
972 emit sigServerName(m_pClient->desktopName);
973 emit sigInformation(szInfo);
974 emit sigConnected();
975}
976
977#if defined(HAVE_UNIX_DOMAIN_SOCKET) && defined(Q_OS_UNIX)
979{
980 QString szErr;
981 qDebug(log) << "CConnectLibVNCServer::slotConnectServer" << szFile;
982 // Set server ip and port
983 m_pClient->serverHost = strdup(szFile.toStdString().c_str());
984 szErr = tr("Connect to local socket server ") + szFile;
985 qDebug(log) << szErr;
986 if(!rfbInitClient(m_pClient, nullptr, nullptr))
987 {
988 szErr += + " " + tr("fail");
989 qCritical(log) << szErr;
990 //emit sigShowMessageBox(tr("Error"), szErr, QMessageBox::Critical);
991 return;
992 }
993
994 QString szInfo = QString("Connect to ") + m_pClient->desktopName;
995 qInfo(log) << szInfo;
996
997 emit sigSetDesktopSize(m_pClient->width, m_pClient->height);
998 emit sigServerName(m_pClient->desktopName);
999 emit sigInformation(szInfo);
1000 emit sigConnected();
1001}
1002#endif
Remote desktop connect interface.
void sigUpdateRect(const QRect &r, const QImage &image)
Notify the CFrmView update image.
void slotConnectProxyServer(QString szHost, quint16 nPort)
[connect local socket server]
virtual int OnProcess() override
Specific operation processing of plug-in connection.
virtual int WakeUp() override
Wake up Connect thread(background thread)
virtual OnInitReturnValue OnInit() override
Specific plug-in realizes connection initialization.
virtual int OnClean() override
Clean.
void sigError(const int nError, const QString &szError=QString())
Triggered when an error is generated.
void sigInformation(const QString &szInfo)
Triggering from a background thread displays information in the main thread without blocking the back...
void sigShowMessageBox(const QString &szTitle, const QString &szMessage, const QMessageBox::Icon &icon=QMessageBox::Information)
Trigger the display of a message dialog (QMessageBox) in the main thread from a background thread wit...
void sigConnected()
Emitted when the plugin is successfully connected.
void sigDisconnect()
Notify the user to call disconnect.
void sigBlockShowWidget(const QString &className, int &nRet, void *pContext)
Blocks the background thread and displays the window in the foreground thread.
CParameterUser m_User
[Instance user]
Data is forwarded over a local socket and SSH tunnel.