From bd1a508aeb9e498dd782678da420d5baf639b4c0 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 15 May 2017 15:01:23 +0200 Subject: Make device with power supply has higher sorting priority. (LP: #1100546) --- src/device-provider-upower.c | 6 +++++- src/device.c | 42 +++++++++++++++++++++++++++++++++++++----- src/device.h | 5 ++++- src/service.c | 28 ++++++++++++++++++++++------ src/testing.c | 3 ++- 5 files changed, 70 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/device-provider-upower.c b/src/device-provider-upower.c index 63f78ad..bfee80f 100644 --- a/src/device-provider-upower.c +++ b/src/device-provider-upower.c @@ -113,6 +113,7 @@ on_get_all_response (GObject * o, GAsyncResult * res, gpointer gdata) gint64 time_to_empty = 0; gint64 time_to_full = 0; gint64 time; + gboolean power_supply = FALSE; IndicatorPowerDevice * device; priv_t * p = get_priv(data->self); GVariant * dict = g_variant_get_child_value (response, 0); @@ -122,6 +123,7 @@ on_get_all_response (GObject * o, GAsyncResult * res, gpointer gdata) g_variant_lookup (dict, "Percentage", "d", &percentage); g_variant_lookup (dict, "TimeToEmpty", "x", &time_to_empty); g_variant_lookup (dict, "TimeToFull", "x", &time_to_full); + g_variant_lookup (dict, "PowerSupply", "b", &power_supply); time = time_to_empty ? time_to_empty : time_to_full; if ((device = g_hash_table_lookup (p->devices, data->path))) @@ -131,6 +133,7 @@ on_get_all_response (GObject * o, GAsyncResult * res, gpointer gdata) INDICATOR_POWER_DEVICE_OBJECT_PATH, data->path, INDICATOR_POWER_DEVICE_PERCENTAGE, percentage, INDICATOR_POWER_DEVICE_TIME, time, + INDICATOR_POWER_DEVICE_POWER_SUPPLY, power_supply, NULL); } else @@ -139,7 +142,8 @@ on_get_all_response (GObject * o, GAsyncResult * res, gpointer gdata) kind, percentage, state, - (time_t)time); + (time_t)time, + power_supply); g_hash_table_insert (p->devices, g_strdup (data->path), diff --git a/src/device.c b/src/device.c index aa5bcee..ef17ca6 100644 --- a/src/device.c +++ b/src/device.c @@ -42,6 +42,7 @@ struct _IndicatorPowerDevicePrivate the time-remaining field for this device, or 0 if not applicable. This is used when generating the time-remaining string. */ GTimer * inestimable; + gboolean power_supply; }; /* Properties */ @@ -53,6 +54,7 @@ enum { PROP_OBJECT_PATH, PROP_PERCENTAGE, PROP_TIME, + PROP_POWER_SUPPLY, N_PROPERTIES }; @@ -116,6 +118,12 @@ indicator_power_device_class_init (IndicatorPowerDeviceClass *klass) 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + properties[PROP_POWER_SUPPLY] = g_param_spec_boolean (INDICATOR_POWER_DEVICE_POWER_SUPPLY, + "power supply", + "The device's power supply", + FALSE, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_object_class_install_properties (object_class, N_PROPERTIES, properties); } @@ -132,6 +140,7 @@ indicator_power_device_init (IndicatorPowerDevice *self) priv->object_path = NULL; priv->percentage = 0.0; priv->time = 0; + priv->power_supply = FALSE; self->priv = priv; } @@ -190,6 +199,10 @@ get_property (GObject * o, guint prop_id, GValue * value, GParamSpec * pspec) g_value_set_uint64 (value, (guint64)priv->time); break; + case PROP_POWER_SUPPLY: + g_value_set_boolean (value, priv->power_supply); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID(o, prop_id, pspec); break; @@ -225,6 +238,10 @@ set_property (GObject * o, guint prop_id, const GValue * value, GParamSpec * psp p->time = (time_t) g_value_get_uint64(value); break; + case PROP_POWER_SUPPLY: + p->power_supply = g_value_get_boolean (value); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID(o, prop_id, pspec); break; @@ -304,6 +321,16 @@ indicator_power_device_get_time (const IndicatorPowerDevice * device) return device->priv->time; } +gboolean +indicator_power_device_get_power_supply (const IndicatorPowerDevice * device) +{ + /* LCOV_EXCL_START */ + g_return_val_if_fail (INDICATOR_IS_POWER_DEVICE(device), FALSE); + /* LCOV_EXCL_STOP */ + + return device->priv->power_supply; +} + /*** **** **** @@ -867,7 +894,8 @@ indicator_power_device_new (const gchar * object_path, UpDeviceKind kind, gdouble percentage, UpDeviceState state, - time_t timestamp) + time_t timestamp, + gboolean power_supply) { GObject * o = g_object_new (INDICATOR_POWER_DEVICE_TYPE, INDICATOR_POWER_DEVICE_KIND, kind, @@ -875,6 +903,7 @@ indicator_power_device_new (const gchar * object_path, INDICATOR_POWER_DEVICE_OBJECT_PATH, object_path, INDICATOR_POWER_DEVICE_PERCENTAGE, percentage, INDICATOR_POWER_DEVICE_TIME, (guint64)timestamp, + INDICATOR_POWER_DEVICE_POWER_SUPPLY, power_supply, NULL); return INDICATOR_POWER_DEVICE(o); } @@ -882,7 +911,7 @@ indicator_power_device_new (const gchar * object_path, IndicatorPowerDevice * indicator_power_device_new_from_variant (GVariant * v) { - g_return_val_if_fail (g_variant_is_of_type (v, G_VARIANT_TYPE("(susdut)")), NULL); + g_return_val_if_fail (g_variant_is_of_type (v, G_VARIANT_TYPE("(susdutb)")), NULL); UpDeviceKind kind = UP_DEVICE_KIND_UNKNOWN; UpDeviceState state = UP_DEVICE_STATE_UNKNOWN; @@ -890,18 +919,21 @@ indicator_power_device_new_from_variant (GVariant * v) const gchar * object_path = NULL; gdouble percentage = 0; guint64 time = 0; + gboolean power_supply = FALSE; - g_variant_get (v, "(&su&sdut)", + g_variant_get (v, "(&su&sdutb)", &object_path, &kind, &icon, &percentage, &state, - &time); + &time, + &power_supply); return indicator_power_device_new (object_path, kind, percentage, state, - (time_t)time); + (time_t)time, + power_supply); } diff --git a/src/device.h b/src/device.h index d867707..fad959a 100644 --- a/src/device.h +++ b/src/device.h @@ -44,6 +44,7 @@ typedef struct _IndicatorPowerDevicePrivate IndicatorPowerDevicePrivate; #define INDICATOR_POWER_DEVICE_OBJECT_PATH "object-path" #define INDICATOR_POWER_DEVICE_PERCENTAGE "percentage" #define INDICATOR_POWER_DEVICE_TIME "time" +#define INDICATOR_POWER_DEVICE_POWER_SUPPLY "power-supply" typedef enum { @@ -107,7 +108,8 @@ IndicatorPowerDevice* indicator_power_device_new (const gchar * object_path, UpDeviceKind kind, gdouble percentage, UpDeviceState state, - time_t time); + time_t time, + gboolean power_supply); /** * Convenience wrapper around indicator_power_device_new() @@ -121,6 +123,7 @@ UpDeviceState indicator_power_device_get_state (const IndicatorPower const gchar * indicator_power_device_get_object_path (const IndicatorPowerDevice * device); gdouble indicator_power_device_get_percentage (const IndicatorPowerDevice * device); time_t indicator_power_device_get_time (const IndicatorPowerDevice * device); +gboolean indicator_power_device_get_power_supply (const IndicatorPowerDevice * device); GStrv indicator_power_device_get_icon_names (const IndicatorPowerDevice * device); GIcon * indicator_power_device_get_gicon (const IndicatorPowerDevice * device); diff --git a/src/service.c b/src/service.c index 76a0db9..5072765 100644 --- a/src/service.c +++ b/src/service.c @@ -160,11 +160,12 @@ get_device_kind_weight (const IndicatorPowerDevice * device) } /* sort devices from most interesting to least interesting on this criteria: - 1. discharging items from least time remaining until most time remaining - 2. charging items from most time left to charge to least time left to charge - 3. charging items with an unknown time remaining - 4. discharging items with an unknown time remaining - 5. batteries, then non-line power, then line-power */ + 1. device that supplied the power to the system + 2. discharging items from least time remaining until most time remaining + 3. charging items from most time left to charge to least time left to charge + 4. charging items with an unknown time remaining + 5. discharging items with an unknown time remaining + 6. batteries, then non-line power, then line-power */ static gint device_compare_func (gconstpointer ga, gconstpointer gb) { @@ -172,6 +173,8 @@ device_compare_func (gconstpointer ga, gconstpointer gb) int state; const IndicatorPowerDevice * a = ga; const IndicatorPowerDevice * b = gb; + const gboolean a_power_supply = indicator_power_device_get_power_supply (a); + const gboolean b_power_supply = indicator_power_device_get_power_supply (b); const int a_state = indicator_power_device_get_state (a); const int b_state = indicator_power_device_get_state (b); const gdouble a_percentage = indicator_power_device_get_percentage (a); @@ -181,6 +184,18 @@ device_compare_func (gconstpointer ga, gconstpointer gb) ret = 0; + if (!ret && (a_power_supply != b_power_supply)) + { + if (a_power_supply) /* a provides power to the system */ + { + ret = -1; + } + else /* b provides power to the system */ + { + ret = 1; + } + } + state = UP_DEVICE_STATE_DISCHARGING; if (!ret && (((a_state == state) && a_time) || ((b_state == state) && b_time))) @@ -1380,7 +1395,8 @@ create_totalled_battery_device (const GList * devices) UP_DEVICE_KIND_BATTERY, percent, state, - time_left); + time_left, + TRUE); } return device; diff --git a/src/testing.c b/src/testing.c index 2822b7e..abcd239 100644 --- a/src/testing.c +++ b/src/testing.c @@ -301,7 +301,8 @@ indicator_power_testing_init (IndicatorPowerTesting * self) UP_DEVICE_KIND_BATTERY, 50.0, UP_DEVICE_STATE_DISCHARGING, - 60*30); + 60*30, + TRUE); /* Mock Provider */ -- cgit v1.2.3