1bd53d
From 2103c5fcf994bb6aebd978553b338436e85fa7ed Mon Sep 17 00:00:00 2001
1bd53d
From: =?UTF-8?q?Florian=20M=C3=BCllner?= <fmuellner@gnome.org>
1bd53d
Date: Wed, 7 Jul 2021 22:05:25 +0200
1bd53d
Subject: [PATCH 1/2] status/powerProfiles: Add power mode selection
1bd53d
1bd53d
Settings' power panel gained support for switchable power profiles
1bd53d
in GNOME 40. It's useful to have that functionality more readily
1bd53d
available, so expose it in the system status menu as well.
1bd53d
1bd53d
https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/3944
1bd53d
1bd53d
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1907>
1bd53d
---
1bd53d
 .../net.hadess.PowerProfiles.xml              |  76 ++++++++++++
1bd53d
 .../gnome-shell-dbus-interfaces.gresource.xml |   1 +
1bd53d
 js/js-resources.gresource.xml                 |   1 +
1bd53d
 js/ui/panel.js                                |   4 +
1bd53d
 js/ui/status/powerProfiles.js                 | 111 ++++++++++++++++++
1bd53d
 po/POTFILES.in                                |   1 +
1bd53d
 6 files changed, 194 insertions(+)
1bd53d
 create mode 100644 data/dbus-interfaces/net.hadess.PowerProfiles.xml
1bd53d
 create mode 100644 js/ui/status/powerProfiles.js
