Skip to content

Commit

Permalink
Simplify/optimize battery util on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrapha committed Sep 18, 2019
1 parent b507df7 commit 4d33dde
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 74 deletions.
106 changes: 34 additions & 72 deletions src/util/battery/batterylinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,99 +7,61 @@
#include <QtDebug>

BatteryLinux::BatteryLinux(QObject* pParent)
: Battery(pParent) {
: Battery(pParent),
m_client(up_client_new()) {
}

BatteryLinux::~BatteryLinux() {
g_object_unref(static_cast<UpClient*>(m_client));
}

void BatteryLinux::read() {
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;
VERIFY_OR_DEBUG_ASSERT(static_cast<UpClient*>(m_client)) {
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

#if UP_CHECK_VERSION(0, 99, 8)
GPtrArray* devices = up_client_get_devices2(client);
VERIFY_OR_DEBUG_ASSERT(devices) {
return;
guint kind;
gboolean isPresent;
guint state;
gdouble percentage;
gint64 timeToEmpty;
gint64 timeToFull;
UpDevice *device = up_client_get_display_device(static_cast<UpClient*>(m_client));
if (device) {
g_object_get(G_OBJECT(device),
"kind", &kind,
"is-present", &isPresent,
"state", &state,
"percentage", &percentage,
"time-to-empty", &timeToEmpty,
"time-to-full", &timeToFull,
NULL);
} else {
isPresent = false;
}
#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;
}
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 (isPresent && kind == UP_DEVICE_KIND_BATTERY) {
if (state == UP_DEVICE_STATE_CHARGING) {
m_chargingState = CHARGING;
m_chargingState = CHARGING;
} else if (state == UP_DEVICE_STATE_DISCHARGING) {
m_chargingState = DISCHARGING;
m_chargingState = DISCHARGING;
} else if (state == UP_DEVICE_STATE_FULLY_CHARGED) {
m_chargingState = CHARGED;
m_chargingState = CHARGED;
} else {
m_chargingState = UNKNOWN;
m_chargingState = UNKNOWN;
}

m_dPercentage = percentage;

// upower tells us the remaining time in seconds (0 if unknown)
if (m_chargingState == CHARGING && timeToFull > 0) {
m_iMinutesLeft = timeToFull / 60;
m_iMinutesLeft = timeToFull / 60;
} else if (m_chargingState == DISCHARGING && timeToEmpty > 0) {
m_iMinutesLeft = timeToEmpty / 60;
m_iMinutesLeft = timeToEmpty / 60;
}
break;
}
} else {
m_iMinutesLeft = Battery::TIME_UNKNOWN;
m_dPercentage = 0.0;
m_chargingState = Battery::UNKNOWN;
}

g_ptr_array_free(devices, TRUE);
g_object_unref(client);
}
5 changes: 3 additions & 2 deletions src/util/battery/batterylinux.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

class BatteryLinux : public Battery {
public:
BatteryLinux(QObject* pParent=nullptr);
virtual ~BatteryLinux();
explicit BatteryLinux(QObject* pParent=nullptr);
~BatteryLinux() override;

protected:
void read() override;
void* m_client;
};

#endif /* UTIL_BATTERY_BATTERYLINUX_H */

0 comments on commit 4d33dde

Please sign in to comment.