Provide setters for name, phys, uniq

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
This commit is contained in:
Peter Hutterer 2013-07-25 16:11:04 +10:00
parent 699399f653
commit ff9d68af0c
3 changed files with 99 additions and 0 deletions

View file

@ -150,12 +150,15 @@ libevdev_set_fd(struct libevdev* dev, int fd)
if (rc < 0) if (rc < 0)
goto out; goto out;
free(dev->name);
dev->name = strdup(buf); dev->name = strdup(buf);
if (!dev->name) { if (!dev->name) {
errno = ENOSPC; errno = ENOSPC;
goto out; goto out;
} }
free(dev->phys);
dev->phys = NULL;
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf); rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
if (rc < 0) { 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)); memset(buf, 0, sizeof(buf));
rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf); rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
if (rc < 0) { if (rc < 0) {
@ -660,6 +665,19 @@ libevdev_get_uniq(const struct libevdev *dev)
return dev->uniq; 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) int libevdev_get_product_id(const struct libevdev *dev)
{ {
return dev->ids.product; return dev->ids.product;

View file

@ -518,6 +518,17 @@ int libevdev_has_event_pending(struct libevdev *dev);
*/ */
const char* libevdev_get_name(const 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 * @ingroup bits
* *
@ -531,6 +542,17 @@ const char* libevdev_get_name(const struct libevdev *dev);
*/ */
const char * libevdev_get_phys(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 * @ingroup bits
* *
@ -542,6 +564,17 @@ const char * libevdev_get_phys(const struct libevdev *dev);
*/ */
const char * libevdev_get_uniq(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 * @ingroup bits
* *

View file

@ -441,6 +441,53 @@ START_TEST(test_device_name)
} }
END_TEST 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) START_TEST(test_device_get_abs_info)
{ {
struct uinput_device* uidev; struct uinput_device* uidev;
@ -876,6 +923,7 @@ libevdev_has_event_test(void)
tc = tcase_create("device info"); tc = tcase_create("device info");
tcase_add_test(tc, test_device_name); tcase_add_test(tc, test_device_name);
tcase_add_test(tc, test_device_set_name);
tcase_add_test(tc, test_device_get_abs_info); tcase_add_test(tc, test_device_get_abs_info);
suite_add_tcase(s, tc); suite_add_tcase(s, tc);