Provide setters for name, phys, uniq
authorPeter Hutterer <peter.hutterer@who-t.net>
Thu, 25 Jul 2013 06:11:04 +0000 (16:11 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Thu, 1 Aug 2013 03:53:05 +0000 (13:53 +1000)
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
libevdev/libevdev.c
libevdev/libevdev.h
test/test-libevdev-has-event.c

index a17e388..983a94a 100644 (file)
@@ -150,12 +150,15 @@ libevdev_set_fd(struct libevdev* dev, int fd)
        if (rc < 0)
                goto out;
 
+       free(dev->name);
        dev->name = strdup(buf);
        if (!dev->name) {
                errno = ENOSPC;
                goto out;
        }
 
+       free(dev->phys);
+       dev->phys = NULL;
        memset(buf, 0, sizeof(buf));
        rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
        if (rc < 0) {
@@ -170,6 +173,8 @@ libevdev_set_fd(struct libevdev* dev, int fd)
                }
        }
 
+       free(dev->uniq);
+       dev->uniq = NULL;
        memset(buf, 0, sizeof(buf));
        rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
        if (rc < 0) {
@@ -660,6 +665,19 @@ libevdev_get_uniq(const struct libevdev *dev)
        return dev->uniq;
 }
 
+#define STRING_SETTER(field) \
+void libevdev_set_##field(struct libevdev *dev, const char *field) \
+{ \
+       if (field == NULL) \
+               return; \
+       free(dev->field); \
+       dev->field = strdup(field); \
+}
+
+STRING_SETTER(name);
+STRING_SETTER(phys);
+STRING_SETTER(uniq);
+
 int libevdev_get_product_id(const struct libevdev *dev)
 {
        return dev->ids.product;
index 427bf39..b85f75b 100644 (file)
@@ -519,6 +519,17 @@ int libevdev_has_event_pending(struct libevdev *dev);
 const char* libevdev_get_name(const struct libevdev *dev);
 
 /**
+ * @ingroup kernel
+ *
+ * @param dev The evdev device
+ * @param name The new, non-NULL, name 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_name(struct libevdev *dev, const char *name);
+
+/**
  * @ingroup bits
  *
  * Virtual devices such as uinput devices have no phys location.
@@ -532,6 +543,17 @@ const char* libevdev_get_name(const struct libevdev *dev);
 const char * libevdev_get_phys(const struct libevdev *dev);
 
 /**
+ * @ingroup kernel
+ *
+ * @param dev The evdev device
+ * @param phys The new, non-NULL, phys 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_phys(struct libevdev *dev, const char *phys);
+
+/**
  * @ingroup bits
  *
  * @param dev The evdev device, already initialized with libevdev_set_fd()
@@ -543,6 +565,17 @@ const char * libevdev_get_phys(const struct libevdev *dev);
 const char * libevdev_get_uniq(const struct libevdev *dev);
 
 /**
+ * @ingroup kernel
+ *
+ * @param dev The evdev device
+ * @param uniq The new, non-NULL, uniq 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_uniq(struct libevdev *dev, const char *uniq);
+
+/**
  * @ingroup bits
  *
  * @param dev The evdev device, already initialized with libevdev_set_fd()
index a39cb3f..7e607e5 100644 (file)
@@ -441,6 +441,53 @@ START_TEST(test_device_name)
 }
 END_TEST
 
+START_TEST(test_device_set_name)
+{
+       struct uinput_device* uidev;
+       struct libevdev *dev;
+       struct input_id ids = {1, 2, 3, 4};
+       const char *str;
+       int rc;
+
+       dev = libevdev_new();
+
+       libevdev_set_name(dev, "the name");
+       libevdev_set_phys(dev, "the phys");
+       libevdev_set_uniq(dev, "the uniq");
+
+       str = libevdev_get_name(dev);
+       ck_assert(str != NULL);
+       ck_assert_int_eq(strcmp(str, "the name"), 0);
+
+       str = libevdev_get_phys(dev);
+       ck_assert(str != NULL);
+       ck_assert_int_eq(strcmp(str, "the phys"), 0);
+
+       str = libevdev_get_uniq(dev);
+       ck_assert(str != NULL);
+       ck_assert_int_eq(strcmp(str, "the uniq"), 0);
+
+       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));;
+
+       str = libevdev_get_name(dev);
+       ck_assert_int_eq(strcmp(str, TEST_DEVICE_NAME), 0);
+
+       str = libevdev_get_phys(dev);
+       ck_assert(str == NULL);
+
+       str = libevdev_get_uniq(dev);
+       ck_assert(str == NULL);
+
+       uinput_device_free(uidev);
+       libevdev_free(dev);
+}
+END_TEST
+
 START_TEST(test_device_get_abs_info)
 {
        struct uinput_device* uidev;
@@ -876,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_get_abs_info);
        suite_add_tcase(s, tc);