ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
keepawake.c File Reference

Linux system sleep prevention using systemd-inhibit. More...

Go to the source code of this file.

Functions

asciichat_error_t platform_enable_keepawake (void)
 
void platform_disable_keepawake (void)
 

Detailed Description

Linux system sleep prevention using systemd-inhibit.

Definition in file linux/keepawake.c.

Function Documentation

◆ platform_disable_keepawake()

void platform_disable_keepawake ( void  )

Definition at line 87 of file linux/keepawake.c.

87 {
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}

Referenced by acds_main(), server_main(), and session_client_like_run().

◆ platform_enable_keepawake()

asciichat_error_t platform_enable_keepawake ( void  )

Definition at line 20 of file linux/keepawake.c.

20 {
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}

Referenced by acds_main(), server_main(), and session_client_like_run().