[PATCH libevdev 4/6] Provide setters for name, phys, uniq
Peter Hutterer
peter.hutterer at who-t.net
Sun Jul 28 22:34:35 PDT 2013
Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
libevdev/libevdev.c | 18 ++++++++++++++++
libevdev/libevdev.h | 33 +++++++++++++++++++++++++++++
test/test-libevdev-has-event.c | 48 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 99 insertions(+)
diff --git a/libevdev/libevdev.c b/libevdev/libevdev.c
index 28ae59f..a7d04a0 100644
--- a/libevdev/libevdev.c
+++ b/libevdev/libevdev.c
@@ -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;
diff --git a/libevdev/libevdev.h b/libevdev/libevdev.h
index 1800b85..aaaa0df 100644
--- a/libevdev/libevdev.h
+++ b/libevdev/libevdev.h
@@ -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()
diff --git a/test/test-libevdev-has-event.c b/test/test-libevdev-has-event.c
index 5321549..e15acb9 100644
--- a/test/test-libevdev-has-event.c
+++ b/test/test-libevdev-has-event.c
@@ -407,6 +407,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;
@@ -839,6 +886,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);
--
1.8.2.1
More information about the Input-tools
mailing list