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 ****************************************************************************/
26 #include <QMessageBox>
27 #include <QInputDialog>
28 #define KMessageBox QMessageBox
29 #define error(parent, message, caption) \
30 critical(parent, caption, message)
32 #include <QApplication>
35 #include <QMouseEvent>
41 // Definition of key modifier mask constants
42 #define KMOD_Alt_R 0x01
43 #define KMOD_Alt_L 0x02
44 #define KMOD_Meta_L 0x04
45 #define KMOD_Control_L 0x08
46 #define KMOD_Shift_L 0x10
48 VncView::VncView(QWidget *parent, const KUrl &url, RemoteView::Quality quality)
56 m_firstPasswordTry(true),
57 m_dontSendClipboard(false),
58 m_horizontalFactor(1.0),
59 m_verticalFactor(1.0),
60 m_forceLocalCursor(false),
61 force_full_repaint(false),
68 connect(&vncThread, SIGNAL(imageUpdated(int, int, int, int)), this, SLOT(updateImage(int, int, int, int)), Qt::BlockingQueuedConnection);
69 connect(&vncThread, SIGNAL(gotCut(const QString&)), this, SLOT(setCut(const QString&)), Qt::BlockingQueuedConnection);
70 connect(&vncThread, SIGNAL(passwordRequest()), this, SLOT(requestPassword()), Qt::BlockingQueuedConnection);
71 connect(&vncThread, SIGNAL(outputErrorMessage(QString)), this, SLOT(outputErrorMessage(QString)));
73 m_clipboard = QApplication::clipboard();
74 connect(m_clipboard, SIGNAL(selectionChanged()), this, SLOT(clipboardSelectionChanged()));
75 connect(m_clipboard, SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
84 // Disconnect all signals so that we don't get any more callbacks from the client thread
85 vncThread.disconnect();
90 void VncView::forceFullRepaint()
92 kDebug(5011) << "forceFullRepaint()";
93 force_full_repaint = true;
97 bool VncView::eventFilter(QObject *obj, QEvent *event)
100 if (event->type() == QEvent::KeyPress ||
101 event->type() == QEvent::KeyRelease ||
102 event->type() == QEvent::MouseButtonDblClick ||
103 event->type() == QEvent::MouseButtonPress ||
104 event->type() == QEvent::MouseButtonRelease ||
105 event->type() == QEvent::Wheel ||
106 event->type() == QEvent::MouseMove)
109 return RemoteView::eventFilter(obj, event);
112 QSize VncView::framebufferSize()
114 return m_frame.size();
117 QSize VncView::sizeHint() const
122 QSize VncView::minimumSizeHint() const
127 void VncView::scaleResize(int w, int h)
129 RemoteView::scaleResize(w, h);
131 kDebug(5011) << "scaleResize(): " <<w << h;
133 m_verticalFactor = (qreal) h / m_frame.height();
134 m_horizontalFactor = (qreal) w / m_frame.width();
136 m_verticalFactor = m_horizontalFactor = qMin(m_verticalFactor, m_horizontalFactor);
138 const qreal newW = m_frame.width() * m_horizontalFactor;
139 const qreal newH = m_frame.height() * m_verticalFactor;
141 setMaximumSize(newW, newH); //This is a hack to force Qt to center the view in the scroll area
142 //also causes the widget's size to flicker
149 void VncView::startQuitting()
151 kDebug(5011) << "about to quit";
153 const bool connected = status() == RemoteView::Connected;
155 setStatus(Disconnecting);
165 const bool quitSuccess = vncThread.wait(500);
167 kDebug(5011) << "startQuitting(): Quit VNC thread success:" << quitSuccess;
169 setStatus(Disconnected);
172 bool VncView::isQuitting()
177 bool VncView::start()
179 vncThread.setHost(m_host);
180 vncThread.setPort(m_port);
182 vncThread.setQuality(quality);
184 // set local cursor on by default because low quality mostly means slow internet connection
185 if (quality == RemoteView::Low) {
186 showDotCursor(RemoteView::CursorOn);
189 setStatus(Connecting);
195 bool VncView::supportsScaling() const
200 bool VncView::supportsLocalCursor() const
205 void VncView::requestPassword()
207 kDebug(5011) << "request password";
209 setStatus(Authenticating);
211 if (!m_url.password().isNull()) {
212 vncThread.setPassword(m_url.password());
217 QString password = QInputDialog::getText(this, //krazy:exclude=qclasses
218 tr("Password required"),
219 tr("Please enter the password for the remote desktop:"),
220 QLineEdit::Password, QString(), &ok);
221 m_firstPasswordTry = false;
223 vncThread.setPassword(password);
229 void VncView::outputErrorMessage(const QString &message)
231 kDebug(5011) << message;
233 if (message == "INTERNAL:APPLE_VNC_COMPATIBILTY") {
234 setCursor(localDotCursor());
235 m_forceLocalCursor = true;
241 emit errorMessage(i18n("VNC failure"), message);
244 void VncView::updateImage(int x, int y, int w, int h)
246 // kDebug(5011) << "got update" << width() << height();
253 if (m_horizontalFactor != 1.0 || m_verticalFactor != 1.0) {
254 // If the view is scaled, grow the update rectangle to avoid artifacts
261 m_frame = vncThread.image();
264 setAttribute(Qt::WA_StaticContents);
265 setAttribute(Qt::WA_OpaquePaintEvent);
266 installEventFilter(this);
268 setCursor(((m_dotCursorState == CursorOn) || m_forceLocalCursor) ? localDotCursor() : Qt::BlankCursor);
270 setMouseTracking(true); // get mouse events even when there is no mousebutton pressed
271 setFocusPolicy(Qt::WheelFocus);
272 setStatus(Connected);
273 // emit framebufferSizeChanged(m_frame.width(), m_frame.height());
278 scaleResize(parentWidget()->width(), parentWidget()->height());
280 scaleResize(width(), height());
287 static QSize old_frame_size = QSize();
288 if ((y == 0 && x == 0) && (m_frame.size() != old_frame_size)) {
289 old_frame_size = m_frame.size();
290 kDebug(5011) << "Updating framebuffer size";
292 //setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
294 scaleResize(parentWidget()->width(), parentWidget()->height());
296 kDebug(5011) << "Resizing: " << m_frame.width() << m_frame.height();
297 resize(m_frame.width(), m_frame.height());
298 //setMaximumSize(m_frame.width(), m_frame.height()); //This is a hack to force Qt to center the view in the scroll area
299 //setMinimumSize(m_frame.width(), m_frame.height());
301 emit framebufferSizeChanged(m_frame.width(), m_frame.height());
305 repaint(qRound(m_x * m_horizontalFactor), qRound(m_y * m_verticalFactor), qRound(m_w * m_horizontalFactor), qRound(m_h * m_verticalFactor));
309 void VncView::setViewOnly(bool viewOnly)
311 RemoteView::setViewOnly(viewOnly);
313 m_dontSendClipboard = viewOnly;
316 setCursor(Qt::ArrowCursor);
318 setCursor(m_dotCursorState == CursorOn ? localDotCursor() : Qt::BlankCursor);
321 void VncView::showDotCursor(DotCursorState state)
323 RemoteView::showDotCursor(state);
325 setCursor(state == CursorOn ? localDotCursor() : Qt::BlankCursor);
328 void VncView::enableScaling(bool scale)
330 RemoteView::enableScaling(scale);
333 //setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
334 //setMinimumSize(1, 1);
336 scaleResize(parentWidget()->width(), parentWidget()->height());
338 scaleResize(width(), height());
340 m_verticalFactor = 1.0;
341 m_horizontalFactor = 1.0;
343 //setMaximumSize(m_frame.width(), m_frame.height()); //This is a hack to force Qt to center the view in the scroll area
344 //setMinimumSize(m_frame.width(), m_frame.height());
345 resize(m_frame.width(), m_frame.height());
349 void VncView::setCut(const QString &text)
351 m_dontSendClipboard = true;
352 m_clipboard->setText(text, QClipboard::Clipboard);
353 m_clipboard->setText(text, QClipboard::Selection);
354 m_dontSendClipboard = false;
357 void VncView::paintEvent(QPaintEvent *event)
359 //kDebug(5011) << "paint event: x: " << m_x << ", y: " << m_y << ", w: " << m_w << ", h: " << m_h;
360 if (m_frame.isNull() || m_frame.format() == QImage::Format_Invalid) {
361 kDebug(5011) << "no valid image to paint";
362 RemoteView::paintEvent(event);
368 QPainter painter(this);
370 if (m_repaint and !force_full_repaint) {
371 // kDebug(5011) << "normal repaint";
372 painter.drawImage(QRect(qRound(m_x*m_horizontalFactor), qRound(m_y*m_verticalFactor),
373 qRound(m_w*m_horizontalFactor), qRound(m_h*m_verticalFactor)),
374 m_frame.copy(m_x, m_y, m_w, m_h).scaled(qRound(m_w*m_horizontalFactor),
375 qRound(m_h*m_verticalFactor),
376 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
378 kDebug(5011) << "resize repaint";
379 QRect rect = event->rect();
380 if (!force_full_repaint and (rect.width() != width() || rect.height() != height())) {
381 kDebug(5011) << "Partial repaint";
382 const int sx = rect.x()/m_horizontalFactor;
383 const int sy = rect.y()/m_verticalFactor;
384 const int sw = rect.width()/m_horizontalFactor;
385 const int sh = rect.height()/m_verticalFactor;
386 painter.drawImage(rect,
387 m_frame.copy(sx, sy, sw, sh).scaled(rect.width(), rect.height(),
388 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
390 kDebug(5011) << "Full repaint" << width() << height() << m_frame.width() << m_frame.height();
391 painter.drawImage(QRect(0, 0, width(), height()),
392 m_frame.scaled(m_frame.width() * m_horizontalFactor, m_frame.height() * m_verticalFactor,
393 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
394 force_full_repaint = false;
398 RemoteView::paintEvent(event);
401 void VncView::resizeEvent(QResizeEvent *event)
403 RemoteView::resizeEvent(event);
404 scaleResize(event->size().width(), event->size().height());
408 bool VncView::event(QEvent *event)
410 switch (event->type()) {
411 case QEvent::KeyPress:
412 case QEvent::KeyRelease:
413 // kDebug(5011) << "keyEvent";
414 keyEventHandler(static_cast<QKeyEvent*>(event));
417 case QEvent::MouseButtonDblClick:
418 case QEvent::MouseButtonPress:
419 case QEvent::MouseButtonRelease:
420 case QEvent::MouseMove:
421 // kDebug(5011) << "mouseEvent";
422 mouseEventHandler(static_cast<QMouseEvent*>(event));
426 // kDebug(5011) << "wheelEvent";
427 wheelEventHandler(static_cast<QWheelEvent*>(event));
431 return RemoteView::event(event);
435 //call with e == 0 to flush held events
436 void VncView::mouseEventHandler(QMouseEvent *e)
438 static bool tap_detected = false;
439 static bool double_tap_detected = false;
440 static bool tap_drag_detected = false;
441 static QTime press_time;
442 static QTime up_time; //used for double clicks/tap&drag, for time after first tap
444 const int TAP_PRESS_TIME = 200;
445 const int DOUBLE_TAP_UP_TIME = 500;
447 if(!e) { //flush held taps
449 m_buttonMask |= 0x01;
450 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
451 m_buttonMask &= 0xfe;
452 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
453 tap_detected = false;
454 } else if(double_tap_detected and press_time.elapsed() > TAP_PRESS_TIME) { //got tap + another press -> tap & drag
455 m_buttonMask |= 0x01;
456 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
457 double_tap_detected = false;
458 tap_drag_detected = true;
464 if(e->x() < 0 or e->y() < 0) { //QScrollArea tends to send invalid events sometimes...
469 cursor_x = qRound(e->x()/m_horizontalFactor);
470 cursor_y = qRound(e->y()/m_verticalFactor);
471 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask); // plain move event
473 if(disable_tapping) { //only move cursor
478 if(e->type() == QEvent::MouseButtonPress or e->type() == QEvent::MouseButtonDblClick) {
480 if(tap_detected and up_time.elapsed() < DOUBLE_TAP_UP_TIME) {
481 tap_detected = false;
482 double_tap_detected = true;
484 QTimer::singleShot(TAP_PRESS_TIME, this, SLOT(mouseEventHandler()));
486 } else if(e->type() == QEvent::MouseButtonRelease) {
487 if(tap_drag_detected) {
488 m_buttonMask &= 0xfe;
489 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
490 tap_drag_detected = false;
491 } else if(double_tap_detected) { //double click
492 double_tap_detected = false;
494 m_buttonMask |= 0x01;
495 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
496 m_buttonMask &= 0xfe;
497 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
498 m_buttonMask |= 0x01;
499 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
500 m_buttonMask &= 0xfe;
501 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
502 } else if(press_time.elapsed() < TAP_PRESS_TIME) { //tap
505 QTimer::singleShot(DOUBLE_TAP_UP_TIME, this, SLOT(mouseEventHandler()));
511 if (e->type() != QEvent::MouseMove) {
512 if ((e->type() == QEvent::MouseButtonPress)) {
513 if (e->button() & Qt::LeftButton)
514 m_buttonMask |= 0x01;
515 if (e->button() & Qt::MidButton)
516 m_buttonMask |= 0x02;
517 if (e->button() & Qt::RightButton)
518 m_buttonMask |= 0x04;
519 } else if (e->type() == QEvent::MouseButtonRelease) {
520 if (e->button() & Qt::LeftButton)
521 m_buttonMask &= 0xfe;
522 if (e->button() & Qt::MidButton)
523 m_buttonMask &= 0xfd;
524 if (e->button() & Qt::RightButton)
525 m_buttonMask &= 0xfb;
529 void VncView::wheelEventHandler(QWheelEvent *event)
532 if (event->delta() < 0)
537 const int x = qRound(event->x() / m_horizontalFactor);
538 const int y = qRound(event->y() / m_verticalFactor);
540 kDebug(5011) << "Wheelevent";
541 vncThread.mouseEvent(x, y, eb | m_buttonMask);
542 vncThread.mouseEvent(x, y, m_buttonMask);
545 void VncView::keyEventHandler(QKeyEvent *e)
547 // strip away autorepeating KeyRelease; see bug #206598
548 if (e->isAutoRepeat() && (e->type() == QEvent::KeyRelease))
551 // parts of this code are based on http://italc.sourcearchive.com/documentation/1.0.9.1/vncview_8cpp-source.html
552 rfbKeySym k = e->nativeVirtualKey();
554 // we do not handle Key_Backtab separately as the Shift-modifier
555 // is already enabled
556 if (e->key() == Qt::Key_Backtab) {
560 const bool pressed = (e->type() == QEvent::KeyPress);
563 if (k == XK_Shift_L || k == XK_Control_L || k == XK_Meta_L || k == XK_Alt_L) {
566 } else if (m_mods.contains(k)) {
574 int current_zoom = -1;
575 if(e->key() == Qt::Key_F8)
576 current_zoom = left_zoom;
577 else if(e->key() == Qt::Key_F7)
578 current_zoom = right_zoom;
580 vncThread.keyEvent(k, pressed);
582 if(current_zoom == -1)
585 //handle zoom buttons
586 if(current_zoom == 0) { //left click
588 m_buttonMask |= 0x01;
590 m_buttonMask &= 0xfe;
591 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
592 } else if(current_zoom == 1) { //right click
594 m_buttonMask |= 0x04;
596 m_buttonMask &= 0xfb;
597 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
598 } else if(current_zoom == 2) { //middle click
600 m_buttonMask |= 0x02;
602 m_buttonMask &= 0xfd;
603 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
604 } else if(current_zoom == 3 and pressed) { //wheel up
606 vncThread.mouseEvent(cursor_x, cursor_y, eb | m_buttonMask);
607 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
608 } else if(current_zoom == 4 and pressed) { //wheel down
610 vncThread.mouseEvent(cursor_x, cursor_y, eb | m_buttonMask);
611 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
612 } else if(current_zoom == 5) { //page up
613 vncThread.keyEvent(0xff55, pressed);
614 } else if(current_zoom == 6) { //page down
615 vncThread.keyEvent(0xff56, pressed);
619 void VncView::unpressModifiers()
621 const QList<unsigned int> keys = m_mods.keys();
622 QList<unsigned int>::const_iterator it = keys.constBegin();
623 while (it != keys.end()) {
624 vncThread.keyEvent(*it, false);
630 void VncView::clipboardSelectionChanged()
634 if (m_status != Connected)
637 if (m_clipboard->ownsSelection() || m_dontSendClipboard)
640 const QString text = m_clipboard->text(QClipboard::Selection);
642 vncThread.clientCut(text);
645 void VncView::clipboardDataChanged()
649 if (m_status != Connected)
652 if (m_clipboard->ownsClipboard() || m_dontSendClipboard)
655 const QString text = m_clipboard->text(QClipboard::Clipboard);
657 vncThread.clientCut(text);
661 void VncView::sendKey(Qt::Key key)
663 int k = 0; //X11 keysym
674 case Qt::Key_PageDown:
677 case Qt::Key_Meta: //TODO: test this.
684 kDebug(5011) << "unhandled Qt::Key value " << key;
688 if (k == XK_Shift_L || k == XK_Control_L || k == XK_Meta_L || k == XK_Alt_L || k == XK_Super_L) {
689 if (m_mods.contains(k)) { //release
691 vncThread.keyEvent(k, false);
694 vncThread.keyEvent(k, true);
696 } else { //normal key
697 vncThread.keyEvent(k, true);
698 vncThread.keyEvent(k, false);
702 void VncView::reloadSettings()
705 left_zoom = settings.value("left_zoom", 0).toInt();
706 right_zoom = settings.value("right_zoom", 1).toInt();
707 disable_tapping = settings.value("disable_tapping", false).toBool();
710 #include "moc_vncview.cpp"