From 1565910c40739b159710d9ed709eecd431e35a22 Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Mon, 13 Jan 2020 22:41:36 +0200 Subject: [PATCH] [nightcolor] Expose transition timings to d-bus Summary: These timings can be useful for the night color applet. For example, it could show "Next transition will start at XXX" or something like that. Reviewers: #kwin, davidedmundson Reviewed By: #kwin, davidedmundson Subscribers: kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D26666 --- colorcorrection/colorcorrectdbusinterface.cpp | 68 +++++++++++++++++++ colorcorrection/colorcorrectdbusinterface.h | 8 +++ colorcorrection/manager.cpp | 47 ++++++++++--- colorcorrection/manager.h | 36 +++++++++- org.kde.kwin.ColorCorrect.xml | 28 ++++++++ 5 files changed, 176 insertions(+), 11 deletions(-) diff --git a/colorcorrection/colorcorrectdbusinterface.cpp b/colorcorrection/colorcorrectdbusinterface.cpp index 80df053d8f..dc46481bf1 100644 --- a/colorcorrection/colorcorrectdbusinterface.cpp +++ b/colorcorrection/colorcorrectdbusinterface.cpp @@ -152,6 +152,46 @@ ColorCorrectDBusInterface::ColorCorrectDBusInterface(Manager *parent) QDBusConnection::sessionBus().send(message); }); + connect(m_manager, &Manager::previousTransitionTimingsChanged, this, [this] { + QVariantMap changedProperties; + changedProperties.insert(QStringLiteral("previousTransitionDateTime"), previousTransitionDateTime()); + changedProperties.insert(QStringLiteral("previousTransitionDuration"), previousTransitionDuration()); + + QDBusMessage message = QDBusMessage::createSignal( + QStringLiteral("/ColorCorrect"), + QStringLiteral("org.freedesktop.DBus.Properties"), + QStringLiteral("PropertiesChanged") + ); + + message.setArguments({ + QStringLiteral("org.kde.kwin.ColorCorrect"), + changedProperties, + QStringList(), // invalidated_properties + }); + + QDBusConnection::sessionBus().send(message); + }); + + connect(m_manager, &Manager::scheduledTransitionTimingsChanged, this, [this] { + QVariantMap changedProperties; + changedProperties.insert(QStringLiteral("scheduledTransitionDateTime"), scheduledTransitionDateTime()); + changedProperties.insert(QStringLiteral("scheduledTransitionDuration"), scheduledTransitionDuration()); + + QDBusMessage message = QDBusMessage::createSignal( + QStringLiteral("/ColorCorrect"), + QStringLiteral("org.freedesktop.DBus.Properties"), + QStringLiteral("PropertiesChanged") + ); + + message.setArguments({ + QStringLiteral("org.kde.kwin.ColorCorrect"), + changedProperties, + QStringList(), // invalidated_properties + }); + + QDBusConnection::sessionBus().send(message); + }); + connect(m_manager, &Manager::configChange, this, &ColorCorrectDBusInterface::nightColorConfigChanged); new ColorCorrectAdaptor(this); QDBusConnection::sessionBus().registerObject(QStringLiteral("/ColorCorrect"), this); @@ -192,6 +232,34 @@ int ColorCorrectDBusInterface::mode() const return m_manager->mode(); } +quint64 ColorCorrectDBusInterface::previousTransitionDateTime() const +{ + const QDateTime dateTime = m_manager->previousTransitionDateTime(); + if (dateTime.isValid()) { + return quint64(dateTime.toSecsSinceEpoch()); + } + return 0; +} + +quint32 ColorCorrectDBusInterface::previousTransitionDuration() const +{ + return quint32(m_manager->previousTransitionDuration()); +} + +quint64 ColorCorrectDBusInterface::scheduledTransitionDateTime() const +{ + const QDateTime dateTime = m_manager->scheduledTransitionDateTime(); + if (dateTime.isValid()) { + return quint64(dateTime.toSecsSinceEpoch()); + } + return 0; +} + +quint32 ColorCorrectDBusInterface::scheduledTransitionDuration() const +{ + return quint32(m_manager->scheduledTransitionDuration()); +} + QHash ColorCorrectDBusInterface::nightColorInfo() { return m_manager->info(); diff --git a/colorcorrection/colorcorrectdbusinterface.h b/colorcorrection/colorcorrectdbusinterface.h index 36fad03b6c..94481aefdf 100644 --- a/colorcorrection/colorcorrectdbusinterface.h +++ b/colorcorrection/colorcorrectdbusinterface.h @@ -43,6 +43,10 @@ class ColorCorrectDBusInterface : public QObject, public QDBusContext Q_PROPERTY(int currentTemperature READ currentTemperature) Q_PROPERTY(int targetTemperature READ targetTemperature) Q_PROPERTY(int mode READ mode) + Q_PROPERTY(quint64 previousTransitionDateTime READ previousTransitionDateTime) + Q_PROPERTY(quint32 previousTransitionDuration READ previousTransitionDuration) + Q_PROPERTY(quint64 scheduledTransitionDateTime READ scheduledTransitionDateTime) + Q_PROPERTY(quint32 scheduledTransitionDuration READ scheduledTransitionDuration) public: explicit ColorCorrectDBusInterface(Manager *parent); @@ -55,6 +59,10 @@ public: int currentTemperature() const; int targetTemperature() const; int mode() const; + quint64 previousTransitionDateTime() const; + quint32 previousTransitionDuration() const; + quint64 scheduledTransitionDateTime() const; + quint32 scheduledTransitionDuration() const; public Q_SLOTS: /** diff --git a/colorcorrection/manager.cpp b/colorcorrection/manager.cpp index c3e94e5370..787961d668 100644 --- a/colorcorrection/manager.cpp +++ b/colorcorrection/manager.cpp @@ -135,10 +135,7 @@ void Manager::hardReset() { cancelAllTimers(); - // Timings of the Sun are not used in the constant mode. - if (m_mode != NightColorMode::Constant) { - updateSunTimings(true); - } + updateTransitionTimings(true); updateTargetTemperature(); if (isAvailable() && isEnabled() && !isInhibited()) { @@ -216,6 +213,26 @@ NightColorMode Manager::mode() const return m_mode; } +QDateTime Manager::previousTransitionDateTime() const +{ + return m_prev.first; +} + +qint64 Manager::previousTransitionDuration() const +{ + return m_prev.first.msecsTo(m_prev.second); +} + +QDateTime Manager::scheduledTransitionDateTime() const +{ + return m_next.first; +} + +qint64 Manager::scheduledTransitionDuration() const +{ + return m_next.first.msecsTo(m_next.second); +} + void Manager::initShortcuts() { QAction *toggleAction = new QAction(this); @@ -320,10 +337,7 @@ void Manager::cancelAllTimers() void Manager::resetQuickAdjustTimer() { - // We don't use timings of the Sun in the constant mode. - if (m_mode != NightColorMode::Constant) { - updateSunTimings(false); - } + updateTransitionTimings(false); updateTargetTemperature(); int tempDiff = qAbs(currentTargetTemp() - m_currentTemp); @@ -389,7 +403,7 @@ void Manager::resetSlowUpdateStartTimer() m_slowUpdateStartTimer->setSingleShot(true); connect(m_slowUpdateStartTimer, &QTimer::timeout, this, &Manager::resetSlowUpdateStartTimer); - updateSunTimings(false); + updateTransitionTimings(false); updateTargetTemperature(); const int diff = QDateTime::currentDateTime().msecsTo(m_next.first); @@ -469,8 +483,16 @@ void Manager::updateTargetTemperature() emit targetTemperatureChanged(); } -void Manager::updateSunTimings(bool force) +void Manager::updateTransitionTimings(bool force) { + if (m_mode == NightColorMode::Constant) { + m_next = DateTimes(); + m_prev = DateTimes(); + emit previousTransitionTimingsChanged(); + emit scheduledTransitionTimingsChanged(); + return; + } + const QDateTime todayNow = QDateTime::currentDateTime(); if (m_mode == NightColorMode::Timings) { @@ -489,6 +511,8 @@ void Manager::updateSunTimings(bool force) m_next = DateTimes(morB.addDays(1), morE.addDays(1)); m_prev = DateTimes(eveB, eveE); } + emit previousTransitionTimingsChanged(); + emit scheduledTransitionTimingsChanged(); return; } @@ -531,6 +555,9 @@ void Manager::updateSunTimings(bool force) } } } + + emit previousTransitionTimingsChanged(); + emit scheduledTransitionTimingsChanged(); } DateTimes Manager::getSunTimings(const QDateTime &dateTime, double latitude, double longitude, bool morning) const diff --git a/colorcorrection/manager.h b/colorcorrection/manager.h index 5b91a4eb15..51c5bc0413 100644 --- a/colorcorrection/manager.h +++ b/colorcorrection/manager.h @@ -175,6 +175,30 @@ public: */ NightColorMode mode() const; + /** + * Returns the datetime that specifies when the previous screen color temperature transition + * had started. Notice that when Night Color operates in the Constant mode, the returned date + * time object is not valid. + */ + QDateTime previousTransitionDateTime() const; + + /** + * Returns the duration of the previous screen color temperature transition, in milliseconds. + */ + qint64 previousTransitionDuration() const; + + /** + * Returns the datetime that specifies when the next screen color temperature transition will + * start. Notice that when Night Color operates in the Constant mode, the returned date time + * object is not valid. + */ + QDateTime scheduledTransitionDateTime() const; + + /** + * Returns the duration of the next screen color temperature transition, in milliseconds. + */ + qint64 scheduledTransitionDuration() const; + // for auto tests void reparseConfigAndReset(); @@ -215,6 +239,16 @@ Q_SIGNALS: */ void modeChanged(); + /** + * Emitted whenever the timings of the previous color temperature transition have changed. + */ + void previousTransitionTimingsChanged(); + + /** + * Emitted whenever the timings of the next color temperature transition have changed. + */ + void scheduledTransitionTimingsChanged(); + private: void initShortcuts(); void readConfig(); @@ -233,7 +267,7 @@ private: void resetSlowUpdateTimer(); void updateTargetTemperature(); - void updateSunTimings(bool force); + void updateTransitionTimings(bool force); DateTimes getSunTimings(const QDateTime &dateTime, double latitude, double longitude, bool morning) const; bool checkAutomaticSunTimings() const; bool daylight() const; diff --git a/org.kde.kwin.ColorCorrect.xml b/org.kde.kwin.ColorCorrect.xml index 7b45f3f1ea..4dd8ab7d59 100644 --- a/org.kde.kwin.ColorCorrect.xml +++ b/org.kde.kwin.ColorCorrect.xml @@ -84,5 +84,33 @@ Valid modes: 0 - automatic, 1 - location, 2 - timings, 3 - constant. --> + + + + + + + + + + + +