From e0f0931e2db38f01375895614dc385cba099c0cd Mon Sep 17 00:00:00 2001 From: Erik Reider <35975961+ErikReider@users.noreply.github.com> Date: Fri, 25 Mar 2022 17:27:36 +0100 Subject: [PATCH] Hide module if UPower service isn't running --- include/modules/upower/upower.hpp | 6 ++++++ src/modules/upower/upower.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/modules/upower/upower.hpp b/include/modules/upower/upower.hpp index 5b4d2f5f..ee70fbef 100644 --- a/include/modules/upower/upower.hpp +++ b/include/modules/upower/upower.hpp @@ -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 diff --git a/src/modules/upower/upower.cpp b/src/modules/upower/upower.cpp index 30326790..8750c86a 100644 --- a/src/modules/upower/upower.cpp +++ b/src/modules/upower/upower.cpp @@ -68,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) { @@ -106,6 +114,7 @@ UPower::~UPower() { g_dbus_connection_signal_unsubscribe(login1_connection, login1_id); login1_id = 0; } + g_bus_unwatch_name(upowerWatcher_id); removeDevices(); } @@ -142,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(data); + up->upowerRunning = true; + up->dp.emit(); +} +void UPower::upowerDisappear(GDBusConnection* conn, const gchar* name, gpointer data) { + UPower* up = static_cast(data); + up->upowerRunning = false; + up->dp.emit(); +} void UPower::removeDevice(const gchar* objectPath) { std::lock_guard guard(m_Mutex); @@ -261,6 +281,14 @@ std::string UPower::timeToString(gint64 time) { auto UPower::update() -> void { std::lock_guard guard(m_Mutex); + // Hide everything if the UPower service is not running + if (!upowerRunning) { + event_box_.set_visible(false); + // Call parent update + AModule::update(); + return; + } + UpDeviceKind kind; UpDeviceState state; double percentage;