fix symbol key
[presencevnc] / src / vncview.cpp
index 154a98f..94c0f61 100644 (file)
 critical(parent, caption, message)
 
 #include <QApplication>
+#include <QBitmap>
+#include <QCheckBox>
+#include <QDialog>
 #include <QImage>
+#include <QHBoxLayout>
+#include <QVBoxLayout>
 #include <QPainter>
 #include <QMouseEvent>
+#include <QPushButton>
 #include <QEvent>
+#include <QSettings>
 #include <QTime>
 #include <QTimer>
 
+
 // Definition of key modifier mask constants
 #define KMOD_Alt_R     0x01
 #define KMOD_Alt_L     0x02
@@ -53,7 +61,6 @@ VncView::VncView(QWidget *parent, const KUrl &url, RemoteView::Quality quality)
         m_repaint(false),
         m_quitFlag(false),
         m_firstPasswordTry(true),
-        m_authenticaionCanceled(false),
         m_dontSendClipboard(false),
         m_horizontalFactor(1.0),
         m_verticalFactor(1.0),
@@ -73,6 +80,8 @@ VncView::VncView(QWidget *parent, const KUrl &url, RemoteView::Quality quality)
     m_clipboard = QApplication::clipboard();
     connect(m_clipboard, SIGNAL(selectionChanged()), this, SLOT(clipboardSelectionChanged()));
     connect(m_clipboard, SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
+
+    reloadSettings();
 }
 
 VncView::~VncView()
@@ -80,10 +89,7 @@ VncView::~VncView()
     unpressModifiers();
 
     // Disconnect all signals so that we don't get any more callbacks from the client thread
-    disconnect(&vncThread, SIGNAL(imageUpdated(int, int, int, int)), this, SLOT(updateImage(int, int, int, int)));
-    disconnect(&vncThread, SIGNAL(gotCut(const QString&)), this, SLOT(setCut(const QString&)));
-    disconnect(&vncThread, SIGNAL(passwordRequest()), this, SLOT(requestPassword()));
-    disconnect(&vncThread, SIGNAL(outputErrorMessage(QString)), this, SLOT(outputErrorMessage(QString)));
+    vncThread.disconnect();
 
     startQuitting();
 }
@@ -145,16 +151,6 @@ void VncView::scaleResize(int w, int h)
     } 
 }
 
-void VncView::updateConfiguration()
-{
-    RemoteView::updateConfiguration();
-
-    // Update the scaling mode in case KeepAspectRatio changed
-    if(parentWidget())
-           scaleResize(parentWidget()->width(), parentWidget()->height());
-    else
-       scaleResize(width(), height());
-}
 
 void VncView::startQuitting()
 {
@@ -174,7 +170,7 @@ void VncView::startQuitting()
 
     const bool quitSuccess = vncThread.wait(500);
 
-    kDebug(5011) << "Quit VNC thread success:" << quitSuccess;
+    kDebug(5011) << "startQuitting(): Quit VNC thread success:" << quitSuccess;
 
     setStatus(Disconnected);
 }
