Signed-off-by: Peter Hutterer <[email protected]>
---
 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 2d18bed..aa73fa1 100644
--- a/src/evdev.c
+++ b/src/evdev.c
@@ -3136,6 +3136,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 465b8a3..4a5f2ec 100644
--- a/src/evdev.h
+++ b/src/evdev.h
@@ -468,6 +468,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 514a611..72cd839 100644
--- a/src/libinput.c
+++ b/src/libinput.c
@@ -2942,6 +2942,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 aa30cfa..088949b 100644
--- a/src/libinput.h
+++ b/src/libinput.h
@@ -3648,6 +3648,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 6fd14e3..e515ada 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.8 {
+       libinput_device_switch_has_switch;
+} LIBINPUT_1.7;
diff --git a/test/test-switch.c b/test/test-switch.c
index 3c5cae7..99c85cd 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 *sw = litest_current_device();
+
+       ck_assert_int_eq(libinput_device_switch_has_switch(sw->libinput_device,
+                                                          LIBINPUT_SWITCH_LID),
+                        1);
+}
+END_TEST
+
 START_TEST(lid_switch)
 {
        struct litest_device *sw = litest_current_device();
@@ -492,6 +502,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.9.3

_______________________________________________
wayland-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/wayland-devel

Reply via email to