kwin/plugins/nightcolor/nightcolordbusinterface.cpp
Vlad Zahorodnii bdfb946267 Convert Night Color into a plugin
Night Color adjusts the color temperature based on the current time in
your location. It's not a generic color correction module per se.

We need a central component that can be used by both night color and
colord integration to tweak gamma ramps and which will be able to
resolve conflicts between the two. The Night Color manager cannot be
such a thing because of its very specific usecase.

This change converts Night Color into a plugin to prepare some space for
such a component.

The tricky part is that the dbus api of Night Color has "ColorCorrect"
in its name. I'm afraid we cannot do that much about it without breaking
API compatibility.
2020-11-27 18:55:01 +00:00

309 lines
9.5 KiB
C++

/*
KWin - the KDE window manager
This file is part of the KDE project.
SPDX-FileCopyrightText: 2017 Roman Gilg <subdiff@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "nightcolordbusinterface.h"
#include "nightcolormanager.h"
#include "colorcorrectadaptor.h"
#include <QDBusMessage>
namespace KWin {
NightColorDBusInterface::NightColorDBusInterface(NightColorManager *parent)
: QObject(parent)
, m_manager(parent)
, m_inhibitorWatcher(new QDBusServiceWatcher(this))
{
m_inhibitorWatcher->setConnection(QDBusConnection::sessionBus());
m_inhibitorWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
connect(m_inhibitorWatcher, &QDBusServiceWatcher::serviceUnregistered,
this, &NightColorDBusInterface::removeInhibitorService);
connect(m_manager, &NightColorManager::inhibitedChanged, this, [this] {
QVariantMap changedProperties;
changedProperties.insert(QStringLiteral("inhibited"), m_manager->isInhibited());
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, &NightColorManager::enabledChanged, this, [this] {
QVariantMap changedProperties;
changedProperties.insert(QStringLiteral("enabled"), m_manager->isEnabled());
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, &NightColorManager::runningChanged, this, [this] {
QVariantMap changedProperties;
changedProperties.insert(QStringLiteral("running"), m_manager->isRunning());
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, &NightColorManager::currentTemperatureChanged, this, [this] {
QVariantMap changedProperties;
changedProperties.insert(QStringLiteral("currentTemperature"), m_manager->currentTemperature());
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, &NightColorManager::targetTemperatureChanged, this, [this] {
QVariantMap changedProperties;
changedProperties.insert(QStringLiteral("targetTemperature"), m_manager->targetTemperature());
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, &NightColorManager::modeChanged, this, [this] {
QVariantMap changedProperties;
changedProperties.insert(QStringLiteral("mode"), uint(m_manager->mode()));
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, &NightColorManager::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, &NightColorManager::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, &NightColorManager::configChange, this, &NightColorDBusInterface::nightColorConfigChanged);
new ColorCorrectAdaptor(this);
QDBusConnection::sessionBus().registerObject(QStringLiteral("/ColorCorrect"), this);
}
bool NightColorDBusInterface::isInhibited() const
{
return m_manager->isInhibited();
}
bool NightColorDBusInterface::isEnabled() const
{
return m_manager->isEnabled();
}
bool NightColorDBusInterface::isRunning() const
{
return m_manager->isRunning();
}
bool NightColorDBusInterface::isAvailable() const
{
return m_manager->isAvailable();
}
int NightColorDBusInterface::currentTemperature() const
{
return m_manager->currentTemperature();
}
int NightColorDBusInterface::targetTemperature() const
{
return m_manager->targetTemperature();
}
int NightColorDBusInterface::mode() const
{
return m_manager->mode();
}
quint64 NightColorDBusInterface::previousTransitionDateTime() const
{
const QDateTime dateTime = m_manager->previousTransitionDateTime();
if (dateTime.isValid()) {
return quint64(dateTime.toSecsSinceEpoch());
}
return 0;
}
quint32 NightColorDBusInterface::previousTransitionDuration() const
{
return quint32(m_manager->previousTransitionDuration());
}
quint64 NightColorDBusInterface::scheduledTransitionDateTime() const
{
const QDateTime dateTime = m_manager->scheduledTransitionDateTime();
if (dateTime.isValid()) {
return quint64(dateTime.toSecsSinceEpoch());
}
return 0;
}
quint32 NightColorDBusInterface::scheduledTransitionDuration() const
{
return quint32(m_manager->scheduledTransitionDuration());
}
QHash<QString, QVariant> NightColorDBusInterface::nightColorInfo()
{
return m_manager->info();
}
bool NightColorDBusInterface::setNightColorConfig(QHash<QString, QVariant> data)
{
return m_manager->changeConfiguration(data);
}
void NightColorDBusInterface::nightColorAutoLocationUpdate(double latitude, double longitude)
{
m_manager->autoLocationUpdate(latitude, longitude);
}
uint NightColorDBusInterface::inhibit()
{
const QString serviceName = QDBusContext::message().service();
if (!m_inhibitors.contains(serviceName)) {
m_inhibitorWatcher->addWatchedService(serviceName);
}
m_inhibitors.insert(serviceName, ++m_lastInhibitionCookie);
m_manager->inhibit();
return m_lastInhibitionCookie;
}
void NightColorDBusInterface::uninhibit(uint cookie)
{
const QString serviceName = QDBusContext::message().service();
uninhibit(serviceName, cookie);
}
void NightColorDBusInterface::uninhibit(const QString &serviceName, uint cookie)
{
const int removedCount = m_inhibitors.remove(serviceName, cookie);
if (!removedCount) {
return;
}
if (!m_inhibitors.contains(serviceName)) {
m_inhibitorWatcher->removeWatchedService(serviceName);
}
m_manager->uninhibit();
}
void NightColorDBusInterface::removeInhibitorService(const QString &serviceName)
{
const auto cookies = m_inhibitors.values(serviceName);
for (const uint &cookie : cookies) {
uninhibit(serviceName, cookie);
}
}
}