ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
linux/keepawake.c
Go to the documentation of this file.
1
7#include <ascii-chat/platform/system.h>
8#include <ascii-chat/log/logging.h>
9#include <ascii-chat/asciichat_errno.h>
10#include <unistd.h>
11
12#ifdef HAVE_LIBSYSTEMD
13#include <systemd/sd-bus.h>
14static int g_inhibit_fd = -1;
15
16// Weak symbols for runtime systemd detection
17extern int sd_bus_default_system(sd_bus **ret) __attribute__((weak));
18#endif
19
20asciichat_error_t platform_enable_keepawake(void) {
21#ifdef HAVE_LIBSYSTEMD
22 // Linux: Use systemd-inhibit if available
23 if (g_inhibit_fd >= 0) {
24 log_debug("Keepawake already enabled");
25 return ASCIICHAT_OK;
26 }
27
28 // Check if systemd is available at runtime (weak symbol)
29 if (sd_bus_default_system == NULL) {
30 log_dev("systemd not available, keepawake not supported");
31 return ASCIICHAT_OK; // Not an error, just unsupported
32 }
33
34 sd_bus *bus = NULL;
35 sd_bus_message *reply = NULL;
36 sd_bus_error error = SD_BUS_ERROR_NULL;
37
38 if (sd_bus_default_system(&bus) < 0) {
39 return SET_ERRNO(ERROR_PLATFORM_INIT, "Failed to connect to system bus");
40 }
41
42 if (!bus) {
43 return SET_ERRNO(ERROR_PLATFORM_INIT, "System bus is NULL");
44 }
45
46 int r = sd_bus_call_method(bus, "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager",
47 "Inhibit", &error, &reply, "ssss",
48 "sleep:idle", // What to inhibit
49 "ascii-chat", // Who
50 "Video/audio streaming", // Why
51 "block" // Mode
52 );
53
54 if (r < 0) {
55 sd_bus_error_free(&error);
56 sd_bus_unref(bus);
57 return SET_ERRNO(ERROR_PLATFORM_INIT, "Failed to inhibit sleep via systemd");
58 }
59
60 if (!reply) {
61 sd_bus_error_free(&error);
62 sd_bus_unref(bus);
63 return SET_ERRNO(ERROR_PLATFORM_INIT, "systemd inhibit reply is NULL");
64 }
65
66 if (sd_bus_message_read(reply, "h", &g_inhibit_fd) < 0) {
67 sd_bus_message_unref(reply);
68 sd_bus_error_free(&error);
69 sd_bus_unref(bus);
70 return SET_ERRNO(ERROR_PLATFORM_INIT, "Failed to read inhibit fd from systemd reply");
71 }
72
73 sd_bus_message_unref(reply);
74 sd_bus_error_free(&error);
75 sd_bus_unref(bus);
76
77 log_debug("Keepawake enabled via systemd-inhibit (fd: %d)", g_inhibit_fd);
78 return ASCIICHAT_OK;
79
80#else
81 // Other POSIX systems: not implemented
82 log_debug("Keepawake not implemented on this platform");
83 return ASCIICHAT_OK;
84#endif
85}
86
88#ifdef HAVE_LIBSYSTEMD
89 if (g_inhibit_fd >= 0) {
90 close(g_inhibit_fd);
91 log_debug("Keepawake disabled (closed inhibit fd: %d)", g_inhibit_fd);
92 g_inhibit_fd = -1;
93 }
94#endif
95}
__attribute__((constructor))
Register fork handlers for common module.
Definition common.c:104
void platform_disable_keepawake(void)
asciichat_error_t platform_enable_keepawake(void)