[PATCH libinput] Add a function to retrieve the udev_device handle from a libinput device

Peter Hutterer peter.hutterer at who-t.net
Sun Nov 30 19:45:32 PST 2014


The libinput device abstracts a number of things but sometimes the underlying
device is important. The udev device provides the necessary handle to access
that underlying device and various sysfs properties that may be necessary.

A function returning the device node would've done the same thing but is more
prone to race conditions than the udev_device.

https://bugs.freedesktop.org/show_bug.cgi?id=85573

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 src/evdev.c    |  6 ++++++
 src/evdev.h    |  3 +++
 src/libinput.c |  6 ++++++
 src/libinput.h | 18 ++++++++++++++++++
 test/device.c  | 13 +++++++++++++
 5 files changed, 46 insertions(+)

diff --git a/src/evdev.c b/src/evdev.c
index 5d178fa..dc31840 100644
--- a/src/evdev.c
+++ b/src/evdev.c
@@ -1610,6 +1610,12 @@ evdev_device_get_id_vendor(struct evdev_device *device)
 	return libevdev_get_id_vendor(device->evdev);
 }
 
+struct udev_device *
+evdev_device_get_udev_device(struct evdev_device *device)
+{
+	return udev_device_ref(device->udev_device);
+}
+
 void
 evdev_device_set_default_calibration(struct evdev_device *device,
 				     const float calibration[6])
diff --git a/src/evdev.h b/src/evdev.h
index e4144ec..033d455 100644
--- a/src/evdev.h
+++ b/src/evdev.h
@@ -234,6 +234,9 @@ evdev_device_get_id_product(struct evdev_device *device);
 unsigned int
 evdev_device_get_id_vendor(struct evdev_device *device);
 
+struct udev_device *
+evdev_device_get_udev_device(struct evdev_device *device);
+
 void
 evdev_device_set_default_calibration(struct evdev_device *device,
 				     const float calibration[6]);
diff --git a/src/libinput.c b/src/libinput.c
index c318eee..96b255a 100644
--- a/src/libinput.c
+++ b/src/libinput.c
@@ -1226,6 +1226,12 @@ libinput_device_set_seat_logical_name(struct libinput_device *device,
 							       name);
 }
 
+LIBINPUT_EXPORT struct udev_device *
+libinput_device_get_udev_device(struct libinput_device *device)
+{
+	return evdev_device_get_udev_device((struct evdev_device *)device);
+}
+
 LIBINPUT_EXPORT void
 libinput_device_led_update(struct libinput_device *device,
 			   enum libinput_led leds)
diff --git a/src/libinput.h b/src/libinput.h
index 26d94ff..d567df4 100644
--- a/src/libinput.h
+++ b/src/libinput.h
@@ -1397,6 +1397,24 @@ libinput_device_set_seat_logical_name(struct libinput_device *device,
 				      const char *name);
 
 /**
+ * Return a udev handle to the device that is this libinput device, if any.
+ * The returned handle has a refcount of at least 1, the caller must call
+ * udev_device_unref() once to release the associated resources.
+ *
+ * Some devices may not have a udev device, or the udev device may be
+ * unobtainable. This function returns NULL if no udev device was available.
+ *
+ * Calling this function multiple times for the same device may not
+ * return the same udev handle each time.
+ *
+ * @param device A previously obtained device
+ * @return A udev handle to the device with a refcount of >= 1 or NULL.
+ * @retval NULL This device is not represented by a udev device
+ */
+struct udev_device *
+libinput_device_get_udev_device(struct libinput_device *device);
+
+/**
  * @ingroup device
  *
  * Update the LEDs on the device, if any. If the device does not have
diff --git a/test/device.c b/test/device.c
index 6843824..84984f4 100644
--- a/test/device.c
+++ b/test/device.c
@@ -618,6 +618,17 @@ START_TEST(device_ids)
 }
 END_TEST
 
+START_TEST(device_get_udev_handle)
+{
+	struct litest_device *dev = litest_current_device();
+	struct udev_device *udev_device;
+
+	udev_device = libinput_device_get_udev_device(dev->libinput_device);
+	ck_assert_notnull(udev_device);
+	udev_device_unref(udev_device);
+}
+END_TEST
+
 int main (int argc, char **argv)
 {
 	litest_add("device:sendevents", device_sendevents_config, LITEST_ANY, LITEST_TOUCHPAD);
@@ -640,5 +651,7 @@ int main (int argc, char **argv)
 	litest_add("device:sendevents", device_disable_topsoftbutton, LITEST_TOPBUTTONPAD, LITEST_ANY);
 	litest_add("device:id", device_ids, LITEST_ANY, LITEST_ANY);
 
+	litest_add("device:udev", device_get_udev_handle, LITEST_ANY, LITEST_ANY);
+
 	return litest_run(argc, argv);
 }
-- 
2.1.0



More information about the wayland-devel mailing list