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 if(event->type() == 200)
110 kDebug(5011) << "eventFilter: 200";
111 return RemoteView::eventFilter(obj, event);
114 QSize VncView::framebufferSize()
116 return m_frame.size();
119 QSize VncView::sizeHint() const
124 QSize VncView::minimumSizeHint() const
129 void VncView::scaleResize(int w, int h)
131 RemoteView::scaleResize(w, h);
133 kDebug(5011) << "scaleResize(): " <<w << h;
135 m_verticalFactor = (qreal) h / m_frame.height();
136 m_horizontalFactor = (qreal) w / m_frame.width();
138 m_verticalFactor = m_horizontalFactor = qMin(m_verticalFactor, m_horizontalFactor);
140 const qreal newW = m_frame.width() * m_horizontalFactor;
141 const qreal newH = m_frame.height() * m_verticalFactor;
143 setMaximumSize(newW, newH); //This is a hack to force Qt to center the view in the scroll area
144 //also causes the widget's size to flicker
151 void VncView::startQuitting()
153 kDebug(5011) << "about to quit";
155 const bool connected = status() == RemoteView::Connected;
157 setStatus(Disconnecting);
167 const bool quitSuccess = vncThread.wait(500);
169 kDebug(5011) << "startQuitting(): Quit VNC thread success:" << quitSuccess;
171 setStatus(Disconnected);
174 bool VncView::isQuitting()
179 bool VncView::start()
181 vncThread.setHost(m_host);
182 vncThread.setPort(m_port);
184 vncThread.setQuality(quality);
186 // set local cursor on by default because low quality mostly means slow internet connection
187 if (quality == RemoteView::Low) {
188 showDotCursor(RemoteView::CursorOn);
191 setStatus(Connecting);
197 bool VncView::supportsScaling() const
202 bool VncView::supportsLocalCursor() const
207 void VncView::requestPassword()
209 kDebug(5011) << "request password";
211 setStatus(Authenticating);
213 if (!m_url.password().isNull()) {
214 vncThread.setPassword(m_url.password());
219 QString password = QInputDialog::getText(this, //krazy:exclude=qclasses
220 tr("Password required"),
221 tr("Please enter the password for the remote desktop:"),
222 QLineEdit::Password, QString(), &ok);
223 m_firstPasswordTry = false;
225 vncThread.setPassword(password);
231 void VncView::outputErrorMessage(const QString &message)
233 kDebug(5011) << message;
235 if (message == "INTERNAL:APPLE_VNC_COMPATIBILTY") {
236 setCursor(localDotCursor());
237 m_forceLocalCursor = true;
243 emit errorMessage(i18n("VNC failure"), message);
246 void VncView::updateImage(int x, int y, int w, int h)
248 // kDebug(5011) << "got update" << width() << height();
255 if (m_horizontalFactor != 1.0 || m_verticalFactor != 1.0) {
256 // If the view is scaled, grow the update rectangle to avoid artifacts
263 m_frame = vncThread.image();
266 setAttribute(Qt::WA_StaticContents);
267 setAttribute(Qt::WA_OpaquePaintEvent);
268 installEventFilter(this);
270 setCursor(((m_dotCursorState == CursorOn) || m_forceLocalCursor) ? localDotCursor() : Qt::BlankCursor);
272 setMouseTracking(true); // get mouse events even when there is no mousebutton pressed
273 setFocusPolicy(Qt::WheelFocus);
274 setStatus(Connected);
275 // emit framebufferSizeChanged(m_frame.width(), m_frame.height());
280 scaleResize(parentWidget()->width(), parentWidget()->height());
282 scaleResize(width(), height());
289 static QSize old_frame_size = QSize();
290 if ((y == 0 && x == 0) && (m_frame.size() != old_frame_size)) {
291 old_frame_size = m_frame.size();
292 kDebug(5011) << "Updating framebuffer size";
294 //setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
296 scaleResize(parentWidget()->width(), parentWidget()->height());
298 kDebug(5011) << "Resizing: " << m_frame.width() << m_frame.height();
299 resize(m_frame.width(), m_frame.height());
300 //setMaximumSize(m_frame.width(), m_frame.height()); //This is a hack to force Qt to center the view in the scroll area
301 //setMinimumSize(m_frame.width(), m_frame.height());
303 emit framebufferSizeChanged(m_frame.width(), m_frame.height());
307 repaint(qRound(m_x * m_horizontalFactor), qRound(m_y * m_verticalFactor), qRound(m_w * m_horizontalFactor), qRound(m_h * m_verticalFactor));
311 void VncView::setViewOnly(bool viewOnly)
313 RemoteView::setViewOnly(viewOnly);
315 m_dontSendClipboard = viewOnly;
318 setCursor(Qt::ArrowCursor);
320 setCursor(m_dotCursorState == CursorOn ? localDotCursor() : Qt::BlankCursor);
323 void VncView::showDotCursor(DotCursorState state)
325 RemoteView::showDotCursor(state);
327 setCursor(state == CursorOn ? localDotCursor() : Qt::BlankCursor);
330 void VncView::enableScaling(bool scale)
332 RemoteView::enableScaling(scale);
335 //setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
336 //setMinimumSize(1, 1);
338 scaleResize(parentWidget()->width(), parentWidget()->height());
340 scaleResize(width(), height());
342 m_verticalFactor = 1.0;
343 m_horizontalFactor = 1.0;
345 //setMaximumSize(m_frame.width(), m_frame.height()); //This is a hack to force Qt to center the view in the scroll area
346 //setMinimumSize(m_frame.width(), m_frame.height());
347 resize(m_frame.width(), m_frame.height());
351 void VncView::setCut(const QString &text)
353 m_dontSendClipboard = true;
354 m_clipboard->setText(text, QClipboard::Clipboard);
355 m_clipboard->setText(text, QClipboard::Selection);
356 m_dontSendClipboard = false;
359 void VncView::paintEvent(QPaintEvent *event)
361 //kDebug(5011) << "paint event: x: " << m_x << ", y: " << m_y << ", w: " << m_w << ", h: " << m_h;
362 if (m_frame.isNull() || m_frame.format() == QImage::Format_Invalid) {
363 kDebug(5011) << "no valid image to paint";
364 RemoteView::paintEvent(event);
370 QPainter painter(this);
372 if (m_repaint and !force_full_repaint) {
373 // kDebug(5011) << "normal repaint";
374 painter.drawImage(QRect(qRound(m_x*m_horizontalFactor), qRound(m_y*m_verticalFactor),
375 qRound(m_w*m_horizontalFactor), qRound(m_h*m_verticalFactor)),
376 m_frame.copy(m_x, m_y, m_w, m_h).scaled(qRound(m_w*m_horizontalFactor),
377 qRound(m_h*m_verticalFactor),
378 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
380 kDebug(5011) << "resize repaint";
381 QRect rect = event->rect();
382 if (!force_full_repaint and (rect.width() != width() || rect.height() != height())) {
383 kDebug(5011) << "Partial repaint";
384 const int sx = rect.x()/m_horizontalFactor;
385 const int sy = rect.y()/m_verticalFactor;
386 const int sw = rect.width()/m_horizontalFactor;
387 const int sh = rect.height()/m_verticalFactor;
388 painter.drawImage(rect,
389 m_frame.copy(sx, sy, sw, sh).scaled(rect.width(), rect.height(),
390 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
392 kDebug(5011) << "Full repaint" << width() << height() << m_frame.width() << m_frame.height();
393 painter.drawImage(QRect(0, 0, width(), height()),
394 m_frame.scaled(m_frame.width() * m_horizontalFactor, m_frame.height() * m_verticalFactor,
395 Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
396 force_full_repaint = false;
400 RemoteView::paintEvent(event);
403 void VncView::resizeEvent(QResizeEvent *event)
405 RemoteView::resizeEvent(event);
406 scaleResize(event->size().width(), event->size().height());
410 bool VncView::event(QEvent *event)
412 switch (event->type()) {
413 case QEvent::KeyPress:
414 case QEvent::KeyRelease:
415 // kDebug(5011) << "keyEvent";
416 keyEventHandler(static_cast<QKeyEvent*>(event));
419 case QEvent::MouseButtonDblClick:
420 case QEvent::MouseButtonPress:
421 case QEvent::MouseButtonRelease:
422 case QEvent::MouseMove:
423 // kDebug(5011) << "mouseEvent";
424 mouseEventHandler(static_cast<QMouseEvent*>(event));
428 // kDebug(5011) << "wheelEvent";
429 wheelEventHandler(static_cast<QWheelEvent*>(event));
432 case QEvent::WindowActivate: //input panel may have been closed, prevent IM from interfering with hardware keyboard
433 setAttribute(Qt::WA_InputMethodEnabled, false);
436 return RemoteView::event(event);
440 //call with e == 0 to flush held events
441 void VncView::mouseEventHandler(QMouseEvent *e)
443 static bool tap_detected = false;
444 static bool double_tap_detected = false;
445 static bool tap_drag_detected = false;
446 static QTime press_time;
447 static QTime up_time; //used for double clicks/tap&drag, for time after first tap
449 const int TAP_PRESS_TIME = 180;
450 const int DOUBLE_TAP_UP_TIME = 500;
452 if(!e) { //flush held taps
454 m_buttonMask |= 0x01;
455 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
456 m_buttonMask &= 0xfe;
457 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
458 tap_detected = false;
459 } else if(double_tap_detected and press_time.elapsed() > TAP_PRESS_TIME) { //got tap + another press -> tap & drag
460 m_buttonMask |= 0x01;
461 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
462 double_tap_detected = false;
463 tap_drag_detected = true;
469 if(e->x() < 0 or e->y() < 0) { //QScrollArea tends to send invalid events sometimes...
474 cursor_x = qRound(e->x()/m_horizontalFactor);
475 cursor_y = qRound(e->y()/m_verticalFactor);
476 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask); // plain move event
478 if(disable_tapping) { //only move cursor
483 if(e->type() == QEvent::MouseButtonPress or e->type() == QEvent::MouseButtonDblClick) {
485 if(tap_detected and up_time.elapsed() < DOUBLE_TAP_UP_TIME) {
486 tap_detected = false;
487 double_tap_detected = true;
489 QTimer::singleShot(TAP_PRESS_TIME, this, SLOT(mouseEventHandler()));
491 } else if(e->type() == QEvent::MouseButtonRelease) {
492 if(tap_drag_detected) {
493 m_buttonMask &= 0xfe;
494 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
495 tap_drag_detected = false;
496 } else if(double_tap_detected) { //double click
497 double_tap_detected = false;
499 m_buttonMask |= 0x01;
500 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
501 m_buttonMask &= 0xfe;
502 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
503 m_buttonMask |= 0x01;
504 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
505 m_buttonMask &= 0xfe;
506 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
507 } else if(press_time.elapsed() < TAP_PRESS_TIME) { //tap
510 QTimer::singleShot(DOUBLE_TAP_UP_TIME, this, SLOT(mouseEventHandler()));
516 if (e->type() != QEvent::MouseMove) {
517 if ((e->type() == QEvent::MouseButtonPress)) {
518 if (e->button() & Qt::LeftButton)
519 m_buttonMask |= 0x01;
520 if (e->button() & Qt::MidButton)
521 m_buttonMask |= 0x02;
522 if (e->button() & Qt::RightButton)
523 m_buttonMask |= 0x04;
524 } else if (e->type() == QEvent::MouseButtonRelease) {
525 if (e->button() & Qt::LeftButton)
526 m_buttonMask &= 0xfe;
527 if (e->button() & Qt::MidButton)
528 m_buttonMask &= 0xfd;
529 if (e->button() & Qt::RightButton)
530 m_buttonMask &= 0xfb;
534 void VncView::wheelEventHandler(QWheelEvent *event)
537 if (event->delta() < 0)
542 const int x = qRound(event->x() / m_horizontalFactor);
543 const int y = qRound(event->y() / m_verticalFactor);
545 kDebug(5011) << "Wheelevent";
546 vncThread.mouseEvent(x, y, eb | m_buttonMask);
547 vncThread.mouseEvent(x, y, m_buttonMask);
550 void VncView::keyEventHandler(QKeyEvent *e)
552 // strip away autorepeating KeyRelease; see bug #206598
553 if (e->isAutoRepeat() && (e->type() == QEvent::KeyRelease))
556 // parts of this code are based on http://italc.sourcearchive.com/documentation/1.0.9.1/vncview_8cpp-source.html
557 rfbKeySym k = e->nativeVirtualKey();
559 // we do not handle Key_Backtab separately as the Shift-modifier
560 // is already enabled
561 if (e->key() == Qt::Key_Backtab) {
565 const bool pressed = (e->type() == QEvent::KeyPress);
568 if (k == XK_Shift_L || k == XK_Control_L || k == XK_Meta_L || k == XK_Alt_L) {
571 } else if (m_mods.contains(k)) {
579 int current_zoom = -1;
580 if(e->key() == Qt::Key_F8)
581 current_zoom = left_zoom;
582 else if(e->key() == Qt::Key_F7)
583 current_zoom = right_zoom;
585 vncThread.keyEvent(k, pressed);
587 kDebug(5011) << "nativeVirtualKey() for '" << e->text() << "' failed.";
591 if(current_zoom == -1)
594 //handle zoom buttons
595 if(current_zoom == 0) { //left click
597 m_buttonMask |= 0x01;
599 m_buttonMask &= 0xfe;
600 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
601 } else if(current_zoom == 1) { //right click
603 m_buttonMask |= 0x04;
605 m_buttonMask &= 0xfb;
606 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
607 } else if(current_zoom == 2) { //middle click
609 m_buttonMask |= 0x02;
611 m_buttonMask &= 0xfd;
612 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
613 } else if(current_zoom == 3 and pressed) { //wheel up
615 vncThread.mouseEvent(cursor_x, cursor_y, eb | m_buttonMask);
616 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
617 } else if(current_zoom == 4 and pressed) { //wheel down
619 vncThread.mouseEvent(cursor_x, cursor_y, eb | m_buttonMask);
620 vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
621 } else if(current_zoom == 5) { //page up
622 vncThread.keyEvent(0xff55, pressed);
623 } else if(current_zoom == 6) { //page down
624 vncThread.keyEvent(0xff56, pressed);
628 void VncView::unpressModifiers()
630 const QList<unsigned int> keys = m_mods.keys();
631 QList<unsigned int>::const_iterator it = keys.constBegin();
632 while (it != keys.end()) {
633 vncThread.keyEvent(*it, false);
639 void VncView::clipboardSelectionChanged()
643 if (m_status != Connected)
646 if (m_clipboard->ownsSelection() || m_dontSendClipboard)
649 const QString text = m_clipboard->text(QClipboard::Selection);
651 vncThread.clientCut(text);
654 void VncView::clipboardDataChanged()
658 if (m_status != Connected)
661 if (m_clipboard->ownsClipboard() || m_dontSendClipboard)
664 const QString text = m_clipboard->text(QClipboard::Clipboard);
666 vncThread.clientCut(text);
670 void VncView::sendKey(Qt::Key key)
672 int k = 0; //X11 keysym
683 case Qt::Key_PageDown:
686 case Qt::Key_Meta: //TODO: test this.
693 kDebug(5011) << "unhandled Qt::Key value " << key;
697 if (k == XK_Shift_L || k == XK_Control_L || k == XK_Meta_L || k == XK_Alt_L || k == XK_Super_L) {
698 if (m_mods.contains(k)) { //release
700 vncThread.keyEvent(k, false);
703 vncThread.keyEvent(k, true);
705 } else { //normal key
706 vncThread.keyEvent(k, true);
707 vncThread.keyEvent(k, false);
711 void VncView::reloadSettings()
714 left_zoom = settings.value("left_zoom", 0).toInt();
715 right_zoom = settings.value("right_zoom", 1).toInt();
716 disable_tapping = settings.value("disable_tapping", false).toBool();
719 //convert commitString into keyevents
720 void VncView::inputMethodEvent(QInputMethodEvent *event)
722 //TODO handle replacements
723 //TODO convert utf8 to X11 keysyms myself, should work with umlauts, kana...
725 QString letters = event->commitString();
726 for(int i = 0; i < letters.length(); i++) {
727 char k = letters.at(i).toLatin1(); //works with all 'normal' keys, not umlauts.
729 kDebug(5011) << "unhandled key";
732 vncThread.keyEvent(k, true);
733 vncThread.keyEvent(k, false);
737 #include "moc_vncview.cpp"