diff options
author | Shih-Yuan Lee (FourDollars) <sylee@canonical.com> | 2013-09-06 14:26:24 +0200 |
---|---|---|
committer | Martin Pitt <martinpitt@gnome.org> | 2013-09-06 14:28:12 +0200 |
commit | 5c0a57cfa01e00c8418419b4086b1369b1bd74aa (patch) | |
tree | 860e9878bd18c1b338c4c7818d84567533c5db0f | |
parent | 09d41eb759f980736bcf51c1fb709dbfb44177c1 (diff) |
linux: Detect the battery of bluetooth input devices
Check the input subdevices of the bluetooth parent device of a battery, if
present. If there is mouse* input child device, is a mouse battery; otherwise,
it is a keyboard battery. This also fixes the PowerSupply attribute for these
to be false, as the batteries of wireless input devices don't power the system.
https://launchpad.net/bugs/1153488
Signed-Off-By: Martin Pitt <martin.pitt@ubuntu.com>
-rw-r--r-- | src/linux/up-device-supply.c | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/src/linux/up-device-supply.c b/src/linux/up-device-supply.c index 1c82379..4a4b431 100644 --- a/src/linux/up-device-supply.c +++ b/src/linux/up-device-supply.c @@ -864,10 +864,16 @@ up_device_supply_coldplug (UpDevice *device) { UpDeviceSupply *supply = UP_DEVICE_SUPPLY (device); gboolean ret = FALSE; + GUdevDevice *bluetooth; GUdevDevice *native; + const gchar *file; + const gchar *device_path = NULL; const gchar *native_path; const gchar *scope; gchar *device_type = NULL; + gchar *input_path = NULL; + GDir *dir = NULL; + GError *error = NULL; UpDeviceKind type = UP_DEVICE_KIND_UNKNOWN; up_device_supply_reset_values (supply); @@ -897,7 +903,50 @@ up_device_supply_coldplug (UpDevice *device) 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. */ + bluetooth = g_udev_device_get_parent_with_subsystem (native, "bluetooth", NULL); + if (bluetooth != NULL) { + device_path = g_udev_device_get_sysfs_path (bluetooth); + if ((dir = g_dir_open (device_path, 0, &error))) { + while ((file = g_dir_read_name (dir))) { + /* Check if it is an input device. */ + if (g_str_has_prefix (file, "input")) { + input_path = g_build_filename (device_path, file, NULL); + break; + } + } + g_dir_close (dir); + } else { + g_warning ("Can not open folder %s: %s", device_path, error->message); + g_error_free (error); + } + g_object_unref (bluetooth); + } + + if (input_path != NULL) { + if ((dir = g_dir_open (input_path, 0, &error))) { + while ((file = g_dir_read_name (dir))) { + /* Check if it is a mouse device. */ + if (g_str_has_prefix (file, "mouse")) { + type = UP_DEVICE_KIND_MOUSE; + break; + } + } + g_dir_close (dir); + } else { + g_warning ("Can not open folder %s: %s", input_path, error->message); + g_error_free (error); + } + g_free (input_path); + 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 */ |