Skip to content

Commit

Permalink
Support both new and old dbus names
Browse files Browse the repository at this point in the history
  • Loading branch information
clefebvre committed Nov 27, 2024
1 parent 2ff9786 commit 59ddb4a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
47 changes: 29 additions & 18 deletions files/usr/share/cinnamon/applets/power@cinnamon.org/applet.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const Settings = imports.ui.settings;

const BrightnessBusName = "org.cinnamon.SettingsDaemon.Power.Screen";
const KeyboardBusName = "org.cinnamon.SettingsDaemon.Power.Keyboard";
const PowerProfilesBusName = "net.hadess.PowerProfiles";
const PowerProfilesBusPath = "/net/hadess/PowerProfiles";

const CSD_BACKLIGHT_NOT_SUPPORTED_CODE = 1;

Expand All @@ -32,17 +30,6 @@ const POWER_PROFILES = {
"performance": _("Performance")
};

const PowerProfilesInterface = `<node>
<interface name="${PowerProfilesBusName}">
<property name="ActiveProfile" type="s" access="readwrite" />
<property name="PerformanceDegraded" type="s" access="read" />
<property name="Profiles" type="aa{sv}" access="read" />
<property name="ActiveProfileHolds" type="aa{sv}" access="read" />
</interface>
</node>`;

const PowerProfilesProxy = Gio.DBusProxy.makeProxyWrapper(PowerProfilesInterface);

function deviceLevelToString(level) {
switch (level) {
case UPDeviceLevel.FULL:
Expand Down Expand Up @@ -402,12 +389,36 @@ class CinnamonPowerApplet extends Applet.TextIconApplet {
this.menu.addMenuItem(this.keyboard);

try {
this._profilesProxy = new PowerProfilesProxy(Gio.DBus.system, PowerProfilesBusName, PowerProfilesBusPath);
} catch (error) {
this._profilesProxy = null;
// Hadess interface
let PowerProfilesInterface = `<node>
<interface name="net.hadess.PowerProfiles">
<property name="ActiveProfile" type="s" access="readwrite" />
<property name="PerformanceDegraded" type="s" access="read" />
<property name="Profiles" type="aa{sv}" access="read" />
<property name="ActiveProfileHolds" type="aa{sv}" access="read" />
</interface>
</node>`;
let PowerProfilesProxy = Gio.DBusProxy.makeProxyWrapper(PowerProfilesInterface);
this._profilesProxy = new PowerProfilesProxy(Gio.DBus.system, "net.haess.PowerProfiles", "/net/hadess/PowerProfiles");
// Upower if hadess doesn't work..
if (!this._profilesProxy.Profiles) {
// UPower interface
let PowerProfilesInterface = `<node>
<interface name="org.freedesktop.UPower.PowerProfiles">
<property name="ActiveProfile" type="s" access="readwrite" />
<property name="PerformanceDegraded" type="s" access="read" />
<property name="Profiles" type="aa{sv}" access="read" />
<property name="ActiveProfileHolds" type="aa{sv}" access="read" />
</interface>
</node>`;
let PowerProfilesProxy = Gio.DBusProxy.makeProxyWrapper(PowerProfilesInterface);
this._profilesProxy = new PowerProfilesProxy(Gio.DBus.system, "org.freedesktop.UPower.PowerProfiles", "/org/freedesktop/UPower/PowerProfiles");
}
} catch {
this._profilesProxy = null;
}

if (this._profilesProxy.Profiles) {
if (this._profilesProxy && this._profilesProxy.Profiles) {
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this.contentSection = new PopupMenu.PopupMenuSection();

Expand Down
46 changes: 25 additions & 21 deletions files/usr/share/cinnamon/cinnamon-settings/modules/cs_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
"performance": _("Performance")
}

POWER_PROFILES_DBUS_NAME = "net.hadess.PowerProfiles"
POWER_PROFILES_DBUS_PATH = "/net/hadess/PowerProfiles"

(UP_ID, UP_VENDOR, UP_MODEL, UP_TYPE, UP_ICON, UP_PERCENTAGE, UP_STATE, UP_BATTERY_LEVEL, UP_SECONDS) = range(9)

try:
Expand Down Expand Up @@ -208,7 +205,22 @@ def on_module_selected(self):
section.add_row(self.sth_switch)

# Power mode
section.add_row(PowerModeComboBox())
connection = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
proxy = None
profiles = None
for dbus_name in ["net.hadess.PowerProfiles", "org.freedesktop.UPower.PowerProfiles"]:
try:
dbus_path = "/" + dbus_name.replace(".", "/")
proxy = Gio.DBusProxy.new_sync(connection, Gio.DBusProxyFlags.NONE, None,
dbus_name, dbus_path, "org.freedesktop.DBus.Properties", None)
profiles = proxy.Get('(ss)', dbus_name, "Profiles")
print(f"Found power profiles on Dbus at {dbus_name}")
break
except:
pass

if profiles:
section.add_row(PowerModeComboBox(proxy, dbus_name, profiles))

# Batteries

Expand Down Expand Up @@ -831,22 +843,13 @@ def add_to_size_group(self, group):


class PowerModeComboBox(SettingsWidget):
def __init__(self, dep_key=None, size_group=None):
def __init__(self, proxy, dbus_name, profiles, dep_key=None, size_group=None):
super(PowerModeComboBox, self).__init__(dep_key=dep_key)

connection = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
self.proxy = Gio.DBusProxy.new_sync(
connection,
Gio.DBusProxyFlags.NONE,
None,
POWER_PROFILES_DBUS_NAME,
POWER_PROFILES_DBUS_PATH,
"org.freedesktop.DBus.Properties",
None)

try:
profiles = []
profiles = self.proxy.Get('(ss)', POWER_PROFILES_DBUS_NAME, "Profiles")
self.proxy = proxy
self.dbus_name = dbus_name

try:
profiles_options = []
for profile in profiles:
name = profile["Profile"]
Expand Down Expand Up @@ -888,17 +891,18 @@ def on_my_value_changed(self, widget):
if tree_iter is not None:
profile = self.model[tree_iter][0]
value = GLib.Variant.new_string(profile)
self.proxy.Set('(ssv)', POWER_PROFILES_DBUS_NAME, "ActiveProfile", value)
self.proxy.Set('(ssv)', self.dbus_name, "ActiveProfile", value)

def on_dbus_changed(self, proxy, sender, signal, params):
if signal == "PropertiesChanged":
self.on_my_setting_changed()

def on_my_setting_changed(self):
try:
active_profile = self.proxy.Get('(ss)', POWER_PROFILES_DBUS_NAME, "ActiveProfile")
active_profile = self.proxy.Get('(ss)', self.dbus_name, "ActiveProfile")
self.content_widget.set_active_iter(self.option_map[active_profile])
except:
except Exception as e:
print(e)
self.content_widget.set_active_iter(None)

def add_to_size_group(self, group):
Expand Down

0 comments on commit 59ddb4a

Please sign in to comment.