File indexing completed on 2025-07-06 04:55:36

0001 /*
0002     SPDX-FileCopyrightText: 2022 Aditya Mehra <aix.m@outlook.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "remotecontroller.h"
0008 
0009 #include <KAboutData>
0010 #include <KLocalizedString>
0011 #include <KPluginFactory>
0012 #include <KSharedConfig>
0013 #include <QDBusConnection>
0014 #include <QDBusConnectionInterface>
0015 #include <QDBusMessage>
0016 #include <QtQml>
0017 
0018 K_PLUGIN_CLASS_WITH_JSON(RemoteController, "kcm_mediacenter_remotecontrollers.json")
0019 
0020 RemoteController::RemoteController(QObject *parent, const KPluginMetaData &metaData)
0021     : KQuickConfigModule(parent, metaData)
0022     , m_devicesModel(new DevicesModel(this))
0023     , m_keyMapModel(new KeyMapModel(this))
0024 {
0025     setButtons(Help);
0026 
0027     // connect to the evdevKeyPressed signal from kcmDbusInterface
0028     connect(&kcmDbusInterface, &KcmDbusInterface::evdevKeyPressed, this, [this](int keyCode) {
0029         Q_EMIT gamepadKeyPressed(keyCode);
0030     });
0031 
0032     const char *uri = "org.kde.private.kcm.remotecontrollers";
0033     qmlRegisterUncreatableType<KeyMapModel>(uri, 1, 0, "KeyMapModel", QStringLiteral("Cannot create an item of type KeyMapModel"));
0034     qmlRegisterUncreatableType<DevicesModel>(uri, 1, 0, "DevicesModel", QStringLiteral("Cannot create an item of type DevicesModel"));
0035 }
0036 
0037 RemoteController::~RemoteController()
0038 {
0039 }
0040 
0041 QString RemoteController::cecKeyConfig(const QString &key)
0042 {
0043     static KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("plasma-remotecontrollersrc"));
0044     static KConfigGroup grp(config, QLatin1String("General"));
0045 
0046     if (grp.isValid()) {
0047         return grp.readEntry(key, QString());
0048     }
0049 
0050     return "Null";
0051 }
0052 
0053 QString RemoteController::gamepadKeyConfig(const QString &key)
0054 {
0055     static KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("plasma-remotecontrollersrc"));
0056     static KConfigGroup grp(config, QLatin1String("Gamepad"));
0057 
0058     if (grp.isValid()) {
0059         return grp.readEntry(key, QString());
0060     }
0061 
0062     return "Null";
0063 }
0064 
0065 void RemoteController::setCecKeyConfig(const QString &button, const QString &key)
0066 {
0067     static KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("plasma-remotecontrollersrc"));
0068     static KConfigGroup grp(config, QLatin1String("General"));
0069 
0070     if (grp.isValid()) {
0071         grp.writeEntry(button, key);
0072         grp.sync();
0073         Q_EMIT cecConfigChanged(button);
0074     }
0075 
0076     m_keyMapModel->refresh();
0077 }
0078 
0079 void RemoteController::setGamepadKeyConfig(const QString &button, const QString &key)
0080 {
0081     static KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("plasma-remotecontrollersrc"));
0082     static KConfigGroup grp(config, QLatin1String("Gamepad"));
0083 
0084     if (grp.isValid()) {
0085         grp.writeEntry(button, key);
0086         grp.sync();
0087         Q_EMIT gamepadConfigChanged(button);
0088     }
0089 
0090     m_keyMapModel->refresh();
0091 }
0092 
0093 void RemoteController::acquireNoOp()
0094 {
0095     QDBusMessage message = QDBusMessage::createMethodCall("org.kde.plasma.remotecontrollers",
0096                                                           "/ControllerManager",
0097                                                           "org.kde.plasma.remotecontrollers.ControllerManager",
0098                                                           "acquireNoOp");
0099     QDBusConnection::sessionBus().call(message);
0100 }
0101 
0102 void RemoteController::releaseNoOp()
0103 {
0104     QDBusMessage message = QDBusMessage::createMethodCall("org.kde.plasma.remotecontrollers",
0105                                                           "/ControllerManager",
0106                                                           "org.kde.plasma.remotecontrollers.ControllerManager",
0107                                                           "releaseNoOp");
0108     QDBusConnection::sessionBus().call(message);
0109 }
0110 
0111 int RemoteController::cecKeyFromRemotePress()
0112 {
0113     QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.plasma.remotecontrollers", "/CEC", "", "sendNextKey");
0114     QDBusMessage response = QDBusConnection::sessionBus().call(msg);
0115     QList<QVariant> responseArg = response.arguments();
0116     return responseArg.at(0).toInt();
0117 }
0118 
0119 KeyMapModel *RemoteController::keyMapModel()
0120 {
0121     if (!m_keyMapModel) {
0122         m_keyMapModel = new KeyMapModel(this);
0123     }
0124     return m_keyMapModel;
0125 }
0126 
0127 DevicesModel *RemoteController::devicesModel()
0128 {
0129     if (!m_devicesModel) {
0130         m_devicesModel = new DevicesModel(this);
0131     }
0132     return m_devicesModel;
0133 }
0134 
0135 #include "remotecontroller.moc"
0136 
0137 #include "moc_remotecontroller.cpp"