util: add a function to parse bool properties
authorJosé Expósito <jose.exposito89@gmail.com>
Sun, 12 Sep 2021 15:09:20 +0000 (17:09 +0200)
committerPeter Hutterer <peter.hutterer@who-t.net>
Sun, 12 Sep 2021 21:16:32 +0000 (21:16 +0000)
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>
src/evdev.c
src/quirks.c
src/util-prop-parsers.c
src/util-prop-parsers.h
test/test-utils.c

index e8b376748f49906df1b3f42791fafefbb301521f..f332bc1b84b03cfcedf5bcdc52d1e4e3b3a543bf 100644 (file)
@@ -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
index 502c8bd08ea22fca55fb31e5b852304df4e980ec..795d29252ed20d5c61e0d3064833272c9bf73710 100644 (file)
@@ -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;
index cfd6c59dc1d346157df1ce52a7d8eeaad484c261..62902520f95a124f1c0a01b1bc945027b1cfc2bb 100644 (file)
@@ -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)
 {
index 5f0d86736103e040d951fabbc593f8f8315af5e4..8fbeacd09dabc9b8230a58c943d7de379d195560 100644 (file)
@@ -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);
index bdefd794d83529f5c1c48a2fe995f6539ac3e715..6d31c1d5317f6433453f282c78cebf55ad364876 100644 (file)
@@ -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);