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 +- tests/test-device.cc | 100 +++++++++++++++++++++++-------------------- tests/test-notify.cc | 9 ++-- 7 files changed, 129 insertions(+), 64 deletions(-) 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 */ diff --git a/tests/test-device.cc b/tests/test-device.cc index 12f89eb..bbe4f31 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -205,7 +205,8 @@ TEST_F(DeviceTest, New) UP_DEVICE_KIND_BATTERY, 50.0, UP_DEVICE_STATE_CHARGING, - 30); + 30, + TRUE); ASSERT_TRUE (device != NULL); ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(device)); ASSERT_EQ (UP_DEVICE_KIND_BATTERY, indicator_power_device_get_kind(device)); @@ -213,6 +214,7 @@ TEST_F(DeviceTest, New) 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)); + ASSERT_TRUE (indicator_power_device_get_power_supply(device)); // cleanup g_object_unref (device); @@ -220,13 +222,14 @@ TEST_F(DeviceTest, New) TEST_F(DeviceTest, NewFromVariant) { - auto variant = g_variant_new("(susdut)", + auto variant = g_variant_new("(susdutb)", "/object/path", guint32(UP_DEVICE_KIND_BATTERY), "icon", 50.0, guint32(UP_DEVICE_STATE_CHARGING), - guint64(30)); + guint64(30), + TRUE); IndicatorPowerDevice * device = indicator_power_device_new_from_variant (variant); ASSERT_TRUE (variant != NULL); ASSERT_TRUE (device != NULL); @@ -236,6 +239,7 @@ TEST_F(DeviceTest, NewFromVariant) 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)); + ASSERT_TRUE (indicator_power_device_get_power_supply(device)); // cleanup g_object_unref (device); @@ -810,7 +814,8 @@ namespace << ' ' << state2str(indicator_power_device_get_state(device)) << ' ' << indicator_power_device_get_time(device)<<'m' << ' ' << int(ceil(indicator_power_device_get_percentage(device)))<<'%' - << ' ' << (path ? path : "nopath"); + << ' ' << (path ? path : "nopath") + << ' ' << (indicator_power_device_get_power_supply(device) ? "1" : "0"); return o.str(); } @@ -818,13 +823,14 @@ namespace IndicatorPowerDevice* str2device(const std::string& str) { auto tokens = g_strsplit(str.c_str(), " ", 0); - g_assert(5u == g_strv_length(tokens)); + g_assert(6u == g_strv_length(tokens)); const auto kind = str2kind(tokens[0]); const auto state = str2state(tokens[1]); const time_t time = atoi(tokens[2]); const double pct = strtod(tokens[3],nullptr); const char* path = !g_strcmp0(tokens[4],"nopath") ? nullptr : tokens[4]; - auto ret = indicator_power_device_new(path, kind, pct, state, time); + const gboolean power_supply = atoi(tokens[5]); + auto ret = indicator_power_device_new(path, kind, pct, state, time, power_supply); g_strfreev(tokens); return ret; } @@ -847,100 +853,100 @@ TEST_F(DeviceTest, ChoosePrimary) } tests[] = { { "one discharging battery", - "battery discharging 10m 60% bat01", - { "battery discharging 10m 60% bat01" } + "battery discharging 10m 60% bat01 1", + { "battery discharging 10m 60% bat01 1" } }, { "merge two discharging batteries", - "battery discharging 20m 70% nopath", - { "battery discharging 10m 60% bat01", "battery discharging 20m 80% bat02" } + "battery discharging 20m 70% nopath 1", + { "battery discharging 10m 60% bat01 1", "battery discharging 20m 80% bat02 1" } }, { "merge two other discharging batteries", - "battery discharging 30m 90% nopath", - { "battery discharging 20m 80% bat01", "battery discharging 30m 100% bat02" } + "battery discharging 30m 90% nopath 1", + { "battery discharging 20m 80% bat01 1", "battery discharging 30m 100% bat02 1" } }, { "merge three discharging batteries", - "battery discharging 30m 80% nopath", - { "battery discharging 10m 60% bat01", "battery discharging 20m 80% bat02", "battery discharging 30m 100% bat03" } + "battery discharging 30m 80% nopath 1", + { "battery discharging 10m 60% bat01 1", "battery discharging 20m 80% bat02 1", "battery discharging 30m 100% bat03 1" } }, { "one charging battery", - "battery charging 10m 60% bat01", - { "battery charging 10m 60% bat01" } + "battery charging 10m 60% bat01 1", + { "battery charging 10m 60% bat01 1" } }, { "merge two charging batteries", - "battery charging 20m 70% nopath", - { "battery charging 10m 60% bat01", "battery charging 20m 80% bat02" } + "battery charging 20m 70% nopath 1", + { "battery charging 10m 60% bat01 1", "battery charging 20m 80% bat02 1" } }, { "merge two other charging batteries", - "battery charging 30m 90% nopath", - { "battery charging 20m 80% bat01", "battery charging 30m 100% bat02" } + "battery charging 30m 90% nopath 1", + { "battery charging 20m 80% bat01 1", "battery charging 30m 100% bat02 1" } }, { "merge three charging batteries", - "battery charging 30m 80% nopath", - { "battery charging 10m 60% bat01", "battery charging 20m 80% bat02", "battery charging 30m 100% bat03" } + "battery charging 30m 80% nopath 1", + { "battery charging 10m 60% bat01 1", "battery charging 20m 80% bat02 1", "battery charging 30m 100% bat03 1" } }, { "one charged battery", - "battery charged 0m 100% bat01", - { "battery charged 0m 100% bat01" } + "battery charged 0m 100% bat01 1", + { "battery charged 0m 100% bat01 1" } }, { "merge one charged, one discharging", - "battery discharging 10m 80% nopath", - { "battery charged 0m 100% bat01", "battery discharging 10m 60% bat02" } + "battery discharging 10m 80% nopath 1", + { "battery charged 0m 100% bat01 1", "battery discharging 10m 60% bat02 1" } }, { "merged one charged, one charging", - "battery charging 10m 80% nopath", - { "battery charged 0m 100% bat01", "battery charging 10m 60% bat02" } + "battery charging 10m 80% nopath 1", + { "battery charged 0m 100% bat01 1", "battery charging 10m 60% bat02 1" } }, { "merged one charged, one charging, one discharging", - "battery discharging 10m 74% nopath", - { "battery charged 0m 100% bat01", "battery charging 10m 60% bat02", "battery discharging 10m 60% bat03" } + "battery discharging 10m 74% nopath 1", + { "battery charged 0m 100% bat01 1", "battery charging 10m 60% bat02 1", "battery discharging 10m 60% bat03 1" } }, { - "one discharging mouse and one discharging battery. pick the one with the least time left", - "battery discharging 10m 60% bat01", - { "battery discharging 10m 60% bat01", "mouse discharging 20m 80% mouse01" } + "one discharging mouse and one discharging battery. ignore mouse because it doesn't supply the power", + "battery discharging 10m 60% bat01 1", + { "battery discharging 10m 60% bat01 1", "mouse discharging 20m 80% mouse01 0" } }, { - "one discharging mouse and a different discharging battery. pick the one with the least time left", - "mouse discharging 20m 80% mouse01", - { "battery discharging 30m 100% bat01", "mouse discharging 20m 80% mouse01" } + "one discharging mouse and a different discharging battery. ignore mouse because it doesn't supply the power", + "battery discharging 30m 100% bat01 1", + { "battery discharging 30m 100% bat01 1", "mouse discharging 20m 80% mouse01 0" } }, { "everything comes before power lines #1", - "battery discharging 10m 60% bat01", - { "battery discharging 10m 60% bat01", "line-power unknown 0m 0% lp01" } + "battery discharging 10m 60% bat01 1", + { "battery discharging 10m 60% bat01 1", "line-power unknown 0m 0% lp01 1" } }, { "everything comes before power lines #2", - "battery charging 10m 60% bat01", - { "battery charging 10m 60% bat01", "line-power unknown 0m 0% lp01" } + "battery charging 10m 60% bat01 1", + { "battery charging 10m 60% bat01 1", "line-power unknown 0m 0% lp01 1" } }, { - "everything comes before power lines #2", - "mouse discharging 20m 80% mouse01", - { "mouse discharging 20m 80% mouse01", "line-power unknown 0m 0% lp01" } + "everything comes before power lines #3 except that the mouse doesn't supply the power", + "line-power unknown 0m 0% lp01 1", + { "mouse discharging 20m 80% mouse01 0", "line-power unknown 0m 0% lp01 1" } }, { // https://bugs.launchpad.net/ubuntu/+source/indicator-power/+bug/1470080/comments/10 "don't select a device with unknown state when we have another device with a known state...", - "battery charged 0m 100% bat01", - { "battery charged 0m 100% bat01", "phone unknown 0m 61% phone01" } + "battery charged 0m 100% bat01 1", + { "battery charged 0m 100% bat01 1", "phone unknown 0m 61% phone01 1" } }, { // https://bugs.launchpad.net/ubuntu/+source/indicator-power/+bug/1470080/comments/10 "...but do select the unknown state device if nothing else is available", - "phone unknown 0m 61% phone01", - { "phone unknown 0m 61% phone01" } + "phone unknown 0m 61% phone01 1", + { "phone unknown 0m 61% phone01 1" } } }; diff --git a/tests/test-notify.cc b/tests/test-notify.cc index acab34f..f8a5517 100644 --- a/tests/test-notify.cc +++ b/tests/test-notify.cc @@ -191,7 +191,8 @@ TEST_F(NotifyFixture, PercentageToLevel) UP_DEVICE_KIND_BATTERY, 50.0, UP_DEVICE_STATE_DISCHARGING, - 30); + 30, + TRUE); // confirm that the power levels trigger at the right percentages for (int i=100; i>=0; --i) @@ -269,7 +270,8 @@ TEST_F(NotifyFixture, LevelsDuringBatteryDrain) UP_DEVICE_KIND_BATTERY, 50.0, UP_DEVICE_STATE_DISCHARGING, - 30); + 30, + TRUE); // set up a notifier and give it the battery so changing the battery's // charge should show up on the bus. @@ -341,7 +343,8 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) UP_DEVICE_KIND_BATTERY, percent_low + 1.0, UP_DEVICE_STATE_DISCHARGING, - 30); + 30, + TRUE); // set up a notifier and give it the battery so changing the battery's // charge should show up on the bus. -- cgit v1.2.3