File indexing completed on 2024-04-21 16:10:59

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 <QDBusMessage>
0015 #include <QDBusConnectionInterface>
0016 #include <QtQml>
0017 
0018 K_PLUGIN_CLASS_WITH_JSON(RemoteController, "kcm_mediacenter_remotecontrollers.json")
0019 
0020 RemoteController::RemoteController(QObject *parent, const QVariantList &args)
0021     : KQuickAddons::ConfigModule(parent, args)
0022     , m_devicesModel(new DevicesModel(this))
0023     , m_keyMapModel(new KeyMapModel(this))
0024 {
0025     setButtons(Help);
0026     KAboutData *about = new KAboutData(QStringLiteral("kcm_mediacenter_remotecontrollers"), //
0027                                        i18n("Configure Remote Controllers"),
0028                                        QStringLiteral("2.0"),
0029                                        QString(),
0030                                        KAboutLicense::LGPL);
0031     setAboutData(about);
0032 
0033     // connect to the evdevKeyPressed signal from kcmDbusInterface
0034     connect(&kcmDbusInterface, &KcmDbusInterface::evdevKeyPressed, this, [this](int keyCode) {
0035         emit gamepadKeyPressed(keyCode);
0036     });
0037 
0038     const QByteArray uri("org.kde.private.kcm.remotecontrollers");
0039     qmlRegisterUncreatableType<KeyMapModel>(uri, 1, 0, "KeyMapModel", QStringLiteral("Cannot create an item of type KeyMapModel"));
0040     qmlRegisterUncreatableType<DevicesModel>(uri, 1, 0, "DevicesModel", QStringLiteral("Cannot create an item of type DevicesModel"));
0041 }
0042 
0043 RemoteController::~RemoteController()
0044 {
0045 }
0046 
0047 QString RemoteController::cecKeyConfig(const QString &key)
0048 {
0049     static KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("plasma-remotecontrollersrc"));
0050     static KConfigGroup grp(config, QLatin1String("General"));
0051 
0052     if (grp.isValid()) {
0053         return grp.readEntry(key, QString());
0054     }
0055 
0056     return "Null";
0057 }
0058 
0059 QString RemoteController::gamepadKeyConfig(const QString &key)
0060 {
0061     static KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("plasma-remotecontrollersrc"));
0062     static KConfigGroup grp(config, QLatin1String("Gamepad"));
0063 
0064     if (grp.isValid()) {
0065         return grp.readEntry(key, QString());
0066     }
0067 
0068     return "Null";
0069 }
0070 
0071 void RemoteController::setCecKeyConfig(const QString &button, const QString &key)
0072 {
0073     static KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("plasma-remotecontrollersrc"));
0074     static KConfigGroup grp(config, QLatin1String("General"));
0075 
0076     if (grp.isValid()) {
0077         grp.writeEntry(button, key);
0078         grp.sync();
0079         emit cecConfigChanged(button);
0080     }
0081 
0082     m_keyMapModel->refresh();
0083 }
0084 
0085 void RemoteController::setGamepadKeyConfig(const QString &button, const QString &key)
0086 {
0087     static KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("plasma-remotecontrollersrc"));
0088     static KConfigGroup grp(config, QLatin1String("Gamepad"));
0089 
0090     if (grp.isValid()) {
0091         grp.writeEntry(button, key);
0092         grp.sync();
0093         emit gamepadConfigChanged(button);
0094     }
0095 
0096     m_keyMapModel->refresh();
0097 }
0098 
0099 void RemoteController::acquireNoOp()
0100 {
0101     QDBusMessage message = QDBusMessage::createMethodCall("org.kde.plasma.remotecontrollers", "/ControllerManager", "org.kde.plasma.remotecontrollers.ControllerManager", "acquireNoOp");
0102     QDBusConnection::sessionBus().call(message);
0103 }
0104 
0105 void RemoteController::releaseNoOp()
0106 {
0107     QDBusMessage message = QDBusMessage::createMethodCall("org.kde.plasma.remotecontrollers", "/ControllerManager", "org.kde.plasma.remotecontrollers.ControllerManager", "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"