[PATCH libinput] Add libinput_device_switch_has_switch()

Peter Hutterer peter.hutterer at who-t.net
Mon Sep 4 05:25:07 UTC 2017


Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 src/evdev.c        | 20 ++++++++++++++++++++
 src/evdev.h        |  4 ++++
 src/libinput.c     |  7 +++++++
 src/libinput.h     | 16 ++++++++++++++++
 src/libinput.sym   |  4 ++++
 test/test-switch.c | 11 +++++++++++
 6 files changed, 62 insertions(+)

diff --git a/src/evdev.c b/src/evdev.c
index 539bc3c0..d08db900 100644
--- a/src/evdev.c
+++ b/src/evdev.c
@@ -3417,6 +3417,26 @@ evdev_device_has_key(struct evdev_device *device, uint32_t code)
 	return libevdev_has_event_code(device->evdev, EV_KEY, code);
 }
 
+int
+evdev_device_has_switch(struct evdev_device *device,
+			enum libinput_switch sw)
+{
+	unsigned int code;
+
+	if (!(device->seat_caps & EVDEV_DEVICE_SWITCH))
+		return -1;
+
+	switch (sw) {
+	case LIBINPUT_SWITCH_LID:
+		code = SW_LID;
+		break;
+	default:
+		return -1;
+	}
+
+	return libevdev_has_event_code(device->evdev, EV_SW, code);
+}
+
 static inline bool
 evdev_is_scrolling(const struct evdev_device *device,
 		   enum libinput_pointer_axis axis)
diff --git a/src/evdev.h b/src/evdev.h
index a7f7ff11..ef3f1213 100644
--- a/src/evdev.h
+++ b/src/evdev.h
@@ -490,6 +490,10 @@ int
 evdev_device_has_key(struct evdev_device *device, uint32_t code);
 
 int
+evdev_device_has_switch(struct evdev_device *device,
+			enum libinput_switch sw);
+
+int
 evdev_device_tablet_pad_get_num_buttons(struct evdev_device *device);
 
 int
diff --git a/src/libinput.c b/src/libinput.c
index 1ca737a0..49de8f27 100644
--- a/src/libinput.c
+++ b/src/libinput.c
@@ -2937,6 +2937,13 @@ libinput_device_keyboard_has_key(struct libinput_device *device, uint32_t code)
 }
 
 LIBINPUT_EXPORT int
+libinput_device_switch_has_switch(struct libinput_device *device,
+				  enum libinput_switch sw)
+{
+	return evdev_device_has_switch((struct evdev_device *)device, sw);
+}
+
+LIBINPUT_EXPORT int
 libinput_device_tablet_pad_get_num_buttons(struct libinput_device *device)
 {
 	return evdev_device_tablet_pad_get_num_buttons((struct evdev_device *)device);
diff --git a/src/libinput.h b/src/libinput.h
index 4a15affc..0a9c9d87 100644
--- a/src/libinput.h
+++ b/src/libinput.h
@@ -3699,6 +3699,22 @@ libinput_device_keyboard_has_key(struct libinput_device *device,
 /**
  * @ingroup device
  *
+ * Check if a @ref LIBINPUT_DEVICE_CAP_SWITCH device has a switch of the
+ * given type.
+ *
+ * @param device A current input device
+ * @param sw Switch to check for
+ *
+ * @return 1 if the device supports this switch, 0 if it does not, -1
+ * on error.
+ */
+int
+libinput_device_switch_has_switch(struct libinput_device *device,
+				  enum libinput_switch sw);
+
+/**
+ * @ingroup device
+ *
  * Return the number of buttons on a device with the
  * @ref LIBINPUT_DEVICE_CAP_TABLET_PAD capability.
  * Buttons on a pad device are numbered sequentially, see @ref
diff --git a/src/libinput.sym b/src/libinput.sym
index 6fd14e37..bb283407 100644
--- a/src/libinput.sym
+++ b/src/libinput.sym
@@ -289,3 +289,7 @@ LIBINPUT_1.7 {
 	libinput_event_switch_get_time;
 	libinput_event_switch_get_time_usec;
 } LIBINPUT_1.5;
+
+LIBINPUT_1.9 {
+	libinput_device_switch_has_switch;
+} LIBINPUT_1.7;
diff --git a/test/test-switch.c b/test/test-switch.c
index 108b3a85..f7a3e9c0 100644
--- a/test/test-switch.c
+++ b/test/test-switch.c
@@ -29,6 +29,16 @@
 #include "libinput-util.h"
 #include "litest.h"
 
+START_TEST(lid_switch_has)
+{
+	struct litest_device *dev = litest_current_device();
+
+	ck_assert_int_eq(libinput_device_switch_has_switch(dev->libinput_device,
+							   LIBINPUT_SWITCH_LID),
+			 1);
+}
+END_TEST
+
 START_TEST(lid_switch)
 {
 	struct litest_device *dev = litest_current_device();
@@ -618,6 +628,7 @@ END_TEST
 void
 litest_setup_tests_lid(void)
 {
+	litest_add("lid:switch", lid_switch_has, LITEST_SWITCH, LITEST_ANY);
 	litest_add("lid:switch", lid_switch, LITEST_SWITCH, LITEST_ANY);
 	litest_add("lid:switch", lid_switch_double, LITEST_SWITCH, LITEST_ANY);
 	litest_add("lid:switch", lid_switch_down_on_init, LITEST_SWITCH, LITEST_ANY);
-- 
2.13.5



More information about the wayland-devel mailing list