summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBastien Nocera <hadess@hadess.net>2013-10-11 14:29:04 +0200
committerBastien Nocera <hadess@hadess.net>2013-10-14 10:42:56 +0200
commitde749a332b8b659ca9ec33103110cf64e43b5109 (patch)
tree57729bd0480071d063d0028e5897a5b5db10af52 /src
parent68aca3a7e7dbbbeab9d6728b1c3d42fa2fdb1538 (diff)
downloadupower-de749a332b8b659ca9ec33103110cf64e43b5109.tar.gz
daemon: Add WarningLevel device property
Diffstat (limited to 'src')
-rw-r--r--src/org.freedesktop.UPower.Device.xml26
-rw-r--r--src/up-daemon.c31
-rw-r--r--src/up-daemon.h5
-rw-r--r--src/up-device.c47
4 files changed, 109 insertions, 0 deletions
diff --git a/src/org.freedesktop.UPower.Device.xml b/src/org.freedesktop.UPower.Device.xml
index 4379fa0..d806299 100644
--- a/src/org.freedesktop.UPower.Device.xml
+++ b/src/org.freedesktop.UPower.Device.xml
@@ -684,6 +684,32 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
</doc:doc>
</property>
+ <property name="WarningLevel" type="u" access="read">
+ <doc:doc>
+ <doc:description>
+ <doc:para>
+ Warning level of the battery:
+ </doc:para>
+ <doc:list>
+ <doc:item>
+ <doc:term>0</doc:term><doc:definition>Unknown</doc:definition>
+ </doc:item>
+ <doc:item>
+ <doc:term>1</doc:term><doc:definition>None</doc:definition>
+ </doc:item>
+ <doc:item>
+ <doc:term>2</doc:term><doc:definition>Low</doc:definition>
+ </doc:item>
+ <doc:item>
+ <doc:term>3</doc:term><doc:definition>Critical</doc:definition>
+ </doc:item>
+ <doc:item>
+ <doc:term>4</doc:term><doc:definition>Action</doc:definition>
+ </doc:item>
+ </doc:list>
+ </doc:description>
+ </doc:doc>
+ </property>
</interface>
</node>
diff --git a/src/up-daemon.c b/src/up-daemon.c
index c57c797..2da05b9 100644
--- a/src/up-daemon.c
+++ b/src/up-daemon.c
@@ -437,6 +437,37 @@ up_daemon_set_on_low_battery (UpDaemon *daemon, gboolean on_low_battery)
g_object_notify (G_OBJECT (daemon), "on-low-battery");
}
+UpDeviceLevel
+up_daemon_compute_warning_level(UpDaemon *daemon,
+ gboolean power_supply,
+ gdouble percentage,
+ gint64 time_to_empty)
+{
+ gboolean use_percentage = TRUE;
+
+ if (!power_supply || !daemon->priv->use_percentage_for_policy)
+ use_percentage = FALSE;
+
+ if (use_percentage) {
+ if (percentage > daemon->priv->low_percentage)
+ return UP_DEVICE_LEVEL_NONE;
+ if (percentage > daemon->priv->critical_percentage)
+ return UP_DEVICE_LEVEL_LOW;
+ if (percentage > daemon->priv->action_percentage)
+ return UP_DEVICE_LEVEL_CRITICAL;
+ return UP_DEVICE_LEVEL_ACTION;
+ } else {
+ if (time_to_empty > daemon->priv->low_time)
+ return UP_DEVICE_LEVEL_NONE;
+ if (time_to_empty > daemon->priv->critical_time)
+ return UP_DEVICE_LEVEL_LOW;
+ if (time_to_empty > daemon->priv->action_time)
+ return UP_DEVICE_LEVEL_CRITICAL;
+ return UP_DEVICE_LEVEL_ACTION;
+ }
+ g_assert_not_reached ();
+}
+
/**
* up_daemon_refresh_battery_devices_cb:
**/
diff --git a/src/up-daemon.h b/src/up-daemon.h
index 6d2e337..d580cbd 100644
--- a/src/up-daemon.h
+++ b/src/up-daemon.h
@@ -83,6 +83,11 @@ void up_daemon_set_on_battery (UpDaemon *daemon,
gboolean on_battery);
void up_daemon_set_on_low_battery (UpDaemon *daemon,
gboolean on_low_battery);
+UpDeviceLevel up_daemon_compute_warning_level(UpDaemon *daemon,
+ gboolean power_supply,
+ gdouble percentage,
+ gint64 time_to_empty);
+
/* exported */
gboolean up_daemon_enumerate_devices (UpDaemon *daemon,
diff --git a/src/up-device.c b/src/up-device.c
index 9465180..15edc0d 100644
--- a/src/up-device.c
+++ b/src/up-device.c
@@ -78,6 +78,7 @@ struct UpDevicePrivate
gint64 time_to_full; /* seconds */
gdouble percentage; /* percent */
gdouble temperature; /* degrees C */
+ UpDeviceLevel warning_level;
};
static gboolean up_device_register_device (UpDevice *device);
@@ -110,6 +111,7 @@ enum {
PROP_PERCENTAGE,
PROP_TEMPERATURE,
PROP_TECHNOLOGY,
+ PROP_WARNING_LEVEL,
PROP_LAST
};
@@ -165,6 +167,32 @@ up_device_error_get_type (void)
return etype;
}
+/* This needs to be called when one of those properties changes:
+ * state
+ * power_supply
+ * percentage
+ * time_to_empty
+ */
+static void
+update_warning_level (UpDevice *device)
+{
+ UpDeviceLevel warning_level;
+
+ if (device->priv->state != UP_DEVICE_STATE_DISCHARGING)
+ warning_level = UP_DEVICE_LEVEL_NONE;
+ else
+ warning_level = up_daemon_compute_warning_level (device->priv->daemon,
+ device->priv->power_supply,
+ device->priv->percentage,
+ device->priv->time_to_empty);
+
+ if (warning_level == device->priv->warning_level)
+ return;
+
+ device->priv->warning_level = warning_level;
+ g_object_notify (G_OBJECT (device), "warning-level");
+}
+
/**
* up_device_get_property:
**/
@@ -251,6 +279,9 @@ up_device_get_property (GObject *object, guint prop_id, GValue *value, GParamSpe
case PROP_TECHNOLOGY:
g_value_set_uint (value, device->priv->technology);
break;
+ case PROP_WARNING_LEVEL:
+ g_value_set_uint (value, device->priv->warning_level);
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -290,6 +321,7 @@ up_device_set_property (GObject *object, guint prop_id, const GValue *value, GPa
break;
case PROP_POWER_SUPPLY:
device->priv->power_supply = g_value_get_boolean (value);
+ update_warning_level (device);
break;
case PROP_ONLINE:
device->priv->online = g_value_get_boolean (value);
@@ -308,6 +340,7 @@ up_device_set_property (GObject *object, guint prop_id, const GValue *value, GPa
break;
case PROP_STATE:
device->priv->state = g_value_get_uint (value);
+ update_warning_level (device);
break;
case PROP_CAPACITY:
device->priv->capacity = g_value_get_double (value);
@@ -335,12 +368,14 @@ up_device_set_property (GObject *object, guint prop_id, const GValue *value, GPa
break;
case PROP_TIME_TO_EMPTY:
device->priv->time_to_empty = g_value_get_int64 (value);
+ update_warning_level (device);
break;
case PROP_TIME_TO_FULL:
device->priv->time_to_full = g_value_get_int64 (value);
break;
case PROP_PERCENTAGE:
device->priv->percentage = g_value_get_double (value);
+ update_warning_level (device);
break;
case PROP_TEMPERATURE:
device->priv->temperature = g_value_get_double (value);
@@ -1151,6 +1186,18 @@ up_device_class_init (UpDeviceClass *klass)
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READWRITE));
+ /**
+ * UpDevice:warning-level:
+ */
+ g_object_class_install_property (object_class,
+ PROP_WARNING_LEVEL,
+ g_param_spec_uint ("warning-level",
+ NULL, NULL,
+ UP_DEVICE_LEVEL_UNKNOWN,
+ UP_DEVICE_LEVEL_LAST,
+ UP_DEVICE_LEVEL_UNKNOWN,
+ G_PARAM_READABLE));
+
dbus_g_error_domain_register (UP_DEVICE_ERROR, NULL, UP_DEVICE_TYPE_ERROR);
}