Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify/optimize battery util on linux #2287

Merged
merged 1 commit into from
Sep 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 38 additions & 77 deletions src/util/battery/batterylinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,99 +7,60 @@
#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() {
VERIFY_OR_DEBUG_ASSERT(static_cast<UpClient*>(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<UpClient*>(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);
}
7 changes: 5 additions & 2 deletions src/util/battery/batterylinux.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
rrrapha marked this conversation as resolved.
Show resolved Hide resolved
};

#endif /* UTIL_BATTERY_BATTERYLINUX_H */