From 4e4497042c65a0127d83b3d9b324cf61a67d70cd Mon Sep 17 00:00:00 2001 From: Kernc Date: Sun, 17 Feb 2019 17:29:10 +0100 Subject: [PATCH] ENH: Add AC power detection for *BSD systems --- src/events.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/events.c b/src/events.c index afdc183..0fef0a8 100644 --- a/src/events.c +++ b/src/events.c @@ -1,8 +1,9 @@ #include "events.h" #include - #include + +#include #include #include "entry.h" @@ -234,6 +235,20 @@ is_on_ac_power () return TRUE; } return FALSE; + +#elif defined(__unix__) && defined(BSD) && !defined(__APPLE__) + // On *BSD, run `apm -a` which returns '1' when AC is online + g_autoptr (GError) err = NULL; + g_autofree char *standard_output = NULL; + char *argv[] = {"apm", "-a", NULL}; + + g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL, + NULL, NULL, &standard_output, NULL, NULL, &err); + if (err) + g_warning ("Unexpected `apm -a` execution error: %s", err->message); + + return standard_output && 0 == g_strcmp0 (g_strstrip (standard_output), "1"); + #else #warning "No battery / AC status support for your platform." #warning "Defaulting to as if 'always on battery' behavior. Patches welcome!"