diff options
author | José Expósito <jose.exposito89@gmail.com> | 2021-09-12 17:09:20 +0200 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2021-09-12 21:16:32 +0000 |
commit | dbcb003c5e579a8dcbcb622f230be4f3672ea3c8 (patch) | |
tree | 06751c238a5c69c991bab03fb6dd18bd3c5893a6 | |
parent | d8088176149a6086927eb765a6e617cf0041ec10 (diff) | |
download | libinput-dbcb003c5e579a8dcbcb622f230be4f3672ea3c8.tar.gz |
util: add a function to parse bool properties
Move the logic used to parse boolean quirks and udev flags to a common
function in utils.
Refactor, no functional changes.
Signed-off-by: José Expósito <jose.exposito89@gmail.com>
-rw-r--r-- | src/evdev.c | 10 | ||||
-rw-r--r-- | src/quirks.c | 18 | ||||
-rw-r--r-- | src/util-prop-parsers.c | 16 | ||||
-rw-r--r-- | src/util-prop-parsers.h | 1 | ||||
-rw-r--r-- | test/test-utils.c | 33 |
5 files changed, 59 insertions, 19 deletions
diff --git a/src/evdev.c b/src/evdev.c index e8b37674..f332bc1b 100644 --- a/src/evdev.c +++ b/src/evdev.c @@ -94,19 +94,21 @@ parse_udev_flag(struct evdev_device *device, const char *property) { const char *val; + bool b; val = udev_device_get_property_value(udev_device, property); if (!val) return false; - if (streq(val, "1")) - return true; - if (!streq(val, "0")) + if (!parse_boolean_property(val, &b)) { evdev_log_error(device, "property %s has invalid value '%s'\n", property, val); - return false; + return false; + } + + return b; } int diff --git a/src/quirks.c b/src/quirks.c index 502c8bd0..795d2925 100644 --- a/src/quirks.c +++ b/src/quirks.c @@ -660,11 +660,7 @@ parse_model(struct quirks_context *ctx, assert(strneq(key, "Model", 5)); - if (streq(value, "1")) - b = true; - else if (streq(value, "0")) - b = false; - else + if (!parse_boolean_property(value, &b)) return false; do { @@ -789,22 +785,14 @@ parse_attr(struct quirks_context *ctx, rc = true; } else if (streq(key, quirk_get_name(QUIRK_ATTR_USE_VELOCITY_AVERAGING))) { p->id = QUIRK_ATTR_USE_VELOCITY_AVERAGING; - if (streq(value, "1")) - b = true; - else if (streq(value, "0")) - b = false; - else + if (!parse_boolean_property(value, &b)) goto out; p->type = PT_BOOL; p->value.b = b; rc = true; } else if (streq(key, quirk_get_name(QUIRK_ATTR_TABLET_SMOOTHING))) { p->id = QUIRK_ATTR_TABLET_SMOOTHING; - if (streq(value, "1")) - b = true; - else if (streq(value, "0")) - b = false; - else + if (!parse_boolean_property(value, &b)) goto out; p->type = PT_BOOL; p->value.b = b; diff --git a/src/util-prop-parsers.c b/src/util-prop-parsers.c index cfd6c59d..62902520 100644 --- a/src/util-prop-parsers.c +++ b/src/util-prop-parsers.c @@ -283,6 +283,22 @@ parse_range_property(const char *prop, int *hi, int *lo) return true; } +bool +parse_boolean_property(const char *prop, bool *b) +{ + if (!prop) + return false; + + if (streq(prop, "1")) + *b = true; + else if (streq(prop, "0")) + *b = false; + else + return false; + + return true; +} + static bool parse_evcode_string(const char *s, int *type_out, int *code_out) { diff --git a/src/util-prop-parsers.h b/src/util-prop-parsers.h index 5f0d8673..8fbeacd0 100644 --- a/src/util-prop-parsers.h +++ b/src/util-prop-parsers.h @@ -36,6 +36,7 @@ int parse_mouse_wheel_click_count_property(const char *prop); bool parse_dimension_property(const char *prop, size_t *width, size_t *height); bool parse_calibration_property(const char *prop, float calibration[6]); bool parse_range_property(const char *prop, int *hi, int *lo); +bool parse_boolean_property(const char *prop, bool *b); #define EVENT_CODE_UNDEFINED 0xffff bool parse_evcode_property(const char *prop, struct input_event *events, size_t *nevents); bool parse_input_prop_property(const char *prop, unsigned int *props_out, size_t *nprops); diff --git a/test/test-utils.c b/test/test-utils.c index bdefd794..6d31c1d5 100644 --- a/test/test-utils.c +++ b/test/test-utils.c @@ -471,6 +471,38 @@ START_TEST(range_prop_parser) } END_TEST +START_TEST(boolean_prop_parser) +{ + struct parser_test_range { + char *tag; + bool success; + bool b; + } tests[] = { + { "0", true, false }, + { "1", true, true }, + { "-1", false, false }, + { "2", false, false }, + { "abcd", false, false }, + { NULL, false, false } + }; + int i; + bool success, b; + + for (i = 0; tests[i].tag != NULL; i++) { + b = false; + success = parse_boolean_property(tests[i].tag, &b); + ck_assert(success == tests[i].success); + if (success) + ck_assert_int_eq(b, tests[i].b); + else + ck_assert_int_eq(b, false); + } + + success = parse_boolean_property(NULL, NULL); + ck_assert(success == false); +} +END_TEST + START_TEST(evcode_prop_parser) { struct parser_test_tuple { @@ -1438,6 +1470,7 @@ litest_utils_suite(void) tcase_add_test(tc, reliability_prop_parser); tcase_add_test(tc, calibration_prop_parser); tcase_add_test(tc, range_prop_parser); + tcase_add_test(tc, boolean_prop_parser); tcase_add_test(tc, evcode_prop_parser); tcase_add_test(tc, input_prop_parser); tcase_add_test(tc, evdev_abs_parser); |