From 4318b73549506f88100c4c5a83edd718f0dea6d9 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Mon, 3 Jun 2013 13:59:06 +1000 Subject: [PATCH] Expose phys/uniq to the caller Signed-off-by: Peter Hutterer --- libevdev/libevdev-int.h | 2 ++ libevdev/libevdev.c | 44 +++++++++++++++++++++++++++++++++++++++ libevdev/libevdev.h | 16 ++++++++++++++ man/Makefile.am | 1 + man/libevdev_get_phys.txt | 41 ++++++++++++++++++++++++++++++++++++ test/libevdev-print.c | 2 ++ 6 files changed, 106 insertions(+) create mode 100644 man/libevdev_get_phys.txt diff --git a/libevdev/libevdev-int.h b/libevdev/libevdev-int.h index b36450c..219d555 100644 --- a/libevdev/libevdev-int.h +++ b/libevdev/libevdev-int.h @@ -53,6 +53,8 @@ struct libevdev { libevdev_log_func_t log; char name[MAX_NAME]; + char *phys; + char *uniq; struct input_id ids; int driver_version; unsigned long bits[NLONGS(EV_CNT)]; diff --git a/libevdev/libevdev.c b/libevdev/libevdev.c index 0bac662..0d4600b 100644 --- a/libevdev/libevdev.c +++ b/libevdev/libevdev.c @@ -180,6 +180,8 @@ libevdev_new_from_fd(int fd, struct libevdev **dev) void libevdev_free(struct libevdev *dev) { + free(dev->phys); + free(dev->uniq); queue_free(dev); free(dev); } @@ -204,6 +206,7 @@ libevdev_set_fd(struct libevdev* dev, int fd) { int rc; int i; + char buf[256]; if (dev->fd != -1) return -EBADF; @@ -216,6 +219,35 @@ libevdev_set_fd(struct libevdev* dev, int fd) if (rc < 0) goto out; + memset(buf, 0, sizeof(buf)); + rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf); + if (rc < 0) { + /* uinput has no phys */ + if (errno != ENOENT) + goto out; + } else { + dev->phys = calloc(strlen(buf) + 1, sizeof(char)); + if (!dev->phys) { + errno = ENOSPC; + goto out; + } + strcpy(dev->phys, buf); + } + + memset(buf, 0, sizeof(buf)); + rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf); + if (rc < 0) { + if (errno != ENOENT) + goto out; + } else { + dev->uniq = calloc(strlen(buf) + 1, sizeof(char)); + if (!dev->uniq) { + errno = ENOSPC; + goto out; + } + strcpy(dev->uniq, buf); + } + rc = ioctl(fd, EVIOCGID, &dev->ids); if (rc < 0) goto out; @@ -588,6 +620,18 @@ libevdev_get_name(const struct libevdev *dev) return dev->name; } +const char * +libevdev_get_phys(const struct libevdev *dev) +{ + return dev->phys; +} + +const char * +libevdev_get_uniq(const struct libevdev *dev) +{ + return dev->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 169c0bc..79c13b3 100644 --- a/libevdev/libevdev.h +++ b/libevdev/libevdev.h @@ -181,6 +181,22 @@ int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_e */ const char* libevdev_get_name(const struct libevdev *dev); +/** + * Virtual devices such as uinput devices have no phys location. + * + * @return The physical location of this device, or NULL if there is none + * + * @note This function is signal safe. + */ +const char * libevdev_get_phys(const struct libevdev *dev); + +/** + * @return The unique identifier for this device, or NULL if there is none + * + * @note This function is signal safe. + */ +const char * libevdev_get_uniq(const struct libevdev *dev); + /** * @return The device's product ID * diff --git a/man/Makefile.am b/man/Makefile.am index 13fcede..d80a7cc 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -9,6 +9,7 @@ man_src = \ libevdev_get_abs_min.txt \ libevdev_get_name.txt \ libevdev_get_num_slots.txt \ + libevdev_get_phys.txt \ libevdev_grab.txt \ libevdev_has_event_type.txt \ libevdev_new.txt \ diff --git a/man/libevdev_get_phys.txt b/man/libevdev_get_phys.txt new file mode 100644 index 0000000..653ff49 --- /dev/null +++ b/man/libevdev_get_phys.txt @@ -0,0 +1,41 @@ +libevdev_get_phys(3) +==================== + +NAME +---- + +libevdev_get_phys, libevdev_get_uniq - retreive the physical location and unique identifier for this device + +SYNOPSIS +-------- + +#include + + +DESCRIPTION +----------- + +*libevdev_get_phys* returns a pointer to a string representing the physical +location of this device. For virtual devices such as those created by +uinput, no physical location is available. + +*libevdev_get_uniq* returns a pointer to a uniq identifier of this device. +Not all devices have such an identifier. + +PARAMETERS +---------- +*dev*:: + Pointer to the libevdev device. + +RETURN VALUE +------------ +*libevdev_get_phys* returns a pointer to a string representing the physical +location of this device, or NULL if none is defined. + +*libevdev_get_uniq* returns a pointer to a uniq identifier of this device, +or NULL if none is defined. + +SEE ALSO +-------- + +libevdev_get_name(3) diff --git a/test/libevdev-print.c b/test/libevdev-print.c index 5d955a7..e6ecee0 100644 --- a/test/libevdev-print.c +++ b/test/libevdev-print.c @@ -137,6 +137,8 @@ main(int argc, char **argv) libevdev_get_product_id(dev)); printf("Evdev version: %x\n", libevdev_get_driver_version(dev)); printf("Input device name: \"%s\"\n", libevdev_get_name(dev)); + printf("Phys location: %s\n", libevdev_get_phys(dev)); + printf("Uniq identifier: %s\n", libevdev_get_uniq(dev)); print_bits(dev); print_props(dev);