X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=src%2Fvncview.cpp;h=aa021c7668554976adbcb9bc742beff15dc516ae;hb=93c71345a93a91c6bd85cb79211990cbf63f3c60;hp=9788390677d9cbcde6c58deb15c0b34762b4cf3c;hpb=cb67997c250b4b2b9c637d46065d8c86739edd2b;p=presencevnc diff --git a/src/vncview.cpp b/src/vncview.cpp index 9788390..aa021c7 100644 --- a/src/vncview.cpp +++ b/src/vncview.cpp @@ -23,26 +23,7 @@ #include "vncview.h" -#include -#include -#define KMessageBox QMessageBox -#define error(parent, message, caption) \ -critical(parent, caption, message) - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include // Definition of key modifier mask constants @@ -52,31 +33,43 @@ 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; + +//in miliseconds +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), cursor_x(0), cursor_y(0), - m_repaint(false), m_quitFlag(false), m_firstPasswordTry(true), m_dontSendClipboard(false), m_horizontalFactor(1.0), m_verticalFactor(1.0), m_forceLocalCursor(false), - force_full_repaint(false), - quality(quality) + quality(quality), + listen_port(listen_port), + transformation_mode(Qt::FastTransformation) { 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); connect(&vncThread, SIGNAL(outputErrorMessage(QString)), this, SLOT(outputErrorMessage(QString))); + //don't miss early connection failures + connect(&vncThread, SIGNAL(finished()), this, SLOT(startQuitting())); + m_clipboard = QApplication::clipboard(); connect(m_clipboard, SIGNAL(selectionChanged()), this, SLOT(clipboardSelectionChanged())); connect(m_clipboard, SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged())); @@ -94,12 +87,6 @@ VncView::~VncView() startQuitting(); } -void VncView::forceFullRepaint() -{ - force_full_repaint = true; - repaint(); -} - bool VncView::eventFilter(QObject *obj, QEvent *event) { if (m_viewOnly) { @@ -109,9 +96,13 @@ bool VncView::eventFilter(QObject *obj, QEvent *event) event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::Wheel || - event->type() == QEvent::MouseMove) + event->type() == QEvent::MouseMove) { + + event->ignore(); return true; + } } + return RemoteView::eventFilter(obj, event); } @@ -130,48 +121,28 @@ QSize VncView::minimumSizeHint() const return size(); } -void VncView::scaleResize(int w, int h) -{ - RemoteView::scaleResize(w, h); - - kDebug(5011) << "scaleResize(): " <width(), parentWidget()->height()); - else - scaleResize(width(), height()); - } + resize(width(), height()); m_initDone = true; @@ -348,22 +303,13 @@ 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(); + useFastTransformations(false); + emit framebufferSizeChanged(m_frame.width(), m_frame.height()); } - m_repaint = true; repaint(qRound(m_x * m_horizontalFactor), qRound(m_y * m_verticalFactor), qRound(m_w * m_horizontalFactor), qRound(m_h * m_verticalFactor)); - m_repaint = false; } void VncView::setViewOnly(bool viewOnly) @@ -385,25 +331,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) @@ -416,57 +373,40 @@ void VncView::setCut(const QString &text) void VncView::paintEvent(QPaintEvent *event) { - //kDebug(5011) << "paint event: x: " << m_x << ", y: " << m_y << ", w: " << m_w << ", h: " << m_h; if (m_frame.isNull() || m_frame.format() == QImage::Format_Invalid) { - kDebug(5011) << "no valid image to paint"; + //no valid image to paint RemoteView::paintEvent(event); return; } event->accept(); + const QRect update_rect = event->rect(); QPainter painter(this); + if (update_rect != rect()) { + // kDebug(5011) << "Partial repaint"; + const int sx = qRound(update_rect.x()/m_horizontalFactor); + const int sy = qRound(update_rect.y()/m_verticalFactor); + const int sw = qRound(update_rect.width()/m_horizontalFactor); + const int sh = qRound(update_rect.height()/m_verticalFactor); + + painter.drawImage(update_rect, + m_frame.copy(sx, sy, sw, sh) + .scaled(update_rect.size(), Qt::IgnoreAspectRatio, transformation_mode)); + } else { + //kDebug(5011) << "Full repaint" << width() << height() << m_frame.width() << m_frame.height(); - 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)); - } else { - //kDebug(5011) << "resize repaint"; - QRect rect = event->rect(); - if (!force_full_repaint and (rect.width() != width() || rect.height() != height())) { - // kDebug(5011) << "Partial repaint"; - const int sx = rect.x()/m_horizontalFactor; - const int sy = rect.y()/m_verticalFactor; - const int sw = rect.width()/m_horizontalFactor; - 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)); - } 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; - } + painter.drawImage(rect(), + m_frame.scaled(size(), Qt::IgnoreAspectRatio, transformation_mode)); } //draw local cursor ourselves, normal mouse pointer doesn't deal with scrolling if((m_dotCursorState == CursorOn) || m_forceLocalCursor) { - const uchar bits[] = { 0xff, 0x8e, 0x8e, 0x8e, 0xff }; - const bool little_endian = (Q_BYTE_ORDER == Q_LITTLE_ENDIAN); - const QBitmap cursorBitmap = QBitmap::fromData(QSize(5,5), bits , little_endian?QImage::Format_Mono:QImage::Format_MonoLSB); - #if QT_VERSION >= 0x040500 painter.setCompositionMode(QPainter::RasterOp_SourceXorDestination); #endif - - painter.drawPixmap(cursor_x-2, cursor_y-2, cursorBitmap); - //TODO update position of last cursor_x/y to avoid artifacts + //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); @@ -475,38 +415,34 @@ void VncView::paintEvent(QPaintEvent *event) void VncView::resizeEvent(QResizeEvent *event) { RemoteView::resizeEvent(event); - scaleResize(event->size().width(), event->size().height()); - forceFullRepaint(); + update(); } bool VncView::event(QEvent *event) { - switch (event->type()) { - case QEvent::KeyPress: - case QEvent::KeyRelease: -// kDebug(5011) << "keyEvent"; - keyEventHandler(static_cast(event)); - return true; - break; - case QEvent::MouseButtonDblClick: - case QEvent::MouseButtonPress: - case QEvent::MouseButtonRelease: - case QEvent::MouseMove: -// kDebug(5011) << "mouseEvent"; - mouseEventHandler(static_cast(event)); - return true; - break; - case QEvent::Wheel: -// kDebug(5011) << "wheelEvent"; - wheelEventHandler(static_cast(event)); - return true; - break; - case QEvent::WindowActivate: //input panel may have been closed, prevent IM from interfering with hardware keyboard - setAttribute(Qt::WA_InputMethodEnabled, false); - //fall through - default: - return RemoteView::event(event); - } + switch (event->type()) { + case QEvent::KeyPress: + case QEvent::KeyRelease: + keyEventHandler(static_cast(event)); + return true; + + case QEvent::MouseButtonDblClick: + case QEvent::MouseButtonPress: + case QEvent::MouseButtonRelease: + case QEvent::MouseMove: + mouseEventHandler(static_cast(event)); + return true; + + case QEvent::Wheel: + wheelEventHandler(static_cast(event)); + return true; + + case QEvent::WindowActivate: //input panel may have been closed, prevent IM from interfering with hardware keyboard + setAttribute(Qt::WA_InputMethodEnabled, false); + //fall through + default: + return RemoteView::event(event); + } } //call with e == 0 to flush held events @@ -518,9 +454,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; @@ -547,12 +480,7 @@ 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->button() == Qt::LeftButton) { //implement touchpad-like input for left button + 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) { @@ -585,19 +513,32 @@ void VncView::mouseEventHandler(QMouseEvent *e) } } 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; + 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); } + + //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) @@ -661,7 +602,7 @@ void VncView::keyEventHandler(QKeyEvent *e) else if(e->key() == Qt::Key_F7) current_zoom = right_zoom; else if (k) { - // kDebug(5011) << "got '" << e->text() << "'."; + // kDebug(5011) << "got '" << e->text() << "'."; vncThread.keyEvent(k, pressed); } else { kDebug(5011) << "nativeVirtualKey() for '" << e->text() << "' failed."; @@ -718,8 +659,6 @@ void VncView::unpressModifiers() void VncView::clipboardSelectionChanged() { - //kDebug(5011); - if (m_status != Connected) return; @@ -733,8 +672,6 @@ void VncView::clipboardSelectionChanged() void VncView::clipboardDataChanged() { - //kDebug(5011); - if (m_status != Connected) return; @@ -796,6 +733,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; @@ -833,10 +779,12 @@ void VncView::sendKeySequence(QKeySequence keys) //to get at individual key presses, we split 'keys' into its components QList key_list; - for(int i = 0; ; i++) { - QString k = keys.toString().section('+', i, i); + int pos = 0; + while(true) { + QString k = keys.toString().section('+', pos, pos); if(k.isEmpty()) break; + //kDebug(5011) << "found key: " << k; if(k == "Alt") { key_list.append(Qt::Key_Alt); @@ -847,6 +795,8 @@ void VncView::sendKeySequence(QKeySequence keys) } else { key_list.append(QKeySequence(k)[0]); } + + pos++; } for(int i = 0; i < key_list.count(); i++) @@ -863,6 +813,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 @@ -874,7 +830,7 @@ void VncView::inputMethodEvent(QInputMethodEvent *event) //kDebug(5011) << event->commitString() << "|" << event->preeditString() << "|" << event->replacementLength() << "|" << event->replacementStart(); QString letters = event->commitString(); for(int i = 0; i < letters.length(); i++) { - char k = letters.at(i).toLatin1(); //works with all 'normal' keys, not umlauts. + char k = letters.at(i).toLatin1(); if(!k) { kDebug(5011) << "unhandled key"; continue; @@ -884,5 +840,12 @@ void VncView::inputMethodEvent(QInputMethodEvent *event) } } - -#include "moc_vncview.cpp" +void VncView::useFastTransformations(bool enabled) +{ + if(enabled or zoomFactor() >= 1.0) { + transformation_mode = Qt::FastTransformation; + } else { + transformation_mode = Qt::SmoothTransformation; + update(); + } +}