Merge pull request #1493 from ErikReider/upower-fixes

Upower fixes
This commit is contained in:
Alex 2022-04-05 16:37:36 +02:00 committed by GitHub
commit 1d03034fbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 5 deletions

View File

@ -34,6 +34,10 @@ class UPower : public AModule {
const gchar *object_path, const gchar *interface_name,
const gchar *signal_name, GVariant *parameters,
gpointer user_data);
static void upowerAppear(GDBusConnection *conn, const gchar *name, const gchar *name_owner,
gpointer data);
static void upowerDisappear(GDBusConnection *connection, const gchar *name, gpointer user_data);
void removeDevice(const gchar *objectPath);
void addDevice(UpDevice *device);
void setDisplayDevice();
@ -67,6 +71,8 @@ class UPower : public AModule {
UPowerTooltip *upower_tooltip;
std::string lastStatus;
bool showAltText;
bool upowerRunning;
guint upowerWatcher_id;
};
} // namespace waybar::modules::upower

View File

@ -71,4 +71,6 @@ depending on the charging state.
- *#upower*
- *#upower.charging*
- *#upower.discharging*
- *#upower.full*
- *#upower.empty*
- *#upower.unknown-status*

View File

@ -359,6 +359,7 @@ if scdoc.found()
'waybar-wlr-workspaces.5.scd',
'waybar-bluetooth.5.scd',
'waybar-sndio.5.scd',
'waybar-upower.5.scd',
]
if (giounix.found() and not get_option('logind').disabled())

View File

@ -22,6 +22,7 @@ UPower::UPower(const std::string& id, const Json::Value& config)
showAltText(false) {
box_.pack_start(icon_);
box_.pack_start(label_);
box_.set_name(name_);
event_box_.add(box_);
// Icon Size
@ -67,6 +68,14 @@ UPower::UPower(const std::string& id, const Json::Value& config)
box_.signal_query_tooltip().connect(sigc::mem_fun(*this, &UPower::show_tooltip_callback));
}
upowerWatcher_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM,
"org.freedesktop.UPower",
G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
upowerAppear,
upowerDisappear,
this,
NULL);
GError* error = NULL;
client = up_client_new_full(NULL, &error);
if (client == NULL) {
@ -105,6 +114,7 @@ UPower::~UPower() {
g_dbus_connection_signal_unsubscribe(login1_connection, login1_id);
login1_id = 0;
}
g_bus_unwatch_name(upowerWatcher_id);
removeDevices();
}
@ -141,6 +151,17 @@ void UPower::prepareForSleep_cb(GDBusConnection* system_bus, const gchar* sender
}
}
}
void UPower::upowerAppear(GDBusConnection* conn, const gchar* name, const gchar* name_owner,
gpointer data) {
UPower* up = static_cast<UPower*>(data);
up->upowerRunning = true;
up->event_box_.set_visible(true);
}
void UPower::upowerDisappear(GDBusConnection* conn, const gchar* name, gpointer data) {
UPower* up = static_cast<UPower*>(data);
up->upowerRunning = false;
up->event_box_.set_visible(false);
}
void UPower::removeDevice(const gchar* objectPath) {
std::lock_guard<std::mutex> guard(m_Mutex);
@ -226,11 +247,13 @@ const std::string UPower::getDeviceStatus(UpDeviceState& state) {
case UP_DEVICE_STATE_CHARGING:
case UP_DEVICE_STATE_PENDING_CHARGE:
return "charging";
case UP_DEVICE_STATE_EMPTY:
case UP_DEVICE_STATE_FULLY_CHARGED:
case UP_DEVICE_STATE_DISCHARGING:
case UP_DEVICE_STATE_PENDING_DISCHARGE:
return "discharging";
case UP_DEVICE_STATE_FULLY_CHARGED:
return "full";
case UP_DEVICE_STATE_EMPTY:
return "empty";
default:
return "unknown-status";
}
@ -258,6 +281,9 @@ std::string UPower::timeToString(gint64 time) {
auto UPower::update() -> void {
std::lock_guard<std::mutex> guard(m_Mutex);
// Don't update widget if the UPower service isn't running
if (!upowerRunning) return;
UpDeviceKind kind;
UpDeviceState state;
double percentage;
@ -344,7 +370,7 @@ auto UPower::update() -> void {
label_.set_markup(onlySpaces ? "" : label_format);
// Set icon
if (!Gtk::IconTheme::get_default()->has_icon(icon_name)) {
if (icon_name == NULL || !Gtk::IconTheme::get_default()->has_icon(icon_name)) {
icon_name = (char*)"battery-missing-symbolic";
}
icon_.set_from_icon_name(icon_name, Gtk::ICON_SIZE_INVALID);

View File

@ -62,7 +62,9 @@ uint UPowerTooltip::updateTooltip(Devices& devices) {
NULL);
// Skip Line_Power and BAT0 devices
if (kind == UP_DEVICE_KIND_LINE_POWER || strcmp(native_path, "BAT0") == 0) continue;
if (kind == UP_DEVICE_KIND_LINE_POWER || native_path == NULL || strlen(native_path) == 0 ||
strcmp(native_path, "BAT0") == 0)
continue;
Gtk::Box* modelBox = new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL);
box->add(*modelBox);
@ -77,6 +79,7 @@ uint UPowerTooltip::updateTooltip(Devices& devices) {
modelBox->add(*deviceIcon);
// Set model
if (model == NULL) model = (gchar*)"";
Gtk::Label* modelLabel = new Gtk::Label(model);
modelBox->add(*modelLabel);
@ -86,7 +89,7 @@ uint UPowerTooltip::updateTooltip(Devices& devices) {
// Set icon
Gtk::Image* icon = new Gtk::Image();
icon->set_pixel_size(iconSize);
if (!Gtk::IconTheme::get_default()->has_icon(icon_name)) {
if (icon_name == NULL || !Gtk::IconTheme::get_default()->has_icon(icon_name)) {
icon_name = (char*)"battery-missing-symbolic";
}
icon->set_from_icon_name(icon_name, Gtk::ICON_SIZE_INVALID);