File indexing completed on 2024-11-10 04:57:04
0001 /* 0002 KWin - the KDE window manager 0003 This file is part of the KDE project. 0004 0005 SPDX-FileCopyrightText: 2017 Roman Gilg <subdiff@gmail.com> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 0010 #include "nightcolordbusinterface.h" 0011 #include "nightcolormanager.h" 0012 #include "nightlightadaptor.h" 0013 0014 #include <QDBusMessage> 0015 0016 namespace KWin 0017 { 0018 0019 NightColorDBusInterface::NightColorDBusInterface(NightColorManager *parent) 0020 : QObject(parent) 0021 , m_manager(parent) 0022 , m_inhibitorWatcher(new QDBusServiceWatcher(this)) 0023 { 0024 m_inhibitorWatcher->setConnection(QDBusConnection::sessionBus()); 0025 m_inhibitorWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration); 0026 connect(m_inhibitorWatcher, &QDBusServiceWatcher::serviceUnregistered, 0027 this, &NightColorDBusInterface::removeInhibitorService); 0028 0029 connect(m_manager, &NightColorManager::inhibitedChanged, this, [this] { 0030 QVariantMap changedProperties; 0031 changedProperties.insert(QStringLiteral("inhibited"), m_manager->isInhibited()); 0032 0033 QDBusMessage message = QDBusMessage::createSignal( 0034 QStringLiteral("/org/kde/KWin/NightLight"), 0035 QStringLiteral("org.freedesktop.DBus.Properties"), 0036 QStringLiteral("PropertiesChanged")); 0037 0038 message.setArguments({ 0039 QStringLiteral("org.kde.KWin.NightLight"), 0040 changedProperties, 0041 QStringList(), // invalidated_properties 0042 }); 0043 0044 QDBusConnection::sessionBus().send(message); 0045 }); 0046 0047 connect(m_manager, &NightColorManager::enabledChanged, this, [this] { 0048 QVariantMap changedProperties; 0049 changedProperties.insert(QStringLiteral("enabled"), m_manager->isEnabled()); 0050 0051 QDBusMessage message = QDBusMessage::createSignal( 0052 QStringLiteral("/org/kde/KWin/NightLight"), 0053 QStringLiteral("org.freedesktop.DBus.Properties"), 0054 QStringLiteral("PropertiesChanged")); 0055 0056 message.setArguments({ 0057 QStringLiteral("org.kde.KWin.NightLight"), 0058 changedProperties, 0059 QStringList(), // invalidated_properties 0060 }); 0061 0062 QDBusConnection::sessionBus().send(message); 0063 }); 0064 0065 connect(m_manager, &NightColorManager::runningChanged, this, [this] { 0066 QVariantMap changedProperties; 0067 changedProperties.insert(QStringLiteral("running"), m_manager->isRunning()); 0068 0069 QDBusMessage message = QDBusMessage::createSignal( 0070 QStringLiteral("/org/kde/KWin/NightLight"), 0071 QStringLiteral("org.freedesktop.DBus.Properties"), 0072 QStringLiteral("PropertiesChanged")); 0073 0074 message.setArguments({ 0075 QStringLiteral("org.kde.KWin.NightLight"), 0076 changedProperties, 0077 QStringList(), // invalidated_properties 0078 }); 0079 0080 QDBusConnection::sessionBus().send(message); 0081 }); 0082 0083 connect(m_manager, &NightColorManager::currentTemperatureChanged, this, [this] { 0084 QVariantMap changedProperties; 0085 changedProperties.insert(QStringLiteral("currentTemperature"), m_manager->currentTemperature()); 0086 0087 QDBusMessage message = QDBusMessage::createSignal( 0088 QStringLiteral("/org/kde/KWin/NightLight"), 0089 QStringLiteral("org.freedesktop.DBus.Properties"), 0090 QStringLiteral("PropertiesChanged")); 0091 0092 message.setArguments({ 0093 QStringLiteral("org.kde.KWin.NightLight"), 0094 changedProperties, 0095 QStringList(), // invalidated_properties 0096 }); 0097 0098 QDBusConnection::sessionBus().send(message); 0099 }); 0100 0101 connect(m_manager, &NightColorManager::targetTemperatureChanged, this, [this] { 0102 QVariantMap changedProperties; 0103 changedProperties.insert(QStringLiteral("targetTemperature"), m_manager->targetTemperature()); 0104 0105 QDBusMessage message = QDBusMessage::createSignal( 0106 QStringLiteral("/org/kde/KWin/NightLight"), 0107 QStringLiteral("org.freedesktop.DBus.Properties"), 0108 QStringLiteral("PropertiesChanged")); 0109 0110 message.setArguments({ 0111 QStringLiteral("org.kde.KWin.NightLight"), 0112 changedProperties, 0113 QStringList(), // invalidated_properties 0114 }); 0115 0116 QDBusConnection::sessionBus().send(message); 0117 }); 0118 0119 connect(m_manager, &NightColorManager::modeChanged, this, [this] { 0120 QVariantMap changedProperties; 0121 changedProperties.insert(QStringLiteral("mode"), uint(m_manager->mode())); 0122 0123 QDBusMessage message = QDBusMessage::createSignal( 0124 QStringLiteral("/org/kde/KWin/NightLight"), 0125 QStringLiteral("org.freedesktop.DBus.Properties"), 0126 QStringLiteral("PropertiesChanged")); 0127 0128 message.setArguments({ 0129 QStringLiteral("org.kde.KWin.NightLight"), 0130 changedProperties, 0131 QStringList(), // invalidated_properties 0132 }); 0133 0134 QDBusConnection::sessionBus().send(message); 0135 }); 0136 0137 connect(m_manager, &NightColorManager::daylightChanged, this, [this] { 0138 QVariantMap changedProperties; 0139 changedProperties.insert(QStringLiteral("daylight"), uint(m_manager->daylight())); 0140 0141 QDBusMessage message = QDBusMessage::createSignal( 0142 QStringLiteral("/org/kde/KWin/NightLight"), 0143 QStringLiteral("org.freedesktop.DBus.Properties"), 0144 QStringLiteral("PropertiesChanged")); 0145 0146 message.setArguments({ 0147 QStringLiteral("org.kde.KWin.NightLight"), 0148 changedProperties, 0149 QStringList(), // invalidated_properties 0150 }); 0151 0152 QDBusConnection::sessionBus().send(message); 0153 }); 0154 0155 connect(m_manager, &NightColorManager::previousTransitionTimingsChanged, this, [this] { 0156 QVariantMap changedProperties; 0157 changedProperties.insert(QStringLiteral("previousTransitionDateTime"), previousTransitionDateTime()); 0158 changedProperties.insert(QStringLiteral("previousTransitionDuration"), previousTransitionDuration()); 0159 0160 QDBusMessage message = QDBusMessage::createSignal( 0161 QStringLiteral("/org/kde/KWin/NightLight"), 0162 QStringLiteral("org.freedesktop.DBus.Properties"), 0163 QStringLiteral("PropertiesChanged")); 0164 0165 message.setArguments({ 0166 QStringLiteral("org.kde.KWin.NightLight"), 0167 changedProperties, 0168 QStringList(), // invalidated_properties 0169 }); 0170 0171 QDBusConnection::sessionBus().send(message); 0172 }); 0173 0174 connect(m_manager, &NightColorManager::scheduledTransitionTimingsChanged, this, [this] { 0175 QVariantMap changedProperties; 0176 changedProperties.insert(QStringLiteral("scheduledTransitionDateTime"), scheduledTransitionDateTime()); 0177 changedProperties.insert(QStringLiteral("scheduledTransitionDuration"), scheduledTransitionDuration()); 0178 0179 QDBusMessage message = QDBusMessage::createSignal( 0180 QStringLiteral("/org/kde/KWin/NightLight"), 0181 QStringLiteral("org.freedesktop.DBus.Properties"), 0182 QStringLiteral("PropertiesChanged")); 0183 0184 message.setArguments({ 0185 QStringLiteral("org.kde.KWin.NightLight"), 0186 changedProperties, 0187 QStringList(), // invalidated_properties 0188 }); 0189 0190 QDBusConnection::sessionBus().send(message); 0191 }); 0192 0193 new NightLightAdaptor(this); 0194 QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/kde/KWin/NightLight"), this); 0195 QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.KWin.NightLight")); 0196 } 0197 0198 NightColorDBusInterface::~NightColorDBusInterface() 0199 { 0200 QDBusConnection::sessionBus().unregisterService(QStringLiteral("org.kde.KWin.NightLight")); 0201 } 0202 0203 bool NightColorDBusInterface::isInhibited() const 0204 { 0205 return m_manager->isInhibited(); 0206 } 0207 0208 bool NightColorDBusInterface::isEnabled() const 0209 { 0210 return m_manager->isEnabled(); 0211 } 0212 0213 bool NightColorDBusInterface::isRunning() const 0214 { 0215 return m_manager->isRunning(); 0216 } 0217 0218 bool NightColorDBusInterface::isAvailable() const 0219 { 0220 return true; // TODO: Night color should register its own dbus service instead. 0221 } 0222 0223 int NightColorDBusInterface::currentTemperature() const 0224 { 0225 return m_manager->currentTemperature(); 0226 } 0227 0228 int NightColorDBusInterface::targetTemperature() const 0229 { 0230 return m_manager->targetTemperature(); 0231 } 0232 0233 int NightColorDBusInterface::mode() const 0234 { 0235 return m_manager->mode(); 0236 } 0237 0238 bool NightColorDBusInterface::daylight() const 0239 { 0240 return m_manager->daylight(); 0241 } 0242 0243 quint64 NightColorDBusInterface::previousTransitionDateTime() const 0244 { 0245 const QDateTime dateTime = m_manager->previousTransitionDateTime(); 0246 if (dateTime.isValid()) { 0247 return quint64(dateTime.toSecsSinceEpoch()); 0248 } 0249 return 0; 0250 } 0251 0252 quint32 NightColorDBusInterface::previousTransitionDuration() const 0253 { 0254 return quint32(m_manager->previousTransitionDuration()); 0255 } 0256 0257 quint64 NightColorDBusInterface::scheduledTransitionDateTime() const 0258 { 0259 const QDateTime dateTime = m_manager->scheduledTransitionDateTime(); 0260 if (dateTime.isValid()) { 0261 return quint64(dateTime.toSecsSinceEpoch()); 0262 } 0263 return 0; 0264 } 0265 0266 quint32 NightColorDBusInterface::scheduledTransitionDuration() const 0267 { 0268 return quint32(m_manager->scheduledTransitionDuration()); 0269 } 0270 0271 void NightColorDBusInterface::setLocation(double latitude, double longitude) 0272 { 0273 m_manager->autoLocationUpdate(latitude, longitude); 0274 } 0275 0276 uint NightColorDBusInterface::inhibit() 0277 { 0278 const QString serviceName = QDBusContext::message().service(); 0279 0280 if (!m_inhibitors.contains(serviceName)) { 0281 m_inhibitorWatcher->addWatchedService(serviceName); 0282 } 0283 0284 m_inhibitors.insert(serviceName, ++m_lastInhibitionCookie); 0285 0286 m_manager->inhibit(); 0287 0288 return m_lastInhibitionCookie; 0289 } 0290 0291 void NightColorDBusInterface::uninhibit(uint cookie) 0292 { 0293 const QString serviceName = QDBusContext::message().service(); 0294 0295 uninhibit(serviceName, cookie); 0296 } 0297 0298 void NightColorDBusInterface::uninhibit(const QString &serviceName, uint cookie) 0299 { 0300 const int removedCount = m_inhibitors.remove(serviceName, cookie); 0301 if (!removedCount) { 0302 return; 0303 } 0304 0305 if (!m_inhibitors.contains(serviceName)) { 0306 m_inhibitorWatcher->removeWatchedService(serviceName); 0307 } 0308 0309 m_manager->uninhibit(); 0310 } 0311 0312 void NightColorDBusInterface::removeInhibitorService(const QString &serviceName) 0313 { 0314 const auto cookies = m_inhibitors.values(serviceName); 0315 for (const uint &cookie : cookies) { 0316 uninhibit(serviceName, cookie); 0317 } 0318 } 0319 0320 void NightColorDBusInterface::preview(uint previewTemp) 0321 { 0322 m_manager->preview(previewTemp); 0323 } 0324 0325 void NightColorDBusInterface::stopPreview() 0326 { 0327 m_manager->stopPreview(); 0328 } 0329 0330 } 0331 0332 #include "moc_nightcolordbusinterface.cpp"