From d9a729e1a77335d72296f7e44dc906b70e801ac8 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Fri, 21 Apr 2017 16:57:39 +1000 Subject: [PATCH] Add libinput_device_switch_has_switch() Signed-off-by: Peter Hutterer --- 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 b4e83194..8df7ca25 100644 --- a/src/evdev.h +++ b/src/evdev.h @@ -489,6 +489,10 @@ evdev_device_has_button(struct evdev_device *device, uint32_t code); 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); diff --git a/src/libinput.c b/src/libinput.c index 1ca737a0..49de8f27 100644 --- a/src/libinput.c +++ b/src/libinput.c @@ -2936,6 +2936,13 @@ libinput_device_keyboard_has_key(struct libinput_device *device, uint32_t code) return evdev_device_has_key((struct evdev_device *)device, 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) { diff --git a/src/libinput.h b/src/libinput.h index 4a15affc..0a9c9d87 100644 --- a/src/libinput.h +++ b/src/libinput.h @@ -3696,6 +3696,22 @@ int libinput_device_keyboard_has_key(struct libinput_device *device, uint32_t code); +/** + * @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 * 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 b7d69bc7..45efeb02 100644 --- a/test/test-switch.c +++ b/test/test-switch.c @@ -39,6 +39,16 @@ START_TEST(switch_has_cap) } END_TEST +START_TEST(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(switch_toggle) { struct litest_device *dev = litest_current_device(); @@ -605,6 +615,7 @@ void litest_setup_tests_lid(void) { litest_add("switch:has", switch_has_cap, LITEST_SWITCH, LITEST_ANY); + litest_add("switch:has", switch_has, LITEST_SWITCH, LITEST_ANY); litest_add("switch:toggle", switch_toggle, LITEST_SWITCH, LITEST_ANY); litest_add("switch:toggle", switch_toggle_double, LITEST_SWITCH, LITEST_ANY); litest_add("switch:toggle", switch_down_on_init, LITEST_SWITCH, LITEST_ANY);