diff options
-rw-r--r-- | data/CMakeLists.txt | 21 | ||||
-rw-r--r-- | data/indicator-power.conf.in | 8 | ||||
-rw-r--r-- | data/indicator-power.desktop.in | 4 | ||||
-rw-r--r-- | data/indicator-power.upstart.desktop.in | 10 | ||||
-rw-r--r-- | src/CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/device-provider-upower.c | 499 | ||||
-rw-r--r-- | src/device-provider-upower.h | 4 | ||||
-rw-r--r-- | src/org.freedesktop.UPower.xml | 43 | ||||
-rw-r--r-- | tests/glib-fixture.h | 4 | ||||
-rw-r--r-- | tests/indicator-power-service-cmdline-battery.cc | 2 | ||||
-rw-r--r-- | tests/test-device.cc | 58 | ||||
-rw-r--r-- | tests/test-notify.cc | 2 |
12 files changed, 375 insertions, 285 deletions
diff --git a/data/CMakeLists.txt b/data/CMakeLists.txt index 51fc415..0750128 100644 --- a/data/CMakeLists.txt +++ b/data/CMakeLists.txt @@ -55,6 +55,27 @@ install (FILES "${XDG_AUTOSTART_FILE}" DESTINATION "${XDG_AUTOSTART_DIR}") ## +## Upstart XDG Autostart Override +## + +# where to install +set (UPSTART_XDG_AUTOSTART_DIR "${CMAKE_INSTALL_FULL_DATAROOTDIR}/upstart/xdg/autostart") +message (STATUS "${UPSTART_XDG_AUTOSTART_DIR} is the Upstart XDG autostart override dir") + +set (UPSTART_XDG_AUTOSTART_NAME "${CMAKE_PROJECT_NAME}.upstart.desktop") +set (UPSTART_XDG_AUTOSTART_FILE "${CMAKE_CURRENT_BINARY_DIR}/${UPSTART_XDG_AUTOSTART_NAME}") +set (UPSTART_XDG_AUTOSTART_FILE_IN "${CMAKE_CURRENT_SOURCE_DIR}/${UPSTART_XDG_AUTOSTART_NAME}.in") + +# build it +set (pkglibexecdir "${CMAKE_INSTALL_FULL_PKGLIBEXECDIR}") +configure_file ("${UPSTART_XDG_AUTOSTART_FILE_IN}" "${UPSTART_XDG_AUTOSTART_FILE}") + +# install it +install (FILES "${UPSTART_XDG_AUTOSTART_FILE}" + DESTINATION "${UPSTART_XDG_AUTOSTART_DIR}" + RENAME "${XDG_AUTOSTART_NAME}") + +## ## Unity Indicator File ## diff --git a/data/indicator-power.conf.in b/data/indicator-power.conf.in index a2bc7aa..665fb35 100644 --- a/data/indicator-power.conf.in +++ b/data/indicator-power.conf.in @@ -1,11 +1,9 @@ -description "Indicator Power Backend" +description "Indicator Power Service" -# Want to move to indicator-services-[start|end], but that's not all -# there yet. Use the signals that exist today for now. - -start on indicators-loaded or indicator-services-start +start on indicator-services-start stop on desktop-end or indicator-services-end respawn +respawn limit 2 10 exec @pkglibexecdir@/indicator-power-service diff --git a/data/indicator-power.desktop.in b/data/indicator-power.desktop.in index 28025a2..c2fd54c 100644 --- a/data/indicator-power.desktop.in +++ b/data/indicator-power.desktop.in @@ -2,8 +2,8 @@ Type=Application Name=Indicator Power Exec=@pkglibexecdir@/indicator-power-service -NotShowIn=Unity; +OnlyShowIn=Unity;GNOME; NoDisplay=true StartupNotify=false Terminal=false - +AutostartCondition=GNOME3 unless-session gnome diff --git a/data/indicator-power.upstart.desktop.in b/data/indicator-power.upstart.desktop.in new file mode 100644 index 0000000..5f95f8e --- /dev/null +++ b/data/indicator-power.upstart.desktop.in @@ -0,0 +1,10 @@ +[Desktop Entry] +Type=Application +Name=Indicator Power +Exec=@pkglibexecdir@/indicator-power-service +OnlyShowIn=Unity;GNOME; +NoDisplay=true +StartupNotify=false +Terminal=false +AutostartCondition=GNOME3 unless-session gnome +Hidden=true diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7a4a297..9b3d815 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 @@ -31,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 400a060..63f78ad 100644 --- a/src/device-provider-upower.c +++ b/src/device-provider-upower.c @@ -17,34 +17,45 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -#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 ***/ -struct _IndicatorPowerDeviceProviderUPowerPriv +typedef struct { GDBusConnection * bus; - - DbusUPower * upower_proxy; - 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; /* when this timer fires, the queued_paths will be refreshed */ guint queued_paths_timer; -}; -typedef IndicatorPowerDeviceProviderUPowerPriv priv_t; + GSList* subscriptions; + + guint name_tag; +} +IndicatorPowerDeviceProviderUPowerPrivate; + +typedef IndicatorPowerDeviceProviderUPowerPrivate priv_t; + +#define get_priv(o) ((priv_t*)indicator_power_device_provider_upower_get_instance_private(o)) + /*** **** GObject boilerplate @@ -57,6 +68,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)) @@ -77,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); @@ -102,7 +114,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); @@ -149,55 +161,55 @@ 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. 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; + 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_get_all_response, + 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. - * - * To make things worse, these DeviceChanged signals come fast and furious - * in common situations like disconnecting a power cable. + * UPower 0.99 added proper PropertyChanged signals, but before that + * it MGR_IFACE emitted a DeviceChanged signal which didn't tell which + * property changed, so all properties had to get refreshed w/GetAll(). * - * 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 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) +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 = self->priv; + 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); @@ -213,7 +225,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)); @@ -226,155 +238,261 @@ 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; - 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); + path = NULL; + while(g_variant_iter_loop(&iter, "o", &path)) + refresh_device_soon (gself, path); - g_strfreev (object_paths); + g_variant_unref(ao); } -} -static void -on_upower_device_changed (DbusUPower * unused G_GNUC_UNUSED, - const char * object_path, - gpointer gself) -{ - refresh_device_soon (gself, object_path); + g_clear_pointer(&v, g_variant_unref); } 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 = get_priv(self); - 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) /* unlikely, but let's handle it */ + { + 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); + if (i != 0) + { + 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* 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(v, NULL); + } + g_variant_unref(v); + } - 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; + priv_t * p; - err = NULL; - proxy = dbus_upower_proxy_new_finish (res, &err); - if (err != NULL) + self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); + p = get_priv(self); + + 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, "DeviceRemoved")) + { + const char* device_path = get_path_from_nth_child(parameters, 0); + 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, "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 */ { - 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, p->devices); + while (g_hash_table_iter_next (&iter, &device_path, NULL)) + refresh_device_soon (self, device_path); } } +/* start listening for UPower events on the bus */ 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; + + self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); + 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, + 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)); + + /* listen for change events from the devices */ + 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)); + + /* rebuild our devices list */ + 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); +} - error = NULL; - tmp = g_bus_get_finish (res, &error); - if (error != NULL) +static void +on_bus_name_vanished(GDBusConnection * connection G_GNUC_UNUSED, + const gchar * name G_GNUC_UNUSED, + gpointer gself) +{ + IndicatorPowerDeviceProviderUPower * self; + priv_t * p; + GSList * l; + + self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(gself); + p = get_priv(self); + + /* clear the devices */ + g_hash_table_remove_all(p->devices); + g_hash_table_remove_all(p->queued_paths); + if (p->queued_paths_timer != 0) { - if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning ("Error acquiring bus: %s", error->message); - g_error_free (error); + g_source_remove(p->queued_paths_timer); + p->queued_paths_timer = 0; } - else - { - IndicatorPowerDeviceProviderUPower * self; - priv_t * p; - - self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER (gself); - p = self->priv; + emit_devices_changed (self); - p->bus = tmp; + /* 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); + p->subscriptions = NULL; - dbus_upower_proxy_new (p->bus, - G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES, - BUS_NAME, - BUS_PATH, - p->cancellable, - on_upower_proxy_ready, - self); - } + /* clear the bus */ + g_clear_object(&p->bus); } /*** @@ -382,14 +500,16 @@ 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; + priv_t * p; + GList * devices; self = INDICATOR_POWER_DEVICE_PROVIDER_UPOWER(provider); + p = get_priv(self); - devices = g_hash_table_get_values (self->priv->devices); + devices = g_hash_table_get_values (p->devices); g_list_foreach (devices, (GFunc)g_object_ref, NULL); return devices; } @@ -405,7 +525,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) { @@ -421,18 +541,15 @@ my_dispose (GObject * o) p->queued_paths_timer = 0; } - if (p->upower_proxy != NULL) + if (p->name_tag != 0) { - g_signal_handlers_disconnect_by_data (p->upower_proxy, self); + g_bus_unwatch_name(p->name_tag); + on_bus_name_vanished(NULL, NULL, self); - g_clear_object (&p->upower_proxy); + p->name_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); + G_OBJECT_CLASS (indicator_power_device_provider_upower_parent_class)->dispose(o); } static void @@ -442,12 +559,12 @@ 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); - G_OBJECT_CLASS (indicator_power_device_provider_upower_parent_class)->dispose (o); + G_OBJECT_CLASS (indicator_power_device_provider_upower_parent_class)->finalize (o); } /*** @@ -461,9 +578,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 @@ -475,30 +589,27 @@ 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; - - p->cancellable = g_cancellable_new (); - - 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); - - g_bus_get (G_BUS_TYPE_SYSTEM, - p->cancellable, - on_bus_ready, - self); + priv_t * p = get_priv(self); + + p->cancellable = g_cancellable_new(); + + 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->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); } /*** @@ -506,7 +617,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); 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 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 @@ -<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" - "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> -<node> - <interface name="org.freedesktop.UPower"> - <method name="HibernateAllowed"> - <arg name="allowed" type="b" direction="out"/> - </method> - <method name="Hibernate"> - </method> - <method name="SuspendAllowed"> - <arg name="allowed" type="b" direction="out"/> - </method> - <method name="Suspend"> - </method> - <method name="AboutToSleep"> - </method> - <method name="EnumerateDevices"> - <arg name="devices" type="ao" direction="out"/> - </method> - <signal name="Resuming"> - </signal> - <signal name="Sleeping"> - </signal> - <signal name="Changed"> - </signal> - <signal name="DeviceChanged"> - <arg type="s"/> - </signal> - <signal name="DeviceRemoved"> - <arg type="s"/> - </signal> - <signal name="DeviceAdded"> - <arg type="s"/> - </signal> - <property name="LidIsPresent" type="b" access="read"/> - <property name="LidIsClosed" type="b" access="read"/> - <property name="OnLowBattery" type="b" access="read"/> - <property name="OnBattery" type="b" access="read"/> - <property name="CanHibernate" type="b" access="read"/> - <property name="CanSuspend" type="b" access="read"/> - <property name="DaemonVersion" type="s" access="read"/> - </interface> -</node>
\ No newline at end of file diff --git a/tests/glib-fixture.h b/tests/glib-fixture.h index d333ab2..ce834a0 100644 --- a/tests/glib-fixture.h +++ b/tests/glib-fixture.h @@ -64,7 +64,7 @@ class GlibFixture : public ::testing::Test const gchar * message, gpointer self) { - auto tmp = g_strdup_printf ("%s:%d \"%s\"", log_domain, (int)log_level, message); + auto tmp = g_strdup_printf ("%s:%d \"%s\"", log_domain, int(log_level), message); static_cast<GlibFixture*>(self)->log[log_level].push_back(tmp); g_free(tmp); } @@ -101,7 +101,7 @@ class GlibFixture : public ::testing::Test static gboolean wait_for_signal__timeout(gpointer name) { - g_error("%s: timed out waiting for signal '%s'", G_STRLOC, (char*)name); + g_error("%s: timed out waiting for signal '%s'", G_STRLOC, static_cast<char*>(name)); return G_SOURCE_REMOVE; } diff --git a/tests/indicator-power-service-cmdline-battery.cc b/tests/indicator-power-service-cmdline-battery.cc index 29ec87d..50ed2bb 100644 --- a/tests/indicator-power-service-cmdline-battery.cc +++ b/tests/indicator-power-service-cmdline-battery.cc @@ -37,7 +37,7 @@ static void on_name_lost (gpointer instance G_GNUC_UNUSED, gpointer loop) { g_message ("exiting: service couldn't acquire or lost ownership of busname"); - g_main_loop_quit ((GMainLoop*)loop); + g_main_loop_quit (static_cast<GMainLoop*>(loop)); } static IndicatorPowerDevice * battery = nullptr; diff --git a/tests/test-device.cc b/tests/test-device.cc index 85ba237..fbeb1c0 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -172,11 +172,11 @@ TEST_F(DeviceTest, Properties) key = INDICATOR_POWER_DEVICE_PERCENTAGE; g_object_set (o, key, 50.0, NULL); g_object_get (o, key, &d, NULL); - ASSERT_EQ((int)d, 50); + ASSERT_EQ(int(d), 50); // TIME key = INDICATOR_POWER_DEVICE_TIME; - g_object_set (o, key, (guint64)30, NULL); + g_object_set (o, key, guint64(30), NULL); g_object_get (o, key, &u64, NULL); ASSERT_EQ(u64, 30); @@ -193,11 +193,11 @@ TEST_F(DeviceTest, New) 30); ASSERT_TRUE (device != NULL); ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(device)); - ASSERT_EQ (indicator_power_device_get_kind(device), UP_DEVICE_KIND_BATTERY); - ASSERT_EQ (indicator_power_device_get_state(device), UP_DEVICE_STATE_CHARGING); - ASSERT_STREQ (indicator_power_device_get_object_path(device), "/object/path"); - ASSERT_EQ ((int)indicator_power_device_get_percentage(device), 50); - ASSERT_EQ (indicator_power_device_get_time(device), 30); + ASSERT_EQ (UP_DEVICE_KIND_BATTERY, indicator_power_device_get_kind(device)); + ASSERT_EQ (UP_DEVICE_STATE_CHARGING, indicator_power_device_get_state(device)); + ASSERT_STREQ ("/object/path", indicator_power_device_get_object_path(device)); + ASSERT_EQ (50, int(indicator_power_device_get_percentage(device))); + ASSERT_EQ (30, indicator_power_device_get_time(device)); // cleanup g_object_unref (device); @@ -205,22 +205,22 @@ TEST_F(DeviceTest, New) TEST_F(DeviceTest, NewFromVariant) { - GVariant * variant = g_variant_new ("(susdut)", - "/object/path", - (guint32) UP_DEVICE_KIND_BATTERY, - "icon", - (gdouble) 50.0, - (guint32) UP_DEVICE_STATE_CHARGING, - (guint64) 30); + auto variant = g_variant_new("(susdut)", + "/object/path", + guint32(UP_DEVICE_KIND_BATTERY), + "icon", + 50.0, + guint32(UP_DEVICE_STATE_CHARGING), + guint64(30)); IndicatorPowerDevice * device = indicator_power_device_new_from_variant (variant); ASSERT_TRUE (variant != NULL); ASSERT_TRUE (device != NULL); ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(device)); - ASSERT_EQ (indicator_power_device_get_kind(device), UP_DEVICE_KIND_BATTERY); - ASSERT_EQ (indicator_power_device_get_state(device), UP_DEVICE_STATE_CHARGING); - ASSERT_STREQ (indicator_power_device_get_object_path(device), "/object/path"); - ASSERT_EQ ((int)indicator_power_device_get_percentage(device), 50); - ASSERT_EQ (indicator_power_device_get_time(device), 30); + ASSERT_EQ (UP_DEVICE_KIND_BATTERY, indicator_power_device_get_kind(device)); + ASSERT_EQ (UP_DEVICE_STATE_CHARGING, indicator_power_device_get_state(device)); + ASSERT_STREQ ("/object/path", indicator_power_device_get_object_path(device)); + ASSERT_EQ (50, int(indicator_power_device_get_percentage(device))); + ASSERT_EQ (30, indicator_power_device_get_time(device)); // cleanup g_object_unref (device); @@ -407,7 +407,7 @@ TEST_F(DeviceTest, IconNames) g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0, - INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60), + INDICATOR_POWER_DEVICE_TIME, guint64(60*60), NULL); g_string_append_printf (expected, "%s-060;", kind_str); g_string_append_printf (expected, "gpm-%s-060;", kind_str); @@ -420,7 +420,7 @@ TEST_F(DeviceTest, IconNames) g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0, - INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60), + INDICATOR_POWER_DEVICE_TIME, guint64(60*60), NULL); g_string_append_printf (expected, "%s-020;", kind_str); g_string_append_printf (expected, "gpm-%s-020;", kind_str); @@ -433,7 +433,7 @@ TEST_F(DeviceTest, IconNames) g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0, - INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15), + INDICATOR_POWER_DEVICE_TIME, guint64(60*15), NULL); g_string_append_printf (expected, "%s-020;", kind_str); g_string_append_printf (expected, "gpm-%s-020;", kind_str); @@ -446,7 +446,7 @@ TEST_F(DeviceTest, IconNames) g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0, - INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60), + INDICATOR_POWER_DEVICE_TIME, guint64(60*60), NULL); g_string_append_printf (expected, "%s-000;", kind_str); g_string_append_printf (expected, "gpm-%s-000;", kind_str); @@ -459,7 +459,7 @@ TEST_F(DeviceTest, IconNames) g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0, - INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15), + INDICATOR_POWER_DEVICE_TIME, guint64(60*15), NULL); g_string_append_printf (expected, "%s-000;", kind_str); g_string_append_printf (expected, "gpm-%s-000;", kind_str); @@ -495,12 +495,12 @@ TEST_F(DeviceTest, Labels) log_count_ipower_expected++; check_label (NULL, NULL); log_count_ipower_expected += 5; - check_header (NULL, NULL, NULL, NULL, NULL); + check_header (nullptr, nullptr, nullptr, nullptr, nullptr); // bad args: a GObject that isn't a device GObject * o = G_OBJECT(g_cancellable_new()); log_count_ipower_expected++; - check_label ((IndicatorPowerDevice*)o, NULL); + check_label (INDICATOR_POWER_DEVICE(o), nullptr); log_count_ipower_expected += 5; check_header (NULL, NULL, NULL, NULL, NULL); g_object_unref (o); @@ -710,7 +710,7 @@ TEST_F(DeviceTest, ChoosePrimary) std::vector<IndicatorPowerDevice*> devices; for(const auto& desc : descriptions) - devices.push_back(indicator_power_device_new(desc.path, desc.kind, desc.percentage, desc.state, (time_t)desc.time)); + devices.push_back(indicator_power_device_new(desc.path, desc.kind, desc.percentage, desc.state, time_t(desc.time))); const struct { std::vector<unsigned int> device_indices; @@ -756,7 +756,7 @@ TEST_F(DeviceTest, ChoosePrimary) EXPECT_EQ(x.kind, indicator_power_device_get_kind(primary)); EXPECT_EQ(x.state, indicator_power_device_get_state(primary)); EXPECT_EQ(x.time, indicator_power_device_get_time(primary)); - EXPECT_EQ((int)ceil(x.percentage), (int)ceil(indicator_power_device_get_percentage(primary))); + EXPECT_EQ(int(ceil(x.percentage)), int(ceil(indicator_power_device_get_percentage(primary)))); g_object_unref(primary); // reverse the list and repeat the test @@ -767,7 +767,7 @@ TEST_F(DeviceTest, ChoosePrimary) EXPECT_EQ(x.kind, indicator_power_device_get_kind(primary)); EXPECT_EQ(x.state, indicator_power_device_get_state(primary)); EXPECT_EQ(x.time, indicator_power_device_get_time(primary)); - EXPECT_EQ((int)ceil(x.percentage), (int)ceil(indicator_power_device_get_percentage(primary))); + EXPECT_EQ(int(ceil(x.percentage)), int(ceil(indicator_power_device_get_percentage(primary)))); g_object_unref(primary); // cleanup diff --git a/tests/test-notify.cc b/tests/test-notify.cc index b5166a0..d056f3f 100644 --- a/tests/test-notify.cc +++ b/tests/test-notify.cc @@ -131,7 +131,7 @@ protected: bus = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, nullptr); g_dbus_connection_set_exit_on_close(bus, FALSE); - g_object_add_weak_pointer(G_OBJECT(bus), (gpointer *)&bus); + g_object_add_weak_pointer(G_OBJECT(bus), reinterpret_cast<gpointer*>(&bus)); notify_init(APP_NAME); } |