[gnome-settings-daemon/wip/hadess/power-saver-on-low-battery: 5/5] power: Enable power-saver profile when low on battery




commit 79899d303794f232090c14a0a67fce5d55aecf77
Author: Bastien Nocera <hadess hadess net>
Date:   Fri Jul 16 13:40:10 2021 +0200

    power: Enable power-saver profile when low on battery
    
    When low on battery, and if the feature is enabled, hold the power
    profile to "power-saver" until the battery is sufficiently recharged.

 ...me.settings-daemon.plugins.power.gschema.xml.in |   5 +
 plugins/power/gsd-power-manager.c                  | 124 +++++++++++++++++++++
 plugins/power/test.py                              |  27 +++++
 3 files changed, 156 insertions(+)
---
diff --git a/data/org.gnome.settings-daemon.plugins.power.gschema.xml.in 
b/data/org.gnome.settings-daemon.plugins.power.gschema.xml.in
index 93c704e9..04b287bd 100644
--- a/data/org.gnome.settings-daemon.plugins.power.gschema.xml.in
+++ b/data/org.gnome.settings-daemon.plugins.power.gschema.xml.in
@@ -41,5 +41,10 @@
       <summary>Power button action</summary>
       <description>The action to take when the system power button is pressed. This action is hard-coded 
(and the setting ignored) on virtual machines (power off) and tablets (suspend).</description>
     </key>
+    <key name="power-saver-profile-on-low-battery" type="b">
+      <default>true</default>
+      <summary>Enable power-saver profile when battery is low</summary>
+      <description>Automatically enable the "power-saver" profile using power-profiles-daemon if the battery 
is low.</description>
+    </key>
   </schema>
 </schemalist>
diff --git a/plugins/power/gsd-power-manager.c b/plugins/power/gsd-power-manager.c
index 95cec9c3..1223b0e2 100644
--- a/plugins/power/gsd-power-manager.c
+++ b/plugins/power/gsd-power-manager.c
@@ -59,6 +59,10 @@
 #define UPOWER_DBUS_INTERFACE                   "org.freedesktop.UPower"
 #define UPOWER_DBUS_INTERFACE_KBDBACKLIGHT      "org.freedesktop.UPower.KbdBacklight"
 
+#define PPD_DBUS_NAME                           "net.hadess.PowerProfiles"
+#define PPD_DBUS_PATH                           "/net/hadess/PowerProfiles"
+#define PPD_DBUS_INTERFACE                      "net.hadess.PowerProfiles"
+
 #define GSD_POWER_SETTINGS_SCHEMA               "org.gnome.settings-daemon.plugins.power"
 
 #define GSD_POWER_DBUS_NAME                     GSD_DBUS_NAME ".Power"
@@ -185,6 +189,10 @@ struct _GsdPowerManager
         gdouble                  ambient_last_absolute;
         gint64                   ambient_last_time;
 
+        /* Power Profiles */
+        GDBusProxy              *power_profiles_proxy;
+        guint32                  power_saver_cookie;
+
         /* Sound */
         guint32                  critical_alert_timeout_id;
 
@@ -1927,6 +1935,63 @@ idle_configure (GsdPowerManager *manager)
         }
 }
 
+static void
+hold_profile_cb (GObject      *source_object,
+                 GAsyncResult *res,
+                 gpointer      user_data)
+{
+        GsdPowerManager *manager = GSD_POWER_MANAGER (user_data);
+        g_autoptr(GError) error = NULL;
+        g_autoptr(GVariant) result = NULL;
+
+        result = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object),
+                                           res,
+                                           &error);
+        if (result == NULL) {
+                g_warning ("Couldn't hold power-saver profile: %s", error->message);
+                return;
+        }
+
+        g_variant_get (result, "(u)", &manager->power_saver_cookie);
+        g_debug ("Holding power-saver profile with cookie %u", manager->power_saver_cookie);
+}
+
+static void
+enable_power_saver (GsdPowerManager *manager)
+{
+        if (!manager->power_profiles_proxy)
+                return;
+        if (!g_settings_get_boolean (manager->settings, "power-saver-profile-on-low-battery"))
+                return;
+
+        g_debug ("Starting hold of power-saver profile");
+
+        g_dbus_proxy_call (manager->power_profiles_proxy,
+                           "HoldProfile",
+                           g_variant_new("(sss)",
+                                         "power-saver",
+                                         "Power saver profile when low on battery",
+                                         GSD_POWER_DBUS_NAME),
+                           G_DBUS_CALL_FLAGS_NONE,
+                           -1, manager->cancellable, hold_profile_cb, manager);
+}
+
+static void
+disable_power_saver (GsdPowerManager *manager)
+{
+        if (!manager->power_profiles_proxy || manager->power_saver_cookie == 0)
+                return;
+
+        g_debug ("Releasing power-saver profile");
+
+        g_dbus_proxy_call (manager->power_profiles_proxy,
+                           "ReleaseProfile",
+                           g_variant_new ("(u)", manager->power_saver_cookie),
+                           G_DBUS_CALL_FLAGS_NONE,
+                           -1, NULL, NULL, NULL);
+        manager->power_saver_cookie = 0;
+}
+
 static void
 main_battery_or_ups_low_changed (GsdPowerManager *manager,
                                  gboolean         is_low)
@@ -1935,6 +2000,10 @@ main_battery_or_ups_low_changed (GsdPowerManager *manager,
                 return;
         manager->battery_is_low = is_low;
         idle_configure (manager);
+        if (is_low)
+                enable_power_saver (manager);
+        else
+                disable_power_saver (manager);
 }
 
 static gboolean
