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();
87 disconnect(&vncThread, SIGNAL(imageUpdated(int, int, int, int)), this, SLOT(updateImage(int, int, int, int)));
88 disconnect(&vncThread, SIGNAL(gotCut(const QString&)), this, SLOT(setCut(const QString&)));
89 disconnect(&vncThread, SIGNAL(passwordRequest()), this, SLOT(requestPassword()));
90 disconnect(&vncThread, SIGNAL(outputErrorMessage(QString)), this, SLOT(outputErrorMessage(QString)));
96 void VncView::forceFullRepaint()
98 force_full_repaint = true;
102 bool VncView::eventFilter(QObject *obj, QEvent *event)
105 if (event->type() == QEvent::KeyPress ||
106 event->type() == QEvent::KeyRelease ||
107 event->type() == QEvent::MouseButtonDblClick ||
108 event->type() == QEvent::MouseButtonPress ||
109 event->type() == QEvent::MouseButtonRelease ||
110 event->type() == QEvent::Wheel ||
111 event->type() == QEvent::MouseMove)
114 return RemoteView::eventFilter(obj, event);
117 QSize VncView::framebufferSize()
119 return m_frame.size();
122 QSize VncView::sizeHint() const
127 QSize VncView::minimumSizeHint() const
132 void VncView::scaleResize(int w, int h)
134 RemoteView::scaleResize(w, h);
136 kDebug(5011) << "scaleResize(): " <<w << h;
138 m_verticalFactor = (qreal) h / m_frame.height();
139 m_horizontalFactor = (qreal) w / m_frame.width();
141 m_verticalFactor = m_horizontalFactor = qMin(m_verticalFactor, m_horizontalFactor);
143 const qreal newW = m_frame.width() * m_horizontalFactor;
144 const qreal newH = m_frame.height() * m_verticalFactor;
146 setMaximumSize(newW, newH); //This is a hack to force Qt to center the view in the scroll area
147 //also causes the widget's size to flicker
153 void VncView::updateConfiguration()
155 RemoteView::updateConfiguration();
157 // Update the scaling mode in case KeepAspectRatio changed
159 scaleResize(parentWidget()->width(), parentWidget()->height());
161 scaleResize(width(), height());
164 void VncView::startQuitting()
166 kDebug(5011) << "about to quit";
168 const bool connected = status() == RemoteView::Connected;
170 setStatus(Disconnecting);
180 const bool quitSuccess = vncThread.wait(500);
182 kDebug(5011) << "startQuitting(): Quit VNC thread success:" << quitSuccess;
184 setStatus(Disconnected);
187 bool VncView::isQuitting()
192 bool VncView::start()
194 vncThread.setHost(m_host);
195 vncThread.setPort(m_port);
197 vncThread.setQuality(quality);
199 // set local cursor on by default because low quality mostly means slow internet connection
200 if (quality == RemoteView::Low) {
201 showDotCursor(RemoteView::CursorOn);
204 setStatus(Connecting);
210 bool VncView::supportsScaling() const
215 bool VncView::supportsLocalCursor() const
220 void VncView::requestPassword()
222 kDebug(5011) << "request password";
224 setStatus(Authenticating);
226 if (!m_url.password().isNull()) {
227 vncThread.setPassword(m_url.password());
232 QString password = QInputDialog::getText(this, //krazy:exclude=qclasses
233 tr("Password required"),
234 tr("Please enter the password for the remote desktop:"),
235 QLineEdit::Password, QString(), &ok);
236 m_firstPasswordTry = false;
238 vncThread.setPassword(password);
244 void VncView::outputErrorMessage(const QString &message)
246 kDebug(5011) << message;
248 if (message == "INTERNAL:APPLE_VNC_COMPATIBILTY") {
249 setCursor(localDotCursor());
250 m_forceLocalCursor = true;
256 emit errorMessage(i18n("VNC failure"), message);
259 void VncView::updateImage(int x, int y, int w, int h)
261 // kDebug(5011) << "got update" << width() << height();
268 if (m_horizontalFactor != 1.0 || m_verticalFactor != 1.0) {
269 // If the view is scaled, grow the update rectangle to avoid artifacts
276 m_frame = vncThread.image();
279 setAttribute(Qt::WA_StaticContents);
280 setAttribute(Qt::WA_OpaquePaintEvent);
281 installEventFilter(this);
283 setCursor(((m_dotCursorState == CursorOn) || m_forceLocalCursor) ? localDotCursor() : Qt::BlankCursor);
285 setMouseTracking(true); // get mouse events even when there is no mousebutton pressed
286 setFocusPolicy(Qt::WheelFocus);
287 setStatus(Connected);
288 // emit framebufferSizeChanged(m_frame.width(), m_frame.height());
293 scaleResize(parentWidget()->width(), parentWidget()->height());
295 scaleResize(width(), height());
302 static QSize old_frame_size = QSize();
303 if ((y == 0 && x == 0) && (m_frame.size() != old_frame_size)) {
304 old_frame_size = m_frame.size();
305 kDebug(5011) << "Updating framebuffer size";
307 //setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
309 scaleResize(parentWidget()->width(), parentWidget()->height());
311 kDebug(5011) << "Resizing: " << m_frame.width() << m_frame.height();
312 resize(m_frame.width(), m_frame.height());
313 //setMaximumSize(m_frame.width(), m_frame.height()); //This is a hack to force Qt to center the view in the scroll area
314 //setMinimumSize(m_frame.width(), m_frame.height());
316 emit framebufferSizeChanged(m_frame.width(), m_frame.height());
320 repaint(qRound(m_x * m_horizontalFactor), qRound(m_y * m_verticalFactor), qRound(m_w * m_horizontalFactor), qRound(m_h * m_verticalFactor));
324 void VncView::setViewOnly(bool viewOnly)
326 RemoteView::setViewOnly(viewOnly);
328 m_dontSendClipboard = viewOnly;
331 setCursor(Qt::ArrowCursor);
333 setCursor(m_dotCursorState == CursorOn ? localDotCursor() : Qt::BlankCursor);
336 void VncView::showDotCursor(DotCursorState state)
338 RemoteView::showDotCursor(state);
340 setCursor(state == CursorOn ? localDotCursor() : Qt::BlankCursor);
343 void VncView::enableScaling(bool scale)
345 RemoteView::enableScaling(scale);
348 //setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
349 //setMinimumSize(1, 1);
351 scaleResize(parentWidget()->width(), parentWidget()->height());
353 scaleResize(width(), height());
355 m_verticalFactor = 1.0;
356 m_horizontalFactor = 1.0;
358 //setMaximumSize(m_frame.width(), m_frame.height()); //This is a hack to force Qt to center the view in the scroll area
359 //setMinimumSize(m_frame.width(), m_frame.height());
360 resize(m_frame.width(), m_frame.height());
364 void VncView::setCut(const QString &text)
366 m_dontSendClipboard = true;
367 m_clipboard->setText(text, QClipboard::Clipboard);
368 m_clipboard->setText(text, QClipboard::Selection);
369 m_dontSendClipboard = false;
372 void VncView::paintEvent(QPaintEvent *event)
374 // kDebug(5011) << "paint event: x: " << m_x << ", y: " << m_y << ", w: " << m_w << ", h: " << m_h;
375 if (m_frame.isNull() || m_frame.format() == QImage::Format_Invalid) {
376 kDebug(5011) << "no valid image to paint";
377 RemoteView::paintEvent(event);
383 QPainter painter(this);
386 // kDebug(5011) << "normal repaint";
387 painter.drawImage(QRect(qRound(m_x*m_horizontalFactor), qRound(m_y*m_verticalFactor),
388 qRound(m_w*m_horizontalFactor), qRound(m_h*m_verticalFactor)),
389 m_frame.copy(m_x, m_y, m_w, m_h).scaled(qRound(m_w*m_horizontalFactor),
390 qRound(m_h*m_verticalFactor),
391 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
393 kDebug(5011) << "resize repaint";
394 QRect rect = event->rect();
395 if (!force_full_repaint and (rect.width() != width() || rect.height() != height())) {
396 kDebug(5011) << "Partial repaint";
397 const int sx = rect.x()/m_horizontalFactor;
398 const int sy = rect.y()/m_verticalFactor;
399 const int sw = rect.width()/m_horizontalFactor;
400 const int sh = rect.height()/m_verticalFactor;
401 painter.drawImage(rect,
402 m_frame.copy(sx, sy, sw, sh).scaled(rect.width(), rect.height(),
403 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
405 kDebug(5011) << "Full repaint" << width() << height() << m_frame.width() << m_frame.height();
406 painter.drawImage(QRect(0, 0, width(), height()),
407 m_frame.scaled(m_frame.width() * m_horizontalFactor, m_frame.height() * m_verticalFactor,
408 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
409 force_full_repaint = false;
413 RemoteView::paintEvent(event);
416 void VncView::resizeEvent(QResizeEvent *event)
418 RemoteView::resizeEvent(event);
419 scaleResize(event->size().width(), event->size().height());
423 bool VncView::event(QEvent *event)
425 switch (event->type()) {
426 case QEvent::KeyPress:
427 case QEvent::KeyRelease:
428 // kDebug(5011) << "keyEvent";
429 keyEventHandler(static_cast<QKeyEvent*>(event));
432 case QEvent::MouseButtonDblClick:
433 case QEvent::MouseButtonPress:
434 case QEvent::MouseButtonRelease:
435 case QEvent::MouseMove:
436 // kDebug(5011) << "mouseEvent";
437 mouseEventHandler(static_cast<QMouseEvent*>(event));
441 // kDebug(5011) << "wheelEvent";
442 wheelEventHandler(static_cast<QWheelEvent*>(event));
446 return RemoteView::event(event);
451 void VncView::mouseEventHandler(QMouseEvent *e)
453 static bool tap_detected = false;
454 static bool double_tap_detected = false;
455 static bool tap_drag_detected = false;
456 static QTime press_time;
457 static QTime up_time; //used for double clicks/tap&drag, for time after first tap
459 const int TAP_PRESS_TIME = 200;
460 const int DOUBLE_TAP_UP_TIME = 500;
462 if(!e) { //flush held taps
464 m_buttonMask |= 0x01;
465 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
466 m_buttonMask &= 0xfe;
467 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
468 tap_detected = false;
469 } else if(double_tap_detected and press_time.elapsed() > TAP_PRESS_TIME) { //got tap + another press -> tap & drag
470 m_buttonMask |= 0x01;
471 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
472 double_tap_detected = false;
473 tap_drag_detected = true;
479 if(e->x() < 0 or e->y() < 0) { //QScrollArea tends to send invalid events sometimes...
484 cursor_x = qRound(e->x()/m_horizontalFactor);
485 cursor_y = qRound(e->y()/m_verticalFactor);
486 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask); // plain move event
488 if(e->type() == QEvent::MouseButtonPress or e->type() == QEvent::MouseButtonDblClick) {
490 if(tap_detected and up_time.elapsed() < DOUBLE_TAP_UP_TIME) {
491 tap_detected = false;
492 double_tap_detected = true;
494 QTimer::singleShot(TAP_PRESS_TIME, this, SLOT(mouseEventHandler()));
496 } else if(e->type() == QEvent::MouseButtonRelease) {
497 if(tap_drag_detected) {
498 m_buttonMask &= 0xfe;
499 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
500 tap_drag_detected = false;
501 } else if(double_tap_detected) { //double click
502 double_tap_detected = false;
504 m_buttonMask |= 0x01;
505 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
506 m_buttonMask &= 0xfe;
507 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
508 m_buttonMask |= 0x01;
509 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
510 m_buttonMask &= 0xfe;
511 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
512 } else if(press_time.elapsed() < TAP_PRESS_TIME) { //tap
515 QTimer::singleShot(DOUBLE_TAP_UP_TIME, this, SLOT(mouseEventHandler()));
521 if (e->type() != QEvent::MouseMove) {
522 if ((e->type() == QEvent::MouseButtonPress)) {
523 if (e->button() & Qt::LeftButton)
524 m_buttonMask |= 0x01;
525 if (e->button() & Qt::MidButton)
526 m_buttonMask |= 0x02;
527 if (e->button() & Qt::RightButton)
528 m_buttonMask |= 0x04;
529 } else if (e->type() == QEvent::MouseButtonRelease) {
530 if (e->button() & Qt::LeftButton)
531 m_buttonMask &= 0xfe;
532 if (e->button() & Qt::MidButton)
533 m_buttonMask &= 0xfd;
534 if (e->button() & Qt::RightButton)
535 m_buttonMask &= 0xfb;
539 void VncView::wheelEventHandler(QWheelEvent *event)
542 if (event->delta() < 0)
547 const int x = qRound(event->x() / m_horizontalFactor);
548 const int y = qRound(event->y() / m_verticalFactor);
550 kDebug(5011) << "Wheelevent";
551 vncThread.mouseEvent(x, y, eb | m_buttonMask);
552 vncThread.mouseEvent(x, y, m_buttonMask);
555 void VncView::keyEventHandler(QKeyEvent *e)
557 // strip away autorepeating KeyRelease; see bug #206598
558 if (e->isAutoRepeat() && (e->type() == QEvent::KeyRelease))
561 // parts of this code are based on http://italc.sourcearchive.com/documentation/1.0.9.1/vncview_8cpp-source.html
562 rfbKeySym k = e->nativeVirtualKey();
564 // we do not handle Key_Backtab separately as the Shift-modifier
565 // is already enabled
566 if (e->key() == Qt::Key_Backtab) {
570 const bool pressed = (e->type() == QEvent::KeyPress);
573 if (k == XK_Shift_L || k == XK_Control_L || k == XK_Meta_L || k == XK_Alt_L) {
576 } else if (m_mods.contains(k)) {
584 int current_zoom = -1;
585 if(e->key() == Qt::Key_F8)
586 current_zoom = left_zoom;
587 else if(e->key() == Qt::Key_F7)
588 current_zoom = right_zoom;
590 vncThread.keyEvent(k, pressed);
592 if(current_zoom == -1)
595 //handle zoom buttons
596 if(current_zoom == 0) { //left click
598 m_buttonMask |= 0x01;
600 m_buttonMask &= 0xfe;
601 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
602 } else if(current_zoom == 1) { //right click
604 m_buttonMask |= 0x04;
606 m_buttonMask &= 0xfb;
607 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
608 } else if(current_zoom == 2 and pressed) { //wheel up
610 vncThread.mouseEvent(cursor_x, cursor_y, eb | m_buttonMask);
611 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
612 } else if(current_zoom == 3 and pressed) { //wheel down
614 vncThread.mouseEvent(cursor_x, cursor_y, eb | m_buttonMask);
615 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
616 } else if(current_zoom == 4) { //page up
617 vncThread.keyEvent(0xff55, pressed);
618 } else if(current_zoom == 5) { //page down
619 vncThread.keyEvent(0xff56, pressed);
623 void VncView::unpressModifiers()
625 const QList<unsigned int> keys = m_mods.keys();
626 QList<unsigned int>::const_iterator it = keys.constBegin();
627 while (it != keys.end()) {
628 vncThread.keyEvent(*it, false);
634 void VncView::clipboardSelectionChanged()
638 if (m_status != Connected)
641 if (m_clipboard->ownsSelection() || m_dontSendClipboard)
644 const QString text = m_clipboard->text(QClipboard::Selection);
646 vncThread.clientCut(text);
649 void VncView::clipboardDataChanged()
653 if (m_status != Connected)
656 if (m_clipboard->ownsClipboard() || m_dontSendClipboard)
659 const QString text = m_clipboard->text(QClipboard::Clipboard);
661 vncThread.clientCut(text);
665 void VncView::sendKey(Qt::Key key)
667 int k = 0; //X11 keysym
678 case Qt::Key_PageDown:
681 case Qt::Key_Meta: //TODO: test this.
688 kDebug(5011) << "unhandled Qt::Key value " << key;
692 if (k == XK_Shift_L || k == XK_Control_L || k == XK_Meta_L || k == XK_Alt_L || k == XK_Super_L) {
693 if (m_mods.contains(k)) { //release
695 vncThread.keyEvent(k, false);
698 vncThread.keyEvent(k, true);
700 } else { //normal key
701 vncThread.keyEvent(k, true);
702 vncThread.keyEvent(k, false);
706 void VncView::reloadSettings()
709 left_zoom = settings.value("left_zoom", 0).toInt();
710 right_zoom = settings.value("right_zoom", 1).toInt();
713 #include "moc_vncview.cpp"