## Description: Detect if the battery comes from bluetooth keyboard or mouse. ## Origin/Author: Shih-Yuan Lee (FourDollars) ## 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:41:16.409825348 +0800 +++ upower-0.9.15/src/linux/up-device-supply.c 2013-07-31 16:42:31.013898187 +0800 @@ -848,9 +848,16 @@ UpDeviceSupply *supply = UP_DEVICE_SUPPLY (device); gboolean ret = FALSE; GUdevDevice *native; + const gchar *file; const gchar *native_path; + const gchar *input_path; const gchar *scope; gchar *device_type = NULL; + gchar *device_path = NULL; + gchar *dir_name = NULL; + gchar *subdir_name = NULL; + GDir *dir = NULL; + GError *error = NULL; UpDeviceKind type = UP_DEVICE_KIND_UNKNOWN; up_device_supply_reset_values (supply); @@ -880,7 +887,35 @@ if (g_ascii_strcasecmp (device_type, "mains") == 0) { type = UP_DEVICE_KIND_LINE_POWER; } else if (g_ascii_strcasecmp (device_type, "battery") == 0) { - type = UP_DEVICE_KIND_BATTERY; + /* Detect if the battery comes from bluetooth keyboard or mouse. */ + if (g_strstr_len (native_path, -1, "bluetooth") != NULL) { + device_path = sysfs_resolve_link (native_path, "device"); + dir_name = g_path_get_dirname (device_path); + dir = g_dir_open (dir_name, 0, &error); + while (file = g_dir_read_name (dir)) { + if (g_str_has_prefix (file, "input")) { + subdir_name = g_build_filename (dir_name, file, NULL); + break; + } + } + g_dir_close (dir); + } + if (subdir_name) { + dir = g_dir_open (subdir_name, 0, &error); + while (file = g_dir_read_name (dir)) { + if (g_str_has_prefix (file, "mouse")) { + type = UP_DEVICE_KIND_MOUSE; + break; + } + } + g_dir_close (dir); + if (type == UP_DEVICE_KIND_UNKNOWN) { + type = UP_DEVICE_KIND_KEYBOARD; + } + } + if (type == UP_DEVICE_KIND_UNKNOWN) { + type = UP_DEVICE_KIND_BATTERY; + } } else if (g_ascii_strcasecmp (device_type, "USB") == 0) { /* use a heuristic to find the device type */ @@ -912,6 +947,9 @@ ret = up_device_supply_refresh (device); out: g_free (device_type); + g_free (device_path); + g_free (dir_name); + g_free (subdir_name); return ret; }