linux: up-backend: Auto CriticalPowerAction

Systemd can be used to determine the better critical actions since systemd
knows which power actions are supported by the system. A "Auto" critical
action was introduced and that invoked the Sleep() systemd-logind API to
set the system power status when the battery level reaches PercentageAction
or TimeAction.

Signed-off-by: Kate Hsuan <hpa@redhat.com>
This commit is contained in:
Kate Hsuan 2026-03-03 13:57:45 +08:00
commit f4764ee174
2 changed files with 23 additions and 3 deletions

View file

@ -104,10 +104,11 @@ ExpectBatteryRecalibration=false
# PowerOff
# Hibernate
# HybridSleep
# Auto (The CriticalPowerAction will be determined by the system)
# Suspend (AllowRiskyCriticalPowerAction should be true to use this option but risky)
# Ignore (AllowRiskyCriticalPowerAction should be true to use this option but risky)
#
# If Suspend isn't available or AllowRiskyCriticalPowerAction=false, HybridSleep will be used
# If HybridSleep isn't available, Hibernate will be used
# If Hibernate isn't available, PowerOff will be used
CriticalPowerAction=HybridSleep
CriticalPowerAction=Auto

View file

@ -571,6 +571,7 @@ up_backend_get_critical_action (UpBackend *backend)
{ "Hibernate", "CanHibernate" },
{ "PowerOff", NULL },
{ "Ignore", NULL },
{ "Sleep", "CanSleep"},
};
g_autofree gchar *action = NULL;
gboolean can_risky = FALSE;
@ -592,6 +593,12 @@ up_backend_get_critical_action (UpBackend *backend)
}
}
/* if "Auto", use "Sleep()" */
if (!g_strcmp0 (action, "Auto")) {
g_free (action);
action = g_strdup_printf ("Sleep");
}
if (action != NULL) {
for (i = 0; i < G_N_ELEMENTS (actions); i++)
if (g_str_equal (actions[i].method, action))
@ -649,7 +656,10 @@ up_backend_take_action (UpBackend *backend)
return;
}
action = g_strdup_printf ("%sWithFlags", method);
if (!g_strcmp0 (method, "Sleep"))
action = g_strdup ("Sleep");
else
action = g_strdup_printf ("%sWithFlags", method);
/* flag 16 is SD_LOGIND_SKIP_INHIBITORS */
res = g_dbus_proxy_call_sync (backend->priv->logind_proxy,
@ -662,9 +672,18 @@ up_backend_take_action (UpBackend *backend)
/* if the new API doesn't work, use old one */
if (error != NULL) {
g_autofree gchar *action_old = NULL;
g_debug ("The new power action API doesn't work, using old one.");
if (!g_strcmp0 (method, "Sleep")) {
/* Sleep() is not available, so PowerOff instead */
g_debug ("Sleep() is not available, using PowerOff instead");
action_old = g_strdup ("PowerOff");
} else {
action_old = g_strdup (method);
}
g_dbus_proxy_call (backend->priv->logind_proxy,
method,
action_old,
g_variant_new ("(b)", FALSE),
G_DBUS_CALL_FLAGS_NONE,
-1,