1bd53d
1bd53d
diff --git a/data/dbus-interfaces/net.hadess.PowerProfiles.xml b/data/dbus-interfaces/net.hadess.PowerProfiles.xml
1bd53d
new file mode 100644
1bd53d
index 000000000..fce04a86d
1bd53d
--- /dev/null
1bd53d
+++ b/data/dbus-interfaces/net.hadess.PowerProfiles.xml
1bd53d
@@ -0,0 +1,76 @@
1bd53d
+
1bd53d
+"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
1bd53d
+
1bd53d
+<node>
1bd53d
+
1bd53d
+  
1bd53d
+      net.hadess.PowerProfiles:
1bd53d
+      @short_description: Power Profiles daemon
1bd53d
+
1bd53d
+      The power-profiles-daemon API is meant to be used by parts of the OS or
1bd53d
+      desktop environment to switch system power profiles based on user choice,
1bd53d
+      or user intent.
1bd53d
+
1bd53d
+      OS components would typically use the "Profiles" property to construct
1bd53d
+      their UI (2 or 3 profiles available), and monitor the "ActiveProfile"
1bd53d
+      and the "PerformanceInhibited" properties to update that UI. The UI
1bd53d
+      would try to set the "ActiveProfile" property if the user selected
1bd53d
+      a different one.
1bd53d
+
1bd53d
+      Note that the reason why the project exists and how it is different from
1bd53d
+      existing projects is explained <ulink href=" https://gitlab.freedesktop.org/hadess/power-profiles-daemon/-/blob/master/README.md">
1bd53d
+      in the project's README file</ulink>.
1bd53d
+
1bd53d
+      The object path will be "/net/hadess/PowerProfiles".
1bd53d
+  -->
1bd53d
+  <interface name="net.hadess.PowerProfiles">
1bd53d
+    
1bd53d
+        ActiveProfile:
1bd53d
+
1bd53d
+        The type of the currently active profile. It might change automatically
1bd53d
+        if the "performance" profile was selected but it got inhibited, in which
1bd53d
+        case the "PerformanceInhibited" property will reflect the reason.
1bd53d
+    -->
1bd53d
+    <property name="ActiveProfile" type="s" access="readwrite"/>
1bd53d
+
1bd53d
+    
1bd53d
+        PerformanceInhibited:
1bd53d
+
1bd53d
+        This will be set if the performance power profile is unavailable, with
1bd53d
+        the value being used to identify the reason for unavailability. As new
1bd53d
+        reasons can be added, it is recommended that front-ends show a generic
1bd53d
+        reason if they do not recognise the value. Possible values are:
1bd53d
+        - "lap-detected" (the computer is sitting on the user's lap)
1bd53d
+        - "high-operating-temperature" (the computer is close to overheating)
1bd53d
+        - "" (the empty string, if not inhibited)
1bd53d
+    -->
1bd53d
+    <property name="PerformanceInhibited" type="s" access="read"/>
1bd53d
+
1bd53d
+    
1bd53d
+        Profiles:
1bd53d
+
1bd53d
+        An array of key-pair values representing each profile. The key named
1bd53d
+        "Driver" (s) identifies the power-profiles-daemon backend code used to
1bd53d
+        implement the profile.
1bd53d
+
1bd53d
+        The key named "Profile" (s) will be one of:
1bd53d
+        - "power-saver" (battery saving profile)
1bd53d
+        - "balanced" (the default  profile)
1bd53d
+        - "performance" (a profile that does not care about noise or battery consumption)
1bd53d
+
1bd53d
+        Only one of each type of profile will be listed, with the daemon choosing the
1bd53d
+        more appropriate "driver" for each profile type.
1bd53d
+    -->
1bd53d
+    <property name="Profiles" type="aa{sv}" access="read"/>
1bd53d
+
1bd53d
+    
1bd53d
+        Actions:
1bd53d
+
1bd53d
+        An array of strings listing each one of the "actions" implemented in
1bd53d
+        the running daemon. This is used by API users to figure out whether
1bd53d
+        particular functionality is available in a version of the daemon.
1bd53d
+    -->
1bd53d
+    <property name="Actions" type="as" access="read"/>
1bd53d
+
1bd53d
+  </interface>
1bd53d
+</node>
1bd53d
diff --git a/data/gnome-shell-dbus-interfaces.gresource.xml b/data/gnome-shell-dbus-interfaces.gresource.xml
1bd53d
index e7972f6cb..6682c462d 100644
1bd53d
--- a/data/gnome-shell-dbus-interfaces.gresource.xml
1bd53d
+++ b/data/gnome-shell-dbus-interfaces.gresource.xml
1bd53d
@@ -1,6 +1,7 @@
1bd53d
 
1bd53d
 <gresources>
1bd53d
   <gresource prefix="/org/gnome/shell/dbus-interfaces">
1bd53d
+    <file preprocess="xml-stripblanks">net.hadess.PowerProfiles.xml</file>
1bd53d
     <file preprocess="xml-stripblanks">net.hadess.SensorProxy.xml</file>
1bd53d
     <file preprocess="xml-stripblanks">net.reactivated.Fprint.Device.xml</file>
1bd53d
     <file preprocess="xml-stripblanks">net.reactivated.Fprint.Manager.xml</file>
1bd53d
diff --git a/js/js-resources.gresource.xml b/js/js-resources.gresource.xml
1bd53d
index b2c603a55..7a94e2ff1 100644
1bd53d
--- a/js/js-resources.gresource.xml
1bd53d
+++ b/js/js-resources.gresource.xml
1bd53d
@@ -134,6 +134,7 @@
1bd53d
     <file>ui/status/nightLight.js</file>
1bd53d
     <file>ui/status/network.js</file>
1bd53d
     <file>ui/status/power.js</file>
1bd53d
+    <file>ui/status/powerProfiles.js</file>
1bd53d
     <file>ui/status/rfkill.js</file>
1bd53d
     <file>ui/status/volume.js</file>
1bd53d
     <file>ui/status/bluetooth.js</file>
1bd53d
diff --git a/js/ui/panel.js b/js/ui/panel.js
1bd53d
index ad11f4ba2..84668e96e 100644
1bd53d
--- a/js/ui/panel.js
1bd53d
+++ b/js/ui/panel.js
1bd53d
@@ -693,6 +693,7 @@ class AggregateMenu extends PanelMenu.Button {
1bd53d
 
1bd53d
         this._remoteAccess = new imports.ui.status.remoteAccess.RemoteAccessApplet();
1bd53d
         this._power = new imports.ui.status.power.Indicator();
1bd53d
+        this._powerProfiles = new imports.ui.status.powerProfiles.Indicator();
1bd53d
         this._rfkill = new imports.ui.status.rfkill.Indicator();
1bd53d
         this._volume = new imports.ui.status.volume.Indicator();
1bd53d
         this._brightness = new imports.ui.status.brightness.Indicator();
1bd53d
@@ -712,6 +713,7 @@ class AggregateMenu extends PanelMenu.Button {
1bd53d
         this._indicators.add_child(this._rfkill);
1bd53d
         this._indicators.add_child(this._volume);
1bd53d
         this._indicators.add_child(this._power);
1bd53d
+        this._indicators.add_child(this._powerProfiles);
1bd53d
 
1bd53d
         this.menu.addMenuItem(this._volume.menu);
1bd53d
         this.menu.addMenuItem(this._brightness.menu);
1bd53d
@@ -726,6 +728,7 @@ class AggregateMenu extends PanelMenu.Button {
1bd53d
         this.menu.addMenuItem(this._location.menu);
1bd53d
         this.menu.addMenuItem(this._rfkill.menu);
1bd53d
         this.menu.addMenuItem(this._power.menu);
1bd53d
+        this.menu.addMenuItem(this._powerProfiles.menu);
1bd53d
         this.menu.addMenuItem(this._nightLight.menu);
1bd53d
         this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
1bd53d
         this.menu.addMenuItem(this._system.menu);
1bd53d
@@ -733,6 +736,7 @@ class AggregateMenu extends PanelMenu.Button {
1bd53d
         menuLayout.addSizeChild(this._location.menu.actor);
1bd53d
         menuLayout.addSizeChild(this._rfkill.menu.actor);
1bd53d
         menuLayout.addSizeChild(this._power.menu.actor);
1bd53d
+        menuLayout.addSizeChild(this._powerProfiles.menu.actor);
1bd53d
         menuLayout.addSizeChild(this._system.menu.actor);
1bd53d
     }
1bd53d
 });
1bd53d
diff --git a/js/ui/status/powerProfiles.js b/js/ui/status/powerProfiles.js
1bd53d
new file mode 100644
1bd53d
index 000000000..f6bc5835b
1bd53d
--- /dev/null
1bd53d
+++ b/js/ui/status/powerProfiles.js
1bd53d
@@ -0,0 +1,111 @@
1bd53d
+// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
1bd53d
+/* exported Indicator */
1bd53d
+
1bd53d
+const { Gio, GObject } = imports.gi;
1bd53d
+
1bd53d
+const Main = imports.ui.main;
1bd53d
+const PanelMenu = imports.ui.panelMenu;
1bd53d
+const PopupMenu = imports.ui.popupMenu;
1bd53d
+
1bd53d
+const { loadInterfaceXML } = imports.misc.fileUtils;
1bd53d
+
1bd53d
+const BUS_NAME = 'net.hadess.PowerProfiles';
1bd53d
+const OBJECT_PATH = '/net/hadess/PowerProfiles';
1bd53d
+
1bd53d
+const PowerProfilesIface = loadInterfaceXML('net.hadess.PowerProfiles');
1bd53d
+const PowerProfilesProxy = Gio.DBusProxy.makeProxyWrapper(PowerProfilesIface);
1bd53d
+
1bd53d
+const PROFILE_LABELS = {
1bd53d
+    'performance': _('Performance Mode'),
1bd53d
+    'balanced': _('Balanced Power'),
1bd53d
+    'power-saver': _('Power Saver'),
1bd53d
+};
1bd53d
+const PROFILE_ICONS = {
1bd53d
+    'performance': 'power-profile-performance-symbolic',
1bd53d
+    'balanced': 'power-profile-balanced-symbolic',
1bd53d
+    'power-saver': 'power-profile-power-saver-symbolic',
1bd53d
+};
1bd53d
+
1bd53d
+var Indicator = GObject.registerClass(
1bd53d
+class Indicator extends PanelMenu.SystemIndicator {
1bd53d
+    _init() {
1bd53d
+        super._init();
1bd53d
+
1bd53d
+        this._profileItems = new Map();
1bd53d
+        this._updateProfiles = true;
1bd53d
+
1bd53d
+        this._proxy = new PowerProfilesProxy(Gio.DBus.system, BUS_NAME, OBJECT_PATH,
1bd53d
+            (proxy, error) => {
1bd53d
+                if (error) {
1bd53d
+                    log(error.message);
1bd53d
+                } else {
1bd53d
+                    this._proxy.connect('g-properties-changed',
1bd53d
+                        (p, properties) => {
1bd53d
+                            const propertyNames = properties.deep_unpack();
1bd53d
+                            this._updateProfiles = 'Profiles' in propertyNames;
1bd53d
+                            this._sync();
1bd53d
+                        });
1bd53d
+                }
1bd53d
+                this._sync();
1bd53d
+            });
1bd53d
+
1bd53d
+        this._item = new PopupMenu.PopupSubMenuMenuItem('', true);
1bd53d
+
1bd53d
+        this._profileSection = new PopupMenu.PopupMenuSection();
1bd53d
+        this._item.menu.addMenuItem(this._profileSection);
1bd53d
+        this._item.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
1bd53d
+        this._item.menu.addSettingsAction(_('Power Settings'),
1bd53d
+            'gnome-power-panel.desktop');
1bd53d
+        this.menu.addMenuItem(this._item);
1bd53d
+
1bd53d
+        Main.sessionMode.connect('updated', this._sessionUpdated.bind(this));
1bd53d
+        this._sessionUpdated();
1bd53d
+        this._sync();
1bd53d
+    }
1bd53d
+
1bd53d
+    _sessionUpdated() {
1bd53d
+        const sensitive = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter;
1bd53d
+        this.menu.setSensitive(sensitive);
1bd53d
+    }
1bd53d
+
1bd53d
+    _sync() {
1bd53d
+        this._item.visible = this._proxy.g_name_owner !== null;
1bd53d
+
1bd53d
+        if (!this._item.visible)
1bd53d
+            return;
1bd53d
+
1bd53d
+        if (this._updateProfiles) {
1bd53d
+            this._profileSection.removeAll();
1bd53d
+            this._profileItems.clear();
1bd53d
+
1bd53d
+            const profiles = this._proxy.Profiles
1bd53d
+                .map(p => p.Profile.unpack())
1bd53d
+                .reverse();
1bd53d
+            for (const profile of profiles) {
1bd53d
+                const label = PROFILE_LABELS[profile];
1bd53d
+                if (!label)
1bd53d
+                    continue;
1bd53d
+
1bd53d
+                const item = new PopupMenu.PopupMenuItem(label);
1bd53d
+                item.connect('activate',
1bd53d
+                    () => (this._proxy.ActiveProfile = profile));
1bd53d
+                this._profileItems.set(profile, item);
1bd53d
+                this._profileSection.addMenuItem(item);
1bd53d
+            }
1bd53d
+            this._updateProfiles = false;
1bd53d
+        }
1bd53d
+
1bd53d
+        for (const [profile, item] of this._profileItems) {
1bd53d
+            item.setOrnament(profile === this._proxy.ActiveProfile
1bd53d
+                ? PopupMenu.Ornament.DOT
1bd53d
+                : PopupMenu.Ornament.NONE);
1bd53d
+        }
1bd53d
+
1bd53d
+        const perfItem = this._profileItems.get('performance');
1bd53d
+        if (perfItem)
1bd53d
+            perfItem.sensitive = this._proxy.PerformanceInhibited === '';
1bd53d
+
1bd53d
+        this._item.label.text = PROFILE_LABELS[this._proxy.ActiveProfile];
1bd53d
+        this._item.icon.icon_name = PROFILE_ICONS[this._proxy.ActiveProfile];
1bd53d
+    }
1bd53d
+});
1bd53d
diff --git a/po/POTFILES.in b/po/POTFILES.in
1bd53d
index cb279c1ee..727cb01a8 100644
1bd53d
--- a/po/POTFILES.in
1bd53d
+++ b/po/POTFILES.in
1bd53d
@@ -61,6 +61,7 @@ js/ui/status/location.js
1bd53d
 js/ui/status/network.js
1bd53d
 js/ui/status/nightLight.js
1bd53d
 js/ui/status/power.js
1bd53d
+js/ui/status/powerProfiles.js
1bd53d
 js/ui/status/remoteAccess.js
1bd53d
 js/ui/status/rfkill.js
1bd53d
 js/ui/status/system.js
1bd53d
-- 
1bd53d
2.31.1
1bd53d
1bd53d
1bd53d
From 0f8a2e2c6c3119492670efce5aff1224f2c3c47f Mon Sep 17 00:00:00 2001
1bd53d
From: =?UTF-8?q?Florian=20M=C3=BCllner?= <fmuellner@gnome.org>
1bd53d
Date: Fri, 6 Aug 2021 21:04:24 +0200
1bd53d
Subject: [PATCH 2/2] powerProfiles: Tweak profile names
1bd53d
1bd53d
After some more discussion, we settled on slightly different
1bd53d
profile names.
1bd53d
1bd53d
https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/4530
1bd53d
1bd53d
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1939>
1bd53d
---
1bd53d
 js/ui/status/powerProfiles.js | 6 +++---
1bd53d
 1 file changed, 3 insertions(+), 3 deletions(-)
1bd53d
1bd53d
diff --git a/js/ui/status/powerProfiles.js b/js/ui/status/powerProfiles.js
1bd53d
index f6bc5835b..61205bbc6 100644
1bd53d
--- a/js/ui/status/powerProfiles.js
1bd53d
+++ b/js/ui/status/powerProfiles.js
1bd53d
@@ -16,9 +16,9 @@ const PowerProfilesIface = loadInterfaceXML('net.hadess.PowerProfiles');
1bd53d
 const PowerProfilesProxy = Gio.DBusProxy.makeProxyWrapper(PowerProfilesIface);
1bd53d
 
1bd53d
 const PROFILE_LABELS = {
1bd53d
-    'performance': _('Performance Mode'),
1bd53d
-    'balanced': _('Balanced Power'),
1bd53d
-    'power-saver': _('Power Saver'),
1bd53d
+    'performance': C_('Power profile', 'Performance'),
1bd53d
+    'balanced': C_('Power profile', 'Balanced'),
1bd53d
+    'power-saver': C_('Power profile', 'Power Saver'),
1bd53d
 };
1bd53d
 const PROFILE_ICONS = {
1bd53d
     'performance': 'power-profile-performance-symbolic',
1bd53d
-- 
1bd53d
2.31.1
1bd53d