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>
This commit is contained in:
José Expósito 2021-09-12 17:09:20 +02:00 committed by Peter Hutterer
parent d808817614
commit dbcb003c5e
5 changed files with 59 additions and 19 deletions

View 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

View 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;

View 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)
{

View 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);

View 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);