ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
linux/keepawake.c
Go to the documentation of this file.
1
11#include <unistd.h>
12
13#ifdef HAVE_LIBSYSTEMD
14#include <systemd/sd-bus.h>
15static int g_inhibit_fd = -1;
16
17// Weak symbols for runtime systemd detection
18extern int sd_bus_default_system(sd_bus **ret) __attribute__((weak));
19#endif
20
22#ifdef HAVE_LIBSYSTEMD
23 // Linux: Use systemd-inhibit if available
24 if (g_inhibit_fd >= 0) {
25 log_debug("Keepawake already enabled");
26 return ASCIICHAT_OK;
27 }
28
29 // Check if systemd is available at runtime (weak symbol)
30 if (sd_bus_default_system == NULL) {
31 log_dev("systemd not available, keepawake not supported");
32 return ASCIICHAT_OK; // Not an error, just unsupported
33 }
34
35 sd_bus *bus = NULL;
36 sd_bus_message *reply = NULL;
37 sd_bus_error error = SD_BUS_ERROR_NULL;
38
39 if (sd_bus_default_system(&bus) < 0) {
40 return SET_ERRNO(ERROR_PLATFORM_INIT, "Failed to connect to system bus");
41 }
42
43 if (!bus) {
44 return SET_ERRNO(ERROR_PLATFORM_INIT, "System bus is NULL");
45 }
46
47 int r = sd_bus_call_method(bus, "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager",
48 "Inhibit", &error, &reply, "ssss",
49 "sleep:idle", // What to inhibit
50 "ascii-chat", // Who
51 "Video/audio streaming", // Why
52 "block" // Mode
53 );
54
55 if (r < 0) {
56 sd_bus_error_free(&error);
57 sd_bus_unref(bus);
58 return SET_ERRNO(ERROR_PLATFORM_INIT, "Failed to inhibit sleep via systemd");
59 }
60
61 if (!reply) {
62 sd_bus_error_free(&error);
63 sd_bus_unref(bus);
64 return SET_ERRNO(ERROR_PLATFORM_INIT, "systemd inhibit reply is NULL");
65 }
66
67 if (sd_bus_message_read(reply, "h", &g_inhibit_fd) < 0) {
68 sd_bus_message_unref(reply);
69 sd_bus_error_free(&error);
70 sd_bus_unref(bus);
71 return SET_ERRNO(ERROR_PLATFORM_INIT, "Failed to read inhibit fd from systemd reply");
72 }
73
74 sd_bus_message_unref(reply);
75 sd_bus_error_free(&error);
76 sd_bus_unref(bus);
77
78 // Register the inhibit fd with the named registry for debug logging
79 NAMED_REGISTER_FD(g_inhibit_fd, "inhibit");
80
81 log_debug("Keepawake enabled via systemd-inhibit fd=%d", g_inhibit_fd);
82 return ASCIICHAT_OK;
83
84#else
85 // Other POSIX systems: not implemented
86 log_debug("Keepawake not implemented on this platform");
87 return ASCIICHAT_OK;
88#endif
89}
90
92#ifdef HAVE_LIBSYSTEMD
93 if (g_inhibit_fd >= 0) {
94 close(g_inhibit_fd);
95 log_debug("Keepawake disabled, closed inhibit fd=%d", g_inhibit_fd);
96 // Unregister from named registry after logging
97 NAMED_UNREGISTER_FD(g_inhibit_fd);
98 g_inhibit_fd = -1;
99 }
100#endif
101}
⚠️‼️ Comprehensive thread-local error context system for ascii-chat
Named object registry for debugging — log identifiable resource names.
#define NAMED_REGISTER_FD(fd, name)
Register an existing file descriptor.
#define NAMED_UNREGISTER_FD(fd)
Unregister a file descriptor.
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
@ ERROR_PLATFORM_INIT
Definition error_codes.h:60
@ ASCIICHAT_OK
Definition error_codes.h:51
#define log_dev(...)
Log a DEV message (most verbose, development only)
Definition log/log.h:534
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:548
void platform_disable_keepawake(void)
Disable system sleep prevention (allow OS to sleep)
asciichat_error_t platform_enable_keepawake(void)
Enable system sleep prevention (keepawake mode)
📝 Logging API with multiple log levels and terminal output control
Network quality metrics for a single participant.
Definition metrics.h:33
Cross-platform system functions interface for ascii-chat.