kwin/plugins/nightcolor/clockskewnotifierengine_linux.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

63 lines
1.6 KiB
C++

/*
SPDX-FileCopyrightText: 2019 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "clockskewnotifierengine_linux.h"
#include <QSocketNotifier>
#include <fcntl.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include <cerrno>
#ifndef TFD_TIMER_CANCEL_ON_SET // only available in newer glib
#define TFD_TIMER_CANCEL_ON_SET (1 << 1)
#endif
namespace KWin
{
LinuxClockSkewNotifierEngine *LinuxClockSkewNotifierEngine::create(QObject *parent)
{
const int fd = timerfd_create(CLOCK_REALTIME, O_CLOEXEC | O_NONBLOCK);
if (fd == -1) {
qWarning("Couldn't create clock skew notifier engine: %s", strerror(errno));
return nullptr;
}
const itimerspec spec = {};
const int ret = timerfd_settime(fd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &spec, nullptr);
if (ret == -1) {
qWarning("Couldn't create clock skew notifier engine: %s", strerror(errno));
close(fd);
return nullptr;
}
return new LinuxClockSkewNotifierEngine(fd, parent);
}
LinuxClockSkewNotifierEngine::LinuxClockSkewNotifierEngine(int fd, QObject *parent)
: ClockSkewNotifierEngine(parent)
, m_fd(fd)
{
const QSocketNotifier *notifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
connect(notifier, &QSocketNotifier::activated, this, &LinuxClockSkewNotifierEngine::handleTimerCancelled);
}
LinuxClockSkewNotifierEngine::~LinuxClockSkewNotifierEngine()
{
close(m_fd);
}
void LinuxClockSkewNotifierEngine::handleTimerCancelled()
{
uint64_t expirationCount;
read(m_fd, &expirationCount, sizeof(expirationCount));
emit clockSkewed();
}
} // namespace KWin