fix refresh issues
[presencevnc] / src / vncview.cpp
index 13834b2..c0f6f16 100644 (file)
@@ -30,6 +30,7 @@
 critical(parent, caption, message)
 
 #include <QApplication>
+#include <QBitmap>
 #include <QCheckBox>
 #include <QDialog>
 #include <QImage>
@@ -51,7 +52,14 @@ critical(parent, caption, message)
 #define KMOD_Control_L         0x08
 #define KMOD_Shift_L   0x10
 
-VncView::VncView(QWidget *parent, const KUrl &url, RemoteView::Quality quality)
+//local cursor width/height in px, should be an odd number
+const int CURSOR_SIZE = 7;
+
+const int TAP_PRESS_TIME = 180;
+const int DOUBLE_TAP_UP_TIME = 500;
+
+
+VncView::VncView(QWidget *parent, const KUrl &url, RemoteView::Quality quality, int listen_port)
         : RemoteView(parent),
         m_initDone(false),
         m_buttonMask(0),
@@ -65,12 +73,14 @@ VncView::VncView(QWidget *parent, const KUrl &url, RemoteView::Quality quality)
         m_verticalFactor(1.0),
         m_forceLocalCursor(false),
        force_full_repaint(false),
-       quality(quality)
+       quality(quality),
+       listen_port(listen_port)
 {
     m_url = url;
     m_host = url.host();
     m_port = url.port();
 
+       //BlockingQueuedConnection can cause deadlocks when exiting, handled in startQuitting()
     connect(&vncThread, SIGNAL(imageUpdated(int, int, int, int)), this, SLOT(updateImage(int, int, int, int)), Qt::BlockingQueuedConnection);
     connect(&vncThread, SIGNAL(gotCut(const QString&)), this, SLOT(setCut(const QString&)), Qt::BlockingQueuedConnection);
     connect(&vncThread, SIGNAL(passwordRequest()), this, SLOT(requestPassword()), Qt::BlockingQueuedConnection);
@@ -129,48 +139,28 @@ QSize VncView::minimumSizeHint() const
     return size();
 }
 
-void VncView::scaleResize(int w, int h)
-{
-    RemoteView::scaleResize(w, h);
-    
-    kDebug(5011) << "scaleResize(): " <<w << h;
-    if (m_scale) {
-        m_verticalFactor = (qreal) h / m_frame.height();
-        m_horizontalFactor = (qreal) w / m_frame.width();
-
-        m_verticalFactor = m_horizontalFactor = qMin(m_verticalFactor, m_horizontalFactor);
-
-        const qreal newW = m_frame.width() * m_horizontalFactor;
-        const qreal newH = m_frame.height() * m_verticalFactor;
-       /*
-        setMaximumSize(newW, newH); //This is a hack to force Qt to center the view in the scroll area
-       //also causes the widget's size to flicker
-       */
-        resize(newW, newH);
-    } 
-}
-
-
 void VncView::startQuitting()
 {
     kDebug(5011) << "about to quit";
 
-    const bool connected = status() == RemoteView::Connected;
+    //const bool connected = status() == RemoteView::Connected;
 
     setStatus(Disconnecting);
 
     m_quitFlag = true;
 
-    if (connected) {
-        vncThread.stop();
-    }
-
-    vncThread.quit();
-
-    const bool quitSuccess = vncThread.wait(500);
-
-    kDebug(5011) << "startQuitting(): Quit VNC thread success:" << quitSuccess;
-
+       //if(connected) //remove if things work without it
+       vncThread.stop();
+
+    const bool quitSuccess = vncThread.wait(700);
+       if(!quitSuccess) {
+               //happens when vncThread wants to call a slot via BlockingQueuedConnection,
+               //needs an event loop in this thread so execution continues after 'emit'
+               QEventLoop loop;
+               if(!loop.processEvents())
+                       kDebug(5011) << "BUG: deadlocked, but no events to deliver?";
+               vncThread.wait(700);
+       }
     setStatus(Disconnected);
 }
 
