Changeset View
Changeset View
Standalone View
Standalone View
colorcorrection/clockskewnotifierengine_linux.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 Vlad Zahorodnii <vladzzag@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License along | ||||
15 | * with this program; if not, write to the Free Software Foundation, Inc., | ||||
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "clockskewnotifierengine_linux.h" | ||||
20 | | ||||
21 | #include <QSocketNotifier> | ||||
22 | | ||||
23 | #include <fcntl.h> | ||||
24 | #include <sys/timerfd.h> | ||||
25 | #include <unistd.h> | ||||
26 | | ||||
27 | namespace KWin | ||||
28 | { | ||||
29 | | ||||
30 | LinuxClockSkewNotifierEngine *LinuxClockSkewNotifierEngine::create(QObject *parent) | ||||
31 | { | ||||
32 | const int fd = timerfd_create(CLOCK_REALTIME, O_CLOEXEC | O_NONBLOCK); | ||||
33 | if (fd == -1) { | ||||
34 | qWarning("Couldn't create clock skew notifier engine: %s", strerror(errno)); | ||||
35 | return nullptr; | ||||
36 | } | ||||
37 | | ||||
38 | const itimerspec spec = {}; | ||||
39 | const int ret = timerfd_settime(fd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &spec, nullptr); | ||||
40 | if (ret == -1) { | ||||
41 | qWarning("Couldn't create clock skew notifier engine: %s", strerror(errno)); | ||||
42 | close(fd); | ||||
43 | return nullptr; | ||||
44 | } | ||||
45 | | ||||
46 | return new LinuxClockSkewNotifierEngine(fd, parent); | ||||
47 | } | ||||
48 | | ||||
49 | LinuxClockSkewNotifierEngine::LinuxClockSkewNotifierEngine(int fd, QObject *parent) | ||||
50 | : ClockSkewNotifierEngine(parent) | ||||
51 | , m_fd(fd) | ||||
52 | { | ||||
53 | const QSocketNotifier *notifier = new QSocketNotifier(fd, QSocketNotifier::Read, this); | ||||
54 | connect(notifier, &QSocketNotifier::activated, this, &LinuxClockSkewNotifierEngine::handleTimerCancelled); | ||||
55 | } | ||||
56 | | ||||
57 | LinuxClockSkewNotifierEngine::~LinuxClockSkewNotifierEngine() | ||||
58 | { | ||||
59 | close(m_fd); | ||||
60 | } | ||||
61 | | ||||
62 | void LinuxClockSkewNotifierEngine::handleTimerCancelled() | ||||
63 | { | ||||
64 | uint64_t expirationCount; | ||||
65 | read(m_fd, &expirationCount, sizeof(expirationCount)); | ||||
66 | | ||||
67 | emit clockSkewed(); | ||||
68 | } | ||||
69 | | ||||
70 | } // namespace KWin |