1 /****************************************************************************
3 ** Copyright (C) 2007-2008 Urs Wolfer <uwolfer @ kde.org>
5 ** This file is part of KDE.
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; see the file COPYING. If not, write to
19 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ** Boston, MA 02110-1301, USA.
22 ****************************************************************************/
24 #include "vncclientthread.h"
26 #include <QMutexLocker>
29 static QString outputErrorMessageString;
31 rfbBool VncClientThread::newclient(rfbClient *cl)
33 VncClientThread *t = (VncClientThread*)rfbClientGetClientData(cl, 0);
36 const int width = cl->width, height = cl->height, depth = cl->format.bitsPerPixel;
37 const int size = width * height * (depth / 8);
39 delete [] t->frameBuffer; // do not leak if we get a new framebuffer size
40 t->frameBuffer = new uint8_t[size];
41 cl->frameBuffer = t->frameBuffer;
42 memset(cl->frameBuffer, '\0', size);
43 cl->format.bitsPerPixel = 32;
44 cl->format.redShift = 16;
45 cl->format.greenShift = 8;
46 cl->format.blueShift = 0;
47 cl->format.redMax = 0xff;
48 cl->format.greenMax = 0xff;
49 cl->format.blueMax = 0xff;
51 switch (t->quality()) {
52 case RemoteView::High:
53 cl->appData.useBGR233 = 0;
54 cl->appData.encodingsString = "copyrect hextile raw";
55 cl->appData.compressLevel = 0;
56 cl->appData.qualityLevel = 9;
58 case RemoteView::Medium:
59 cl->appData.useBGR233 = 0;
60 cl->appData.encodingsString = "tight zrle ultra copyrect hextile zlib corre rre raw";
61 cl->appData.compressLevel = 5;
62 cl->appData.qualityLevel = 7;
65 case RemoteView::Unknown:
67 cl->appData.useBGR233 = 1;
68 cl->appData.encodingsString = "tight zrle ultra copyrect hextile zlib corre rre raw";
69 cl->appData.compressLevel = 9;
70 cl->appData.qualityLevel = 1;
73 SetFormatAndEncodings(cl);
78 void VncClientThread::updatefb(rfbClient* cl, int x, int y, int w, int h)
80 //kDebug(5011) << "updated client: x: " << x << ", y: " << y << ", w: " << w << ", h: " << h;
82 const int width = cl->width, height = cl->height;
84 const QImage img(cl->frameBuffer, width, height, QImage::Format_RGB32);
87 kDebug(5011) << "image not loaded";
90 VncClientThread *t = (VncClientThread*)rfbClientGetClientData(cl, 0);
95 t->emitUpdated(x, y, w, h);
98 void VncClientThread::cuttext(rfbClient* cl, const char *text, int textlen)
100 const QString cutText = QString::fromUtf8(text, textlen);
101 kDebug(5011) << "cuttext: " << cutText;
103 if (!cutText.isEmpty()) {
104 VncClientThread *t = (VncClientThread*)rfbClientGetClientData(cl, 0);
107 t->emitGotCut(cutText);
111 char *VncClientThread::passwdHandler(rfbClient *cl)
113 kDebug(5011) << "password request" << kBacktrace();
115 VncClientThread *t = (VncClientThread*)rfbClientGetClientData(cl, 0);
118 t->m_passwordError = true;
119 t->passwordRequest();
121 return strdup(t->password().toLocal8Bit());
124 void VncClientThread::setPassword(const QString &password)
126 if(password.isNull()) //cancelled, don't retry
127 m_passwordError = false;
129 m_password = password;
132 void VncClientThread::outputHandler(const char *format, ...)
135 va_start(args, format);
138 message.vsprintf(format, args);
142 message = message.trimmed();
144 kDebug(5011) << message;
146 if ((message.contains("Couldn't convert ")) ||
147 (message.contains("Unable to connect to VNC server")))
148 outputErrorMessageString = i18n("Server not found.");
149 else if ((message.contains("VNC connection failed: Authentication failed, too many tries")) ||
150 (message.contains("VNC connection failed: Too many authentication failures")))
151 outputErrorMessageString = i18n("VNC authentication failed because of too many authentication tries.");
152 else if (message.contains("VNC connection failed: Authentication failed"))
153 outputErrorMessageString = i18n("VNC authentication failed.");
154 else if (message.contains("VNC server closed connection"))
155 outputErrorMessageString = i18n("VNC server closed connection.");
156 else if (message.contains("VNC server supports protocol version 3.889")) // see http://bugs.kde.org/162640
157 outputErrorMessageString = "INTERNAL:APPLE_VNC_COMPATIBILTY"; // internal messages, not displayed to user
160 VncClientThread::VncClientThread(QObject *parent)
164 QMutexLocker locker(&mutex);
167 QTimer *outputErrorMessagesCheckTimer = new QTimer(this);
168 outputErrorMessagesCheckTimer->setInterval(500);
169 connect(outputErrorMessagesCheckTimer, SIGNAL(timeout()), this, SLOT(checkOutputErrorMessage()));
170 outputErrorMessagesCheckTimer->start();
173 VncClientThread::~VncClientThread()
177 const bool quitSuccess = wait(1000);
180 kDebug(5011) << "~VncClientThread(): Quit failed";
182 delete [] frameBuffer;
183 //cl is free()d when event loop exits.
186 void VncClientThread::checkOutputErrorMessage()
188 if (!outputErrorMessageString.isEmpty()) {
189 QString errorMessage = outputErrorMessageString;
190 outputErrorMessageString.clear();
191 // show authentication failure error only after the 3rd unsuccessful try
192 if ((errorMessage != i18n("VNC authentication failed.")) || m_passwordError)
193 emit outputErrorMessage(errorMessage);
197 void VncClientThread::setHost(const QString &host)
199 QMutexLocker locker(&mutex);
203 void VncClientThread::setPort(int port)
205 QMutexLocker locker(&mutex);
209 void VncClientThread::setQuality(RemoteView::Quality quality)
214 RemoteView::Quality VncClientThread::quality() const
219 void VncClientThread::setImage(const QImage &img)
221 QMutexLocker locker(&mutex);
225 const QImage VncClientThread::image(int x, int y, int w, int h)
227 QMutexLocker locker(&mutex);
229 if (w == 0) // full image requested
232 return m_image.copy(x, y, w, h);
235 void VncClientThread::emitUpdated(int x, int y, int w, int h)
237 emit imageUpdated(x, y, w, h);
240 void VncClientThread::emitGotCut(const QString &text)
245 void VncClientThread::stop()
250 //also abort listening for connections, should be safe without locking
252 cl->listenSpecified = false;
254 QMutexLocker locker(&mutex);
258 void VncClientThread::run()
260 QMutexLocker locker(&mutex);
262 int passwd_failures = 0;
263 while (!m_stopped) { // try to connect as long as the server allows
264 m_passwordError = false;
265 outputErrorMessageString.clear(); //don't deliver error messages of old instances...
267 rfbClientLog = outputHandler;
268 rfbClientErr = outputHandler;
269 cl = rfbGetClient(8, 3, 4); // bitsPerSample, samplesPerPixel, bytesPerPixel
270 cl->MallocFrameBuffer = newclient;
271 cl->canHandleNewFBSize = true;
272 cl->GetPassword = passwdHandler;
273 cl->GotFrameBufferUpdate = updatefb;
274 cl->GotXCutText = cuttext;
275 rfbClientSetClientData(cl, 0, this);
277 cl->serverHost = strdup(m_host.toUtf8().constData());
279 if (m_port < 0 || !m_port) // port is invalid or empty...
280 m_port = 5900; // fallback: try an often used VNC port
282 if (m_port >= 0 && m_port < 100) // the user most likely used the short form (e.g. :1)
284 cl->serverPort = m_port;
286 cl->listenSpecified = rfbBool(listen_port > 0);
287 cl->listenPort = listen_port;
289 kDebug(5011) << "--------------------- trying init ---------------------";
291 if (rfbInitClient(cl, 0, 0))
295 if (m_passwordError) {
297 if(passwd_failures < 3)
298 continue; //that's ok, try again
303 return; //no cleanup necessary, cl was free()d by rfbInitClient()
308 // Main VNC event loop
310 const int i = WaitForMessage(cl, 500);
311 if(m_stopped or i < 0)
315 if (!HandleRFBServerMessage(cl))
320 while (!m_eventQueue.isEmpty()) {
321 ClientEvent* clientEvent = m_eventQueue.dequeue();
322 clientEvent->fire(cl);
329 // Cleanup allocated resources
331 rfbClientCleanup(cl);
335 ClientEvent::~ClientEvent()
339 void PointerClientEvent::fire(rfbClient* cl)
341 SendPointerEvent(cl, m_x, m_y, m_buttonMask);
344 void KeyClientEvent::fire(rfbClient* cl)
346 SendKeyEvent(cl, m_key, m_pressed);
349 void ClientCutEvent::fire(rfbClient* cl)
351 SendClientCutText(cl, text.toUtf8().data(), text.size());
354 void VncClientThread::mouseEvent(int x, int y, int buttonMask)
356 QMutexLocker lock(&mutex);
360 m_eventQueue.enqueue(new PointerClientEvent(x, y, buttonMask));
363 void VncClientThread::keyEvent(int key, bool pressed)
365 QMutexLocker lock(&mutex);
369 m_eventQueue.enqueue(new KeyClientEvent(key, pressed));
372 void VncClientThread::clientCut(const QString &text)
374 QMutexLocker lock(&mutex);
378 m_eventQueue.enqueue(new ClientCutEvent(text));