idle_detect  0.8.3.0
Provides overall idle detection for a linux computer
idle_detect.h
1 /*
2  * Copyright (C) 2025 James C. Owens
3  *
4  * This code is licensed under the MIT license. See LICENSE.md in the repository.
5  */
6 
7 #ifndef IDLE_DETECT_H
8 #define IDLE_DETECT_H
9 
10 #include <condition_variable>
11 #include <cstdint> // For int64_t
12 #include <thread>
13 #include <util.h>
14 
15 // Forward declare Wayland types
16 struct wl_display;
17 struct wl_registry;
18 struct wl_seat;
19 struct ext_idle_notifier_v1;
20 struct ext_idle_notification_v1;
21 
22 namespace IdleDetect {
23 // Function to get the user's idle time in seconds
24 int64_t GetIdleTimeSeconds();
25 
32 {
33 public:
34  enum State {
35  UNKNOWN,
36  NORMAL,
37  FORCED_ACTIVE,
38  FORCED_IDLE
39  };
40 
45 
49  std::condition_variable cv_idle_detect_control_monitor_thread;
50 
55 
58 
63 
69  bool IsInitialized() const;
70 
75  State GetState() const;
76 
82  static std::string StateToString(const State& state);
83 
88  std::string StateToString() const;
89 
90 private:
95  mutable std::mutex mtx_idle_detect_control_monitor;
96 
101 
108  std::atomic<State> m_state;
109 
114  std::atomic<bool> m_initialized;
115 };
116 
123 {
124 public:
127 
130 
132  WaylandIdleMonitor(const WaylandIdleMonitor&) = delete;
133  WaylandIdleMonitor& operator=(const WaylandIdleMonitor&) = delete;
135  WaylandIdleMonitor& operator=(WaylandIdleMonitor&&) = delete;
136 
138  bool Start(int notification_timeout_ms);
139 
141  void Stop();
142 
144  bool IsAvailable() const;
145 
147  int64_t GetIdleSeconds() const;
148 
150  bool IsIdle() const;
151 
152 public: // Accessible to static C callbacks
153  wl_seat* m_seat;
154  ext_idle_notifier_v1* m_idle_notifier;
155  uint32_t m_seat_id;
156  uint32_t m_idle_notifier_id;
157  std::atomic<bool> m_is_idle;
158  std::atomic<int64_t> m_idle_start_time;
159 
160 private:
162  std::thread m_monitor_thread;
163 
165  std::atomic<bool> m_interrupt_monitor;
166 
169 
171  std::atomic<bool> m_initialized;
172 
174  wl_display* m_display;
175  wl_registry* m_registry;
176 
178  ext_idle_notification_v1* m_idle_notification;
179 
182 
184  bool InitializeWayland();
185 
187  void CleanupWayland();
188 
190  void CreateIdleNotification();
191 
193  void WaylandMonitorThread();
194 
196  static void HandleGlobal(void *data, wl_registry *registry, uint32_t name, const char *interface, uint32_t version);
197 
199  static void HandleGlobalRemove(void *data, wl_registry *registry, uint32_t name);
200 
202  static void HandleIdled(void *data, ext_idle_notification_v1 *notification);
203 
205  static void HandleResumed(void *data, ext_idle_notification_v1 *notification);
206 
208  static const void* c_registry_listener_ptr;
209 
212 };
213 
214 } // namespace IdleDetect
215 
220 class IdleDetectConfig : public Config
221 {
225  void ProcessArgs() override;
226 };
227 
228 #endif // IDLE_DETECT_H
static void HandleIdled(void *data, ext_idle_notification_v1 *notification)
Private method to handle idle notification events.
void IdleDetectControlMonitorThread()
Method to instantiate the tty monitor thread.
Definition: idle_detect.cpp:743
The IdleDetectConfig class. This specializes the Config class and implements the virtual method Proce...
Definition: idle_detect.h:220
bool Start(int notification_timeout_ms)
Initializes the Wayland display and registry, and starts the idle notifier.
Definition: idle_detect.cpp:1024
The WaylandIdleMonitor class implements the ext_idle_notifier_v1 protocol to monitor idle state in Wa...
Definition: idle_detect.h:122
int m_notification_timeout_ms
Timeout for idle notification in milliseconds.
Definition: idle_detect.h:181
std::atomic< bool > m_initialized
This holds the flag as to whether the tty monitor has been initialized and is provided by the IsIniti...
Definition: idle_detect.h:114
void ProcessArgs() override
The is the ProcessArgs() implementation for idle_detect.
Definition: idle_detect.cpp:75
std::atomic< bool > m_interrupt_monitor
Mutex for thread synchronization.
Definition: idle_detect.h:165
void WaylandMonitorThread()
Private method to run the Wayland event loop in a separate thread.
Definition: idle_detect.cpp:1285
static void HandleGlobal(void *data, wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
Private method to handle global events.
void Stop()
Stops the Wayland idle monitor and cleans up resources.
Definition: idle_detect.cpp:1092
std::mutex mtx_idle_detect_control_monitor_thread
This provides lock control for the tty monitor worker thread itself.
Definition: idle_detect.h:100
WaylandIdleMonitor()
Constructor.
Definition: idle_detect.cpp:1000
int m_interrupt_pipe_fd[2]
Pipe for thread interrupt handling.
Definition: idle_detect.h:168
The Config class is a singleton that stores program config read from the config file, with applied defaults if the config file cannot be read, or a config parameter is not in the config file.
Definition: util.h:233
ext_idle_notification_v1 * m_idle_notification
Idle notification object.
Definition: idle_detect.h:178
The IdleDetectControlMonitor class is a singleton that monitors the idle_detect control pipe...
Definition: idle_detect.h:31
~WaylandIdleMonitor()
Destructor.
Definition: idle_detect.cpp:1019
int64_t GetIdleSeconds() const
Provides the number of seconds the session has been idle via the ext_idle_notifier_v1 protocol...
Definition: idle_detect.cpp:1386
IdleDetectControlMonitor()
Constructor.
Definition: idle_detect.cpp:738
void CreateIdleNotification()
Private method to create the idle notification object.
Definition: idle_detect.cpp:1259
Definition: idle_detect.cpp:182
std::thread m_idle_detect_control_monitor_thread
Holds the actual idle_detect monitor thread.
Definition: idle_detect.h:44
std::atomic< bool > m_interrupt_idle_detect_control_monitor
Atomic boolean that interrupts the idle_detect monitor thread.
Definition: idle_detect.h:54
bool IsInitialized() const
Provides a flag to indicate whether the monitor has been initialized. This is used in main in the app...
Definition: idle_detect.cpp:951
bool IsIdle() const
Checks if the Wayland session is idle.
Definition: idle_detect.cpp:1381
std::atomic< bool > m_initialized
Flag to indicate whether the monitor has been initialized.
Definition: idle_detect.h:171
State GetState() const
Returns the state of the idle control monitor. This is NORMAL, FORCED_ACTIVE or FORCED_IDLE.
Definition: idle_detect.cpp:957
std::condition_variable cv_idle_detect_control_monitor_thread
Condition variable for control/synchronization of the idle_detect monitor threads.
Definition: idle_detect.h:49
static void HandleResumed(void *data, ext_idle_notification_v1 *notification)
Private method to handle resumed events.
std::mutex mtx_idle_detect_control_monitor
This is the mutex member that provides lock control for the tty monitor object. This is used to ensur...
Definition: idle_detect.h:95
std::thread m_monitor_thread
The WaylandIdleMonitor thread that monitors the Wayland session for idle state changes.
Definition: idle_detect.h:162
bool InitializeWayland()
Private method to initialize Wayland and set up the idle notification.
Definition: idle_detect.cpp:1142
static void HandleGlobalRemove(void *data, wl_registry *registry, uint32_t name)
Private method to handle global removal events.
bool IsAvailable() const
Checks if the Wayland idle monitor is available.
Definition: idle_detect.cpp:1376
std::atomic< State > m_state
Holds the current state of the idle monitor. NORMAL means idle detect follows the normal threshold (t...
Definition: idle_detect.h:108
static const void * c_registry_listener_ptr
Pointer to the registry listener struct for C linkage.
Definition: idle_detect.h:208
std::string StateToString() const
Returns the string representation of the idle monitor object state.
Definition: idle_detect.cpp:984
void CleanupWayland()
Private method to clean up Wayland resources.
Definition: idle_detect.cpp:1217
static const void * c_idle_notification_listener_ptr
Pointer to the idle notification listener struct for C linkage.
Definition: idle_detect.h:211
wl_display * m_display
Wayland display and registry objects.
Definition: idle_detect.h:174