Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/mce.ini
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,13 @@ PatternUserManual=9;1;0;1;0;0;0;0;0
# Percentage at wich the battery is considerd "low" and a warning appears
LowPercentage=10

# Enable this to use capacity_level from sysfs for shutdown policy
UseCapacityLevel=false

# When UseCapacityLevel = true, enable this to consider battery as empty when
# capacity_level is "low" instead of "critical"
CapacityLevelLowCritical=false

[Display]

# Time in seconds between the display going dim and it turning off entirely
Expand Down
41 changes: 35 additions & 6 deletions src/modules/battery-upower.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ G_MODULE_EXPORT module_info_struct module_info = {
#define MCE_CONF_BATTERY_SECTION "Battery"
#define MCE_CONF_CRIT_VOLTAGE_KEY "CriticalVoltage"
#define MCE_CONF_LOW_PERCENT_KEY "LowPercentage"
#define MCE_CONF_USE_CAPACITY_LEVEL "UseCapacityLevel"
#define MCE_CONF_CAPACITY_LOW_CRITICAL_KEY "CapacityLevelLowCritical"


/** Skip these devices */
Expand All @@ -83,6 +85,8 @@ static struct {
time_t force_state;
gdouble min_voltage;
int low_percentage;
gboolean use_capacity_level;
gboolean level_low_critical;
} private = {0};

/** Battery properties available via UPower */
Expand All @@ -91,6 +95,7 @@ struct {
gdouble percentage;
gdouble voltage;
gboolean charger_online;
gchar *supply_level;
Comment thread
sicelo marked this conversation as resolved.
} upowbat = {0};

/** Battery properties in mce statemachine compatible form */
Expand All @@ -113,6 +118,7 @@ upowbat_init(void)
{
upowbat.percentage = 50;
upowbat.voltage = 3.8;
upowbat.supply_level = NULL;
upowbat.state = UP_DEVICE_STATE_UNKNOWN;
}

Expand All @@ -135,6 +141,7 @@ upowbat_update(void)
gdouble percentage;
gdouble voltage;
guint state;
gchar *supply_level;

if (private.battery == NULL)
return;
Expand All @@ -143,6 +150,7 @@ upowbat_update(void)
"percentage", &percentage,
"state", &state,
"voltage", &voltage,
"supply-level", &supply_level,
NULL);

if (upowbat.percentage != percentage) {
Expand All @@ -164,6 +172,12 @@ upowbat_update(void)
}
}

if (g_strcmp0(upowbat.supply_level, supply_level) != 0) {
mce_log(LL_DEBUG, "%s: Capacity Level: %s -> %s", MODULE_NAME, upowbat.supply_level, supply_level);
g_free(upowbat.supply_level);
upowbat.supply_level = supply_level;
Comment thread
sicelo marked this conversation as resolved.
}

if (upowbat.state != state) {
if (upowbat.state == UP_DEVICE_STATE_FULLY_CHARGED && state == UP_DEVICE_STATE_CHARGING) {
/* Prevent 'fully charged' -> 'charging' transition */
Expand Down Expand Up @@ -191,13 +205,21 @@ mcebat_update_from_upowbat(void)
upowbat.state == UP_DEVICE_STATE_PENDING_CHARGE;
}

if (upowbat.state == UP_DEVICE_STATE_EMPTY ||
upowbat.voltage < private.min_voltage && !mcebat.charger_connected)
mcebat.status = BATTERY_STATUS_EMPTY;
else if (upowbat.percentage < private.low_percentage)
mcebat.status = BATTERY_STATUS_LOW;
else if (upowbat.state == UP_DEVICE_STATE_FULLY_CHARGED)
if (upowbat.state == UP_DEVICE_STATE_FULLY_CHARGED) {
mcebat.status = BATTERY_STATUS_FULL;
} else if (private.use_capacity_level) {
if (g_strcmp0(upowbat.supply_level, "Critical") == 0 ||
(private.level_low_critical && g_strcmp0(upowbat.supply_level, "Low") == 0))
mcebat.status = BATTERY_STATUS_EMPTY;
else if (g_strcmp0(upowbat.supply_level, "Low") == 0)
mcebat.status = BATTERY_STATUS_LOW;
} else {
if (upowbat.state == UP_DEVICE_STATE_EMPTY ||
(upowbat.voltage < private.min_voltage && !mcebat.charger_connected))
mcebat.status = BATTERY_STATUS_EMPTY;
else if (upowbat.percentage < private.low_percentage)
mcebat.status = BATTERY_STATUS_LOW;
}
}

static inline const char *
Expand Down Expand Up @@ -476,6 +498,10 @@ xup_battery_connect_handlers(void)
g_signal_connect(private.battery, "notify::voltage",
G_CALLBACK(xup_battery_properties_changed_cb),
NULL);

g_signal_connect(private.battery, "notify::supply-level",
G_CALLBACK(xup_battery_properties_changed_cb),
NULL);
}

/**
Expand Down Expand Up @@ -526,6 +552,7 @@ xup_battery_remove_dev(void)
xup_battery_disconnect_handlers();
g_object_unref(private.battery);
private.battery = NULL;
g_free(upowbat.supply_level);
upowbat_init();
mcebat_init();
}
Expand Down Expand Up @@ -639,6 +666,8 @@ const gchar *g_module_check_init(GModule *module)
if(private.min_voltage > 0.1)
mce_log(LL_INFO, "%s: critical voltage set set to %f", MODULE_NAME, private.min_voltage);
private.low_percentage = mce_conf_get_int(MCE_CONF_BATTERY_SECTION, MCE_CONF_LOW_PERCENT_KEY, 5, NULL);
private.use_capacity_level = mce_conf_get_bool(MCE_CONF_BATTERY_SECTION, MCE_CONF_USE_CAPACITY_LEVEL, false, NULL);
private.level_low_critical = mce_conf_get_bool(MCE_CONF_BATTERY_SECTION, MCE_CONF_CAPACITY_LOW_CRITICAL_KEY, false, NULL);

/* Find battery/charger devices and add them to private */
xup_find_devices();
Expand Down