From: Peter Hutterer Date: Wed, 22 Apr 2015 04:36:25 +0000 (+1000) Subject: Add libinput_device_keyboard_has_key() X-Git-Tag: 0.15.0~47 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4996076a7c59690ad02162923b23b763a3b76bcd;p=platform%2Fupstream%2Flibinput.git Add libinput_device_keyboard_has_key() Similar to libinput_device_pointer_has_button(), this function returns whether a given device has a specific keycode. This enables a caller to determine if the device is really a keyboard (check for KEY_A-KEY_Z) or just a media key device (check for KEY_PLAY or somesuch), depending on the context required. Signed-off-by: Peter Hutterer Reviewed-by: Hans de Goede Reviewed-by: Derek Foreman --- diff --git a/src/evdev.c b/src/evdev.c index 8774a990..964b3ba1 100644 --- a/src/evdev.c +++ b/src/evdev.c @@ -2148,6 +2148,15 @@ evdev_device_has_button(struct evdev_device *device, uint32_t code) return libevdev_has_event_code(device->evdev, EV_KEY, code); } +int +evdev_device_has_key(struct evdev_device *device, uint32_t code) +{ + if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD)) + return -1; + + return libevdev_has_event_code(device->evdev, EV_KEY, 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 9969d515..9f244534 100644 --- a/src/evdev.h +++ b/src/evdev.h @@ -316,6 +316,9 @@ evdev_device_get_size(struct evdev_device *device, int evdev_device_has_button(struct evdev_device *device, uint32_t code); +int +evdev_device_has_key(struct evdev_device *device, uint32_t code); + double evdev_device_transform_x(struct evdev_device *device, double x, diff --git a/src/libinput.c b/src/libinput.c index 7dcf2965..5ef7edfb 100644 --- a/src/libinput.c +++ b/src/libinput.c @@ -1547,6 +1547,12 @@ libinput_device_pointer_has_button(struct libinput_device *device, uint32_t code return evdev_device_has_button((struct evdev_device *)device, code); } +LIBINPUT_EXPORT int +libinput_device_keyboard_has_key(struct libinput_device *device, uint32_t code) +{ + return evdev_device_has_key((struct evdev_device *)device, code); +} + LIBINPUT_EXPORT struct libinput_event * libinput_event_device_notify_get_base_event(struct libinput_event_device_notify *event) { diff --git a/src/libinput.h b/src/libinput.h index b4b373e8..9ef8b8f4 100644 --- a/src/libinput.h +++ b/src/libinput.h @@ -1708,6 +1708,22 @@ libinput_device_get_size(struct libinput_device *device, int libinput_device_pointer_has_button(struct libinput_device *device, uint32_t code); +/** + * @ingroup device + * + * Check if a @ref LIBINPUT_DEVICE_CAP_KEYBOARD device has a key with the + * given code (see linux/input.h). + * + * @param device A current input device + * @param code Key code to check for, e.g. KEY_ESC + * + * @return 1 if the device supports this key code, 0 if it does not, -1 + * on error. + */ +int +libinput_device_keyboard_has_key(struct libinput_device *device, + uint32_t code); + /** * @ingroup device * diff --git a/src/libinput.sym b/src/libinput.sym index 431870f4..9c11174b 100644 --- a/src/libinput.sym +++ b/src/libinput.sym @@ -135,3 +135,8 @@ global: libinput_device_config_middle_emulation_is_available; libinput_device_config_middle_emulation_set_enabled; } LIBINPUT_0.12.0; + +LIBINPUT_0.15.0 { +global: + libinput_device_keyboard_has_key; +} LIBINPUT_0.14.0; diff --git a/test/keyboard.c b/test/keyboard.c index cb7ad522..a477cfb7 100644 --- a/test/keyboard.c +++ b/test/keyboard.c @@ -290,12 +290,32 @@ START_TEST(keyboard_key_auto_release) } END_TEST +START_TEST(keyboard_has_key) +{ + struct litest_device *dev = litest_current_device(); + struct libinput_device *device = dev->libinput_device; + unsigned int code; + int evdev_has, libinput_has; + + ck_assert(libinput_device_has_capability( + device, + LIBINPUT_DEVICE_CAP_KEYBOARD)); + + for (code = 0; code < KEY_CNT; code++) { + evdev_has = libevdev_has_event_code(dev->evdev, EV_KEY, code); + libinput_has = libinput_device_keyboard_has_key(device, code); + ck_assert_int_eq(evdev_has, libinput_has); + } +} +END_TEST + int main(int argc, char **argv) { litest_add_no_device("keyboard:seat key count", keyboard_seat_key_count); litest_add_no_device("keyboard:key counting", keyboard_ignore_no_pressed_release); litest_add_no_device("keyboard:key counting", keyboard_key_auto_release); + litest_add("keyboard:keys", keyboard_has_key, LITEST_KEYS, LITEST_ANY); return litest_run(argc, argv); }