[PATCH libevdev 5/6] Add setters for product/vendor/bustype/version

Peter Hutterer peter.hutterer at who-t.net
Sun Jul 28 22:34:36 PDT 2013


Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 libevdev/libevdev.c            | 11 +++++++++++
 libevdev/libevdev.h            | 44 ++++++++++++++++++++++++++++++++++++++++++
 test/test-libevdev-has-event.c | 37 +++++++++++++++++++++++++++++++++++
 3 files changed, 92 insertions(+)

diff --git a/libevdev/libevdev.c b/libevdev/libevdev.c
index a7d04a0..c366dcf 100644
--- a/libevdev/libevdev.c
+++ b/libevdev/libevdev.c
@@ -678,6 +678,17 @@ STRING_SETTER(name);
 STRING_SETTER(phys);
 STRING_SETTER(uniq);
 
+#define PRODUCT_SETTER(name, field) \
+void libevdev_set_##name(struct libevdev *dev, int field) \
+{ \
+	dev->ids.field = field;\
+}
+
+PRODUCT_SETTER(product_id, product);
+PRODUCT_SETTER(vendor_id, vendor);
+PRODUCT_SETTER(bustype, bustype);
+PRODUCT_SETTER(version, version);
+
 int libevdev_get_product_id(const struct libevdev *dev)
 {
 	return dev->ids.product;
diff --git a/libevdev/libevdev.h b/libevdev/libevdev.h
index aaaa0df..e64cc2b 100644
--- a/libevdev/libevdev.h
+++ b/libevdev/libevdev.h
@@ -587,6 +587,17 @@ void libevdev_set_uniq(struct libevdev *dev, const char *uniq);
 int libevdev_get_product_id(const struct libevdev *dev);
 
 /**
+ * @ingroup kernel
+ *
+ * @param dev The evdev device
+ * @param product_id The product ID to assign to this device
+ *
+ * @note This function may be called before libevdev_set_fd(). A call to
+ * libevdev_set_fd() will overwrite any previously set value.
+ */
+void libevdev_set_product_id(struct libevdev *dev, int product_id);
+
+/**
  * @ingroup bits
  *
  * @param dev The evdev device, already initialized with libevdev_set_fd()
@@ -598,6 +609,17 @@ int libevdev_get_product_id(const struct libevdev *dev);
 int libevdev_get_vendor_id(const struct libevdev *dev);
 
 /**
+ * @ingroup kernel
+ *
+ * @param dev The evdev device
+ * @param vendor_id The vendor ID to assign to this device
+ *
+ * @note This function may be called before libevdev_set_fd(). A call to
+ * libevdev_set_fd() will overwrite any previously set value.
+ */
+void libevdev_set_vendor_id(struct libevdev *dev, int vendor_id);
+
+/**
  * @ingroup bits
  *
  * @param dev The evdev device, already initialized with libevdev_set_fd()
@@ -609,6 +631,17 @@ int libevdev_get_vendor_id(const struct libevdev *dev);
 int libevdev_get_bustype(const struct libevdev *dev);
 
 /**
+ * @ingroup kernel
+ *
+ * @param dev The evdev device
+ * @param bustype The bustype to assign to this device
+ *
+ * @note This function may be called before libevdev_set_fd(). A call to
+ * libevdev_set_fd() will overwrite any previously set value.
+ */
+void libevdev_set_bustype(struct libevdev *dev, int bustype);
+
+/**
  * @ingroup bits
  *
  * @param dev The evdev device, already initialized with libevdev_set_fd()
@@ -620,6 +653,17 @@ int libevdev_get_bustype(const struct libevdev *dev);
 int libevdev_get_version(const struct libevdev *dev);
 
 /**
+ * @ingroup kernel
+ *
+ * @param dev The evdev device
+ * @param version The version to assign to this device
+ *
+ * @note This function may be called before libevdev_set_fd(). A call to
+ * libevdev_set_fd() will overwrite any previously set value.
+ */
+void libevdev_set_version(struct libevdev *dev, int version);
+
+/**
  * @ingroup bits
  *
  * @param dev The evdev device, already initialized with libevdev_set_fd()
diff --git a/test/test-libevdev-has-event.c b/test/test-libevdev-has-event.c
index e15acb9..35b1fd5 100644
--- a/test/test-libevdev-has-event.c
+++ b/test/test-libevdev-has-event.c
@@ -454,6 +454,42 @@ START_TEST(test_device_set_name)
 }
 END_TEST
 
+START_TEST(test_device_set_ids)
+{
+	struct uinput_device* uidev;
+	struct libevdev *dev;
+	struct input_id ids = {1, 2, 3, 4};
+	int rc;
+
+	dev = libevdev_new();
+
+	libevdev_set_product_id(dev, 10);
+	libevdev_set_vendor_id(dev, 20);
+	libevdev_set_bustype(dev, 30);
+	libevdev_set_version(dev, 40);
+
+	ck_assert_int_eq(libevdev_get_product_id(dev), 10);
+	ck_assert_int_eq(libevdev_get_vendor_id(dev), 20);
+	ck_assert_int_eq(libevdev_get_bustype(dev), 30);
+	ck_assert_int_eq(libevdev_get_version(dev), 40);
+
+	rc = uinput_device_new_with_events(&uidev, TEST_DEVICE_NAME, &ids,
+					   EV_ABS, ABS_X,
+					   -1);
+	ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
+	rc = libevdev_set_fd(dev, uinput_device_get_fd(uidev));
+	ck_assert_msg(rc == 0, "Failed to init device: %s", strerror(-rc));;
+
+	ck_assert_int_eq(libevdev_get_bustype(dev), ids.bustype);
+	ck_assert_int_eq(libevdev_get_vendor_id(dev), ids.vendor);
+	ck_assert_int_eq(libevdev_get_product_id(dev), ids.product);
+	ck_assert_int_eq(libevdev_get_version(dev), ids.version);
+
+	uinput_device_free(uidev);
+	libevdev_free(dev);
+}
+END_TEST
+
 START_TEST(test_device_get_abs_info)
 {
 	struct uinput_device* uidev;
@@ -887,6 +923,7 @@ libevdev_has_event_test(void)
 	tc = tcase_create("device info");
 	tcase_add_test(tc, test_device_name);
 	tcase_add_test(tc, test_device_set_name);
+	tcase_add_test(tc, test_device_set_ids);
 	tcase_add_test(tc, test_device_get_abs_info);
 	suite_add_tcase(s, tc);
 
-- 
1.8.2.1



More information about the Input-tools mailing list