Skip to content

Commit 1d03034

Browse files
authored
Merge pull request #1493 from ErikReider/upower-fixes
Upower fixes
2 parents d4a0748 + a7ed1ed commit 1d03034

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

include/modules/upower/upower.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class UPower : public AModule {
3434
const gchar *object_path, const gchar *interface_name,
3535
const gchar *signal_name, GVariant *parameters,
3636
gpointer user_data);
37+
static void upowerAppear(GDBusConnection *conn, const gchar *name, const gchar *name_owner,
38+
gpointer data);
39+
static void upowerDisappear(GDBusConnection *connection, const gchar *name, gpointer user_data);
40+
3741
void removeDevice(const gchar *objectPath);
3842
void addDevice(UpDevice *device);
3943
void setDisplayDevice();
@@ -67,6 +71,8 @@ class UPower : public AModule {
6771
UPowerTooltip *upower_tooltip;
6872
std::string lastStatus;
6973
bool showAltText;
74+
bool upowerRunning;
75+
guint upowerWatcher_id;
7076
};
7177

7278
} // namespace waybar::modules::upower

man/waybar-upower.5.scd

+2
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,6 @@ depending on the charging state.
7171
- *#upower*
7272
- *#upower.charging*
7373
- *#upower.discharging*
74+
- *#upower.full*
75+
- *#upower.empty*
7476
- *#upower.unknown-status*

meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ if scdoc.found()
359359
'waybar-wlr-workspaces.5.scd',
360360
'waybar-bluetooth.5.scd',
361361
'waybar-sndio.5.scd',
362+
'waybar-upower.5.scd',
362363
]
363364

364365
if (giounix.found() and not get_option('logind').disabled())

src/modules/upower/upower.cpp

+29-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ UPower::UPower(const std::string& id, const Json::Value& config)
2222
showAltText(false) {
2323
box_.pack_start(icon_);
2424
box_.pack_start(label_);
25+
box_.set_name(name_);
2526
event_box_.add(box_);
2627

2728
// Icon Size
@@ -67,6 +68,14 @@ UPower::UPower(const std::string& id, const Json::Value& config)
6768
box_.signal_query_tooltip().connect(sigc::mem_fun(*this, &UPower::show_tooltip_callback));
6869
}
6970

71+
upowerWatcher_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM,
72+
"org.freedesktop.UPower",
73+
G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
74+
upowerAppear,
75+
upowerDisappear,
76+
this,
77+
NULL);
78+
7079
GError* error = NULL;
7180
client = up_client_new_full(NULL, &error);
7281
if (client == NULL) {
@@ -105,6 +114,7 @@ UPower::~UPower() {
105114
g_dbus_connection_signal_unsubscribe(login1_connection, login1_id);
106115
login1_id = 0;
107116
}
117+
g_bus_unwatch_name(upowerWatcher_id);
108118
removeDevices();
109119
}
110120

@@ -141,6 +151,17 @@ void UPower::prepareForSleep_cb(GDBusConnection* system_bus, const gchar* sender
141151
}
142152
}
143153
}
154+
void UPower::upowerAppear(GDBusConnection* conn, const gchar* name, const gchar* name_owner,
155+
gpointer data) {
156+
UPower* up = static_cast<UPower*>(data);
157+
up->upowerRunning = true;
158+
up->event_box_.set_visible(true);
159+
}
160+
void UPower::upowerDisappear(GDBusConnection* conn, const gchar* name, gpointer data) {
161+
UPower* up = static_cast<UPower*>(data);
162+
up->upowerRunning = false;
163+
up->event_box_.set_visible(false);
164+
}
144165

145166
void UPower::removeDevice(const gchar* objectPath) {
146167
std::lock_guard<std::mutex> guard(m_Mutex);
@@ -226,11 +247,13 @@ const std::string UPower::getDeviceStatus(UpDeviceState& state) {
226247
case UP_DEVICE_STATE_CHARGING:
227248
case UP_DEVICE_STATE_PENDING_CHARGE:
228249
return "charging";
229-
case UP_DEVICE_STATE_EMPTY:
230-
case UP_DEVICE_STATE_FULLY_CHARGED:
231250
case UP_DEVICE_STATE_DISCHARGING:
232251
case UP_DEVICE_STATE_PENDING_DISCHARGE:
233252
return "discharging";
253+
case UP_DEVICE_STATE_FULLY_CHARGED:
254+
return "full";
255+
case UP_DEVICE_STATE_EMPTY:
256+
return "empty";
234257
default:
235258
return "unknown-status";
236259
}
@@ -258,6 +281,9 @@ std::string UPower::timeToString(gint64 time) {
258281
auto UPower::update() -> void {
259282
std::lock_guard<std::mutex> guard(m_Mutex);
260283

284+
// Don't update widget if the UPower service isn't running
285+
if (!upowerRunning) return;
286+
261287
UpDeviceKind kind;
262288
UpDeviceState state;
263289
double percentage;
@@ -344,7 +370,7 @@ auto UPower::update() -> void {
344370
label_.set_markup(onlySpaces ? "" : label_format);
345371

346372
// Set icon
347-
if (!Gtk::IconTheme::get_default()->has_icon(icon_name)) {
373+
if (icon_name == NULL || !Gtk::IconTheme::get_default()->has_icon(icon_name)) {
348374
icon_name = (char*)"battery-missing-symbolic";
349375
}
350376
icon_.set_from_icon_name(icon_name, Gtk::ICON_SIZE_INVALID);

src/modules/upower/upower_tooltip.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ uint UPowerTooltip::updateTooltip(Devices& devices) {
6262
NULL);
6363

6464
// Skip Line_Power and BAT0 devices
65-
if (kind == UP_DEVICE_KIND_LINE_POWER || strcmp(native_path, "BAT0") == 0) continue;
65+
if (kind == UP_DEVICE_KIND_LINE_POWER || native_path == NULL || strlen(native_path) == 0 ||
66+
strcmp(native_path, "BAT0") == 0)
67+
continue;
6668

6769
Gtk::Box* modelBox = new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL);
6870
box->add(*modelBox);
@@ -77,6 +79,7 @@ uint UPowerTooltip::updateTooltip(Devices& devices) {
7779
modelBox->add(*deviceIcon);
7880

7981
// Set model
82+
if (model == NULL) model = (gchar*)"";
8083
Gtk::Label* modelLabel = new Gtk::Label(model);
8184
modelBox->add(*modelLabel);
8285

@@ -86,7 +89,7 @@ uint UPowerTooltip::updateTooltip(Devices& devices) {
8689
// Set icon
8790
Gtk::Image* icon = new Gtk::Image();
8891
icon->set_pixel_size(iconSize);
89-
if (!Gtk::IconTheme::get_default()->has_icon(icon_name)) {
92+
if (icon_name == NULL || !Gtk::IconTheme::get_default()->has_icon(icon_name)) {
9093
icon_name = (char*)"battery-missing-symbolic";
9194
}
9295
icon->set_from_icon_name(icon_name, Gtk::ICON_SIZE_INVALID);

0 commit comments

Comments
 (0)