From d0fc63394c52933c8380fe56b9ea2aebe7791f19 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 7 Sep 2014 23:56:10 -0500 Subject: silence warnings generated by clang -Weverything --- src/notifier.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src') diff --git a/src/notifier.c b/src/notifier.c index 81cd6f1..9835d4f 100644 --- a/src/notifier.c +++ b/src/notifier.c @@ -25,8 +25,6 @@ #include -#define HINT_INTERACTIVE "x-canonical-switch-to-application" - typedef enum { POWER_LEVEL_CRITICAL, @@ -99,7 +97,7 @@ power_level_to_dbus_string (const PowerLevel power_level) } } -PowerLevel +static PowerLevel get_battery_power_level (IndicatorPowerDevice * battery) { static const double percent_critical = 2.0; @@ -178,7 +176,6 @@ notification_show(IndicatorPowerNotifier * self) body = g_strdup_printf(_("%.0f%% charge remaining"), pct); nn = notify_notification_new(_("Battery Low"), body, NULL); g_free (body); - /*notify_notification_set_hint(nn, HINT_INTERACTIVE, g_variant_new_boolean(TRUE));*/ /* if we can show it, keep it */ error = NULL; -- cgit v1.2.3 From 26e4c859e957efb0831c70fc69323da6ffda4005 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 7 Sep 2014 23:57:04 -0500 Subject: add upower 0.99 support --- src/CMakeLists.txt | 4 - src/device-provider-upower.c | 388 +++++++++++++++++++++++++---------------- src/org.freedesktop.UPower.xml | 43 ----- 3 files changed, 239 insertions(+), 196 deletions(-) delete mode 100644 src/org.freedesktop.UPower.xml (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7a4a297..66ea239 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -16,10 +16,6 @@ set(SERVICE_MANUAL_SOURCES # generated sources include(GdbusCodegen) set(SERVICE_GENERATED_SOURCES) -add_gdbus_codegen_with_namespace(SERVICE_GENERATED_SOURCES dbus-upower - org.freedesktop - Dbus - ${CMAKE_CURRENT_SOURCE_DIR}/org.freedesktop.UPower.xml) add_gdbus_codegen_with_namespace(SERVICE_GENERATED_SOURCES dbus-battery com.canonical.indicator.power Dbus diff --git a/src/device-provider-upower.c b/src/device-provider-upower.c index 400a060..9a796e3 100644 --- a/src/device-provider-upower.c +++ b/src/device-provider-upower.c @@ -17,13 +17,16 @@ * with this program. If not, see . */ -#include "dbus-upower.h" #include "device.h" #include "device-provider.h" #include "device-provider-upower.h" #define BUS_NAME "org.freedesktop.UPower" -#define BUS_PATH "/org/freedesktop/UPower" + +#define MGR_IFACE "org.freedesktop.UPower" +#define MGR_PATH "/org/freedesktop/UPower" + +#define DISPLAY_DEVICE_PATH "/org/freedesktop/UPower/devices/DisplayDevice" /*** **** private struct @@ -33,7 +36,6 @@ struct _IndicatorPowerDeviceProviderUPowerPriv { GDBusConnection * bus; - DbusUPower * upower_proxy; GHashTable * devices; /* dbus object path --> IndicatorPowerDevice */ GCancellable * cancellable; @@ -42,6 +44,10 @@ struct _IndicatorPowerDeviceProviderUPowerPriv /* when this timer fires, the queued_paths will be refreshed */ guint queued_paths_timer; + + GSList* subscriptions; + + guint watch_tag; }; typedef IndicatorPowerDeviceProviderUPowerPriv priv_t; @@ -152,42 +158,44 @@ update_device_from_object_path (IndicatorPowerDeviceProviderUPower * self, priv_t * p = self->priv; struct device_get_all_data * data; + /* Symbolic composite item for indicator display. + * Sounds great but let's ignore it so that we'll have + * consistent behavior on both upower<0.99 and >=0.99. + * FIXME: Revisit the decision when we drop upower<0.99. */ + if (!g_strcmp0(path, DISPLAY_DEVICE_PATH)) + return; + data = g_slice_new (struct device_get_all_data); data->path = g_strdup (path); data->self = self; - g_dbus_connection_call (p->bus, - BUS_NAME, - path, - "org.freedesktop.DBus.Properties", - "GetAll", - g_variant_new ("(s)", "org.freedesktop.UPower.Device"), - G_VARIANT_TYPE("(a{sv})"), - G_DBUS_CALL_FLAGS_NO_AUTO_START, - -1, /* default timeout */ - p->cancellable, - on_device_properties_ready, - data); + g_dbus_connection_call(p->bus, + BUS_NAME, + path, + "org.freedesktop.DBus.Properties", + "GetAll", + g_variant_new ("(s)", "org.freedesktop.UPower.Device"), + G_VARIANT_TYPE("(a{sv})"), + G_DBUS_CALL_FLAGS_NO_AUTO_START, + -1, /* default timeout */ + p->cancellable, + on_device_properties_ready, + data); } /* - * UPower doesn't seem to be sending PropertyChanged signals. - * - * Instead, it's got a DIY mechanism for notification: a DeviceChanged signal - * that doesn't tell us which property changed, so to refresh we need to - * rebuild all the properties with a GetAll() call. + * UPower 0.99 added proper PropertyChanged signals, but before that + * it MGR_IFACE emitted a DeviceChanged signal which doesn't tell which + * property changed, so all properties had to get refreshed w/GetAll(). * - * To make things worse, these DeviceChanged signals come fast and furious - * in common situations like disconnecting a power cable. - * - * This code tries to reduce bus traffic by adding a timer to wait a small bit - * before rebuilding our proxy's properties. This helps to fold multiple - * DeviceChanged events into a single rebuild. + * Changes tend to come in small bursts when cables are connected or + * disconnected, so this timer tries to fold them together by waiting + * a small bit before making calling GetAll(). */ /* rebuild all the proxies listed in our queued_paths hashset */ static gboolean -on_queued_paths_timer (gpointer gself) +on_queued_paths_timer(gpointer gself) { gpointer path; GHashTableIter iter; @@ -226,155 +234,233 @@ refresh_device_soon (IndicatorPowerDeviceProviderUPower * self, ***/ static void -on_upower_device_enumerations_ready (GObject * proxy, - GAsyncResult * res, - gpointer gself) +on_enumerate_devices_response(GObject * bus, + GAsyncResult * res, + gpointer gself) { - GError * err; - char ** object_paths; - - err = NULL; - dbus_upower_call_enumerate_devices_finish (DBUS_UPOWER(proxy), - &object_paths, - res, - &err); + GError* error; + GVariant* v; - if (err != NULL) + error = NULL; + v = g_dbus_connection_call_finish(G_DBUS_CONNECTION(bus), res, &error); + if (v == NULL) { - g_warning ("Unable to get UPower devices: %s", err->message); - g_error_free (err); + if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning ("Unable to enumerate UPower devices: %s", error->message); + g_error_free (error); } - else + else if (g_variant_is_of_type(v, G_VARIANT_TYPE("(ao)"))) { - guint i; + GVariant* ao; + GVariantIter iter; + const gchar* path = NULL; - for (i=0; object_paths && object_paths[i]; i++) - refresh_device_soon (gself, object_paths[i]); + ao = g_variant_get_child_value(v, 0); + g_variant_iter_init(&iter, ao); + while(g_variant_iter_loop(&iter, "o", &path)) + refresh_device_soon (gself, path); - g_strfreev (object_paths); + g_variant_unref(ao); + g_variant_unref(v); } } static void -on_upower_device_changed (DbusUPower * unused G_GNUC_UNUSED, - const char * object_path, - gpointer gself) -{ - refresh_device_soon (gself, object_path); -} - -static void -on_upower_device_added (DbusUPower * unused G_GNUC_UNUSED, - const char * object_path, - gpointer gself) +on_device_properties_changed(GDBusConnection * connection G_GNUC_UNUSED, + const gchar * sender_name G_GNUC_UNUSED, + const gchar * object_path, + const gchar * interface_name G_GNUC_UNUSED, + const gchar * signal_name G_GNUC_UNUSED, + GVariant * parameters, + gpointer gself) { - refresh_device_soon (gself, object_path); -} + IndicatorPowerDeviceProviderUPower* self; + priv_t* p; + IndicatorPowerDevice* device; -static void -on_upower_device_removed (DbusUPower * unused G_GNUC_UNUSED, - const char * object_path, - gpointer gself) -{ - IndicatorPowerDeviceProviderUPower * self; + self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); + p = self->priv; - self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER (gself); - g_hash_table_remove (self->priv->devices, object_path); - g_hash_table_remove (self->priv->queued_paths, object_path); + device = g_hash_table_lookup(p->devices, object_path); + if (device == NULL) + { + refresh_device_soon (self, object_path); + } + else if ((parameters != NULL) && g_variant_n_children(parameters)>=2) + { + gboolean changed = FALSE; + GVariant* dict; + GVariantIter iter; + gchar* key; + GVariant* value; + + dict = g_variant_get_child_value(parameters, 1); + g_variant_iter_init(&iter, dict); + while (g_variant_iter_next(&iter, "{sv}", &key, &value)) + { + if (!g_strcmp0(key, "TimeToFull") || !g_strcmp0(key, "TimeToEmpty")) + { + const gint64 i = g_variant_get_int64(value); + g_object_set(device, INDICATOR_POWER_DEVICE_TIME, (guint64)i, NULL); + changed = TRUE; + } + else if (!g_strcmp0(key, "Percentage")) + { + const gdouble d = g_variant_get_double(value); + g_object_set(device, INDICATOR_POWER_DEVICE_PERCENTAGE, d, NULL); + changed = TRUE; + } + else if (!g_strcmp0(key, "Type")) + { + const guint32 u = g_variant_get_uint32(value); + g_object_set(device, INDICATOR_POWER_DEVICE_KIND, (gint)u, NULL); + changed = TRUE; + } + else if (!g_strcmp0(key, "State")) + { + const guint32 u = g_variant_get_uint32(value); + g_object_set (device, INDICATOR_POWER_DEVICE_STATE, (gint)u, NULL); + changed = TRUE; + } + } + g_variant_unref(dict); - emit_devices_changed (self); + if (changed) + emit_devices_changed(self); + } } -static void -on_upower_resuming (DbusUPower * unused G_GNUC_UNUSED, - gpointer gself) +static const gchar* +get_path_from_nth_child(GVariant* parameters, gsize i) { - IndicatorPowerDeviceProviderUPower * self; - GHashTableIter iter; - gpointer object_path; + const gchar* path = NULL; - self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER (gself); + if ((parameters != NULL) && g_variant_n_children(parameters)>i) + { + GVariant* child = g_variant_get_child_value(parameters, i); + if (g_variant_is_of_type(child, G_VARIANT_TYPE_STRING) || /* UPower >= 0.99 */ + g_variant_is_of_type(child, G_VARIANT_TYPE_OBJECT_PATH)) /* UPower >= 0.99 */ + { + path = g_variant_get_string(child, NULL); + } + g_variant_unref(child); + } - g_debug ("Resumed from hibernate/sleep; queueing all devices for a refresh"); - g_hash_table_iter_init (&iter, self->priv->devices); - while (g_hash_table_iter_next (&iter, &object_path, NULL)) - refresh_device_soon (self, object_path); + return path; } static void -on_upower_proxy_ready (GObject * source G_GNUC_UNUSED, - GAsyncResult * res, - gpointer gself) +on_upower_signal(GDBusConnection * connection G_GNUC_UNUSED, + const gchar * sender_name G_GNUC_UNUSED, + const gchar * object_path G_GNUC_UNUSED, + const gchar * interface_name G_GNUC_UNUSED, + const gchar * signal_name, + GVariant * parameters, + gpointer gself) { - GError * err; - DbusUPower * proxy; + IndicatorPowerDeviceProviderUPower* self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); - err = NULL; - proxy = dbus_upower_proxy_new_finish (res, &err); - if (err != NULL) + if (!g_strcmp0(signal_name, "DeviceAdded")) { - g_warning ("Unable to get UPower proxy: %s", err->message); - g_error_free (err); + refresh_device_soon (self, get_path_from_nth_child(parameters, 0)); } - else + else if (!g_strcmp0(signal_name, "DeviceChanged")) + { + refresh_device_soon (self, get_path_from_nth_child(parameters, 0)); + } + else if (!g_strcmp0(signal_name, "DeviceRemoved")) + { + const char* device_path = get_path_from_nth_child(parameters, 0); + g_hash_table_remove(self->priv->devices, device_path); + g_hash_table_remove(self->priv->queued_paths, device_path); + emit_devices_changed(self); + } + else if (!g_strcmp0(signal_name, "Resuming")) { - IndicatorPowerDeviceProviderUPower * self; - priv_t * p; - - self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER (gself); - p = self->priv; - - p->upower_proxy = proxy; - g_signal_connect (proxy, "resuming", - G_CALLBACK (on_upower_resuming), self); - g_signal_connect (proxy, "device-changed", - G_CALLBACK (on_upower_device_changed), self); - g_signal_connect (proxy, "device-added", - G_CALLBACK (on_upower_device_added), self); - g_signal_connect (proxy, "device-removed", - G_CALLBACK (on_upower_device_removed), self); - - dbus_upower_call_enumerate_devices (p->upower_proxy, - p->cancellable, - on_upower_device_enumerations_ready, - self); + GHashTableIter iter; + gpointer device_path = NULL; + g_debug("Resumed from hibernate/sleep; queueing all devices for a refresh"); + g_hash_table_iter_init (&iter, self->priv->devices); + while (g_hash_table_iter_next (&iter, &device_path, NULL)) + refresh_device_soon (self, device_path); } } static void -on_bus_ready (GObject * source_object G_GNUC_UNUSED, - GAsyncResult * res, - gpointer gself) +on_bus_name_appeared(GDBusConnection * bus, + const gchar * name G_GNUC_UNUSED, + const gchar * name_owner, + gpointer gself) { - GError * error; - GDBusConnection * tmp; + IndicatorPowerDeviceProviderUPower* self; + priv_t* p; + guint tag; - error = NULL; - tmp = g_bus_get_finish (res, &error); - if (error != NULL) - { - if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning ("Error acquiring bus: %s", error->message); - g_error_free (error); - } - else - { - IndicatorPowerDeviceProviderUPower * self; - priv_t * p; + self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); + p = self->priv; + p->bus = G_DBUS_CONNECTION(g_object_ref(bus)); + + tag = g_dbus_connection_signal_subscribe(p->bus, + name_owner, + MGR_IFACE, + NULL /*signal_name*/, + MGR_PATH, + NULL /*arg0*/, + G_DBUS_SIGNAL_FLAGS_NONE, + on_upower_signal, + self, + NULL); + p->subscriptions = g_slist_prepend(p->subscriptions, GUINT_TO_POINTER(tag)); + + tag = g_dbus_connection_signal_subscribe(p->bus, + name_owner, + "org.freedesktop.DBus.Properties", + "PropertiesChanged", + NULL /*object_path*/, + "org.freedesktop.UPower.Device", /*arg0*/ + G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE, + on_device_properties_changed, + self, + NULL); + p->subscriptions = g_slist_prepend(p->subscriptions, GUINT_TO_POINTER(tag)); + + g_hash_table_remove_all(p->devices); + g_hash_table_remove_all(p->queued_paths); + g_dbus_connection_call(p->bus, + BUS_NAME, + MGR_PATH, + MGR_IFACE, + "EnumerateDevices", + NULL, + G_VARIANT_TYPE("(ao)"), + G_DBUS_CALL_FLAGS_NO_AUTO_START, + -1, /* default timeout */ + p->cancellable, + on_enumerate_devices_response, + self); +} - self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER (gself); - p = self->priv; +static void +clear_signal_subscriptions(IndicatorPowerDeviceProviderUPower* self) +{ + GSList* l; + priv_t* p = self->priv; + for (l=p->subscriptions; l!=NULL; l=l->next) + g_dbus_connection_signal_unsubscribe(p->bus, GPOINTER_TO_UINT(l->data)); + g_slist_free(p->subscriptions); + p->subscriptions = NULL; +} - p->bus = tmp; +static void +on_bus_name_vanished(GDBusConnection * connection G_GNUC_UNUSED, + const gchar * name G_GNUC_UNUSED, + gpointer gself) +{ + IndicatorPowerDeviceProviderUPower * self; - dbus_upower_proxy_new (p->bus, - G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES, - BUS_NAME, - BUS_PATH, - p->cancellable, - on_upower_proxy_ready, - self); - } + self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); + clear_signal_subscriptions(self); + g_clear_object(&self->priv->bus); } /*** @@ -382,13 +468,12 @@ on_bus_ready (GObject * source_object G_GNUC_UNUSED, ***/ static GList * -my_get_devices (IndicatorPowerDeviceProvider * provider) +my_get_devices(IndicatorPowerDeviceProvider * provider) { GList * devices; IndicatorPowerDeviceProviderUPower * self; self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(provider); - devices = g_hash_table_get_values (self->priv->devices); g_list_foreach (devices, (GFunc)g_object_ref, NULL); return devices; @@ -421,16 +506,18 @@ my_dispose (GObject * o) p->queued_paths_timer = 0; } - if (p->upower_proxy != NULL) + clear_signal_subscriptions(self); + + if (p->watch_tag != 0) { - g_signal_handlers_disconnect_by_data (p->upower_proxy, self); + g_bus_unwatch_name(p->watch_tag); - g_clear_object (&p->upower_proxy); + p->watch_tag = 0; } g_hash_table_remove_all (p->devices); - g_clear_object (&p->bus); + g_clear_object(&p->bus); G_OBJECT_CLASS (indicator_power_device_provider_upower_parent_class)->dispose (o); } @@ -495,10 +582,13 @@ indicator_power_device_provider_upower_init (IndicatorPowerDeviceProviderUPower g_free, NULL); - g_bus_get (G_BUS_TYPE_SYSTEM, - p->cancellable, - on_bus_ready, - self); + p->watch_tag = g_bus_watch_name(G_BUS_TYPE_SYSTEM, + BUS_NAME, + G_BUS_NAME_WATCHER_FLAGS_NONE, + on_bus_name_appeared, + on_bus_name_vanished, + self, + NULL); } /*** diff --git a/src/org.freedesktop.UPower.xml b/src/org.freedesktop.UPower.xml deleted file mode 100644 index 7b73583..0000000 --- a/src/org.freedesktop.UPower.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file -- cgit v1.2.3 From cede6ed3caa5b6e55a39ae4d3098479f8981ec64 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 8 Sep 2014 08:13:43 -0500 Subject: in device-provider-upower, use glib's G_ADD_PRIVATE() macro --- src/CMakeLists.txt | 1 + src/device-provider-upower.c | 64 ++++++++++++++++++++++++-------------------- src/device-provider-upower.h | 4 --- 3 files changed, 36 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 66ea239..9b3d815 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -27,6 +27,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) # add warnings/coverage info on handwritten files # but not the autogenerated ones... set(C_WARNING_ARGS "${C_WARNING_ARGS} -Wno-bad-function-cast") # g_clear_object() +set(C_WARNING_ARGS "${C_WARNING_ARGS} -Wno-used-but-marked-unused") # G_ADD_PRIVATE set(C_WARNING_ARGS "${C_WARNING_ARGS} -Wno-disabled-macro-expansion") # G_DEFINE_TYPE set(C_WARNING_ARGS "${C_WARNING_ARGS} -Wno-assign-enum") # GParamFlags set(C_WARNING_ARGS "${C_WARNING_ARGS} -Wno-switch-enum") diff --git a/src/device-provider-upower.c b/src/device-provider-upower.c index 9a796e3..1b1bc66 100644 --- a/src/device-provider-upower.c +++ b/src/device-provider-upower.c @@ -32,7 +32,7 @@ **** private struct ***/ -struct _IndicatorPowerDeviceProviderUPowerPriv +typedef struct { GDBusConnection * bus; @@ -48,9 +48,13 @@ struct _IndicatorPowerDeviceProviderUPowerPriv GSList* subscriptions; guint watch_tag; -}; +} +IndicatorPowerDeviceProviderUPowerPrivate; + +typedef IndicatorPowerDeviceProviderUPowerPrivate priv_t; + +#define get_priv(o) ((priv_t*)indicator_power_device_provider_upower_get_instance_private(o)) -typedef IndicatorPowerDeviceProviderUPowerPriv priv_t; /*** **** GObject boilerplate @@ -63,6 +67,7 @@ G_DEFINE_TYPE_WITH_CODE ( IndicatorPowerDeviceProviderUPower, indicator_power_device_provider_upower, G_TYPE_OBJECT, + G_ADD_PRIVATE(IndicatorPowerDeviceProviderUPower) G_IMPLEMENT_INTERFACE (INDICATOR_TYPE_POWER_DEVICE_PROVIDER, indicator_power_device_provider_interface_init)) @@ -108,7 +113,7 @@ on_device_properties_ready (GObject * o, GAsyncResult * res, gpointer gdata) gint64 time_to_full = 0; gint64 time; IndicatorPowerDevice * device; - IndicatorPowerDeviceProviderUPowerPriv * p = data->self->priv; + priv_t * p = get_priv(data->self); GVariant * dict = g_variant_get_child_value (response, 0); g_variant_lookup (dict, "Type", "u", &kind); @@ -155,7 +160,7 @@ static void update_device_from_object_path (IndicatorPowerDeviceProviderUPower * self, const char * path) { - priv_t * p = self->priv; + priv_t * p = get_priv(self); struct device_get_all_data * data; /* Symbolic composite item for indicator display. @@ -203,7 +208,7 @@ on_queued_paths_timer(gpointer gself) priv_t * p; self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER (gself); - p = self->priv; + p = get_priv(self); /* create new proxies for all the queued paths */ g_hash_table_iter_init (&iter, p->queued_paths); @@ -221,7 +226,7 @@ static void refresh_device_soon (IndicatorPowerDeviceProviderUPower * self, const char * object_path) { - priv_t * p = self->priv; + priv_t * p = get_priv(self); g_hash_table_add (p->queued_paths, g_strdup (object_path)); @@ -279,7 +284,7 @@ on_device_properties_changed(GDBusConnection * connection G_GNUC_UNUSED, IndicatorPowerDevice* device; self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); - p = self->priv; + p = get_priv(self); device = g_hash_table_lookup(p->devices, object_path); if (device == NULL) @@ -358,7 +363,11 @@ on_upower_signal(GDBusConnection * connection G_GNUC_UNUSED, GVariant * parameters, gpointer gself) { - IndicatorPowerDeviceProviderUPower* self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); + IndicatorPowerDeviceProviderUPower * self; + priv_t * p; + + self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); + p = get_priv(self); if (!g_strcmp0(signal_name, "DeviceAdded")) { @@ -371,8 +380,8 @@ on_upower_signal(GDBusConnection * connection G_GNUC_UNUSED, else if (!g_strcmp0(signal_name, "DeviceRemoved")) { const char* device_path = get_path_from_nth_child(parameters, 0); - g_hash_table_remove(self->priv->devices, device_path); - g_hash_table_remove(self->priv->queued_paths, device_path); + g_hash_table_remove(p->devices, device_path); + g_hash_table_remove(p->queued_paths, device_path); emit_devices_changed(self); } else if (!g_strcmp0(signal_name, "Resuming")) @@ -380,7 +389,7 @@ on_upower_signal(GDBusConnection * connection G_GNUC_UNUSED, GHashTableIter iter; gpointer device_path = NULL; g_debug("Resumed from hibernate/sleep; queueing all devices for a refresh"); - g_hash_table_iter_init (&iter, self->priv->devices); + g_hash_table_iter_init (&iter, p->devices); while (g_hash_table_iter_next (&iter, &device_path, NULL)) refresh_device_soon (self, device_path); } @@ -397,7 +406,7 @@ on_bus_name_appeared(GDBusConnection * bus, guint tag; self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); - p = self->priv; + p = get_priv(self); p->bus = G_DBUS_CONNECTION(g_object_ref(bus)); tag = g_dbus_connection_signal_subscribe(p->bus, @@ -444,7 +453,7 @@ static void clear_signal_subscriptions(IndicatorPowerDeviceProviderUPower* self) { GSList* l; - priv_t* p = self->priv; + priv_t* p = get_priv(self); for (l=p->subscriptions; l!=NULL; l=l->next) g_dbus_connection_signal_unsubscribe(p->bus, GPOINTER_TO_UINT(l->data)); g_slist_free(p->subscriptions); @@ -457,10 +466,13 @@ on_bus_name_vanished(GDBusConnection * connection G_GNUC_UNUSED, gpointer gself) { IndicatorPowerDeviceProviderUPower * self; + priv_t * p; self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); + p = get_priv(self); + clear_signal_subscriptions(self); - g_clear_object(&self->priv->bus); + g_clear_object(&p->bus); } /*** @@ -470,11 +482,14 @@ on_bus_name_vanished(GDBusConnection * connection G_GNUC_UNUSED, static GList * my_get_devices(IndicatorPowerDeviceProvider * provider) { - GList * devices; IndicatorPowerDeviceProviderUPower * self; + priv_t * p; + GList * devices; self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(provider); - devices = g_hash_table_get_values (self->priv->devices); + p = get_priv(self); + + devices = g_hash_table_get_values (p->devices); g_list_foreach (devices, (GFunc)g_object_ref, NULL); return devices; } @@ -490,7 +505,7 @@ my_dispose (GObject * o) priv_t * p; self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(o); - p = self->priv; + p = get_priv(self); if (p->cancellable != NULL) { @@ -529,7 +544,7 @@ my_finalize (GObject * o) priv_t * p; self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(o); - p = self->priv; + p = get_priv(self); g_hash_table_destroy (p->devices); g_hash_table_destroy (p->queued_paths); @@ -548,9 +563,6 @@ indicator_power_device_provider_upower_class_init (IndicatorPowerDeviceProviderU object_class->dispose = my_dispose; object_class->finalize = my_finalize; - - g_type_class_add_private (klass, - sizeof (IndicatorPowerDeviceProviderUPowerPriv)); } static void @@ -562,13 +574,7 @@ indicator_power_device_provider_interface_init (IndicatorPowerDeviceProviderInte static void indicator_power_device_provider_upower_init (IndicatorPowerDeviceProviderUPower * self) { - IndicatorPowerDeviceProviderUPowerPriv * p; - - p = G_TYPE_INSTANCE_GET_PRIVATE (self, - INDICATOR_TYPE_POWER_DEVICE_PROVIDER_UPOWER, - IndicatorPowerDeviceProviderUPowerPriv); - - self->priv = p; + priv_t * p = get_priv(self); p->cancellable = g_cancellable_new (); diff --git a/src/device-provider-upower.h b/src/device-provider-upower.h index 7bfecd9..f385479 100644 --- a/src/device-provider-upower.h +++ b/src/device-provider-upower.h @@ -45,8 +45,6 @@ G_BEGIN_DECLS typedef struct _IndicatorPowerDeviceProviderUPower IndicatorPowerDeviceProviderUPower; -typedef struct _IndicatorPowerDeviceProviderUPowerPriv - IndicatorPowerDeviceProviderUPowerPriv; typedef struct _IndicatorPowerDeviceProviderUPowerClass IndicatorPowerDeviceProviderUPowerClass; @@ -56,8 +54,6 @@ typedef struct _IndicatorPowerDeviceProviderUPowerClass struct _IndicatorPowerDeviceProviderUPower { GObject parent_instance; - - IndicatorPowerDeviceProviderUPowerPriv * priv; }; struct _IndicatorPowerDeviceProviderUPowerClass -- cgit v1.2.3 From 84a32ae9eb5238884c2ca1842fab4aab36b37207 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 8 Sep 2014 08:34:50 -0500 Subject: copyediting. --- src/device-provider-upower.c | 106 +++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 53 deletions(-) (limited to 'src') diff --git a/src/device-provider-upower.c b/src/device-provider-upower.c index 1b1bc66..e17bc40 100644 --- a/src/device-provider-upower.c +++ b/src/device-provider-upower.c @@ -163,10 +163,9 @@ update_device_from_object_path (IndicatorPowerDeviceProviderUPower * self, priv_t * p = get_priv(self); struct device_get_all_data * data; - /* Symbolic composite item for indicator display. - * Sounds great but let's ignore it so that we'll have - * consistent behavior on both upower<0.99 and >=0.99. - * FIXME: Revisit the decision when we drop upower<0.99. */ + /* Symbolic composite item. Nice idea! But its composite rules + differ from Design's so (for now) don't use it. + https://wiki.ubuntu.com/Power#Handling_multiple_batteries */ if (!g_strcmp0(path, DISPLAY_DEVICE_PATH)) return; @@ -190,27 +189,26 @@ update_device_from_object_path (IndicatorPowerDeviceProviderUPower * self, /* * UPower 0.99 added proper PropertyChanged signals, but before that - * it MGR_IFACE emitted a DeviceChanged signal which doesn't tell which + * it MGR_IFACE emitted a DeviceChanged signal which didn't tell which * property changed, so all properties had to get refreshed w/GetAll(). * - * Changes tend to come in small bursts when cables are connected or - * disconnected, so this timer tries to fold them together by waiting - * a small bit before making calling GetAll(). + * Changes often come in bursts, so this timer tries to fold them together + * by waiting a small bit before making calling GetAll(). */ -/* rebuild all the proxies listed in our queued_paths hashset */ +/* rebuild all the devices listed in our queued_paths hashset */ static gboolean on_queued_paths_timer(gpointer gself) { - gpointer path; - GHashTableIter iter; IndicatorPowerDeviceProviderUPower * self; priv_t * p; + GHashTableIter iter; + gpointer path; self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER (gself); p = get_priv(self); - /* create new proxies for all the queued paths */ + /* create new devices for all the queued paths */ g_hash_table_iter_init (&iter, p->queued_paths); while (g_hash_table_iter_next (&iter, &path, NULL)) update_device_from_object_path (self, path); @@ -306,25 +304,33 @@ on_device_properties_changed(GDBusConnection * connection G_GNUC_UNUSED, if (!g_strcmp0(key, "TimeToFull") || !g_strcmp0(key, "TimeToEmpty")) { const gint64 i = g_variant_get_int64(value); - g_object_set(device, INDICATOR_POWER_DEVICE_TIME, (guint64)i, NULL); + g_object_set(device, + INDICATOR_POWER_DEVICE_TIME, (guint64)i, + NULL); changed = TRUE; } else if (!g_strcmp0(key, "Percentage")) { const gdouble d = g_variant_get_double(value); - g_object_set(device, INDICATOR_POWER_DEVICE_PERCENTAGE, d, NULL); + g_object_set(device, + INDICATOR_POWER_DEVICE_PERCENTAGE, d, + NULL); changed = TRUE; } else if (!g_strcmp0(key, "Type")) { const guint32 u = g_variant_get_uint32(value); - g_object_set(device, INDICATOR_POWER_DEVICE_KIND, (gint)u, NULL); + g_object_set(device, + INDICATOR_POWER_DEVICE_KIND, (gint)u, + NULL); changed = TRUE; } else if (!g_strcmp0(key, "State")) { const guint32 u = g_variant_get_uint32(value); - g_object_set (device, INDICATOR_POWER_DEVICE_STATE, (gint)u, NULL); + g_object_set(device, + INDICATOR_POWER_DEVICE_STATE, (gint)u, + NULL); changed = TRUE; } } @@ -342,11 +348,11 @@ get_path_from_nth_child(GVariant* parameters, gsize i) if ((parameters != NULL) && g_variant_n_children(parameters)>i) { - GVariant* child = g_variant_get_child_value(parameters, i); - if (g_variant_is_of_type(child, G_VARIANT_TYPE_STRING) || /* UPower >= 0.99 */ - g_variant_is_of_type(child, G_VARIANT_TYPE_OBJECT_PATH)) /* UPower >= 0.99 */ + GVariant* v = g_variant_get_child_value(parameters, i); + if (g_variant_is_of_type(v, G_VARIANT_TYPE_STRING) || /* UPower < 0.99 */ + g_variant_is_of_type(v, G_VARIANT_TYPE_OBJECT_PATH)) /* and >= 0.99 */ { - path = g_variant_get_string(child, NULL); + path = g_variant_get_string(v, NULL); } g_variant_unref(child); } @@ -373,10 +379,6 @@ on_upower_signal(GDBusConnection * connection G_GNUC_UNUSED, { refresh_device_soon (self, get_path_from_nth_child(parameters, 0)); } - else if (!g_strcmp0(signal_name, "DeviceChanged")) - { - refresh_device_soon (self, get_path_from_nth_child(parameters, 0)); - } else if (!g_strcmp0(signal_name, "DeviceRemoved")) { const char* device_path = get_path_from_nth_child(parameters, 0); @@ -384,7 +386,11 @@ on_upower_signal(GDBusConnection * connection G_GNUC_UNUSED, g_hash_table_remove(p->queued_paths, device_path); emit_devices_changed(self); } - else if (!g_strcmp0(signal_name, "Resuming")) + else if (!g_strcmp0(signal_name, "DeviceChanged")) /* UPower < 0.99 */ + { + refresh_device_soon (self, get_path_from_nth_child(parameters, 0)); + } + else if (!g_strcmp0(signal_name, "Resuming")) /* UPower < 0.99 */ { GHashTableIter iter; gpointer device_path = NULL; @@ -401,8 +407,8 @@ on_bus_name_appeared(GDBusConnection * bus, const gchar * name_owner, gpointer gself) { - IndicatorPowerDeviceProviderUPower* self; - priv_t* p; + IndicatorPowerDeviceProviderUPower * self; + priv_t * p; guint tag; self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); @@ -449,17 +455,6 @@ on_bus_name_appeared(GDBusConnection * bus, self); } -static void -clear_signal_subscriptions(IndicatorPowerDeviceProviderUPower* self) -{ - GSList* l; - priv_t* p = get_priv(self); - for (l=p->subscriptions; l!=NULL; l=l->next) - g_dbus_connection_signal_unsubscribe(p->bus, GPOINTER_TO_UINT(l->data)); - g_slist_free(p->subscriptions); - p->subscriptions = NULL; -} - static void on_bus_name_vanished(GDBusConnection * connection G_GNUC_UNUSED, const gchar * name G_GNUC_UNUSED, @@ -471,7 +466,13 @@ on_bus_name_vanished(GDBusConnection * connection G_GNUC_UNUSED, self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); p = get_priv(self); - clear_signal_subscriptions(self); + /* clear the subscriptions */ + for (l=p->subscriptions; l!=NULL; l=l->next) + g_dbus_connection_signal_unsubscribe(p->bus, GPOINTER_TO_UINT(l->data)); + g_slist_free(p->subscriptions); + p->subscriptions = NULL; + + /* clear the bus */ g_clear_object(&p->bus); } @@ -521,18 +522,17 @@ my_dispose (GObject * o) p->queued_paths_timer = 0; } - clear_signal_subscriptions(self); - if (p->watch_tag != 0) { g_bus_unwatch_name(p->watch_tag); + on_bus_name_vanished(NULL, NULL, self); p->watch_tag = 0; - } + } g_hash_table_remove_all (p->devices); - g_clear_object(&p->bus); + G_OBJECT_CLASS (indicator_power_device_provider_upower_parent_class)->dispose (o); } @@ -576,17 +576,17 @@ indicator_power_device_provider_upower_init (IndicatorPowerDeviceProviderUPower { priv_t * p = get_priv(self); - p->cancellable = g_cancellable_new (); + p->cancellable = g_cancellable_new(); - p->devices = g_hash_table_new_full (g_str_hash, - g_str_equal, - g_free, - g_object_unref); + p->devices = g_hash_table_new_full(g_str_hash, + g_str_equal, + g_free, + g_object_unref); - p->queued_paths = g_hash_table_new_full (g_str_hash, - g_str_equal, - g_free, - NULL); + p->queued_paths = g_hash_table_new_full(g_str_hash, + g_str_equal, + g_free, + NULL); p->watch_tag = g_bus_watch_name(G_BUS_TYPE_SYSTEM, BUS_NAME, @@ -602,7 +602,7 @@ indicator_power_device_provider_upower_init (IndicatorPowerDeviceProviderUPower ***/ IndicatorPowerDeviceProvider * -indicator_power_device_provider_upower_new (void) +indicator_power_device_provider_upower_new(void) { gpointer o = g_object_new (INDICATOR_TYPE_POWER_DEVICE_PROVIDER_UPOWER, NULL); -- cgit v1.2.3 From 84265a5fcd07fc494739e93fb2430b927ed5b2df Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 8 Sep 2014 09:50:22 -0500 Subject: more copyediting --- src/device-provider-upower.c | 85 ++++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/device-provider-upower.c b/src/device-provider-upower.c index e17bc40..63f78ad 100644 --- a/src/device-provider-upower.c +++ b/src/device-provider-upower.c @@ -35,10 +35,11 @@ typedef struct { GDBusConnection * bus; - - GHashTable * devices; /* dbus object path --> IndicatorPowerDevice */ GCancellable * cancellable; + /* dbus object path --> IndicatorPowerDevice */ + GHashTable * devices; + /* a hashset of paths whose devices need to be refreshed */ GHashTable * queued_paths; @@ -47,7 +48,7 @@ typedef struct GSList* subscriptions; - guint watch_tag; + guint name_tag; } IndicatorPowerDeviceProviderUPowerPrivate; @@ -88,11 +89,11 @@ emit_devices_changed (IndicatorPowerDeviceProviderUPower * self) } static void -on_device_properties_ready (GObject * o, GAsyncResult * res, gpointer gdata) +on_get_all_response (GObject * o, GAsyncResult * res, gpointer gdata) { + struct device_get_all_data * data = gdata; GError * error; GVariant * response; - struct device_get_all_data * data = gdata; error = NULL; response = g_dbus_connection_call_finish (G_DBUS_CONNECTION(o), res, &error); @@ -183,7 +184,7 @@ update_device_from_object_path (IndicatorPowerDeviceProviderUPower * self, G_DBUS_CALL_FLAGS_NO_AUTO_START, -1, /* default timeout */ p->cancellable, - on_device_properties_ready, + on_get_all_response, data); } @@ -254,18 +255,20 @@ on_enumerate_devices_response(GObject * bus, } else if (g_variant_is_of_type(v, G_VARIANT_TYPE("(ao)"))) { - GVariant* ao; + GVariant * ao; GVariantIter iter; - const gchar* path = NULL; + const gchar * path; ao = g_variant_get_child_value(v, 0); g_variant_iter_init(&iter, ao); + path = NULL; while(g_variant_iter_loop(&iter, "o", &path)) refresh_device_soon (gself, path); g_variant_unref(ao); - g_variant_unref(v); } + + g_clear_pointer(&v, g_variant_unref); } static void @@ -285,7 +288,7 @@ on_device_properties_changed(GDBusConnection * connection G_GNUC_UNUSED, p = get_priv(self); device = g_hash_table_lookup(p->devices, object_path); - if (device == NULL) + if (device == NULL) /* unlikely, but let's handle it */ { refresh_device_soon (self, object_path); } @@ -304,10 +307,13 @@ on_device_properties_changed(GDBusConnection * connection G_GNUC_UNUSED, if (!g_strcmp0(key, "TimeToFull") || !g_strcmp0(key, "TimeToEmpty")) { const gint64 i = g_variant_get_int64(value); - g_object_set(device, - INDICATOR_POWER_DEVICE_TIME, (guint64)i, - NULL); - changed = TRUE; + if (i != 0) + { + g_object_set(device, + INDICATOR_POWER_DEVICE_TIME, (guint64)i, + NULL); + changed = TRUE; + } } else if (!g_strcmp0(key, "Percentage")) { @@ -354,7 +360,7 @@ get_path_from_nth_child(GVariant* parameters, gsize i) { path = g_variant_get_string(v, NULL); } - g_variant_unref(child); + g_variant_unref(v); } return path; @@ -401,6 +407,7 @@ on_upower_signal(GDBusConnection * connection G_GNUC_UNUSED, } } +/* start listening for UPower events on the bus */ static void on_bus_name_appeared(GDBusConnection * bus, const gchar * name G_GNUC_UNUSED, @@ -415,6 +422,7 @@ on_bus_name_appeared(GDBusConnection * bus, p = get_priv(self); p->bus = G_DBUS_CONNECTION(g_object_ref(bus)); + /* listen for signals from the boss */ tag = g_dbus_connection_signal_subscribe(p->bus, name_owner, MGR_IFACE, @@ -427,6 +435,7 @@ on_bus_name_appeared(GDBusConnection * bus, NULL); p->subscriptions = g_slist_prepend(p->subscriptions, GUINT_TO_POINTER(tag)); + /* listen for change events from the devices */ tag = g_dbus_connection_signal_subscribe(p->bus, name_owner, "org.freedesktop.DBus.Properties", @@ -439,8 +448,7 @@ on_bus_name_appeared(GDBusConnection * bus, NULL); p->subscriptions = g_slist_prepend(p->subscriptions, GUINT_TO_POINTER(tag)); - g_hash_table_remove_all(p->devices); - g_hash_table_remove_all(p->queued_paths); + /* rebuild our devices list */ g_dbus_connection_call(p->bus, BUS_NAME, MGR_PATH, @@ -462,11 +470,22 @@ on_bus_name_vanished(GDBusConnection * connection G_GNUC_UNUSED, { IndicatorPowerDeviceProviderUPower * self; priv_t * p; + GSList * l; self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); p = get_priv(self); - /* clear the subscriptions */ + /* clear the devices */ + g_hash_table_remove_all(p->devices); + g_hash_table_remove_all(p->queued_paths); + if (p->queued_paths_timer != 0) + { + g_source_remove(p->queued_paths_timer); + p->queued_paths_timer = 0; + } + emit_devices_changed (self); + + /* clear the bus subscriptions */ for (l=p->subscriptions; l!=NULL; l=l->next) g_dbus_connection_signal_unsubscribe(p->bus, GPOINTER_TO_UINT(l->data)); g_slist_free(p->subscriptions); @@ -522,19 +541,15 @@ my_dispose (GObject * o) p->queued_paths_timer = 0; } - if (p->watch_tag != 0) + if (p->name_tag != 0) { - g_bus_unwatch_name(p->watch_tag); + g_bus_unwatch_name(p->name_tag); on_bus_name_vanished(NULL, NULL, self); - p->watch_tag = 0; - } - - g_hash_table_remove_all (p->devices); - - + p->name_tag = 0; + } - G_OBJECT_CLASS (indicator_power_device_provider_upower_parent_class)->dispose (o); + G_OBJECT_CLASS (indicator_power_device_provider_upower_parent_class)->dispose(o); } static void @@ -549,7 +564,7 @@ my_finalize (GObject * o) g_hash_table_destroy (p->devices); g_hash_table_destroy (p->queued_paths); - G_OBJECT_CLASS (indicator_power_device_provider_upower_parent_class)->dispose (o); + G_OBJECT_CLASS (indicator_power_device_provider_upower_parent_class)->finalize (o); } /*** @@ -588,13 +603,13 @@ indicator_power_device_provider_upower_init (IndicatorPowerDeviceProviderUPower g_free, NULL); - p->watch_tag = g_bus_watch_name(G_BUS_TYPE_SYSTEM, - BUS_NAME, - G_BUS_NAME_WATCHER_FLAGS_NONE, - on_bus_name_appeared, - on_bus_name_vanished, - self, - NULL); + p->name_tag = g_bus_watch_name(G_BUS_TYPE_SYSTEM, + BUS_NAME, + G_BUS_NAME_WATCHER_FLAGS_NONE, + on_bus_name_appeared, + on_bus_name_vanished, + self, + NULL); } /*** -- cgit v1.2.3 From 0c312c160b796c6c724c26e8a1063843c23b9ba3 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 8 Sep 2014 16:14:45 -0500 Subject: drop our gcc -Weverything fixes in notifier.c so that they don't step on the changes in lp-1296431-low-power-snap-decisions --- src/notifier.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/notifier.c b/src/notifier.c index 9835d4f..81cd6f1 100644 --- a/src/notifier.c +++ b/src/notifier.c @@ -25,6 +25,8 @@ #include +#define HINT_INTERACTIVE "x-canonical-switch-to-application" + typedef enum { POWER_LEVEL_CRITICAL, @@ -97,7 +99,7 @@ power_level_to_dbus_string (const PowerLevel power_level) } } -static PowerLevel +PowerLevel get_battery_power_level (IndicatorPowerDevice * battery) { static const double percent_critical = 2.0; @@ -176,6 +178,7 @@ notification_show(IndicatorPowerNotifier * self) body = g_strdup_printf(_("%.0f%% charge remaining"), pct); nn = notify_notification_new(_("Battery Low"), body, NULL); g_free (body); + /*notify_notification_set_hint(nn, HINT_INTERACTIVE, g_variant_new_boolean(TRUE));*/ /* if we can show it, keep it */ error = NULL; -- cgit v1.2.3