From f234dc612b46f7dc98b59a52fca9f8a3769924c0 Mon Sep 17 00:00:00 2001 From: Raphael Graf Date: Wed, 18 Sep 2019 11:43:09 +0200 Subject: [PATCH] Simplify/optimize battery util on linux --- src/util/battery/batterylinux.cpp | 115 ++++++++++-------------------- src/util/battery/batterylinux.h | 7 +- 2 files changed, 43 insertions(+), 79 deletions(-) diff --git a/src/util/battery/batterylinux.cpp b/src/util/battery/batterylinux.cpp index 22361cb646d..aa96a510d97 100644 --- a/src/util/battery/batterylinux.cpp +++ b/src/util/battery/batterylinux.cpp @@ -7,99 +7,60 @@ #include BatteryLinux::BatteryLinux(QObject* pParent) - : Battery(pParent) { + : Battery(pParent), + m_client(up_client_new()) { } BatteryLinux::~BatteryLinux() { + g_object_unref(static_cast(m_client)); } void BatteryLinux::read() { + VERIFY_OR_DEBUG_ASSERT(static_cast(m_client)) { + return; + } m_iMinutesLeft = Battery::TIME_UNKNOWN; m_dPercentage = 0.0; m_chargingState = Battery::UNKNOWN; - // NOTE(rryan): It would be nice if we could create the client - // once. However, while testing this up_client_get_devices(client) returned - // an empty list when I tried to re-use the UpClient instance. - UpClient* client = up_client_new(); - VERIFY_OR_DEBUG_ASSERT(client) { - return; + UpDevice *device = up_client_get_display_device(static_cast(m_client)); + if (!device) { + return; } -#if !UP_CHECK_VERSION(0, 9, 99) - // Re-enumerate in case a device is added. - up_client_enumerate_devices_sync(client, NULL, NULL); -#endif + guint kind = UP_DEVICE_KIND_UNKNOWN; + gboolean isPresent = false; + guint state = UP_DEVICE_STATE_UNKNOWN; + gdouble percentage = 0.0; + gint64 timeToEmpty = 0; + gint64 timeToFull = 0; + g_object_get(G_OBJECT(device), + "kind", &kind, + "is-present", &isPresent, + "state", &state, + "percentage", &percentage, + "time-to-empty", &timeToEmpty, + "time-to-full", &timeToFull, + NULL); -#if UP_CHECK_VERSION(0, 99, 8) - GPtrArray* devices = up_client_get_devices2(client); - VERIFY_OR_DEBUG_ASSERT(devices) { - return; - } -#else - // This deprecated function doesn't set the free function for - // the array elements so we need to do it! - // https://bugs.freedesktop.org/show_bug.cgi?id=106740 - // https://gitlab.freedesktop.org/upower/upower/issues/14 - GPtrArray* devices = up_client_get_devices(client); - VERIFY_OR_DEBUG_ASSERT(devices) { - return; + if (!isPresent || kind != UP_DEVICE_KIND_BATTERY) { + return; } - g_ptr_array_set_free_func(devices, (GDestroyNotify) g_object_unref); -#endif - - for (guint i = 0; i < devices->len; ++i) { - gpointer device = g_ptr_array_index(devices, i); - VERIFY_OR_DEBUG_ASSERT(device) { - continue; - } - - gboolean online; - gdouble percentage; - guint state; - guint kind; - gint64 timeToEmpty; - gint64 timeToFull; - g_object_get(G_OBJECT(device), - "percentage", &percentage, - "online", &online, - "state", &state, - "kind", &kind, - "time-to-empty", &timeToEmpty, - "time-to-full", &timeToFull, - NULL); - // qDebug() << "BatteryLinux::read()" - // << "online" << online - // << "percentage" << percentage - // << "state" << state - // << "kind" << kind - // << "timeToEmpty" << timeToEmpty - // << "timeToFull" << timeToFull; - - if (kind == UP_DEVICE_KIND_BATTERY) { - if (state == UP_DEVICE_STATE_CHARGING) { - m_chargingState = CHARGING; - } else if (state == UP_DEVICE_STATE_DISCHARGING) { - m_chargingState = DISCHARGING; - } else if (state == UP_DEVICE_STATE_FULLY_CHARGED) { - m_chargingState = CHARGED; - } else { - m_chargingState = UNKNOWN; - } + if (state == UP_DEVICE_STATE_CHARGING) { + m_chargingState = CHARGING; + } else if (state == UP_DEVICE_STATE_DISCHARGING) { + m_chargingState = DISCHARGING; + } else if (state == UP_DEVICE_STATE_FULLY_CHARGED) { + m_chargingState = CHARGED; + } - m_dPercentage = percentage; + m_dPercentage = percentage; - // upower tells us the remaining time in seconds (0 if unknown) - if (m_chargingState == CHARGING && timeToFull > 0) { - m_iMinutesLeft = timeToFull / 60; - } else if (m_chargingState == DISCHARGING && timeToEmpty > 0) { - m_iMinutesLeft = timeToEmpty / 60; - } - break; - } + // upower tells us the remaining time in seconds (0 if unknown) + if (m_chargingState == CHARGING && timeToFull > 0) { + m_iMinutesLeft = timeToFull / 60; + } else if (m_chargingState == DISCHARGING && timeToEmpty > 0) { + m_iMinutesLeft = timeToEmpty / 60; } - - g_ptr_array_free(devices, TRUE); - g_object_unref(client); } diff --git a/src/util/battery/batterylinux.h b/src/util/battery/batterylinux.h index 997e4382db6..92161718052 100644 --- a/src/util/battery/batterylinux.h +++ b/src/util/battery/batterylinux.h @@ -5,11 +5,14 @@ class BatteryLinux : public Battery { public: - BatteryLinux(QObject* pParent=nullptr); - virtual ~BatteryLinux(); + explicit BatteryLinux(QObject* pParent=nullptr); + ~BatteryLinux() override; protected: void read() override; + + private: + void* m_client; }; #endif /* UTIL_BATTERY_BATTERYLINUX_H */