kdeconnect-kde/plugins/remotekeyboard/remotekeyboardplugin.cpp
Holger Kaelberer 040ad7357b kdeconnect-kde: Add remotekeyboard plugin
Allow to inject keypress events to remote peers (most notably Android devices)

Notes / open issues / possible improvements:

- For the json-payload I used the syntax of the key-events as sent by mousepad-plugin with the addition of a "sendAck"-flag. If "sendAck" is set to true the remote peer should echo a key-event if it could be handled, thus allowing the local client to find out whether the key was accepted. For performance reasons, it's allowed to send multi-char strings in the "key" property (performs much better if you send a whole string via "echo '...' |  kdeconnect-cli ..." e.g.)

- kdeconnect-cli: For now takes a string and transforms it into single key-events for visible characters only. In a first implementation I used a kbhit() helper that used termios.h to catch and relay keypresses interactively (including some special-events), which was not optimal. A better approch might be to use linux input-api directly. Would this be an option regarding cross-platform compatibility or can I assume to develop for Linux only? Being a command-line guy, I'd really like to have a fully featured kdeconnect-cli interface ;-)

- Factor out the Qt::Key-to-internal keymap to some core-helper because it corresponds to the mapping in the mousepad-plugin?

- The plasmoid is not perfect as it is: A single line containing a non-echoing TextField (i.e. it eats all the KeyPress events), and only ack-ed keypress-packets from the peer device are injected if they contain visible keys. Advantage: the user sees whether his key-presses are accepted by the peer device. Disadvantage: The echoed text does not correspond 1:1 to what is shown on the peer's display, user might be confused when typing without success. I played around with different variations each of which with its proper shortcomings:
1. An echoing Textfield for typing: Has the advantage that the user can directly see what he is typing, which makes interaction in the typing field easier, BUT messes up interaction if the Editor on the peer is changed silently and does not notify the user if his keypresses are not handled by the peer.
2. A non-echoing TextField for typing PLUS a readonly one for printing visible echoed keys. Disadvantage: same as for the previous one and uses more space on the plasmoid.
Comments? Ideas?

REVIEW: 129727
BUG: 370919
2017-01-10 21:12:42 +01:00

144 lines
4.8 KiB
C++

/**
* Copyright 2016 Holger Kaelberer <holger.k@elberer.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "remotekeyboardplugin.h"
#include <KPluginFactory>
#include <KLocalizedString>
#include <QDebug>
#include <QString>
#include <QVariantMap>
K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_remotekeyboard.json", registerPlugin< RemoteKeyboardPlugin >(); )
Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_REMOTEKEYBOARD, "kdeconnect.plugin.remotekeyboard");
// Mapping of Qt::Key to internal codes, corresponds to the mapping in mousepadplugin
QMap<int, int> specialKeysMap = {
//0, // Invalid
{Qt::Key_Backspace, 1},
{Qt::Key_Tab, 2},
//XK_Linefeed, // 3
{Qt::Key_Left, 4},
{Qt::Key_Up, 5},
{Qt::Key_Right, 6},
{Qt::Key_Down, 7},
{Qt::Key_PageUp, 8},
{Qt::Key_PageDown, 9},
{Qt::Key_Home, 10},
{Qt::Key_End, 11},
{Qt::Key_Return, 12},
{Qt::Key_Enter, 12},
{Qt::Key_Delete, 13},
{Qt::Key_Escape, 14},
{Qt::Key_SysReq, 15},
{Qt::Key_ScrollLock, 16},
//0, // 17
//0, // 18
//0, // 19
//0, // 20
{Qt::Key_F1, 21},
{Qt::Key_F2, 22},
{Qt::Key_F3, 23},
{Qt::Key_F4, 24},
{Qt::Key_F5, 25},
{Qt::Key_F6, 26},
{Qt::Key_F7, 27},
{Qt::Key_F8, 28},
{Qt::Key_F9, 29},
{Qt::Key_F10, 30},
{Qt::Key_F11, 31},
{Qt::Key_F12, 32},
};
RemoteKeyboardPlugin::RemoteKeyboardPlugin(QObject* parent, const QVariantList& args)
: KdeConnectPlugin(parent, args)
{
}
RemoteKeyboardPlugin::~RemoteKeyboardPlugin()
{
}
bool RemoteKeyboardPlugin::receivePackage(const NetworkPackage& np)
{
if (np.type() == PACKAGE_TYPE_REMOTEKEYBOARD) {
if (!np.has("isAck") || !np.has("key")) {
qCWarning(KDECONNECT_PLUGIN_REMOTEKEYBOARD) << "Invalid packet of type"
<< PACKAGE_TYPE_REMOTEKEYBOARD;
return false;
}
// qCWarning(KDECONNECT_PLUGIN_REMOTEKEYBOARD) << "Received keypress" << np;
Q_EMIT keyPressReceived(np.get<QString>("key"),
np.get<int>("specialKey", 0),
np.get<int>("shift", false),
np.get<int>("ctrl", false),
np.get<int>("alt", false));
return true;
}
return true;
}
void RemoteKeyboardPlugin::sendKeyPress(const QString& key, int specialKey,
bool shift, bool ctrl,
bool alt, bool sendAck) const
{
NetworkPackage np(PACKAGE_TYPE_REMOTEKEYBOARD_REQUEST, {
{"key", key},
{"specialKey", specialKey},
{"shift", shift},
{"ctrl", ctrl},
{"alt", alt},
{"sendAck", sendAck}
});
sendPackage(np);
}
void RemoteKeyboardPlugin::sendQKeyEvent(const QVariantMap& keyEvent, bool sendAck) const
{
if (!keyEvent.contains("key"))
return;
int k = translateQtKey(keyEvent.value("key").toInt());
int modifiers = keyEvent.value("modifiers").toInt();
sendKeyPress(keyEvent.value("text").toString(), k,
modifiers & Qt::ShiftModifier,
modifiers & Qt::ControlModifier,
modifiers & Qt::AltModifier,
sendAck);
}
int RemoteKeyboardPlugin::translateQtKey(int qtKey) const
{
return specialKeysMap.value(qtKey, 0);
}
void RemoteKeyboardPlugin::connected()
{
QDBusConnection::sessionBus().registerObject(dbusPath(), this,
QDBusConnection::ExportScriptableInvokables
| QDBusConnection::ExportScriptableSignals);
}
QString RemoteKeyboardPlugin::dbusPath() const
{
return "/modules/kdeconnect/devices/" + device()->id() + "/remotekeyboard";
}
#include "remotekeyboardplugin.moc"