Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a power-profiles-daemon module #2971

Merged
merged 9 commits into from
Mar 4, 2024
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Local time
- Battery
- UPower
- Power profiles daemon
- Network
- Bluetooth
- Pulseaudio
Expand Down
46 changes: 46 additions & 0 deletions include/modules/power_profiles_daemon.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <fmt/format.h>

#include "ALabel.hpp"
#include "giomm/dbusproxy.h"

namespace waybar::modules {

struct Profile {
std::string name;
std::string driver;
};

class PowerProfilesDaemon : public ALabel {
public:
PowerProfilesDaemon(const std::string &, const Json::Value &);
auto update() -> void override;
void profileChangedCb(const Gio::DBus::Proxy::MapChangedProperties &,
const std::vector<Glib::ustring> &);
void busConnectedCb(Glib::RefPtr<Gio::AsyncResult> &r);
void getAllPropsCb(Glib::RefPtr<Gio::AsyncResult> &r);
void setPropCb(Glib::RefPtr<Gio::AsyncResult> &r);
void populateInitState();
bool handleToggle(GdkEventButton *const &e) override;

private:
// True if we're connected to the dbug interface. False if we're
// not.
bool connected_;
// Look for a profile name in the list of available profiles and
// switch activeProfile_ to it.
void switchToProfile(std::string const &);
// Used to toggle/display the profiles
std::vector<Profile> availableProfiles_;
// Points to the active profile in the profiles list
std::vector<Profile>::iterator activeProfile_;
// Current CSS class applied to the label
std::string currentStyle_;
// Format string
std::string tooltipFormat_;
// DBus Proxy used to track the current active profile
Glib::RefPtr<Gio::DBus::Proxy> powerProfilesProxy_;
};

} // namespace waybar::modules
72 changes: 72 additions & 0 deletions man/waybar-power-profiles-daemon.5.scd
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
waybar-power-profiles-daemon(5)

# NAME

waybar - power-profiles-daemon module

# DESCRIPTION

The *power-profiles-daemon* module displays the active power-profiles-daemon profile and cycle through the available profiles on click.

# FILES

$XDG_CONFIG_HOME/waybar/config

# CONFIGURATION


[- *Option*
:- *Typeof*
:- *Default*
:= *Description*
|[ *format*
:[ string
:[ "{icon}"
:[ Message displayed on the bar. {icon} and {profile} are respectively substituted with the icon representing the active profile and its full name.
|[ *tooltip-format*
:[ string
:[ "Power profile: {profile}\\nDriver: {driver}"
:[ Messaged displayed in the module tooltip. {icon} and {profile} are respectively substituted with the icon representing the active profile and its full name.
|[ *tooltip*
:[ bool
:[ true
:[ Display the tooltip.
|[ *format-icons*
:[ object
:[ See default value in the example below.
:[ Icons used to represent the various power-profile. *Note*: the default configuration uses the font-awesome icons. You may want to override it if you don't have this font installed on your system.


# CONFIGURATION EXAMPLES

Compact display (default config):

```
"power-profiles-daemon": {
"format": "{icon}",
"tooltip-format": "Power profile: {profile}\nDriver: {driver}",
"tooltip": true,
"format-icons": {
"default": "",
"performance": "",
"balanced": "",
"power-saver": ""
}
}
```

Display the full profile name:

```
"power-profiles-daemon": {
"format": "{icon} {profile}",
"tooltip-format": "Power profile: {profile}\nDriver: {driver}",
"tooltip": true,
"format-icons": {
"default": "",
"performance": "",
"balanced": "",
"power-saver": ""
}
}
```
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ if is_linux
'src/modules/cpu_usage/linux.cpp',
'src/modules/memory/common.cpp',
'src/modules/memory/linux.cpp',
'src/modules/power_profiles_daemon.cpp',
'src/modules/systemd_failed_units.cpp',
)
man_files += files(
Expand All @@ -221,6 +222,7 @@ if is_linux
'man/waybar-cpu.5.scd',
'man/waybar-memory.5.scd',
'man/waybar-systemd-failed-units.5.scd',
'man/waybar-power-profiles-daemon.5.scd',
)
elif is_dragonfly or is_freebsd or is_netbsd or is_openbsd
add_project_arguments('-DHAVE_CPU_BSD', language: 'cpp')
Expand Down Expand Up @@ -577,4 +579,3 @@ if clangtidy.found()
'-p', meson.project_build_root()
] + src_files)
endif

13 changes: 12 additions & 1 deletion resources/config.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"idle_inhibitor",
"pulseaudio",
"network",
"power-profiles-daemon",
"cpu",
"memory",
"temperature",
Expand Down Expand Up @@ -147,6 +148,17 @@
"battery#bat2": {
"bat": "BAT2"
},
"power-profiles-daemon": {
"format": "{icon}",
"tooltip-format": "Power profile: {profile}\nDriver: {driver}",
"tooltip": true,
"format-icons": {
"default": "",
"performance": "",
"balanced": "",
"power-saver": ""
}
},
"network": {
// "interface": "wlp2*", // (Optional) To force the use of this interface
"format-wifi": "{essid} ({signalStrength}%) ",
Expand Down Expand Up @@ -188,4 +200,3 @@
// "exec": "$HOME/.config/waybar/mediaplayer.py --player spotify 2> /dev/null" // Filter player based on name
}
}

20 changes: 20 additions & 0 deletions resources/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ button:hover {
#mode,
#idle_inhibitor,
#scratchpad,
#power-profiles-daemon,
#mpd {
padding: 0 10px;
color: #ffffff;
Expand Down Expand Up @@ -139,6 +140,25 @@ button:hover {
animation-direction: alternate;
}

#power-profiles-daemon {
padding-right: 15px;
}

#power-profiles-daemon.performance {
background-color: #f53c3c;
color: #ffffff;
}

#power-profiles-daemon.balanced {
background-color: #2980b9;
color: #ffffff;
}

#power-profiles-daemon.power-saver {
background-color: #2ecc71;
color: #000000;
}

label:focus {
background-color: #000000;
}
Expand Down
4 changes: 4 additions & 0 deletions src/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
#endif
#if defined(__linux__)
#include "modules/bluetooth.hpp"
#include "modules/power_profiles_daemon.hpp"
#endif
#ifdef HAVE_LOGIND_INHIBITOR
#include "modules/inhibitor.hpp"
Expand Down Expand Up @@ -282,6 +283,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name,
if (ref == "bluetooth") {
return new waybar::modules::Bluetooth(id, config_[name]);
}
if (ref == "power-profiles-daemon") {
return new waybar::modules::PowerProfilesDaemon(id, config_[name]);
}
#endif
#ifdef HAVE_LOGIND_INHIBITOR
if (ref == "inhibitor") {
Expand Down
Loading