mirror of
https://gitlab.freedesktop.org/upower/upower.git
synced 2025-12-27 02:00:05 +01:00
add the battery state enumerated type property
This commit is contained in:
parent
f1c5630274
commit
2da987f9bc
4 changed files with 67 additions and 1 deletions
|
|
@ -54,3 +54,27 @@ devkit_power_convert_type_to_text (DevkitPowerType type_enum)
|
|||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
const char *
|
||||
devkit_power_convert_state_to_text (DevkitPowerState state_enum)
|
||||
{
|
||||
const char *state = NULL;
|
||||
switch (state_enum) {
|
||||
case DEVKIT_POWER_STATE_CHARGING:
|
||||
state = "charging";
|
||||
break;
|
||||
case DEVKIT_POWER_STATE_DISCHARGING:
|
||||
state = "discharging";
|
||||
break;
|
||||
case DEVKIT_POWER_STATE_EMPTY:
|
||||
state = "empty";
|
||||
break;
|
||||
case DEVKIT_POWER_STATE_FULLY_CHARGED:
|
||||
state = "fully-charged";
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached ();
|
||||
break;
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,16 @@ typedef enum {
|
|||
DEVKIT_POWER_TYPE_UNKNOWN
|
||||
} DevkitPowerType;
|
||||
|
||||
typedef enum {
|
||||
DEVKIT_POWER_STATE_CHARGING,
|
||||
DEVKIT_POWER_STATE_DISCHARGING,
|
||||
DEVKIT_POWER_STATE_EMPTY,
|
||||
DEVKIT_POWER_STATE_FULLY_CHARGED,
|
||||
DEVKIT_POWER_STATE_UNKNOWN
|
||||
} DevkitPowerState;
|
||||
|
||||
const char *devkit_power_convert_type_to_text (DevkitPowerType type_enum);
|
||||
const char *devkit_power_convert_state_to_text (DevkitPowerState state_enum);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
|
|
|
|||
|
|
@ -42,6 +42,8 @@
|
|||
/*--------------------------------------------------------------------------------------------------------------*/
|
||||
#include "devkit-power-source-glue.h"
|
||||
|
||||
#define DK_POWER_MIN_CHARGED_PERCENTAGE 60
|
||||
|
||||
struct DevkitPowerSourcePrivate
|
||||
{
|
||||
DBusGConnection *system_bus_connection;
|
||||
|
|
@ -61,6 +63,7 @@ struct DevkitPowerSourcePrivate
|
|||
DevkitPowerType type;
|
||||
|
||||
gboolean line_power_online;
|
||||
DevkitPowerState battery_state;
|
||||
|
||||
double battery_energy;
|
||||
double battery_energy_empty;
|
||||
|
|
@ -89,6 +92,7 @@ enum
|
|||
PROP_UPDATE_TIME,
|
||||
PROP_TYPE,
|
||||
PROP_LINE_POWER_ONLINE,
|
||||
PROP_BATTERY_STATE,
|
||||
PROP_BATTERY_ENERGY,
|
||||
PROP_BATTERY_ENERGY_EMPTY,
|
||||
PROP_BATTERY_ENERGY_FULL,
|
||||
|
|
@ -148,7 +152,9 @@ get_property (GObject *object,
|
|||
case PROP_LINE_POWER_ONLINE:
|
||||
g_value_set_boolean (value, source->priv->line_power_online);
|
||||
break;
|
||||
|
||||
case PROP_BATTERY_STATE:
|
||||
g_value_set_string (value, devkit_power_convert_state_to_text (source->priv->battery_state));
|
||||
break;
|
||||
case PROP_BATTERY_ENERGY:
|
||||
g_value_set_double (value, source->priv->battery_energy);
|
||||
break;
|
||||
|
|
@ -244,6 +250,10 @@ devkit_power_source_class_init (DevkitPowerSourceClass *klass)
|
|||
object_class,
|
||||
PROP_BATTERY_ENERGY,
|
||||
g_param_spec_double ("battery-energy", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
|
||||
g_object_class_install_property (
|
||||
object_class,
|
||||
PROP_BATTERY_STATE,
|
||||
g_param_spec_string ("battery-state", NULL, NULL, NULL, G_PARAM_READABLE));
|
||||
g_object_class_install_property (
|
||||
object_class,
|
||||
PROP_BATTERY_ENERGY_EMPTY,
|
||||
|
|
@ -497,12 +507,23 @@ update_battery (DevkitPowerSource *source)
|
|||
if (is_charging)
|
||||
source->priv->battery_energy_rate *= -1.0;
|
||||
|
||||
/* get a precise percentage */
|
||||
source->priv->battery_percentage = 100.0 * source->priv->battery_energy / source->priv->battery_energy_full;
|
||||
if (source->priv->battery_percentage < 0)
|
||||
source->priv->battery_percentage = 0;
|
||||
if (source->priv->battery_percentage > 100.0)
|
||||
source->priv->battery_percentage = 100.0;
|
||||
|
||||
/* get the state */
|
||||
if (is_charging)
|
||||
source->priv->battery_state = DEVKIT_POWER_STATE_CHARGING;
|
||||
else if (is_discharging)
|
||||
source->priv->battery_state = DEVKIT_POWER_STATE_DISCHARGING;
|
||||
else if (source->priv->battery_percentage > DK_POWER_MIN_CHARGED_PERCENTAGE)
|
||||
source->priv->battery_state = DEVKIT_POWER_STATE_FULLY_CHARGED;
|
||||
else
|
||||
source->priv->battery_state = DEVKIT_POWER_STATE_EMPTY;
|
||||
|
||||
g_free (status);
|
||||
return TRUE;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -202,6 +202,18 @@
|
|||
</doc:para></doc:description></doc:doc>
|
||||
</property>
|
||||
|
||||
<property name="battery-state" type="s" access="read">
|
||||
<doc:doc><doc:description><doc:para>
|
||||
The battery power state.
|
||||
Known values are "fully-charged", "empty", "charging",
|
||||
"discharging" or "unknown".
|
||||
</doc:para><doc:para>
|
||||
This property is only valid if the property
|
||||
<doc:ref type="property" to="Source:type">type</doc:ref>
|
||||
has the value "battery".
|
||||
</doc:para></doc:description></doc:doc>
|
||||
</property>
|
||||
|
||||
<property name="battery-technology" type="s" access="read">
|
||||
<doc:doc><doc:description><doc:para>
|
||||
Technology used in the battery; known values are "Li-ion".
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue