plugins/nightcolor: use FileDescriptor class

This commit is contained in:
Xaver Hugl 2022-07-27 13:24:53 +02:00
parent 9c4f3d447f
commit ca7a2cdef6
2 changed files with 11 additions and 19 deletions

View file

@ -22,40 +22,33 @@ namespace KWin
LinuxClockSkewNotifierEngine *LinuxClockSkewNotifierEngine::create(QObject *parent)
{
const int fd = timerfd_create(CLOCK_REALTIME, O_CLOEXEC | O_NONBLOCK);
if (fd == -1) {
FileDescriptor fd{timerfd_create(CLOCK_REALTIME, O_CLOEXEC | O_NONBLOCK)};
if (!fd.isValid()) {
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);
const int ret = timerfd_settime(fd.get(), 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);
return new LinuxClockSkewNotifierEngine(std::move(fd), parent);
}
LinuxClockSkewNotifierEngine::LinuxClockSkewNotifierEngine(int fd, QObject *parent)
LinuxClockSkewNotifierEngine::LinuxClockSkewNotifierEngine(FileDescriptor &&fd, QObject *parent)
: ClockSkewNotifierEngine(parent)
, m_fd(fd)
, m_fd(std::move(fd))
{
const QSocketNotifier *notifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
const QSocketNotifier *notifier = new QSocketNotifier(m_fd.get(), 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));
read(m_fd.get(), &expirationCount, sizeof(expirationCount));
Q_EMIT clockSkewed();
}

View file

@ -7,6 +7,7 @@
#pragma once
#include "clockskewnotifierengine_p.h"
#include "utils/filedescriptor.h"
namespace KWin
{
@ -16,17 +17,15 @@ class LinuxClockSkewNotifierEngine : public ClockSkewNotifierEngine
Q_OBJECT
public:
~LinuxClockSkewNotifierEngine() override;
static LinuxClockSkewNotifierEngine *create(QObject *parent);
private Q_SLOTS:
void handleTimerCancelled();
private:
LinuxClockSkewNotifierEngine(int fd, QObject *parent);
LinuxClockSkewNotifierEngine(FileDescriptor &&fd, QObject *parent);
int m_fd;
FileDescriptor m_fd;
};
} // namespace KWin