## Description: Try to work out if the device is powering the system. ## Origin/Author: Richard Hughes & Martin Pitt ## Bug: https://bugs.launchpad.net/bugs/1153488 Index: upower-0.9.15/src/linux/up-device-supply.c =================================================================== --- upower-0.9.15.orig/src/linux/up-device-supply.c 2013-07-31 14:37:36.256733679 +0800 +++ upower-0.9.15/src/linux/up-device-supply.c 2013-07-31 16:43:13.000000000 +0800 @@ -147,6 +147,7 @@ UpDeviceSupply *supply = UP_DEVICE_SUPPLY (device); UpDeviceKind type; UpDeviceState state; + gboolean is_power_supply; gboolean is_present; g_return_val_if_fail (UP_IS_DEVICE_SUPPLY (supply), FALSE); @@ -156,8 +157,11 @@ "type", &type, "state", &state, "is-present", &is_present, + "power-supply", &is_power_supply, NULL); + if (!is_power_supply) + return FALSE; if (type != UP_DEVICE_KIND_BATTERY) return FALSE; if (state == UP_DEVICE_STATE_UNKNOWN) @@ -845,6 +849,7 @@ gboolean ret = FALSE; GUdevDevice *native; const gchar *native_path; + const gchar *scope; gchar *device_type = NULL; UpDeviceKind type = UP_DEVICE_KIND_UNKNOWN; @@ -858,23 +863,29 @@ goto out; } + /* try to work out if the device is powering the system */ + scope = g_udev_device_get_sysfs_attr (native, "scope"); + if (scope != NULL && g_ascii_strcasecmp (scope, "device") == 0) { + supply->priv->is_power_supply = FALSE; + } else if (scope != NULL && g_ascii_strcasecmp (scope, "system") == 0) { + supply->priv->is_power_supply = TRUE; + } else { + g_debug ("taking a guess for power supply scope"); + supply->priv->is_power_supply = TRUE; + } + /* try to detect using the device type */ device_type = up_device_supply_get_string (native_path, "type"); if (device_type != NULL) { if (g_ascii_strcasecmp (device_type, "mains") == 0) { type = UP_DEVICE_KIND_LINE_POWER; - supply->priv->is_power_supply = TRUE; } else if (g_ascii_strcasecmp (device_type, "battery") == 0) { type = UP_DEVICE_KIND_BATTERY; - supply->priv->is_power_supply = TRUE; } else if (g_ascii_strcasecmp (device_type, "USB") == 0) { /* use a heuristic to find the device type */ - if (g_strstr_len (native_path, -1, "wacom_") != NULL || - g_strstr_len (native_path, -1, "wacom_") != NULL) { + if (g_strstr_len (native_path, -1, "wacom_") != NULL) { type = UP_DEVICE_KIND_TABLET; - } else if (g_strstr_len (native_path, -1, "magicmouse_") != NULL) { - type = UP_DEVICE_KIND_MOUSE; } else { g_warning ("did not recognise USB path %s, please report", native_path);