From 6639ae1a4081d35198f6cd17162bb30ac2da534d Mon Sep 17 00:00:00 2001 From: Ognjen Galic Date: Tue, 13 Mar 2018 03:08:29 +0100 Subject: [PATCH] display_device: support the "Not Charging" state On Lenovo ThinkPads with the linux-next kernel, due to the new kernel interface for the charging thresholds one battery could appear as "pending-charge" in UPower thus breaking the DisplayDevice that GNOME/MATE uses in the tray icon, displaying an unknown battery. This patch adds a set of rules that the display device will follow: 1) If there is a SINGLE battery that is pending a charge, we will display it as if the battery is full. That is because the state of "pending-charge" is too complicated for the UI and could break it on other desktops (such as Xfce). 2) If there is more than one battery but one is pending a charge, we will return "Charging" because the other battery is charging. None of the above will occur if there is no batteries or the state is not unknown. Signed-off-by: Ognjen Galic --- src/up-daemon.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/up-daemon.c b/src/up-daemon.c index 1a5dddc..e82604e 100644 --- a/src/up-daemon.c +++ b/src/up-daemon.c @@ -160,6 +160,7 @@ up_daemon_update_display_battery (UpDaemon *daemon) gint64 time_to_empty_total = 0; gint64 time_to_full_total = 0; gboolean is_present_total = FALSE; + guint num_pending = 0; guint num_batteries = 0; /* Gather state from each device */ @@ -221,6 +222,8 @@ up_daemon_update_display_battery (UpDaemon *daemon) else if (state == UP_DEVICE_STATE_FULLY_CHARGED && state_total == UP_DEVICE_STATE_UNKNOWN) state_total = UP_DEVICE_STATE_FULLY_CHARGED; + else if (state == UP_DEVICE_STATE_PENDING_CHARGE) + num_pending++; /* sum up composite */ kind_total = UP_DEVICE_KIND_BATTERY; @@ -235,6 +238,22 @@ up_daemon_update_display_battery (UpDaemon *daemon) num_batteries++; } + /* The display battery has an unknown state (Usually ThinkPads on Linux 4.17 with + * the battery charge threshold states set). */ + if (num_batteries > 0 && state_total == UP_DEVICE_STATE_UNKNOWN) { + /* If all batteries are pending a charge, return the total state + * as "fully charging". We return that all are fully charged because + * if a battery is pending a charge, it has been fully charged up to + * its threshold and is in idle. We could return "pending charge" here + * but that is too complicated for the display battery */ + if (num_pending == num_batteries) + state_total = UP_DEVICE_STATE_FULLY_CHARGED; + /* Else if there is one battery that is pending but one battery is not, + * we will return "charging" as the other is charging. */ + else if (num_pending > 0) + state_total = UP_DEVICE_STATE_CHARGING; + } + /* Handle multiple batteries */ if (num_batteries <= 1) goto out; -- 2.14.1