Add libinput_device_keyboard_has_key()
authorPeter Hutterer <peter.hutterer@who-t.net>
Wed, 22 Apr 2015 04:36:25 +0000 (14:36 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Thu, 23 Apr 2015 00:32:31 +0000 (10:32 +1000)
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 <peter.hutterer@who-t.net>
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
Reviewed-by: Derek Foreman <derekf@osg.samsung.com>
src/evdev.c
src/evdev.h
src/libinput.c
src/libinput.h
src/libinput.sym
test/keyboard.c

index 8774a990da4b9559829891b5151159562b66da74..964b3ba1647995866aaefbc685ca07df761252bd 100644 (file)
@@ -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)
index 9969d5158f68f4efa9cc15f5a3142db87e442470..9f2445348678a6d755fc0e591a4d6d7130edd3e1 100644 (file)
@@ -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,
index 7dcf29657056ce19781ac7320576e753809456b4..5ef7edfb2e10fdf8200dd9f307cd4edca5f486f7 100644 (file)
@@ -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)
 {
index b4b373e8409c1c8ec7c1d7f60abd42ac412a7325..9ef8b8f42b985ac6b64d7ba68b9d578eda9b1246 100644 (file)
@@ -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. <i>KEY_ESC</i>
+ *
+ * @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
  *
index 431870f40289e807d8c3956f17359159a438af8c..9c11174bb576abed56f2888553f36033de9d29d6 100644 (file)
@@ -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;
index cb7ad52203124df57f2178a45429832d28a52d5f..a477cfb77991710df55629e5815b1d774f5e4d0f 100644 (file)
@@ -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);
 }