don't use 32bit color depth on N900 (16bit display); on other platforms, use 32bit...
[presencevnc] / src / vncclientthread.cpp
index ac71844..d35f71d 100644 (file)
 
 #include "vncclientthread.h"
 
-#include <QCoreApplication>
 #include <QMutexLocker>
 #include <QTimer>
 
 static QString outputErrorMessageString;
 
+#ifdef Q_WS_MAEMO_5
+//N900 display has 16bit depth (R/G/B with 5/6/5 bpp)
+const int MAX_COLOR_DEPTH = 16;
+#else
+const int MAX_COLOR_DEPTH = 32;
+#endif
+
+
 rfbBool VncClientThread::newclient(rfbClient *cl)
 {
     VncClientThread *t = (VncClientThread*)rfbClientGetClientData(cl, 0);
     Q_ASSERT(t);
 
-    const int width = cl->width, height = cl->height, depth = cl->format.bitsPerPixel;
-    const int size = width * height * (depth / 8);
-    if (t->frameBuffer)
-        delete [] t->frameBuffer; // do not leak if we get a new framebuffer size
-    t->frameBuffer = new uint8_t[size];
-    cl->frameBuffer = t->frameBuffer;
-    memset(cl->frameBuffer, '\0', size);
-    cl->format.bitsPerPixel = 32;
-    cl->format.redShift = 16;
-    cl->format.greenShift = 8;
-    cl->format.blueShift = 0;
-    cl->format.redMax = 0xff;
-    cl->format.greenMax = 0xff;
-    cl->format.blueMax = 0xff;
-
     switch (t->quality()) {
     case RemoteView::High:
+        cl->format.bitsPerPixel = MAX_COLOR_DEPTH;
         cl->appData.useBGR233 = 0;
         cl->appData.encodingsString = "copyrect hextile raw";
         cl->appData.compressLevel = 0;
         cl->appData.qualityLevel = 9;
         break;
     case RemoteView::Medium:
+        cl->format.bitsPerPixel = 16;
         cl->appData.useBGR233 = 0;
         cl->appData.encodingsString = "tight zrle ultra copyrect hextile zlib corre rre raw";
         cl->appData.compressLevel = 5;
@@ -65,12 +59,38 @@ rfbBool VncClientThread::newclient(rfbClient *cl)
     case RemoteView::Low:
     case RemoteView::Unknown:
     default:
-        cl->appData.useBGR233 = 1;
+        cl->format.bitsPerPixel = 16; //TODO: add support for 8bit (needs color map)
         cl->appData.encodingsString = "tight zrle ultra copyrect hextile zlib corre rre raw";
         cl->appData.compressLevel = 9;
         cl->appData.qualityLevel = 1;
     }
 
+    if(cl->format.bitsPerPixel == 16) {
+        cl->format.depth = 16; //number of useful bits in the pixel value
+        cl->format.redShift = 11;
+        cl->format.greenShift = 5;
+        cl->format.blueShift = 0;
+        cl->format.redMax = 0x1f;
+        cl->format.greenMax = 0x3f;
+        cl->format.blueMax = 0x1f;
+    } else {
+        cl->format.depth = 24; //number of useful bits in the pixel value
+        cl->format.redShift = 16;
+        cl->format.greenShift = 8;
+        cl->format.blueShift = 0;
+        cl->format.redMax = 0xff;
+        cl->format.greenMax = 0xff;
+        cl->format.blueMax = 0xff;
+    }
+
+    if (t->frameBuffer)
+        delete [] t->frameBuffer; // do not leak if we get a new framebuffer size
+    const int size = cl->width * cl->height * (cl->format.bitsPerPixel / 8);
+    t->frameBuffer = new uint8_t[size];
+    cl->frameBuffer = t->frameBuffer;
+    memset(cl->frameBuffer, '\0', size);
+
+
     SetFormatAndEncodings(cl);
 
     return true;
@@ -78,11 +98,14 @@ 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;
-
-    const QImage img(cl->frameBuffer, width, height, QImage::Format_RGB32);
+    const QImage img(
+            cl->frameBuffer,
+            cl->width,
+            cl->height,
+            (cl->format.bitsPerPixel==16)?QImage::Format_RGB16:QImage::Format_RGB32
+    );
 
     if (img.isNull()) {
         kDebug(5011) << "image not loaded";
@@ -116,12 +139,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;
@@ -139,27 +170,21 @@ void VncClientThread::outputHandler(const char *format, ...)
     if ((message.contains("Couldn't convert ")) ||
             (message.contains("Unable to connect to VNC server")))
         outputErrorMessageString = i18n("Server not found.");
-
-    if ((message.contains("VNC connection failed: Authentication failed, too many tries")) ||
+    else if ((message.contains("VNC connection failed: Authentication failed, too many tries")) ||
             (message.contains("VNC connection failed: Too many authentication failures")))
         outputErrorMessageString = i18n("VNC authentication failed because of too many authentication tries.");
-
-    if (message.contains("VNC connection failed: Authentication failed"))
+    else if (message.contains("VNC connection failed: Authentication failed"))
         outputErrorMessageString = i18n("VNC authentication failed.");
-
-    if (message.contains("VNC server closed connection"))
+    else if (message.contains("VNC server closed connection"))
         outputErrorMessageString = i18n("VNC server closed connection.");
-
-    // internal messages, not displayed to user
-    if (message.contains("VNC server supports protocol version 3.889")) // see http://bugs.kde.org/162640
-        outputErrorMessageString = "INTERNAL:APPLE_VNC_COMPATIBILTY";
+    else if (message.contains("VNC server supports protocol version 3.889")) // see http://bugs.kde.org/162640
+        outputErrorMessageString = "INTERNAL:APPLE_VNC_COMPATIBILTY"; // internal messages, not displayed to user
 }
 
 VncClientThread::VncClientThread(QObject *parent)
-        : QThread(parent)
-        , frameBuffer(0)
+    : QThread(parent)
+    , frameBuffer(0)
 {
-       outputErrorMessageString.clear(); //don't deliver error messages of old instances...
     QMutexLocker locker(&mutex);
     m_stopped = false;
 
@@ -173,11 +198,11 @@ VncClientThread::~VncClientThread()
 {
     stop();
 
-    const bool quitSuccess = wait(1000);
+    const bool quitSuccess = wait(4000);
+
+    if(!quitSuccess)
+        kDebug(5011) << "~VncClientThread(): Quit failed";
 
-       if(!quitSuccess)
-               kDebug(5011) << "~VncClientThread(): Quit failed";
-    
     delete [] frameBuffer;
     //cl is free()d when event loop exits.
 }
@@ -185,12 +210,11 @@ VncClientThread::~VncClientThread()
 void VncClientThread::checkOutputErrorMessage()
 {
     if (!outputErrorMessageString.isEmpty()) {
-        kDebug(5011) << outputErrorMessageString;
         QString errorMessage = outputErrorMessageString;
         outputErrorMessageString.clear();
         // show authentication failure error only after the 3rd unsuccessful try
         if ((errorMessage != i18n("VNC authentication failed.")) || m_passwordError)
-            outputErrorMessage(errorMessage);
+            emit outputErrorMessage(errorMessage);
     }
 }
 
@@ -244,15 +268,15 @@ void VncClientThread::emitGotCut(const QString &text)
 
 void VncClientThread::stop()
 {
-       if(m_stopped)
-               return;
+    if(m_stopped)
+        return;
 
-       //also abort listening for connections, should be safe without locking
-       if(listen_port)
-               cl->listenSpecified = false;
+    //also abort listening for connections, should be safe without locking
+    if(listen_port)
+        cl->listenSpecified = false;
 
-       QMutexLocker locker(&mutex);
-       m_stopped = true;
+    QMutexLocker locker(&mutex);
+    m_stopped = true;
 }
 
 void VncClientThread::run()
@@ -262,10 +286,11 @@ void VncClientThread::run()
     int passwd_failures = 0;
     while (!m_stopped) { // try to connect as long as the server allows
         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;
@@ -282,31 +307,24 @@ 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(listen_port) { //listen for incoming connections
-                       int argc = 2;
-                       char* argv[2] = { "x", "-listen" }; //this isn't exactly elegant..
-                       cl->listenPort = listen_port;
-                       if (rfbInitClient(cl, &argc, argv))
-                               break;
-               } else { //connect to host
-                       if (rfbInitClient(cl, 0, 0))
-                               break;
-               }
+        if (rfbInitClient(cl, 0, 0))
+            break;
 
+        //init failed...
         if (m_passwordError) {
-                       passwd_failures++;
-                       if(passwd_failures > 2) {
-                               m_stopped = true;
-                               return;
-                       }
-                       continue;
-               }
-
-               //clean, just exit
-               m_stopped = true;
-        return;
+            passwd_failures++;
+            if(passwd_failures < 3)
+                continue; //that's ok, try again
+        }
+
+        //stop connecting
+        m_stopped = true;
+        return; //no cleanup necessary, cl was free()d by rfbInitClient()
     }
 
     locker.unlock();
@@ -314,8 +332,8 @@ void VncClientThread::run()
     // Main VNC event loop
     while (!m_stopped) {
         const int i = WaitForMessage(cl, 500);
-               if(m_stopped or i < 0)
-                       break;
+        if(m_stopped or i < 0)
+            break;
 
         if (i)
             if (!HandleRFBServerMessage(cl))
@@ -334,7 +352,7 @@ void VncClientThread::run()
 
     // Cleanup allocated resources
     locker.relock();
-       rfbClientCleanup(cl);
+    rfbClientCleanup(cl);
     m_stopped = true;
 }
 
@@ -383,5 +401,3 @@ void VncClientThread::clientCut(const QString &text)
 
     m_eventQueue.enqueue(new ClientCutEvent(text));
 }
-
-#include "moc_vncclientthread.cpp"