@@ -216,11 +212,6 @@ void VncView::requestPassword()
 {
     kDebug(5011) << "request password";
 
-    if (m_authenticaionCanceled) {
-        startQuitting();
-        return;
-    }
-
     setStatus(Authenticating);
 
     if (!m_url.password().isNull()) {
@@ -228,16 +219,54 @@ void VncView::requestPassword()
         return;
     }
 
-    bool ok;
-    QString password = QInputDialog::getText(this, //krazy:exclude=qclasses
-                                             tr("Password required"),
-                                             tr("Please enter the password for the remote desktop:"),
-                                             QLineEdit::Password, QString(), &ok);
-    m_firstPasswordTry = false;
-    if (ok)
-        vncThread.setPassword(password);
-    else
-        m_authenticaionCanceled = true;
+       QSettings settings;
+       settings.beginGroup("hosts");
+       QString password = settings.value(QString("%1/password").arg(m_host), "").toString();
+       //check for saved password
+       if(m_firstPasswordTry and !password.isEmpty()) {
+               kDebug(5011) << "Trying saved password";
+               m_firstPasswordTry = false;
+               vncThread.setPassword(password);
+               return;
+       }
+       m_firstPasswordTry = false;
+
+       //build dialog
+       QDialog dialog(this);
+       dialog.setWindowTitle(tr("Password required"));
+
+       QLineEdit passwordbox;
+       passwordbox.setEchoMode(QLineEdit::Password);
+       passwordbox.setText(password);
+       QCheckBox save_password(tr("Save Password"));
+       save_password.setChecked(!password.isEmpty()); //offer to overwrite saved password
+       QPushButton ok_button(tr("Done"));
+       ok_button.setMaximumWidth(100);
+       connect(&ok_button, SIGNAL(clicked()),
+               &dialog, SLOT(accept()));
+
+       QHBoxLayout layout1;
+       QVBoxLayout layout2;
+       layout2.addWidget(&passwordbox);
+       layout2.addWidget(&save_password);
+       layout1.addLayout(&layout2);
+       layout1.addWidget(&ok_button);
+       dialog.setLayout(&layout1);
+
+       if(dialog.exec()) { //dialog accepted
+               password = passwordbox.text();
+
+               if(save_password.isChecked()) {
+                       kDebug(5011) << "Saving password for host '" << m_host << "'";
+
+                       settings.setValue(QString("%1/password").arg(m_host), password);
+                       settings.sync();
+               }
+
+               vncThread.setPassword(password);
+       } else {
+               startQuitting();
+       }
 }
 
 void VncView::outputErrorMessage(const QString &message)
@@ -257,7 +286,21 @@ void VncView::outputErrorMessage(const QString &message)
 
 void VncView::updateImage(int x, int y, int w, int h)
 {
-//     kDebug(5011) << "got update" << width() << height();
+       if(!QApplication::focusWidget()) { //no focus, we're probably minimized
+               return;
+       }
+     //kDebug(5011) << "got update" << width() << height();
+     static unsigned int frames = 0;
+     static unsigned int updates = 0;
+     static QTime time = QTime::currentTime();
+     updates++;
+     if(updates % 100 == 0)
+            kDebug(5011) << "u/s: " << updates/double(time.elapsed()) * 1000.0;
+if(x == 0 and y == 0) {
+       frames++;
+     if(frames % 100 == 0)
+            kDebug(5011) << "f/s: " << frames/double(time.elapsed()) * 1000.0;
+}
 
     m_x = x;
     m_y = y;
@@ -266,10 +309,13 @@ void VncView::updateImage(int x, int y, int w, int h)
 
     if (m_horizontalFactor != 1.0 || m_verticalFactor != 1.0) {
         // If the view is scaled, grow the update rectangle to avoid artifacts
-        m_x-=1;
-        m_y-=1;
-        m_w+=2;
-        m_h+=2;
+        int x_extrapixels = 1.0/m_horizontalFactor+1;
+        int y_extrapixels = 1.0/m_verticalFactor+1;
+
+        m_x-=x_extrapixels;
+        m_y-=y_extrapixels;
+        m_w+=2*x_extrapixels;
+        m_h+=2*y_extrapixels;
     }
 
     m_frame = vncThread.image();
@@ -370,7 +416,7 @@ 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;
+     //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";
         RemoteView::paintEvent(event);
@@ -381,7 +427,7 @@ void VncView::paintEvent(QPaintEvent *event)
 
     QPainter painter(this);
 
-    if (m_repaint) {
+    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)), 
@@ -389,10 +435,10 @@ void VncView::paintEvent(QPaintEvent *event)
                                                                   qRound(m_h*m_verticalFactor),
                                                                   Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
     } else {
-         kDebug(5011) << "resize repaint";
+         //kDebug(5011) << "resize repaint";
         QRect rect = event->rect();
         if (!force_full_repaint and (rect.width() != width() || rect.height() != height())) {
-             kDebug(5011) << "Partial repaint";
+          //   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;
@@ -409,6 +455,17 @@ void VncView::paintEvent(QPaintEvent *event)
         }
     }
 
