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_authenticaionCanceled(false),
58 m_dontSendClipboard(false),
59 m_horizontalFactor(1.0),
60 m_verticalFactor(1.0),
61 m_forceLocalCursor(false),
62 force_full_repaint(false),
69 connect(&vncThread, SIGNAL(imageUpdated(int, int, int, int)), this, SLOT(updateImage(int, int, int, int)), Qt::BlockingQueuedConnection);
70 connect(&vncThread, SIGNAL(gotCut(const QString&)), this, SLOT(setCut(const QString&)), Qt::BlockingQueuedConnection);
71 connect(&vncThread, SIGNAL(passwordRequest()), this, SLOT(requestPassword()), Qt::BlockingQueuedConnection);
72 connect(&vncThread, SIGNAL(outputErrorMessage(QString)), this, SLOT(outputErrorMessage(QString)));
74 m_clipboard = QApplication::clipboard();
75 connect(m_clipboard, SIGNAL(selectionChanged()), this, SLOT(clipboardSelectionChanged()));
76 connect(m_clipboard, SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
85 // Disconnect all signals so that we don't get any more callbacks from the client thread
86 disconnect(&vncThread, SIGNAL(imageUpdated(int, int, int, int)), this, SLOT(updateImage(int, int, int, int)));
87 disconnect(&vncThread, SIGNAL(gotCut(const QString&)), this, SLOT(setCut(const QString&)));
88 disconnect(&vncThread, SIGNAL(passwordRequest()), this, SLOT(requestPassword()));
89 disconnect(&vncThread, SIGNAL(outputErrorMessage(QString)), this, SLOT(outputErrorMessage(QString)));
94 void VncView::forceFullRepaint()
96 force_full_repaint = true;
100 bool VncView::eventFilter(QObject *obj, QEvent *event)
103 if (event->type() == QEvent::KeyPress ||
104 event->type() == QEvent::KeyRelease ||
105 event->type() == QEvent::MouseButtonDblClick ||
106 event->type() == QEvent::MouseButtonPress ||
107 event->type() == QEvent::MouseButtonRelease ||
108 event->type() == QEvent::Wheel ||
109 event->type() == QEvent::MouseMove)
112 return RemoteView::eventFilter(obj, event);
115 QSize VncView::framebufferSize()
117 return m_frame.size();
120 QSize VncView::sizeHint() const
125 QSize VncView::minimumSizeHint() const
130 void VncView::scaleResize(int w, int h)
132 RemoteView::scaleResize(w, h);
134 kDebug(5011) << "scaleResize(): " <<w << h;
136 m_verticalFactor = (qreal) h / m_frame.height();
137 m_horizontalFactor = (qreal) w / m_frame.width();
139 m_verticalFactor = m_horizontalFactor = qMin(m_verticalFactor, m_horizontalFactor);
141 const qreal newW = m_frame.width() * m_horizontalFactor;
142 const qreal newH = m_frame.height() * m_verticalFactor;
144 setMaximumSize(newW, newH); //This is a hack to force Qt to center the view in the scroll area
145 //also causes the widget's size to flicker
151 void VncView::updateConfiguration()
153 RemoteView::updateConfiguration();
155 // Update the scaling mode in case KeepAspectRatio changed
157 scaleResize(parentWidget()->width(), parentWidget()->height());
159 scaleResize(width(), height());
162 void VncView::startQuitting()
164 kDebug(5011) << "about to quit";
166 const bool connected = status() == RemoteView::Connected;
168 setStatus(Disconnecting);
178 const bool quitSuccess = vncThread.wait(500);
180 kDebug(5011) << "Quit VNC thread success:" << quitSuccess;
182 setStatus(Disconnected);
185 bool VncView::isQuitting()
190 bool VncView::start()
192 vncThread.setHost(m_host);
193 vncThread.setPort(m_port);
195 vncThread.setQuality(quality);
197 // set local cursor on by default because low quality mostly means slow internet connection
198 if (quality == RemoteView::Low) {
199 showDotCursor(RemoteView::CursorOn);
202 setStatus(Connecting);
208 bool VncView::supportsScaling() const
213 bool VncView::supportsLocalCursor() const
218 void VncView::requestPassword()
220 kDebug(5011) << "request password";
222 if (m_authenticaionCanceled) {
227 setStatus(Authenticating);
229 if (!m_url.password().isNull()) {
230 vncThread.setPassword(m_url.password());
235 QString password = QInputDialog::getText(this, //krazy:exclude=qclasses
236 tr("Password required"),
237 tr("Please enter the password for the remote desktop:"),
238 QLineEdit::Password, QString(), &ok);
239 m_firstPasswordTry = false;
241 vncThread.setPassword(password);
243 m_authenticaionCanceled = true;
246 void VncView::outputErrorMessage(const QString &message)
248 kDebug(5011) << message;
250 if (message == "INTERNAL:APPLE_VNC_COMPATIBILTY") {
251 setCursor(localDotCursor());
252 m_forceLocalCursor = true;
258 emit errorMessage(i18n("VNC failure"), message);
261 void VncView::updateImage(int x, int y, int w, int h)
263 // kDebug(5011) << "got update" << width() << height();
270 if (m_horizontalFactor != 1.0 || m_verticalFactor != 1.0) {
271 // If the view is scaled, grow the update rectangle to avoid artifacts
278 m_frame = vncThread.image();
281 setAttribute(Qt::WA_StaticContents);
282 setAttribute(Qt::WA_OpaquePaintEvent);
283 installEventFilter(this);
285 setCursor(((m_dotCursorState == CursorOn) || m_forceLocalCursor) ? localDotCursor() : Qt::BlankCursor);
287 setMouseTracking(true); // get mouse events even when there is no mousebutton pressed
288 setFocusPolicy(Qt::WheelFocus);
289 setStatus(Connected);
290 // emit framebufferSizeChanged(m_frame.width(), m_frame.height());
295 scaleResize(parentWidget()->width(), parentWidget()->height());
297 scaleResize(width(), height());
304 static QSize old_frame_size = QSize();
305 if ((y == 0 && x == 0) && (m_frame.size() != old_frame_size)) {
306 old_frame_size = m_frame.size();
307 kDebug(5011) << "Updating framebuffer size";
309 //setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
311 scaleResize(parentWidget()->width(), parentWidget()->height());
313 kDebug(5011) << "Resizing: " << m_frame.width() << m_frame.height();
314 resize(m_frame.width(), m_frame.height());
315 //setMaximumSize(m_frame.width(), m_frame.height()); //This is a hack to force Qt to center the view in the scroll area
316 //setMinimumSize(m_frame.width(), m_frame.height());
318 emit framebufferSizeChanged(m_frame.width(), m_frame.height());
322 repaint(qRound(m_x * m_horizontalFactor), qRound(m_y * m_verticalFactor), qRound(m_w * m_horizontalFactor), qRound(m_h * m_verticalFactor));
326 void VncView::setViewOnly(bool viewOnly)
328 RemoteView::setViewOnly(viewOnly);
330 m_dontSendClipboard = viewOnly;
333 setCursor(Qt::ArrowCursor);
335 setCursor(m_dotCursorState == CursorOn ? localDotCursor() : Qt::BlankCursor);
338 void VncView::showDotCursor(DotCursorState state)
340 RemoteView::showDotCursor(state);
342 setCursor(state == CursorOn ? localDotCursor() : Qt::BlankCursor);
345 void VncView::enableScaling(bool scale)
347 RemoteView::enableScaling(scale);
350 //setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
351 //setMinimumSize(1, 1);
353 scaleResize(parentWidget()->width(), parentWidget()->height());
355 scaleResize(width(), height());
357 m_verticalFactor = 1.0;
358 m_horizontalFactor = 1.0;
360 //setMaximumSize(m_frame.width(), m_frame.height()); //This is a hack to force Qt to center the view in the scroll area
361 //setMinimumSize(m_frame.width(), m_frame.height());
362 resize(m_frame.width(), m_frame.height());
366 void VncView::setCut(const QString &text)
368 m_dontSendClipboard = true;
369 m_clipboard->setText(text, QClipboard::Clipboard);
370 m_clipboard->setText(text, QClipboard::Selection);
371 m_dontSendClipboard = false;
374 void VncView::paintEvent(QPaintEvent *event)
376 // kDebug(5011) << "paint event: x: " << m_x << ", y: " << m_y << ", w: " << m_w << ", h: " << m_h;
377 if (m_frame.isNull() || m_frame.format() == QImage::Format_Invalid) {
378 kDebug(5011) << "no valid image to paint";
379 RemoteView::paintEvent(event);
385 QPainter painter(this);
388 // kDebug(5011) << "normal repaint";
389 painter.drawImage(QRect(qRound(m_x*m_horizontalFactor), qRound(m_y*m_verticalFactor),
390 qRound(m_w*m_horizontalFactor), qRound(m_h*m_verticalFactor)),
391 m_frame.copy(m_x, m_y, m_w, m_h).scaled(qRound(m_w*m_horizontalFactor),
392 qRound(m_h*m_verticalFactor),
393 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
395 kDebug(5011) << "resize repaint";
396 QRect rect = event->rect();
397 if (!force_full_repaint and (rect.width() != width() || rect.height() != height())) {
398 kDebug(5011) << "Partial repaint";
399 const int sx = rect.x()/m_horizontalFactor;
400 const int sy = rect.y()/m_verticalFactor;
401 const int sw = rect.width()/m_horizontalFactor;
402 const int sh = rect.height()/m_verticalFactor;
403 painter.drawImage(rect,
404 m_frame.copy(sx, sy, sw, sh).scaled(rect.width(), rect.height(),
405 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
407 kDebug(5011) << "Full repaint" << width() << height() << m_frame.width() << m_frame.height();
408 painter.drawImage(QRect(0, 0, width(), height()),
409 m_frame.scaled(m_frame.width() * m_horizontalFactor, m_frame.height() * m_verticalFactor,
410 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
411 force_full_repaint = false;
415 RemoteView::paintEvent(event);
418 void VncView::resizeEvent(QResizeEvent *event)
420 RemoteView::resizeEvent(event);
421 scaleResize(event->size().width(), event->size().height());
425 bool VncView::event(QEvent *event)
427 switch (event->type()) {
428 case QEvent::KeyPress:
429 case QEvent::KeyRelease:
430 // kDebug(5011) << "keyEvent";
431 keyEventHandler(static_cast<QKeyEvent*>(event));
434 case QEvent::MouseButtonDblClick:
435 case QEvent::MouseButtonPress:
436 case QEvent::MouseButtonRelease:
437 case QEvent::MouseMove:
438 // kDebug(5011) << "mouseEvent";
439 mouseEventHandler(static_cast<QMouseEvent*>(event));
443 // kDebug(5011) << "wheelEvent";
444 wheelEventHandler(static_cast<QWheelEvent*>(event));
448 return RemoteView::event(event);
453 void VncView::mouseEventHandler(QMouseEvent *e)
455 static bool tap_detected = false;
456 static bool double_tap_detected = false;
457 static bool tap_drag_detected = false;
458 static QTime press_time;
459 static QTime up_time; //used for double clicks/tap&drag, for time after first tap
461 const int TAP_PRESS_TIME = 200;
462 const int DOUBLE_TAP_UP_TIME = 500;
464 if(!e) { //flush held taps
466 m_buttonMask |= 0x01;
467 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
468 m_buttonMask &= 0xfe;
469 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
470 tap_detected = false;
471 } else if(double_tap_detected and press_time.elapsed() > TAP_PRESS_TIME) { //got tap + another press -> tap & drag
472 m_buttonMask |= 0x01;
473 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
474 double_tap_detected = false;
475 tap_drag_detected = true;
481 if(e->x() < 0 or e->y() < 0) { //QScrollArea tends to send invalid events sometimes...
486 cursor_x = qRound(e->x()/m_horizontalFactor);
487 cursor_y = qRound(e->y()/m_verticalFactor);
488 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask); // plain move event
490 if(e->type() == QEvent::MouseButtonPress or e->type() == QEvent::MouseButtonDblClick) {
492 if(tap_detected and up_time.elapsed() < DOUBLE_TAP_UP_TIME) {
493 tap_detected = false;
494 double_tap_detected = true;
496 QTimer::singleShot(TAP_PRESS_TIME, this, SLOT(mouseEventHandler()));
498 } else if(e->type() == QEvent::MouseButtonRelease) {
499 if(tap_drag_detected) {
500 m_buttonMask &= 0xfe;
501 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
502 tap_drag_detected = false;
503 } else if(double_tap_detected) { //double click
504 double_tap_detected = false;
506 m_buttonMask |= 0x01;
507 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
508 m_buttonMask &= 0xfe;
509 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
510 m_buttonMask |= 0x01;
511 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
512 m_buttonMask &= 0xfe;
513 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
514 } else if(press_time.elapsed() < TAP_PRESS_TIME) { //tap
517 QTimer::singleShot(DOUBLE_TAP_UP_TIME, this, SLOT(mouseEventHandler()));
523 if (e->type() != QEvent::MouseMove) {
524 if ((e->type() == QEvent::MouseButtonPress)) {
525 if (e->button() & Qt::LeftButton)
526 m_buttonMask |= 0x01;
527 if (e->button() & Qt::MidButton)
528 m_buttonMask |= 0x02;
529 if (e->button() & Qt::RightButton)
530 m_buttonMask |= 0x04;
531 } else if (e->type() == QEvent::MouseButtonRelease) {
532 if (e->button() & Qt::LeftButton)
533 m_buttonMask &= 0xfe;
534 if (e->button() & Qt::MidButton)
535 m_buttonMask &= 0xfd;
536 if (e->button() & Qt::RightButton)
537 m_buttonMask &= 0xfb;
541 void VncView::wheelEventHandler(QWheelEvent *event)
544 if (event->delta() < 0)
549 const int x = qRound(event->x() / m_horizontalFactor);
550 const int y = qRound(event->y() / m_verticalFactor);
552 kDebug(5011) << "Wheelevent";
553 vncThread.mouseEvent(x, y, eb | m_buttonMask);
554 vncThread.mouseEvent(x, y, m_buttonMask);
557 void VncView::keyEventHandler(QKeyEvent *e)
559 // strip away autorepeating KeyRelease; see bug #206598
560 if (e->isAutoRepeat() && (e->type() == QEvent::KeyRelease))
563 // parts of this code are based on http://italc.sourcearchive.com/documentation/1.0.9.1/vncview_8cpp-source.html
564 rfbKeySym k = e->nativeVirtualKey();
566 // we do not handle Key_Backtab separately as the Shift-modifier
567 // is already enabled
568 if (e->key() == Qt::Key_Backtab) {
572 const bool pressed = (e->type() == QEvent::KeyPress);
575 if (k == XK_Shift_L || k == XK_Control_L || k == XK_Meta_L || k == XK_Alt_L) {
578 } else if (m_mods.contains(k)) {
586 int current_zoom = -1;
587 if(e->key() == Qt::Key_F8)
588 current_zoom = left_zoom;
589 else if(e->key() == Qt::Key_F7)
590 current_zoom = right_zoom;
592 vncThread.keyEvent(k, pressed);
594 if(current_zoom == -1)
597 //handle zoom buttons
598 if(current_zoom == 0) { //left click
600 m_buttonMask |= 0x01;
602 m_buttonMask &= 0xfe;
603 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
604 } else if(current_zoom == 1) { //right click
606 m_buttonMask |= 0x04;
608 m_buttonMask &= 0xfb;
609 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
610 } else if(current_zoom == 2) { //wheel up
612 vncThread.mouseEvent(cursor_x, cursor_y, eb | m_buttonMask);
613 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
614 } else if(current_zoom == 3) { //wheel down
616 vncThread.mouseEvent(cursor_x, cursor_y, eb | m_buttonMask);
617 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
618 } else if(current_zoom == 4) { //page up
619 vncThread.keyEvent(0xff55, pressed);
620 } else if(current_zoom == 5) { //page down
621 vncThread.keyEvent(0xff56, pressed);
625 void VncView::unpressModifiers()
627 const QList<unsigned int> keys = m_mods.keys();
628 QList<unsigned int>::const_iterator it = keys.constBegin();
629 while (it != keys.end()) {
630 vncThread.keyEvent(*it, false);
636 void VncView::clipboardSelectionChanged()
640 if (m_status != Connected)
643 if (m_clipboard->ownsSelection() || m_dontSendClipboard)
646 const QString text = m_clipboard->text(QClipboard::Selection);
648 vncThread.clientCut(text);
651 void VncView::clipboardDataChanged()
655 if (m_status != Connected)
658 if (m_clipboard->ownsClipboard() || m_dontSendClipboard)
661 const QString text = m_clipboard->text(QClipboard::Clipboard);
663 vncThread.clientCut(text);
667 void VncView::sendKey(Qt::Key key)
669 int k = 0; //X11 keysym
680 case Qt::Key_PageDown:
683 case Qt::Key_Meta: //TODO: test this.
690 kDebug(5011) << "unhandled Qt::Key value " << key;
694 if (k == XK_Shift_L || k == XK_Control_L || k == XK_Meta_L || k == XK_Alt_L || k == XK_Super_L) {
695 if (m_mods.contains(k)) { //release
697 vncThread.keyEvent(k, false);
700 vncThread.keyEvent(k, true);
702 } else { //normal key
703 vncThread.keyEvent(k, true);
704 vncThread.keyEvent(k, false);
708 void VncView::reloadSettings()
711 left_zoom = settings.value("left_zoom", 0).toInt();
712 right_zoom = settings.value("right_zoom", 1).toInt();
715 #include "moc_vncview.cpp"