diff options
author | Bastien Nocera <hadess@hadess.net> | 2017-01-24 13:10:54 +0100 |
---|---|---|
committer | Bastien Nocera <hadess@hadess.net> | 2017-01-24 17:45:58 +0100 |
commit | e3b6e1426b08b9569c301fc2a9442cdba3ba6b8a (patch) | |
tree | a9c2bb68eaf2805246987595eddd2607ed02974d | |
parent | 140b18f8f4302abc8db8f9cfc04796dd71bd5f64 (diff) | |
download | upower-e3b6e1426b08b9569c301fc2a9442cdba3ba6b8a.tar.gz |
linux: Simplify up_device_supply_guess_type()
By exiting early.
-rw-r--r-- | src/linux/up-device-supply.c | 138 |
1 files changed, 71 insertions, 67 deletions
diff --git a/src/linux/up-device-supply.c b/src/linux/up-device-supply.c index 8125d9d..719d4df 100644 --- a/src/linux/up-device-supply.c +++ b/src/linux/up-device-supply.c @@ -907,94 +907,98 @@ up_device_supply_guess_type (GUdevDevice *native, UpDeviceKind type = UP_DEVICE_KIND_UNKNOWN; 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; - } else if (g_ascii_strcasecmp (device_type, "battery") == 0) { - guint i; - const char *class[] = { "hid", "bluetooth" }; - - for (i = 0; i < G_N_ELEMENTS(class) && type == UP_DEVICE_KIND_UNKNOWN; i++) { - /* Detect if the battery comes from bluetooth keyboard or mouse. */ - GUdevDevice *bluetooth; - GDir *dir; - gchar *input_path = NULL; - GError *error = NULL; - - bluetooth = g_udev_device_get_parent_with_subsystem (native, class[i], NULL); - if (bluetooth != NULL) { - const gchar *device_path; - gchar *subdir; - - device_path = g_udev_device_get_sysfs_path (bluetooth); - - /* There may be an extra subdirectory here */ - subdir = g_build_filename (device_path, "input", NULL); - if (!g_file_test (subdir, G_FILE_TEST_IS_DIR)) { - g_free(subdir); - subdir = g_strdup (device_path); - } + if (device_type == NULL) + return type; - if ((dir = g_dir_open (subdir, 0, &error))) { - const char *file; - 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 (subdir, 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_free (subdir); - g_object_unref (bluetooth); - } + if (g_ascii_strcasecmp (device_type, "mains") == 0) { + type = UP_DEVICE_KIND_LINE_POWER; + goto out; + } - if (input_path == NULL) - continue; + if (g_ascii_strcasecmp (device_type, "battery") == 0) { + guint i; + const char *class[] = { "hid", "bluetooth" }; + + for (i = 0; i < G_N_ELEMENTS(class) && type == UP_DEVICE_KIND_UNKNOWN; i++) { + /* Detect if the battery comes from bluetooth keyboard or mouse. */ + GUdevDevice *bluetooth; + GDir *dir; + gchar *input_path = NULL; + GError *error = NULL; + + bluetooth = g_udev_device_get_parent_with_subsystem (native, class[i], NULL); + if (bluetooth != NULL) { + const gchar *device_path; + gchar *subdir; + + device_path = g_udev_device_get_sysfs_path (bluetooth); + + /* There may be an extra subdirectory here */ + subdir = g_build_filename (device_path, "input", NULL); + if (!g_file_test (subdir, G_FILE_TEST_IS_DIR)) { + g_free(subdir); + subdir = g_strdup (device_path); + } - if ((dir = g_dir_open (input_path, 0, &error))) { + if ((dir = g_dir_open (subdir, 0, &error))) { const char *file; 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; + /* Check if it is an input device. */ + if (g_str_has_prefix (file, "input")) { + input_path = g_build_filename (subdir, file, NULL); break; } } g_dir_close (dir); } else { - g_warning ("Can not open folder %s: %s", input_path, error->message); + g_warning ("Can not open folder %s: %s", device_path, error->message); g_error_free (error); } - g_free (input_path); - if (type == UP_DEVICE_KIND_UNKNOWN) { - type = UP_DEVICE_KIND_KEYBOARD; - } + g_free (subdir); + g_object_unref (bluetooth); } - if (type == UP_DEVICE_KIND_UNKNOWN) { - type = UP_DEVICE_KIND_BATTERY; - } - } else if (g_ascii_strcasecmp (device_type, "USB") == 0) { + if (input_path == NULL) + continue; - /* use a heuristic to find the device type */ - if (g_strstr_len (native_path, -1, "wacom_") != NULL) { - type = UP_DEVICE_KIND_TABLET; + if ((dir = g_dir_open (input_path, 0, &error))) { + const char *file; + 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 ("did not recognise USB path %s, please report", - native_path); + g_warning ("Can not open folder %s: %s", input_path, error->message); + g_error_free (error); } - } else { - g_warning ("did not recognise type %s, please report", device_type); + 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) { - g_free (device_type); + /* use a heuristic to find the device type */ + if (g_strstr_len (native_path, -1, "wacom_") != NULL) { + type = UP_DEVICE_KIND_TABLET; + } else { + g_warning ("did not recognise USB path %s, please report", + native_path); + } + } else { + g_warning ("did not recognise type %s, please report", device_type); } +out: + g_free (device_type); return type; } |