diff options
author | Lennart Poettering <lennart@poettering.net> | 2023-06-05 12:14:12 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2023-06-05 12:21:28 +0200 |
commit | 7f1520c6a150f4eea531ef6e57d5bd37c682af21 (patch) | |
tree | dad300b3b9225f4339844ad9535a9508f2ed305a | |
parent | battery-util: be more careful when determining whether we are in a low batter... (diff) | |
download | systemd-7f1520c6a150f4eea531ef6e57d5bd37c682af21.tar.xz systemd-7f1520c6a150f4eea531ef6e57d5bd37c682af21.zip |
ac-power: add --low switch to systemd-ac-power tool
This allows checking from shell scripts whether the system is in a low
battery state. It just exposed the code we anyway have in a directly
accessible way.
This is also very useful for testing things.
-rw-r--r-- | man/systemd-ac-power.xml | 8 | ||||
-rw-r--r-- | src/ac-power/ac-power.c | 26 |
2 files changed, 30 insertions, 4 deletions
diff --git a/man/systemd-ac-power.xml b/man/systemd-ac-power.xml index a4c9378cfd..e06a88a975 100644 --- a/man/systemd-ac-power.xml +++ b/man/systemd-ac-power.xml @@ -50,6 +50,14 @@ <listitem><para>Show result as text instead of just returning success or failure.</para></listitem> </varlistentry> + <varlistentry> + <term><option>--low</option></term> + + <listitem><para>Instead of showing AC power state, show low battery state. In this case will return + zero if all batteries are currently discharging and below 5% of maximum charge. Returns non-zero + otherwise.</para></listitem> + </varlistentry> + <xi:include href="standard-options.xml" xpointer="help" /> <xi:include href="standard-options.xml" xpointer="version" /> </variablelist> diff --git a/src/ac-power/ac-power.c b/src/ac-power/ac-power.c index 62181a83df..f027c31208 100644 --- a/src/ac-power/ac-power.c +++ b/src/ac-power/ac-power.c @@ -8,12 +8,18 @@ static bool arg_verbose = false; +static enum { + ACTION_AC_POWER, + ACTION_LOW, +} arg_action = ACTION_AC_POWER; + static void help(void) { printf("%s\n\n" "Report whether we are connected to an external power source.\n\n" " -h --help Show this help\n" " --version Show package version\n" - " -v --verbose Show state as text\n", + " -v --verbose Show state as text\n" + " --low Checks if battery is discharing and low\n", program_invocation_short_name); } @@ -21,12 +27,14 @@ static int parse_argv(int argc, char *argv[]) { enum { ARG_VERSION = 0x100, + ARG_LOW, }; static const struct option options[] = { { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, ARG_VERSION }, { "verbose", no_argument, NULL, 'v' }, + { "low", no_argument, NULL, ARG_LOW }, {} }; @@ -50,6 +58,10 @@ static int parse_argv(int argc, char *argv[]) { arg_verbose = true; break; + case ARG_LOW: + arg_action = ACTION_LOW; + break; + case '?': return -EINVAL; @@ -78,9 +90,15 @@ static int run(int argc, char *argv[]) { if (r <= 0) return r; - r = on_ac_power(); - if (r < 0) - return log_error_errno(r, "Failed to read AC status: %m"); + if (arg_action == ACTION_AC_POWER) { + r = on_ac_power(); + if (r < 0) + return log_error_errno(r, "Failed to read AC status: %m"); + } else { + r = battery_is_discharging_and_low(); + if (r < 0) + return log_error_errno(r, "Failed to read battery discharging + low status: %m"); + } if (arg_verbose) puts(yes_no(r)); |