File indexing completed on 2024-04-28 16:45:10
0001 /* 0002 Work sponsored by the LiMux project of the city of Munich: 0003 SPDX-FileCopyrightText: 2018 Kai Uwe Broulik <kde@broulik.de> 0004 0005 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0006 */ 0007 0008 #include "kscreenapplet.h" 0009 0010 #include <QMetaEnum> 0011 #include <QQmlEngine> // for qmlRegisterType 0012 0013 #include <QDBusConnection> 0014 #include <QDBusMessage> 0015 0016 #include <KScreen/Config> 0017 #include <KScreen/ConfigMonitor> 0018 #include <KScreen/GetConfigOperation> 0019 #include <KScreen/Output> 0020 0021 #include <algorithm> 0022 0023 KScreenApplet::KScreenApplet(QObject *parent, const KPluginMetaData &data, const QVariantList &args) 0024 : Plasma::Applet(parent, data, args) 0025 { 0026 } 0027 0028 KScreenApplet::~KScreenApplet() = default; 0029 0030 void KScreenApplet::init() 0031 { 0032 qmlRegisterUncreatableType<KScreen::OsdAction>("org.kde.private.kscreen", 1, 0, "OsdAction", QStringLiteral("Can't create OsdAction")); 0033 connect(new KScreen::GetConfigOperation(KScreen::GetConfigOperation::NoEDID), 0034 &KScreen::ConfigOperation::finished, 0035 this, 0036 [this](KScreen::ConfigOperation *op) { 0037 m_screenConfiguration = qobject_cast<KScreen::GetConfigOperation *>(op)->config(); 0038 0039 KScreen::ConfigMonitor::instance()->addConfig(m_screenConfiguration); 0040 connect(KScreen::ConfigMonitor::instance(), &KScreen::ConfigMonitor::configurationChanged, this, &KScreenApplet::checkOutputs); 0041 0042 checkOutputs(); 0043 }); 0044 } 0045 0046 int KScreenApplet::connectedOutputCount() const 0047 { 0048 return m_connectedOutputCount; 0049 } 0050 0051 void KScreenApplet::applyLayoutPreset(Action action) 0052 { 0053 const QMetaEnum actionEnum = QMetaEnum::fromType<KScreen::OsdAction::Action>(); 0054 Q_ASSERT(actionEnum.isValid()); 0055 0056 const QString presetName = QString::fromLatin1(actionEnum.valueToKey(action)); 0057 if (presetName.isEmpty()) { 0058 return; 0059 } 0060 0061 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kded5"), 0062 QStringLiteral("/modules/kscreen"), 0063 QStringLiteral("org.kde.KScreen"), 0064 QStringLiteral("applyLayoutPreset")); 0065 0066 msg.setArguments({presetName}); 0067 0068 QDBusConnection::sessionBus().call(msg, QDBus::NoBlock); 0069 } 0070 0071 void KScreenApplet::checkOutputs() 0072 { 0073 if (!m_screenConfiguration) { 0074 return; 0075 } 0076 0077 const int oldConnectedOutputCount = m_connectedOutputCount; 0078 0079 const auto outputs = m_screenConfiguration->outputs(); 0080 m_connectedOutputCount = std::count_if(outputs.begin(), outputs.end(), [](const KScreen::OutputPtr &output) { 0081 return output->isConnected(); 0082 }); 0083 0084 if (m_connectedOutputCount != oldConnectedOutputCount) { 0085 emit connectedOutputCountChanged(); 0086 } 0087 } 0088 0089 QVariant KScreenApplet::availableActions() 0090 { 0091 // Need to wrap it in a QVariant, otherwise QML doesn't like the return type 0092 return QVariant::fromValue(KScreen::OsdAction::availableActions()); 0093 } 0094 0095 K_PLUGIN_CLASS_WITH_JSON(KScreenApplet, "metadata.json") 0096 0097 #include "kscreenapplet.moc"