@@ -183,7 +173,7 @@ bool VncView::start()
 {
     vncThread.setHost(m_host);
     vncThread.setPort(m_port);
-
+       vncThread.setListenPort(listen_port); //if port is != 0, thread will listen for connections
     vncThread.setQuality(quality);
 
     // set local cursor on by default because low quality mostly means slow internet connection
@@ -288,18 +278,8 @@ void VncView::updateImage(int x, int y, int w, int h)
        if(!QApplication::focusWidget()) { //no focus, we're probably minimized
                return;
        }
+
      //kDebug(5011) << "got update" << width() << height();
-     static unsigned int frames = 0;
-     static unsigned int updates = 0;
-     static QTime time = QTime::currentTime();
-     updates++;
-     if(updates % 100 == 0)
-            kDebug(5011) << "u/s: " << updates/double(time.elapsed()) * 1000.0;
-if(x == 0 and y == 0) {
-       frames++;
-     if(frames % 100 == 0)
-            kDebug(5011) << "f/s: " << frames/double(time.elapsed()) * 1000.0;
-}
 
     m_x = x;
     m_y = y;
@@ -308,8 +288,8 @@ if(x == 0 and y == 0) {
 
     if (m_horizontalFactor != 1.0 || m_verticalFactor != 1.0) {
         // If the view is scaled, grow the update rectangle to avoid artifacts
-        int x_extrapixels = 1.0/m_horizontalFactor+1;
-        int y_extrapixels = 1.0/m_verticalFactor+1;
+        int x_extrapixels = 1.0/m_horizontalFactor + 1;
+        int y_extrapixels = 1.0/m_verticalFactor + 1;
 
         m_x-=x_extrapixels;
         m_y-=y_extrapixels;
@@ -332,12 +312,7 @@ if(x == 0 and y == 0) {
 //         emit framebufferSizeChanged(m_frame.width(), m_frame.height());
         emit connected();
         
-        if (m_scale) {
-            if (parentWidget())
-                scaleResize(parentWidget()->width(), parentWidget()->height());
-           else
-                scaleResize(width(), height());
-        } 
+               resize(width(), height());
         
         m_initDone = true;
 
@@ -347,16 +322,7 @@ if(x == 0 and y == 0) {
     if ((y == 0 && x == 0) && (m_frame.size() != old_frame_size)) {
            old_frame_size = m_frame.size();
         kDebug(5011) << "Updating framebuffer size";
-        if (m_scale) {
-            //setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
-            if (parentWidget())
-                scaleResize(parentWidget()->width(), parentWidget()->height());
-        } else {
-            kDebug(5011) << "Resizing: " << m_frame.width() << m_frame.height();
-            resize(m_frame.width(), m_frame.height());
-            //setMaximumSize(m_frame.width(), m_frame.height()); //This is a hack to force Qt to center the view in the scroll area
-            //setMinimumSize(m_frame.width(), m_frame.height());
-        }
+               setZoomLevel();
         emit framebufferSizeChanged(m_frame.width(), m_frame.height());
     }
 
@@ -384,25 +350,36 @@ void VncView::showDotCursor(DotCursorState state)
     setCursor(state == CursorOn ? localDotCursor() : Qt::BlankCursor);
 }
 
-void VncView::enableScaling(bool scale)
+//level should be in [0, 100]
+void VncView::setZoomLevel(int level)
 {
-    RemoteView::enableScaling(scale);
+       Q_ASSERT(parentWidget() != 0);
 
-    if (scale) {
-        //setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
-        //setMinimumSize(1, 1);
-        if (parentWidget())
-            scaleResize(parentWidget()->width(), parentWidget()->height());
-           else
-               scaleResize(width(), height());
-    } else {
-        m_verticalFactor = 1.0;
-        m_horizontalFactor = 1.0;
+       if(level == -1) { //handle resize
+               resize(m_frame.width()*m_horizontalFactor, m_frame.height()*m_verticalFactor);
+               return;
+       }
 
-        //setMaximumSize(m_frame.width(), m_frame.height()); //This is a hack to force Qt to center the view in the scroll area
-        //setMinimumSize(m_frame.width(), m_frame.height());
-        resize(m_frame.width(), m_frame.height());
-    }
+       double magnification;
+       if(level == 100) {
+               magnification = 2.0;
+       } else if(level >= 90) {
+               magnification = 1.0;
+       } else {
+               const double min_horiz_magnification = double(parentWidget()->width())/m_frame.width();
+               const double min_vert_magnification = double(parentWidget()->height())/m_frame.height();
+               const double fit_screen_magnification = qMin(min_horiz_magnification, min_vert_magnification);
+
+               //level=90 => magnification=1.0, level=0 => magnification=fit_screen_magnification
+               magnification = (level)/90.0*(1.0 - fit_screen_magnification) + fit_screen_magnification;
+       }
+
+       if(magnification < 0                    //remote display smaller than local?
+       or magnification != magnification)      //nan
+               magnification = 1.0;
+       
+       m_verticalFactor = m_horizontalFactor = magnification;
+       resize(m_frame.width()*magnification, m_frame.height()*magnification);
 }
 
 void VncView::setCut(const QString &text)
@@ -426,13 +403,17 @@ void VncView::paintEvent(QPaintEvent *event)
 
     QPainter painter(this);
 
+       Qt::TransformationMode transformation_mode = Qt::SmoothTransformation;
+       if( m_horizontalFactor >= 1.0 )
+               transformation_mode = Qt::FastTransformation;
+
     if (m_repaint and !force_full_repaint) {
 //         kDebug(5011) << "normal repaint";
         painter.drawImage(QRect(qRound(m_x*m_horizontalFactor), qRound(m_y*m_verticalFactor),
                                 qRound(m_w*m_horizontalFactor), qRound(m_h*m_verticalFactor)), 
                           m_frame.copy(m_x, m_y, m_w, m_h).scaled(qRound(m_w*m_horizontalFactor), 
                                                                   qRound(m_h*m_verticalFactor),
-                                                                  Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
+                                                                  Qt::IgnoreAspectRatio, transformation_mode));
     } else {
          //kDebug(5011) << "resize repaint";
         QRect rect = event->rect();
@@ -444,24 +425,32 @@ void VncView::paintEvent(QPaintEvent *event)
             const int sh = rect.height()/m_verticalFactor;
             painter.drawImage(rect, 
                               m_frame.copy(sx, sy, sw, sh).scaled(rect.width(), rect.height(),
-                                                                  Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
+                                                                  Qt::IgnoreAspectRatio, transformation_mode));
         } else {
              kDebug(5011) << "Full repaint" << width() << height() << m_frame.width() << m_frame.height();
             painter.drawImage(QRect(0, 0, width(), height()), 
                               m_frame.scaled(m_frame.width() * m_horizontalFactor, m_frame.height() * m_verticalFactor,
-                                             Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
-           force_full_repaint = false;
+                                             Qt::IgnoreAspectRatio, transformation_mode));
+                       force_full_repaint = false;
         }
     }
 
+       //draw local cursor ourselves, normal mouse pointer doesn't deal with scrolling
+       if((m_dotCursorState == CursorOn) || m_forceLocalCursor) {
+#if QT_VERSION >= 0x040500
+               painter.setCompositionMode(QPainter::RasterOp_SourceXorDestination);
+#endif
+               //rectangle size includes 1px pen width
+               painter.drawRect(cursor_x*m_horizontalFactor - CURSOR_SIZE/2, cursor_y*m_verticalFactor - CURSOR_SIZE/2, CURSOR_SIZE-1, CURSOR_SIZE-1);
+       }
+
     RemoteView::paintEvent(event);
 }
 
 void VncView::resizeEvent(QResizeEvent *event)
 {
     RemoteView::resizeEvent(event);
-    scaleResize(event->size().width(), event->size().height());
-    forceFullRepaint();
+    update();
 }
 
 bool VncView::event(QEvent *event)
@@ -503,9 +492,6 @@ void VncView::mouseEventHandler(QMouseEvent *e)
        static QTime press_time;
        static QTime up_time; //used for double clicks/tap&drag, for time after first tap
 
-       const int TAP_PRESS_TIME = 180;
-       const int DOUBLE_TAP_UP_TIME = 500;
-
        if(!e) { //flush held taps
                if(tap_detected) {
                        m_buttonMask |= 0x01;
@@ -532,60 +518,65 @@ void VncView::mouseEventHandler(QMouseEvent *e)
        cursor_y = qRound(e->y()/m_verticalFactor);
        vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask); // plain move event
 
-       if(disable_tapping) { //only move cursor
-               e->ignore();
-               return;
-       }
-
-       if(e->type() == QEvent::MouseButtonPress or e->type() == QEvent::MouseButtonDblClick) {
-               press_time.start();
-               if(tap_detected and up_time.elapsed() < DOUBLE_TAP_UP_TIME) {
-                       tap_detected = false;
-                       double_tap_detected = true;
+       if(!disable_tapping and e->button() == Qt::LeftButton) { //implement touchpad-like input for left button
+               if(e->type() == QEvent::MouseButtonPress or e->type() == QEvent::MouseButtonDblClick) {
+                       press_time.start();
+                       if(tap_detected and up_time.elapsed() < DOUBLE_TAP_UP_TIME) {
+                               tap_detected = false;
+                               double_tap_detected = true;
+
+                               QTimer::singleShot(TAP_PRESS_TIME, this, SLOT(mouseEventHandler()));
+                       }
+               } else if(e->type() == QEvent::MouseButtonRelease) {
+                       if(tap_drag_detected) {
+                               m_buttonMask &= 0xfe;
+                               vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
+                               tap_drag_detected = false;
+                       } else if(double_tap_detected) { //double click
+                               double_tap_detected = false;
+
+                               m_buttonMask |= 0x01;
+                               vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
+                               m_buttonMask &= 0xfe;
+                               vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
+                               m_buttonMask |= 0x01;
+                               vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
+                               m_buttonMask &= 0xfe;
+                               vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
+                       } else if(press_time.elapsed() < TAP_PRESS_TIME) { //tap
+                               up_time.start();
+                               tap_detected = true;
+                               QTimer::singleShot(DOUBLE_TAP_UP_TIME, this, SLOT(mouseEventHandler()));
+                       }
 
-                       QTimer::singleShot(TAP_PRESS_TIME, this, SLOT(mouseEventHandler()));
                }
-       } else if(e->type() == QEvent::MouseButtonRelease) {
-               if(tap_drag_detected) {
-                       m_buttonMask &= 0xfe;
-                       vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
-                       tap_drag_detected = false;
-               } else if(double_tap_detected) { //double click
-                       double_tap_detected = false;
-
-                       m_buttonMask |= 0x01;
-                       vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
-                       m_buttonMask &= 0xfe;
-                       vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
-                       m_buttonMask |= 0x01;
-                       vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
-                       m_buttonMask &= 0xfe;
-                       vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
-               } else if(press_time.elapsed() < TAP_PRESS_TIME) { //tap
-                       up_time.start();
-                       tap_detected = true;
-                       QTimer::singleShot(DOUBLE_TAP_UP_TIME, this, SLOT(mouseEventHandler()));
+       } else { //middle or right button, send directly
+               if ((e->type() == QEvent::MouseButtonPress)) {
+                   if (e->button() & Qt::MidButton)
+                       m_buttonMask |= 0x02;
+                   if (e->button() & Qt::RightButton)
+                       m_buttonMask |= 0x04;
+               } else if (e->type() == QEvent::MouseButtonRelease) {
+                   if (e->button() & Qt::MidButton)
+                       m_buttonMask &= 0xfd;
+                   if (e->button() & Qt::RightButton)
+                       m_buttonMask &= 0xfb;
                }
-
+               vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
        }
 
-/* for reference:
-    if (e->type() != QEvent::MouseMove) {
-        if ((e->type() == QEvent::MouseButtonPress)) {
-            if (e->button() & Qt::LeftButton)
-                m_buttonMask |= 0x01;
-            if (e->button() & Qt::MidButton)
-                m_buttonMask |= 0x02;
-            if (e->button() & Qt::RightButton)
-                m_buttonMask |= 0x04;
-        } else if (e->type() == QEvent::MouseButtonRelease) {
-            if (e->button() & Qt::LeftButton)
-                m_buttonMask &= 0xfe;
-            if (e->button() & Qt::MidButton)
-                m_buttonMask &= 0xfd;
-            if (e->button() & Qt::RightButton)
-                m_buttonMask &= 0xfb;
-       */
+       //prevent local cursor artifacts
+       static int old_cursor_x = cursor_x;
+       static int old_cursor_y = cursor_y;
+       if(((m_dotCursorState == CursorOn) || m_forceLocalCursor)
+       and (cursor_x != old_cursor_x or cursor_y != old_cursor_y)) {
+               //clear last position
+               repaint(old_cursor_x*m_horizontalFactor - CURSOR_SIZE/2, old_cursor_y*m_verticalFactor - CURSOR_SIZE/2, CURSOR_SIZE, CURSOR_SIZE);
+               //and refresh new one
+               repaint(cursor_x*m_horizontalFactor - CURSOR_SIZE/2, cursor_y*m_verticalFactor - CURSOR_SIZE/2, CURSOR_SIZE, CURSOR_SIZE);
+
+               old_cursor_x = cursor_x; old_cursor_y = cursor_y;
+       }
 }
 
 void VncView::wheelEventHandler(QWheelEvent *event)
@@ -599,7 +590,6 @@ void VncView::wheelEventHandler(QWheelEvent *event)
     const int x = qRound(event->x() / m_horizontalFactor);
     const int y = qRound(event->y() / m_verticalFactor);
 
-       kDebug(5011) << "Wheelevent";
     vncThread.mouseEvent(x, y, eb | m_buttonMask);
     vncThread.mouseEvent(x, y, m_buttonMask);
 }
@@ -622,6 +612,16 @@ void VncView::keyEventHandler(QKeyEvent *e)
 
     const bool pressed = (e->type() == QEvent::KeyPress);
 
+#ifdef Q_WS_MAEMO_5
+    //don't send ISO_Level3_Shift (would break things like Win+0-9)
+    //also enable IM so symbol key works
+    if(k == 0xfe03) {
+           setAttribute(Qt::WA_InputMethodEnabled, pressed);
+           e->ignore();
+           return;
+    }
+#endif
+
     // handle modifiers
     if (k == XK_Shift_L || k == XK_Control_L || k == XK_Meta_L || k == XK_Alt_L) {
         if (pressed) {
@@ -697,8 +697,6 @@ void VncView::unpressModifiers()
 
 void VncView::clipboardSelectionChanged()
 {
-    kDebug(5011);
-
     if (m_status != Connected)
         return;
 
@@ -712,8 +710,6 @@ void VncView::clipboardSelectionChanged()
 
 void VncView::clipboardDataChanged()
 {
-    kDebug(5011);
-
     if (m_status != Connected)
         return;
 
@@ -775,6 +771,15 @@ void VncView::sendKey(Qt::Key key)
        case Qt::Key_F12:
                k = 0xffbe + int(key - Qt::Key_F1);
                break;
+       case Qt::Key_Pause:
+               k = 0xff13;
+               break;
+       case Qt::Key_Print:
+               k = 0xff61;
+               break;
+       case Qt::Key_Menu:
+               k = 0xff67;
+               break;
        case Qt::Key_Meta:
        case Qt::MetaModifier:
                k = XK_Super_L;
@@ -816,7 +821,7 @@ void VncView::sendKeySequence(QKeySequence keys)
                QString k = keys.toString().section('+', i, i);
                if(k.isEmpty())
                        break;
-               kDebug(5011) << "found key: " << k;
+               //kDebug(5011) << "found key: " << k;
                if(k == "Alt") {
                        key_list.append(Qt::Key_Alt);
                } else if(k == "Ctrl") {
@@ -842,6 +847,12 @@ void VncView::reloadSettings()
        left_zoom = settings.value("left_zoom", 0).toInt();
        right_zoom = settings.value("right_zoom", 1).toInt();
        disable_tapping = settings.value("disable_tapping", false).toBool();
+
+       bool always_show_local_cursor = settings.value("always_show_local_cursor", false).toBool();
+       if(always_show_local_cursor)
+               showDotCursor(CursorOn);
+
+       enableScaling(true);
 }
 
 //convert commitString into keyevents