+       //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);
+
+               painter.setCompositionMode(QPainter::RasterOp_SourceXorDestination);
+               painter.drawPixmap(cursor_x-2, cursor_y-2, cursorBitmap);
+               //TODO update position of last cursor_x/y to avoid artifacts
+       }
+
     RemoteView::paintEvent(event);
 }
 
@@ -441,12 +498,15 @@ bool VncView::event(QEvent *event)
         wheelEventHandler(static_cast<QWheelEvent*>(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);
     }
 }
 
-
+//call with e == 0 to flush held events
 void VncView::mouseEventHandler(QMouseEvent *e)
 {
        static bool tap_detected = false;
@@ -454,7 +514,9 @@ void VncView::mouseEventHandler(QMouseEvent *e)
        static bool tap_drag_detected = false;
        static QTime press_time;
        static QTime up_time; //used for double clicks/tap&drag, for time after first tap
-static QTime foo;
+
+       const int TAP_PRESS_TIME = 180;
+       const int DOUBLE_TAP_UP_TIME = 500;
 
        if(!e) { //flush held taps
                if(tap_detected) {
@@ -463,10 +525,9 @@ static QTime foo;
                        m_buttonMask &= 0xfe;
                        vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
                        tap_detected = false;
-               } else if(double_tap_detected and press_time.elapsed() > 200) { //got tap + another press -> tap & drag
+               } else if(double_tap_detected and press_time.elapsed() > TAP_PRESS_TIME) { //got tap + another press -> tap & drag
                        m_buttonMask |= 0x01;
                        vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
-                       foo.start();
                        double_tap_detected = false;
                        tap_drag_detected = true;
                }
@@ -483,13 +544,18 @@ static QTime foo;
        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->type() == QEvent::MouseButtonPress or e->type() == QEvent::MouseButtonDblClick) {
                press_time.start();
-               if(tap_detected and up_time.elapsed() < 500) {
+               if(tap_detected and up_time.elapsed() < DOUBLE_TAP_UP_TIME) {
                        tap_detected = false;
                        double_tap_detected = true;
 
-                       QTimer::singleShot(200, this, SLOT(mouseEventHandler()));
+                       QTimer::singleShot(TAP_PRESS_TIME, this, SLOT(mouseEventHandler()));
                }
        } else if(e->type() == QEvent::MouseButtonRelease) {
                if(tap_drag_detected) {
@@ -507,10 +573,10 @@ static QTime foo;
                        vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
                        m_buttonMask &= 0xfe;
                        vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
-               } else if(press_time.elapsed() < 200) { //tap
+               } else if(press_time.elapsed() < TAP_PRESS_TIME) { //tap
                        up_time.start();
                        tap_detected = true;
-                       QTimer::singleShot(500, this, SLOT(mouseEventHandler()));
+                       QTimer::singleShot(DOUBLE_TAP_UP_TIME, this, SLOT(mouseEventHandler()));
                }
 
        }
@@ -545,7 +611,6 @@ void VncView::wheelEventHandler(QWheelEvent *event)
     const int x = qRound(event->x() / m_horizontalFactor);
     const int y = qRound(event->y() / m_verticalFactor);
 
-       kDebug(5011) << "Wheelevent";
     vncThread.mouseEvent(x, y, eb | m_buttonMask);
     vncThread.mouseEvent(x, y, m_buttonMask);
 }
@@ -553,8 +618,9 @@ void VncView::wheelEventHandler(QWheelEvent *event)
 void VncView::keyEventHandler(QKeyEvent *e)
 {
     // strip away autorepeating KeyRelease; see bug #206598
-    if (e->isAutoRepeat() && (e->type() == QEvent::KeyRelease))
+    if (e->isAutoRepeat() && (e->type() == QEvent::KeyRelease)) {
         return;
+    }
 
 // parts of this code are based on http://italc.sourcearchive.com/documentation/1.0.9.1/vncview_8cpp-source.html
     rfbKeySym k = e->nativeVirtualKey();
@@ -567,6 +633,16 @@ void VncView::keyEventHandler(QKeyEvent *e)
 
     const bool pressed = (e->type() == QEvent::KeyPress);
 
+#ifdef Q_WS_MAEMO_5
+    //don't send ISO_Level3_Shift (would break things like Win+0-9)
+    //also enable IM so symbol key works
+    if(k == 0xfe03) {
+           setAttribute(Qt::WA_InputMethodEnabled, pressed);
+           e->ignore();
+           return;
+    }
+#endif
+
     // handle modifiers
     if (k == XK_Shift_L || k == XK_Control_L || k == XK_Meta_L || k == XK_Alt_L) {
         if (pressed) {
@@ -579,22 +655,54 @@ void VncView::keyEventHandler(QKeyEvent *e)
     }
 
 
-    //handle clicks via zoom buttons
-    if(e->key() == Qt::Key_F8) {
-       if(pressed)
-               m_buttonMask |= 0x01;
-       else
-               m_buttonMask &= 0xfe;
-       vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
-    } else if(e->key() == Qt::Key_F7) {
-       if(pressed)
-               m_buttonMask |= 0x04;
-       else
-               m_buttonMask &= 0xfb;
-       vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
-    } else if (k) {
-        vncThread.keyEvent(k, pressed);
-    }
+       int current_zoom = -1;
+       if(e->key() == Qt::Key_F8)
+               current_zoom = left_zoom;
+       else if(e->key() == Qt::Key_F7)
+               current_zoom = right_zoom;
+       else if (k) {
+       //      kDebug(5011) << "got '" << e->text() << "'.";
+               vncThread.keyEvent(k, pressed);
+       } else {
+               kDebug(5011) << "nativeVirtualKey() for '" << e->text() << "' failed.";
+               return;
+       }       
+       
+       if(current_zoom == -1)
+               return;
+
+       //handle zoom buttons
+       if(current_zoom == 0) { //left click
+               if(pressed)
+                       m_buttonMask |= 0x01;
+               else
+                       m_buttonMask &= 0xfe;
+               vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
+       } else if(current_zoom == 1) { //right click
+               if(pressed)
+                       m_buttonMask |= 0x04;
+               else
+                       m_buttonMask &= 0xfb;
+               vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
+       } else if(current_zoom == 2) { //middle click
+               if(pressed)
+                       m_buttonMask |= 0x02;
+               else
+                       m_buttonMask &= 0xfd;
+               vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
+       } else if(current_zoom == 3 and pressed) { //wheel up
+               int eb = 0x8;
+               vncThread.mouseEvent(cursor_x, cursor_y, eb | m_buttonMask);
+               vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
+       } else if(current_zoom == 4 and pressed) { //wheel down
+               int eb = 0x10;
+               vncThread.mouseEvent(cursor_x, cursor_y, eb | m_buttonMask);
+               vncThread.mouseEvent(cursor_x, cursor_y, m_buttonMask);
+       } else if(current_zoom == 5) { //page up
+               vncThread.keyEvent(0xff55, pressed);
+       } else if(current_zoom == 6) { //page down
+               vncThread.keyEvent(0xff56, pressed);
+       }
 }
 
 void VncView::unpressModifiers()
@@ -610,7 +718,7 @@ void VncView::unpressModifiers()
 
 void VncView::clipboardSelectionChanged()
 {
-    kDebug(5011);
+    //kDebug(5011);
 
     if (m_status != Connected)
         return;
@@ -625,7 +733,7 @@ void VncView::clipboardSelectionChanged()
 
 void VncView::clipboardDataChanged()
 {
-    kDebug(5011);
+    //kDebug(5011);
 
     if (m_status != Connected)
         return;
@@ -641,7 +749,8 @@ void VncView::clipboardDataChanged()
 //fake key events
 void VncView::sendKey(Qt::Key key)
 {
-       int k = 0; //X11 keysym
+       //convert Qt::Key into x11 keysym
+       int k = 0;
        switch(key) {
        case Qt::Key_Escape:
                k = 0xff1b;
@@ -655,14 +764,52 @@ void VncView::sendKey(Qt::Key key)
        case Qt::Key_PageDown:
                k = 0xff56;
                break;
-       case Qt::Key_Meta: //TODO: test this.
+       case Qt::Key_Return:
+               k = 0xff0d;
+               break;
+       case Qt::Key_Insert:
+               k = 0xff63;
+               break;
+       case Qt::Key_Delete:
+               k = 0xffff;
+               break;
+       case Qt::Key_Home:
+               k = 0xff50;
+               break;
+       case Qt::Key_End:
+               k = 0xff57;
+               break;
+       case Qt::Key_Backspace:
+               k = 0xff08;
+               break;
+       case Qt::Key_F1:
+       case Qt::Key_F2:
+       case Qt::Key_F3:
+       case Qt::Key_F4:
+       case Qt::Key_F5:
+       case Qt::Key_F6:
+       case Qt::Key_F7:
+       case Qt::Key_F8:
+       case Qt::Key_F9:
+       case Qt::Key_F10:
+       case Qt::Key_F11:
+       case Qt::Key_F12:
+               k = 0xffbe + int(key - Qt::Key_F1);
+               break;
+       case Qt::Key_Meta:
+       case Qt::MetaModifier:
                k = XK_Super_L;
                break;
        case Qt::Key_Alt:
+       case Qt::AltModifier:
                k = XK_Alt_L;
                break;
+       case Qt::Key_Control:
+       case Qt::ControlModifier:
+               k = XK_Control_L;
+               break;
        default:
-               kDebug(5011) << "unhandled Qt::Key value " << key;
+               kDebug(5011) << "sendKey(): Unhandled Qt::Key value " << key;
                return;
        }
 
@@ -680,4 +827,62 @@ void VncView::sendKey(Qt::Key key)
        }
 }
 
+void VncView::sendKeySequence(QKeySequence keys)
+{
+       Q_ASSERT(keys.count() <= 1); //we can only handle a single combination
+
+       //to get at individual key presses, we split 'keys' into its components
+       QList<int> key_list;
+       for(int i = 0; ; i++) {
+               QString k = keys.toString().section('+', i, i);
+               if(k.isEmpty())
+                       break;
+               //kDebug(5011) << "found key: " << k;
+               if(k == "Alt") {
+                       key_list.append(Qt::Key_Alt);
+               } else if(k == "Ctrl") {
+                       key_list.append(Qt::Key_Control);
+               } else if(k == "Meta") {
+                       key_list.append(Qt::Key_Meta);
+               } else {
+                       key_list.append(QKeySequence(k)[0]);
+               }
+       }
+       
+       for(int i = 0; i < key_list.count(); i++)
+               sendKey(Qt::Key(key_list.at(i)));
+
+       //release modifiers (everything before final key)
+       for(int i = key_list.count()-2; i >= 0; i--)
+               sendKey(Qt::Key(key_list.at(i)));
+}
+
+void VncView::reloadSettings()
+{
+       QSettings settings;
+       left_zoom = settings.value("left_zoom", 0).toInt();
+       right_zoom = settings.value("right_zoom", 1).toInt();
+       disable_tapping = settings.value("disable_tapping", false).toBool();
+}
+
+//convert commitString into keyevents
+void VncView::inputMethodEvent(QInputMethodEvent *event)
+{
+       //TODO handle replacements
+       //NOTE for the return key to work Qt needs to enable multiline input, which only works for Q(Plain)TextEdit
+
+       //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.
+               if(!k) {
+                       kDebug(5011) << "unhandled key";
+                       continue;
+               }
+               vncThread.keyEvent(k, true);
+               vncThread.keyEvent(k, false);
+       }
+}
+
+
 #include "moc_vncview.cpp"