Skip to content

Commit e0f0931

Browse files
committed
Hide module if UPower service isn't running
1 parent 0140606 commit e0f0931

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
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

src/modules/upower/upower.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ UPower::UPower(const std::string& id, const Json::Value& config)
6868
box_.signal_query_tooltip().connect(sigc::mem_fun(*this, &UPower::show_tooltip_callback));
6969
}
7070

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+
7179
GError* error = NULL;
7280
client = up_client_new_full(NULL, &error);
7381
if (client == NULL) {
@@ -106,6 +114,7 @@ UPower::~UPower() {
106114
g_dbus_connection_signal_unsubscribe(login1_connection, login1_id);
107115
login1_id = 0;
108116
}
117+
g_bus_unwatch_name(upowerWatcher_id);
109118
removeDevices();
110119
}
111120

@@ -142,6 +151,17 @@ void UPower::prepareForSleep_cb(GDBusConnection* system_bus, const gchar* sender
142151
}
143152
}
144153
}
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->dp.emit();
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->dp.emit();
164+
}
145165

146166
void UPower::removeDevice(const gchar* objectPath) {
147167
std::lock_guard<std::mutex> guard(m_Mutex);
@@ -261,6 +281,14 @@ std::string UPower::timeToString(gint64 time) {
261281
auto UPower::update() -> void {
262282
std::lock_guard<std::mutex> guard(m_Mutex);
263283

284+
// Hide everything if the UPower service is not running
285+
if (!upowerRunning) {
286+
event_box_.set_visible(false);
287+
// Call parent update
288+
AModule::update();
289+
return;
290+
}
291+
264292
UpDeviceKind kind;
265293
UpDeviceState state;
266294
double percentage;

0 commit comments

Comments
 (0)