2017-01-23 08:08:27 +00:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2017 Holger Kaelberer <holger.k@elberer.de>
|
2017-01-23 08:08:27 +00:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2017-01-23 08:08:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "remotekeyboardplugin.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "plugin_remotekeyboard_debug.h"
|
2017-01-23 08:08:27 +00:00
|
|
|
#include <KPluginFactory>
|
|
|
|
#include <QDebug>
|
2023-05-16 21:17:54 +01:00
|
|
|
#include <QKeySequence>
|
2017-01-23 08:08:27 +00:00
|
|
|
#include <QString>
|
|
|
|
#include <QVariantMap>
|
|
|
|
|
2019-06-12 21:16:54 +01:00
|
|
|
K_PLUGIN_CLASS_WITH_JSON(RemoteKeyboardPlugin, "kdeconnect_remotekeyboard.json")
|
2017-01-23 08:08:27 +00:00
|
|
|
|
|
|
|
// Mapping of Qt::Key to internal codes, corresponds to the mapping in mousepadplugin
|
|
|
|
QMap<int, int> specialKeysMap = {
|
2022-09-10 22:23:52 +01:00
|
|
|
// 0, // Invalid
|
2017-01-23 08:08:27 +00:00
|
|
|
{Qt::Key_Backspace, 1},
|
|
|
|
{Qt::Key_Tab, 2},
|
2022-09-10 22:23:52 +01:00
|
|
|
// XK_Linefeed, // 3
|
2017-01-23 08:08:27 +00:00
|
|
|
{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},
|
2022-09-10 22:23:52 +01:00
|
|
|
// 0, // 17
|
|
|
|
// 0, // 18
|
|
|
|
// 0, // 19
|
|
|
|
// 0, // 20
|
2017-01-23 08:08:27 +00:00
|
|
|
{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},
|
|
|
|
};
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
RemoteKeyboardPlugin::RemoteKeyboardPlugin(QObject *parent, const QVariantList &args)
|
2017-01-23 08:08:27 +00:00
|
|
|
: KdeConnectPlugin(parent, args)
|
|
|
|
, m_remoteState(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool RemoteKeyboardPlugin::receivePacket(const NetworkPacket &np)
|
2017-01-23 08:08:27 +00:00
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
if (np.type() == PACKET_TYPE_MOUSEPAD_ECHO) {
|
2019-06-10 15:40:28 +01:00
|
|
|
if (!np.has(QStringLiteral("isAck")) || !np.has(QStringLiteral("key"))) {
|
2022-09-10 22:23:52 +01:00
|
|
|
qCWarning(KDECONNECT_PLUGIN_REMOTEKEYBOARD) << "Invalid packet of type" << PACKET_TYPE_MOUSEPAD_ECHO;
|
2017-01-23 08:08:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// qCWarning(KDECONNECT_PLUGIN_REMOTEKEYBOARD) << "Received keypress" << np;
|
2019-06-10 15:40:28 +01:00
|
|
|
Q_EMIT keyPressReceived(np.get<QString>(QStringLiteral("key")),
|
|
|
|
np.get<int>(QStringLiteral("specialKey"), 0),
|
|
|
|
np.get<int>(QStringLiteral("shift"), false),
|
|
|
|
np.get<int>(QStringLiteral("ctrl"), false),
|
|
|
|
np.get<int>(QStringLiteral("alt"), 0));
|
2017-01-23 08:08:27 +00:00
|
|
|
return true;
|
2018-03-04 19:48:51 +00:00
|
|
|
} else if (np.type() == PACKET_TYPE_MOUSEPAD_KEYBOARDSTATE) {
|
2022-09-10 22:23:52 +01:00
|
|
|
// qCWarning(KDECONNECT_PLUGIN_REMOTEKEYBOARD) << "Received keyboardstate" << np;
|
2019-06-10 15:40:28 +01:00
|
|
|
if (m_remoteState != np.get<bool>(QStringLiteral("state"))) {
|
|
|
|
m_remoteState = np.get<bool>(QStringLiteral("state"));
|
2017-01-23 08:08:27 +00:00
|
|
|
Q_EMIT remoteStateChanged(m_remoteState);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void RemoteKeyboardPlugin::sendKeyPress(const QString &key, int specialKey, bool shift, bool ctrl, bool alt, bool sendAck) const
|
2017-01-23 08:08:27 +00:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_MOUSEPAD_REQUEST,
|
|
|
|
{{QStringLiteral("key"), key},
|
|
|
|
{QStringLiteral("specialKey"), specialKey},
|
|
|
|
{QStringLiteral("shift"), shift},
|
|
|
|
{QStringLiteral("ctrl"), ctrl},
|
|
|
|
{QStringLiteral("alt"), alt},
|
|
|
|
{QStringLiteral("sendAck"), sendAck}});
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2017-01-23 08:08:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void RemoteKeyboardPlugin::sendQKeyEvent(const QVariantMap &keyEvent, bool sendAck) const
|
2017-01-23 08:08:27 +00:00
|
|
|
{
|
2023-05-16 21:17:54 +01:00
|
|
|
if (!keyEvent.contains(QStringLiteral("key"))) {
|
2017-01-23 08:08:27 +00:00
|
|
|
return;
|
2023-05-16 21:17:54 +01:00
|
|
|
}
|
|
|
|
const int key = keyEvent.value(QStringLiteral("key")).toInt();
|
|
|
|
int k = translateQtKey(key);
|
2019-06-10 15:40:28 +01:00
|
|
|
int modifiers = keyEvent.value(QStringLiteral("modifiers")).toInt();
|
2023-05-16 21:17:54 +01:00
|
|
|
|
|
|
|
// Qt will be calling xkb_state_key_get_utf8 to create this string.
|
|
|
|
// As documented, it will be giving us weird strings with Ctrl combinations:
|
|
|
|
// https://xkbcommon.org/doc/current/group__keysyms.html
|
|
|
|
// Instead, just use QKeySequence to tell us which key that is and move on
|
|
|
|
QString text = keyEvent.value(QStringLiteral("text")).toString();
|
|
|
|
if (!text.isEmpty() && !text[0].isLetterOrNumber()) {
|
|
|
|
text = QKeySequence(key).toString().toLower();
|
|
|
|
}
|
|
|
|
|
2023-07-16 15:20:34 +01:00
|
|
|
sendKeyPress(text, k, modifiers & Qt::ShiftModifier, modifiers & Qt::ControlModifier, modifiers & Qt::AltModifier, sendAck);
|
2017-01-23 08:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int RemoteKeyboardPlugin::translateQtKey(int qtKey) const
|
|
|
|
{
|
|
|
|
return specialKeysMap.value(qtKey, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString RemoteKeyboardPlugin::dbusPath() const
|
|
|
|
{
|
2019-06-10 15:40:28 +01:00
|
|
|
return QStringLiteral("/modules/kdeconnect/devices/") + device()->id() + QStringLiteral("/remotekeyboard");
|
2017-01-23 08:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "remotekeyboardplugin.moc"
|