Skip to content

Instantly share code, notes, and snippets.

@nHurD
Last active August 29, 2015 13:57
Show Gist options
  • Save nHurD/9490807 to your computer and use it in GitHub Desktop.
Save nHurD/9490807 to your computer and use it in GitHub Desktop.
diff --git a/src/widgets/battery.c b/src/widgets/battery.c
index 9b9df18..3c871d3 100644
--- a/src/widgets/battery.c
+++ b/src/widgets/battery.c
@@ -3,15 +3,15 @@
#include "util/dbus_helpers.h"
static int
-widget_send_update (struct widget *widget, DBusGProxy *properties_proxy, char *pathbuf) {
+widget_send_update (struct widget *widget, DBusGProxy *properties_proxy, char *dbus_path) {
gdouble percentage;
guint state;
gint64 time_to_empty64, time_to_full64;
- proxy_double_value(&percentage, properties_proxy, pathbuf, "Percentage");
- proxy_uint_value(&state, properties_proxy, pathbuf, "State");
- proxy_int64_value(&time_to_empty64, properties_proxy, pathbuf, "TimeToEmpty");
- proxy_int64_value(&time_to_full64, properties_proxy, pathbuf, "TimeToFull");
+ proxy_double_value(&percentage, properties_proxy, dbus_path, "Percentage");
+ proxy_uint_value(&state, properties_proxy, dbus_path, "State");
+ proxy_int64_value(&time_to_empty64, properties_proxy, dbus_path, "TimeToEmpty");
+ proxy_int64_value(&time_to_full64, properties_proxy, dbus_path, "TimeToFull");
/* jansson fails with 64-bit integers, but the time left on the battery
should fit in a 32-bit integer */
@@ -38,7 +38,10 @@ widget_send_update (struct widget *widget, DBusGProxy *properties_proxy, char *p
static void
widget_cleanup (void *arg) {
LOG_INFO("widget cleanup: battery");
- DBusGProxy **proxy_ref = arg;
+ gpointer *data = (gpointer *)arg;
+
+ DBusGProxy **proxy_ref = (DBusGProxy **)data[0];
+ char *tmp = (char *)data[1];
if (proxy_ref[0] != NULL) {
g_object_unref(proxy_ref[0]);
@@ -46,6 +49,7 @@ widget_cleanup (void *arg) {
if (proxy_ref[1] != NULL) {
g_object_unref(proxy_ref[1]);
}
+ free(tmp);
}
void*
@@ -56,11 +60,15 @@ widget_init (struct widget *widget) {
DBusGConnection *conn;
DBusGProxy *proxy;
DBusGProxy *properties_proxy;
- char pathbuf[128];
+ char *dbus_path, *dbus_path_template = "/org/freedesktop/UPower/devices/battery_%s";
+ int dbus_path_length = 0;
GError *error = NULL;
conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
- sprintf(pathbuf, "/org/freedesktop/UPower/devices/battery_%s", config.name);
+
+ dbus_path_length = snprintf(NULL, 0, dbus_path_template, config.name);
+ dbus_path = malloc(dbus_path_length + 1);
+ snprintf(dbus_path, dbus_path_length + 1, dbus_path_template, config.name);
if (conn == NULL) {
LOG_ERR("dbus: failed to open connection to bus: %s\n", error->message);
@@ -71,9 +79,10 @@ widget_init (struct widget *widget) {
if ((proxy = dbus_g_proxy_new_for_name(conn,
"org.freedesktop.UPower",
- pathbuf,
+ dbus_path,
"org.freedesktop.UPower.Device.Properties")) == NULL) {
LOG_ERR("dbus: failed to create proxy object");
+ free(dbus_path);
return 0;
}
@@ -88,16 +97,20 @@ widget_init (struct widget *widget) {
}
unsigned int state;
- if (!proxy_uint_value(&state, properties_proxy, pathbuf, "State")) {
+ if (!proxy_uint_value(&state, properties_proxy, dbus_path, "State")) {
LOG_ERR("dbus: invalid battery");
return 0;
}
DBusGProxy *proxy_ptr[2] = { proxy, properties_proxy };
- pthread_cleanup_push(widget_cleanup, proxy_ptr);
+ gpointer *data = malloc(sizeof(gpointer) * 2);
+ data[0] = (gpointer)proxy_ptr;
+ data[1] = (gpointer)dbus_path;
+
+ pthread_cleanup_push(widget_cleanup, data);
for (;;) {
- widget_send_update(widget, properties_proxy, pathbuf);
+ widget_send_update(widget, properties_proxy, dbus_path);
sleep(20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment