fix indentation
[presencevnc] / src / vncclientthread.cpp
index 2d1fc9f..23d91c5 100644 (file)
@@ -49,25 +49,25 @@ rfbBool VncClientThread::newclient(rfbClient *cl)
     cl->format.blueMax = 0xff;
 
     switch (t->quality()) {
-    case RemoteView::High:
-        cl->appData.useBGR233 = 0;
-        cl->appData.encodingsString = "copyrect hextile raw";
-        cl->appData.compressLevel = 0;
-        cl->appData.qualityLevel = 9;
-        break;
-    case RemoteView::Medium:
-        cl->appData.useBGR233 = 0;
-        cl->appData.encodingsString = "tight zrle ultra copyrect hextile zlib corre rre raw";
-        cl->appData.compressLevel = 5;
-        cl->appData.qualityLevel = 7;
-        break;
-    case RemoteView::Low:
-    case RemoteView::Unknown:
-    default:
-        cl->appData.useBGR233 = 1;
-        cl->appData.encodingsString = "tight zrle ultra copyrect hextile zlib corre rre raw";
-        cl->appData.compressLevel = 9;
-        cl->appData.qualityLevel = 1;
+        case RemoteView::High:
+            cl->appData.useBGR233 = 0;
+            cl->appData.encodingsString = "copyrect hextile raw";
+            cl->appData.compressLevel = 0;
+            cl->appData.qualityLevel = 9;
+            break;
+        case RemoteView::Medium:
+            cl->appData.useBGR233 = 0;
+            cl->appData.encodingsString = "tight zrle ultra copyrect hextile zlib corre rre raw";
+            cl->appData.compressLevel = 5;
+            cl->appData.qualityLevel = 7;
+            break;
+        case RemoteView::Low:
+        case RemoteView::Unknown:
+        default:
+            cl->appData.useBGR233 = 1;
+            cl->appData.encodingsString = "tight zrle ultra copyrect hextile zlib corre rre raw";
+            cl->appData.compressLevel = 9;
+            cl->appData.qualityLevel = 1;
     }
 
     SetFormatAndEncodings(cl);
@@ -77,7 +77,7 @@ rfbBool VncClientThread::newclient(rfbClient *cl)
 
 void VncClientThread::updatefb(rfbClient* cl, int x, int y, int w, int h)
 {
-//     kDebug(5011) << "updated client: x: " << x << ", y: " << y << ", w: " << w << ", h: " << h;
+    //kDebug(5011) << "updated client: x: " << x << ", y: " << y << ", w: " << w << ", h: " << h;
 
     const int width = cl->width, height = cl->height;
 
@@ -98,7 +98,7 @@ void VncClientThread::updatefb(rfbClient* cl, int x, int y, int w, int h)
 void VncClientThread::cuttext(rfbClient* cl, const char *text, int textlen)
 {
     const QString cutText = QString::fromUtf8(text, textlen);
-    kDebug(5011) << cutText;
+    kDebug(5011) << "cuttext: " << cutText;
 
     if (!cutText.isEmpty()) {
         VncClientThread *t = (VncClientThread*)rfbClientGetClientData(cl, 0);
@@ -115,12 +115,20 @@ char *VncClientThread::passwdHandler(rfbClient *cl)
     VncClientThread *t = (VncClientThread*)rfbClientGetClientData(cl, 0);
     Q_ASSERT(t);
 
-    t->passwordRequest();
     t->m_passwordError = true;
+    t->passwordRequest();
 
     return strdup(t->password().toLocal8Bit());
 }
 
+void VncClientThread::setPassword(const QString &password)
+{
+    if(password.isNull()) //cancelled, don't retry
+        m_passwordError = false;
+
+    m_password = password;
+}
+
 void VncClientThread::outputHandler(const char *format, ...)
 {
     va_list args;
@@ -154,9 +162,9 @@ void VncClientThread::outputHandler(const char *format, ...)
         outputErrorMessageString = "INTERNAL:APPLE_VNC_COMPATIBILTY";
 }
 
-VncClientThread::VncClientThread(QObject *parent)
-        : QThread(parent)
-        , frameBuffer(0)
+    VncClientThread::VncClientThread(QObject *parent)
+    : QThread(parent)
+      , frameBuffer(0)
 {
     QMutexLocker locker(&mutex);
     m_stopped = false;
@@ -171,10 +179,11 @@ VncClientThread::~VncClientThread()
 {
     stop();
 
-    const bool quitSuccess = wait(500);
+    const bool quitSuccess = wait(1000);
+
+    if(!quitSuccess)
+        kDebug(5011) << "~VncClientThread(): Quit failed";
 
-    kDebug(5011) << "~VncClientThread(): Quit VNC thread success:" << quitSuccess;
-    
     delete [] frameBuffer;
     //cl is free()d when event loop exits.
 }
@@ -241,7 +250,14 @@ void VncClientThread::emitGotCut(const QString &text)
 
 void VncClientThread::stop()
 {
-//TODO: not locking the mutex leads to a crash, but at least it stops.
+    if(m_stopped)
+        return;
+
+    //also abort listening for connections, should be safe without locking
+    if(listen_port)
+        cl->listenSpecified = false;
+
+    QMutexLocker locker(&mutex);
     m_stopped = true;
 }
 
@@ -249,13 +265,14 @@ void VncClientThread::run()
 {
     QMutexLocker locker(&mutex);
 
+    int passwd_failures = 0;
     while (!m_stopped) { // try to connect as long as the server allows
-           kDebug(5011) << "enter loop";
         m_passwordError = false;
+        outputErrorMessageString.clear(); //don't deliver error messages of old instances...
 
         rfbClientLog = outputHandler;
         rfbClientErr = outputHandler;
-        cl = rfbGetClient(8, 3, 4);
+        cl = rfbGetClient(8, 3, 4); // bitsPerSample, samplesPerPixel, bytesPerPixel
         cl->MallocFrameBuffer = newclient;
         cl->canHandleNewFBSize = true;
         cl->GetPassword = passwdHandler;
@@ -272,25 +289,34 @@ void VncClientThread::run()
             m_port += 5900;
         cl->serverPort = m_port;
 
+        cl->listenSpecified = rfbBool(listen_port > 0);
+        cl->listenPort = listen_port;
+
         kDebug(5011) << "--------------------- trying init ---------------------";
 
         if (rfbInitClient(cl, 0, 0))
             break;
 
-        if (m_passwordError)
-            continue;
+        //init failed...
+        if (m_passwordError) {
+            passwd_failures++;
+            if(passwd_failures < 3)
+                continue; //that's ok, try again
+        }
 
-        return;
+        //stop connecting
+        m_stopped = true;
+        return; //no cleanup necessary, cl was free()d by rfbInitClient()
     }
 
     locker.unlock();
 
     // Main VNC event loop
     while (!m_stopped) {
-           kDebug(5011) << "entering main event loop";
         const int i = WaitForMessage(cl, 500);
-        if (i < 0)
+        if(m_stopped or i < 0)
             break;
+
         if (i)
             if (!HandleRFBServerMessage(cl))
                 break;
@@ -357,5 +383,3 @@ void VncClientThread::clientCut(const QString &text)
 
     m_eventQueue.enqueue(new ClientCutEvent(text));
 }
-
-#include "moc_vncclientthread.cpp"