File indexing completed on 2024-05-12 05:14:49

0001 /*
0002  *  kernelwakealarm.h  -  kernel alarm to wake from suspend
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2023 one-d-wide <one-d-wide@protonmail.com>
0005  *  SPDX-FileCopyrightText: 2023 David Jarvie <djarvie@kde.org>
0006  *
0007  *  SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #pragma once
0011 
0012 #include <QtSystemDetection>
0013 
0014 #include <ctime>
0015 #include <optional>
0016 
0017 namespace KAlarmCal
0018 {
0019 class KADateTime;
0020 }
0021 
0022 /*==============================================================================
0023 = Manages single kernel alarm instance that wakes system from suspend on expiry.
0024 =
0025 = Supported on:
0026 =    * Linux (if `CAP_WAKE_ALARM` is set, see `capabilities(7)`)
0027 =
0028 = Destroying the instance will disarm the alarm
0029 ==============================================================================*/
0030 class KernelWakeAlarm
0031 {
0032 public:
0033     KernelWakeAlarm();
0034     KernelWakeAlarm(const KernelWakeAlarm&);
0035     ~KernelWakeAlarm();
0036 
0037     KernelWakeAlarm& operator=(const KernelWakeAlarm&);
0038 
0039     /** Return whether this instance was constructed successfully and can be used. */
0040     bool isValid() const;
0041 
0042     /** Set the kernel alarm timer.
0043      *  @return true if successful;
0044      *          false if invalid instance, @p triggerTime has already expired,
0045      *                or error calling timerfd_settime().
0046      */
0047     bool arm(const KAlarmCal::KADateTime& triggerTime);
0048 
0049     /** Cancel the kernel alarm timer if already set. */
0050     void disarm();
0051 
0052     /** Determine whether kernel alarms can be set. */
0053     static bool isAvailable();
0054 
0055 private:
0056 #ifdef Q_OS_LINUX
0057     bool arm(time_t triggerSeconds);
0058 
0059     time_t             mTriggerTime;
0060     std::optional<int> mTimerFd;
0061     static int         mAvailable;
0062 #endif
0063 };
0064 
0065 // vim: et sw=4: