Add libevdev_property_from_name()
authorPeter Hutterer <peter.hutterer@who-t.net>
Mon, 18 Aug 2014 00:33:18 +0000 (10:33 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Tue, 19 Aug 2014 22:43:15 +0000 (08:43 +1000)
12717d79 "Add libevdev_event_type/code_from_name() resolvers" added the
lookup functions for types and codes, this commit adds the missing ones for
input properties.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
libevdev/libevdev-names.c
libevdev/libevdev.h
libevdev/libevdev.sym
libevdev/make-event-names.py
test/test-event-codes.c

index 10669a3..ad318de 100644 (file)
@@ -141,3 +141,23 @@ libevdev_event_code_from_name_n(unsigned int type, const char *name, size_t len)
 
        return entry ? (int)entry->value : -1;
 }
+
+LIBEVDEV_EXPORT int
+libevdev_property_from_name(const char *name)
+{
+       return libevdev_property_from_name_n(name, strlen(name));
+}
+
+LIBEVDEV_EXPORT int
+libevdev_property_from_name_n(const char *name, size_t len)
+{
+       struct name_lookup lookup;
+       const struct name_entry *entry;
+
+       lookup.name = name;
+       lookup.len = len;
+
+       entry = lookup_name(prop_names, ARRAY_LENGTH(prop_names), &lookup);
+
+       return entry ? (int)entry->value : -1;
+}
index 4f33cb7..321f980 100644 (file)
@@ -2085,6 +2085,36 @@ int libevdev_event_code_from_name_n(unsigned int type, const char *name,
                                    size_t len);
 
 /**
+ * @ingroup misc
+ *
+ * Look up an input property by its name. Properties start with the fixed
+ * prefix "INPUT_PROP_" followed by their name (eg., "INPUT_PROP_POINTER").
+ * The prefix must be included in the name. It returns the constant assigned
+ * to the property or -1 if not found.
+ *
+ * @param name A non-NULL string describing an input property
+ *
+ * @return The given code constant for the name or -1 if not found.
+ */
+int libevdev_property_from_name(const char *name);
+
+/**
+ * @ingroup misc
+ *
+ * Look up an input property by its name. Properties start with the fixed
+ * prefix "INPUT_PROP_" followed by their name (eg., "INPUT_PROP_POINTER").
+ * The prefix must be included in the name. It returns the constant assigned
+ * to the property or -1 if not found.
+ *
+ * @param name A non-NULL string describing an input property
+ * @param len The length of the string in @p name excluding any terminating 0
+ * character.
+ *
+ * @return The given code constant for the name or -1 if not found.
+ */
+int libevdev_property_from_name_n(const char *name, size_t len);
+
+/**
  * @ingroup bits
  *
  * Get the repeat delay and repeat period values for this device. This
index 228e555..374712f 100644 (file)
@@ -106,6 +106,8 @@ local:
 LIBEVDEV_1_3 {
 global:
        libevdev_set_device_log_function;
+       libevdev_property_from_name;
+       libevdev_property_from_name_n;
 
 local:
        *;
index 149f093..980283f 100755 (executable)
@@ -133,7 +133,10 @@ def print_lookup_table(bits):
                print_lookup(bits, prefix[:-1].lower())
        print("};")
        print("")
-
+       print("static const struct name_entry prop_names[] = {")
+       print_lookup(bits, "input_prop")
+       print("};")
+       print("")
 
 def print_mapping_table(bits):
        print("/* THIS FILE IS GENERATED, DO NOT EDIT */")
index ac2f738..b64940e 100644 (file)
@@ -99,6 +99,38 @@ START_TEST(test_key_invalid)
 }
 END_TEST
 
+START_TEST(test_properties)
+{
+       struct prop {
+               int val;
+               const char *name;
+       } lut[] = {
+               { INPUT_PROP_DIRECT, "INPUT_PROP_DIRECT" },
+               { INPUT_PROP_POINTER, "INPUT_PROP_POINTER" },
+               { INPUT_PROP_MAX, "INPUT_PROP_MAX" },
+               { -1, NULL}
+       };
+       struct prop *p = lut;
+       while (p->val != -1) {
+               ck_assert_int_eq(libevdev_property_from_name(p->name), p->val);
+               p++;
+       }
+}
+END_TEST
+
+START_TEST(test_properties_invalid)
+{
+       ck_assert_int_eq(libevdev_property_from_name("EV_ABS"), -1);
+       ck_assert_int_eq(libevdev_property_from_name("INPUT_PROP"), -1);
+       ck_assert_int_eq(libevdev_property_from_name("INPUT_PROP_"), -1);
+       ck_assert_int_eq(libevdev_property_from_name("INPUT_PROP_FOO"), -1);
+
+       ck_assert_int_eq(libevdev_property_from_name_n("INPUT_PROP_POINTER", 11), -1);
+       ck_assert_int_eq(libevdev_property_from_name_n("INPUT_PROP_POINTER",
+                                               strlen("INPUT_PROP_POINTER") - 1), -1);
+}
+END_TEST
+
 Suite *
 event_code_suite(void)
 {
@@ -114,5 +146,10 @@ event_code_suite(void)
        tcase_add_test(tc, test_key_invalid);
        suite_add_tcase(s, tc);
 
+       tc = tcase_create("property tests");
+       tcase_add_test(tc, test_properties);
+       tcase_add_test(tc, test_properties_invalid);
+       suite_add_tcase(s, tc);
+
        return s;
 }