diff --git a/kded/daemon.cpp b/kded/daemon.cpp index db8107b..6ea4b52 100644 --- a/kded/daemon.cpp +++ b/kded/daemon.cpp @@ -1,215 +1,220 @@ /************************************************************************************* * Copyright (C) 2012 by Alejandro Fiestas Olivares * * * * 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) any later version. * * * * 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, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * *************************************************************************************/ #include "daemon.h" #include "serializer.h" #include "generator.h" #include "device.h" #include #include #include #include #include #include #include #include #include K_PLUGIN_FACTORY(KScreenDaemonFactory, registerPlugin();) K_EXPORT_PLUGIN(KScreenDaemonFactory("kscreen", "kscreen")) KScreenDaemon::KScreenDaemon(QObject* parent, const QList< QVariant >& ) : KDEDModule(parent) , m_monitoredConfig(0) , m_iteration(0) , m_pendingSave(false) , m_monitoring(false) , m_timer(new QTimer()) { setenv("KSCREEN_BACKEND", "XRandR", 1); KActionCollection *coll = new KActionCollection(this); KAction* action = coll->addAction("display"); action->setText(i18n("Switch Display" )); action->setGlobalShortcut(KShortcut(Qt::Key_Display)); connect(Device::self(), SIGNAL(lidIsClosedChanged(bool,bool)), SLOT(lidClosedChanged())); m_timer->setInterval(300); m_timer->setSingleShot(true); connect(m_timer, SIGNAL(timeout()), SLOT(applyGenericConfig())); connect(action, SIGNAL(triggered(bool)), SLOT(displayButton())); connect(Generator::self(), SIGNAL(ready()), SLOT(init())); monitorConnectedChange(); } KScreenDaemon::~KScreenDaemon() { Generator::destroy(); Device::destroy(); } void KScreenDaemon::init() { applyConfig(); } void KScreenDaemon::applyConfig() { qDebug() << "Applying config"; if (Serializer::configExists()) { applyKnownConfig(); return; } applyIdealConfig(); } void KScreenDaemon::applyKnownConfig() { setMonitorForChanges(false); KScreen::Config::setConfig(Serializer::config(Serializer::currentId())); - QMetaObject::invokeMethod(this, "setMonitorForChanges", Qt::QueuedConnection, Q_ARG(bool, true)); + QMetaObject::invokeMethod(this, "scheduleMonitorChange", Qt::QueuedConnection); } void KScreenDaemon::applyIdealConfig() { setMonitorForChanges(true); KScreen::Config::setConfig(Generator::self()->idealConfig()); } void KScreenDaemon::configChanged() { qDebug() << "Change detected"; if (m_pendingSave) { return; } qDebug() << "Scheduling screen save"; m_pendingSave = true; QMetaObject::invokeMethod(this, "saveCurrentConfig", Qt::QueuedConnection); } void KScreenDaemon::saveCurrentConfig() { qDebug() << "Saving current config"; m_pendingSave = false; Serializer::saveConfig(KScreen::Config::current()); } void KScreenDaemon::displayButton() { qDebug() << "displayBtn triggered"; if (m_timer->isActive()) { qDebug() << "Too fast cowboy"; return; } m_timer->start(); } void KScreenDaemon::applyGenericConfig() { if (m_iteration == 5) { m_iteration = 0; } setMonitorForChanges(true); m_iteration++; qDebug() << "displayButton: " << m_iteration; KScreen::Config::setConfig(Generator::self()->displaySwitch(m_iteration)); } void KScreenDaemon::lidClosedChanged(bool lidIsClosed) { //If the laptop is closed, use ideal config WITHOUT saving it if (lidIsClosed) { setMonitorForChanges(false); KScreen::Config::setConfig(Generator::self()->idealConfig()); return; } //If the lid is open, restore config (or generate a new one if needed applyConfig(); } void KScreenDaemon::monitorConnectedChange() { if (!m_monitoredConfig) { m_monitoredConfig = KScreen::Config::current(); if (!m_monitoredConfig) { return; } KScreen::ConfigMonitor::instance()->addConfig(m_monitoredConfig); } KScreen::OutputList outputs = m_monitoredConfig->outputs(); Q_FOREACH(KScreen::Output* output, outputs) { connect(output, SIGNAL(isConnectedChanged()), SLOT(applyConfig())); } } void KScreenDaemon::setMonitorForChanges(bool enabled) { if (m_monitoring == enabled) { return; } if (!m_monitoredConfig) { m_monitoredConfig = KScreen::Config::current(); if (!m_monitoredConfig) { return; } KScreen::ConfigMonitor::instance()->addConfig(m_monitoredConfig); } m_monitoring = enabled; KScreen::OutputList outputs = m_monitoredConfig->outputs(); Q_FOREACH(KScreen::Output* output, outputs) { if (m_monitoring) { enableMonitor(output); } else { disableMonitor(output); } } } +void KScreenDaemon::scheduleMonitorChange() +{ + QMetaObject::invokeMethod(this, "setMonitorForChanges", Qt::QueuedConnection, Q_ARG(bool, true)); +} + void KScreenDaemon::enableMonitor(KScreen::Output* output) { connect(output, SIGNAL(currentModeChanged()), SLOT(configChanged())); connect(output, SIGNAL(isEnabledChanged()), SLOT(configChanged())); connect(output, SIGNAL(isPrimaryChanged()), SLOT(configChanged())); connect(output, SIGNAL(outputChanged()), SLOT(configChanged())); connect(output, SIGNAL(clonesChanged()), SLOT(configChanged())); connect(output, SIGNAL(posChanged()), SLOT(configChanged())); connect(output, SIGNAL(rotationChanged()), SLOT(configChanged())); } void KScreenDaemon::disableMonitor(KScreen::Output* output) { disconnect(output, SIGNAL(currentModeChanged()), this, SLOT(configChanged())); disconnect(output, SIGNAL(isEnabledChanged()), this, SLOT(configChanged())); disconnect(output, SIGNAL(isPrimaryChanged()), this, SLOT(configChanged())); disconnect(output, SIGNAL(outputChanged()), this, SLOT(configChanged())); disconnect(output, SIGNAL(clonesChanged()), this, SLOT(configChanged())); disconnect(output, SIGNAL(posChanged()), this, SLOT(configChanged())); disconnect(output, SIGNAL(rotationChanged()), this, SLOT(configChanged())); } \ No newline at end of file diff --git a/kded/daemon.h b/kded/daemon.h index a05e02e..4f83199 100644 --- a/kded/daemon.h +++ b/kded/daemon.h @@ -1,61 +1,62 @@ /************************************************************************************* * Copyright (C) 2012 by Alejandro Fiestas Olivares * * * * 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) any later version. * * * * 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, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * *************************************************************************************/ #ifndef KSCREN_DAEMON_H #define KSCREN_DAEMON_H #include #include #include class QTimer; class KDE_EXPORT KScreenDaemon : public KDEDModule { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "org.kde.KScreen") public: KScreenDaemon(QObject *parent, const QList&); virtual ~KScreenDaemon(); public Q_SLOTS: void init(); void applyConfig(); void applyKnownConfig(); void applyIdealConfig(); void configChanged(); void saveCurrentConfig(); void displayButton(); void applyGenericConfig(); void lidClosedChanged(bool lidIsClosed); void setMonitorForChanges(bool enabled); + void scheduleMonitorChange(); private: void monitorConnectedChange(); void enableMonitor(KScreen::Output *output); void disableMonitor(KScreen::Output *output); KScreen::Config* m_monitoredConfig; quint8 m_iteration; bool m_pendingSave; bool m_monitoring; QTimer* m_timer; }; #endif /*KSCREN_DAEMON_H*/