@@ -2078,6 +2147,39 @@ screensaver_signal_cb (GDBusProxy *proxy,
                 handle_wake_up_screen (GSD_POWER_MANAGER (user_data));
 }
 
+static void
+power_profiles_proxy_signal_cb (GDBusProxy  *proxy,
+                               const gchar *sender_name,
+                               const gchar *signal_name,
+                               GVariant    *parameters,
+                               gpointer     user_data)
+{
+        GsdPowerManager *manager = GSD_POWER_MANAGER (user_data);
+
+        if (g_strcmp0 (signal_name, "ProfileReleased") != 0)
+                return;
+        manager->power_saver_cookie = 0;
+}
+
+static void
+power_profiles_proxy_ready_cb (GObject             *source_object,
+                              GAsyncResult        *res,
+                              gpointer             user_data)
+{
+        g_autoptr(GError) error = NULL;
+        GsdPowerManager *manager = GSD_POWER_MANAGER (user_data);
+
+        manager->power_profiles_proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
+        if (manager->power_profiles_proxy == NULL) {
+                g_debug ("Could not connect to power-profiles-daemon: %s", error->message);
+                return;
+        }
+
+        g_signal_connect (manager->power_profiles_proxy, "g-signal",
+                          G_CALLBACK (power_profiles_proxy_signal_cb),
+                          manager);
+}
+
 static void
 power_keyboard_proxy_ready_cb (GObject             *source_object,
                                GAsyncResult        *res,
@@ -2289,6 +2391,14 @@ engine_settings_key_changed_cb (GSettings *settings,
                 idle_configure (manager);
                 return;
         }
+        if (g_str_equal (key, "power-saver-profile-on-low-battery")) {
+                if (manager->battery_is_low &&
+                    g_settings_get_boolean (settings, key))
+                        enable_power_saver (manager);
+                else
+                        disable_power_saver (manager);
+                return;
+        }
 }
 
 static void
@@ -2599,6 +2709,17 @@ on_rr_screen_acquired (GObject      *object,
         g_signal_connect (manager->up_client, "notify::on-battery",
                           G_CALLBACK (up_client_on_battery_cb), manager);
 
+        /* connect to power-profiles-daemon */
+        g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
+                                  G_DBUS_PROXY_FLAGS_NONE,
+                                  NULL,
+                                  PPD_DBUS_NAME,
+                                  PPD_DBUS_PATH,
+                                  PPD_DBUS_INTERFACE,
+                                  manager->cancellable,
+                                  power_profiles_proxy_ready_cb,
+                                  manager);
+
         /* connect to UPower for keyboard backlight control */
         manager->kbd_brightness_now = -1;
         g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
@@ -2862,6 +2983,9 @@ gsd_power_manager_stop (GsdPowerManager *manager)
 
         g_clear_object (&manager->screensaver_proxy);
 
+        disable_power_saver (manager);
+        g_clear_object (&manager->power_profiles_proxy);
+
         play_loop_stop (&manager->critical_alert_timeout_id);
 
         g_clear_object (&manager->idle_monitor);
diff --git a/plugins/power/test.py b/plugins/power/test.py
index bb5861a4..4f32ae8c 100755
--- a/plugins/power/test.py
+++ b/plugins/power/test.py
@@ -76,6 +76,11 @@ class PowerPluginBase(gsdtestcase.GSDTestCase):
             'gnome_screensaver', stdout=subprocess.PIPE)
         gsdtestcase.set_nonblock(self.screensaver.stdout)
 
+        # start mock power-profiles-daemon
+        (self.ppd, self.obj_ppd) = self.spawn_server_template(
+            'power_profiles_daemon', stdout=subprocess.PIPE)
+        gsdtestcase.set_nonblock(self.ppd.stdout)
+
         self.session_log = OutputChecker()
         self.session = subprocess.Popen(['gnome-session', '-f',
                                          '-a', os.path.join(self.workdir, 'autostart'),
@@ -154,6 +159,8 @@ class PowerPluginBase(gsdtestcase.GSDTestCase):
         self.upowerd.wait()
         self.screensaver.terminate()
         self.screensaver.wait()
+        self.ppd.terminate()
+        self.ppd.wait()
         self.stop_session()
         self.stop_mutter()
         self.stop_logind()
@@ -1302,5 +1309,25 @@ class PowerPluginTest8(PowerPluginBase):
 
         self.assertEqual(exc.exception.get_dbus_message(), 'No usable backlight could be found!')
 
+    def test_power_saver_on_low_battery(self):
+        obj_props = dbus.Interface(self.obj_ppd, dbus.PROPERTIES_IFACE)
+
+        self.set_composite_battery_discharging()
+        time.sleep(0.5)
+        holds = obj_props.Get('net.hadess.PowerProfiles', 'ActiveProfileHolds')
+        self.assertEqual(len(holds), 0)
+
+        self.set_composite_battery_critical()
+        time.sleep(0.5)
+        holds = obj_props.Get('net.hadess.PowerProfiles', 'ActiveProfileHolds')
+        self.assertEqual(len(holds), 1)
+        self.assertEqual(holds[0]['Profile'], 'power-saver')
+        self.assertEqual(holds[0]['ApplicationId'], 'org.gnome.SettingsDaemon.Power')
+
+        self.set_composite_battery_discharging()
+        time.sleep(0.5)
+        holds = obj_props.Get('net.hadess.PowerProfiles', 'ActiveProfileHolds')
+        self.assertEqual(len(holds), 0)
+
 # avoid writing to stderr
 unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2))


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]