Add testing get libinput devices list for libinput haltests
authordyamy-lee <dyamy.lee@samsung.com>
Wed, 21 Apr 2021 07:08:45 +0000 (16:08 +0900)
committerduna.oh <duna.oh@samsung.com>
Fri, 27 Jan 2023 05:46:22 +0000 (14:46 +0900)
For saving libinput devices list, create struct about device info(Capability, path) and function for handling libinput add event.
It only saves real devices. It means, it skips uinput device and checks property of udev device.

Change-Id: Ibf42abe728c550e40da13897c02b8abdc7235baa

haltests/meson.build
haltests/test_hal_libinput.cpp
haltests/test_hal_libinput.h
haltests/test_hal_libinput_info.cpp [new file with mode: 0644]
haltests/test_hal_libinput_info.h [new file with mode: 0644]

index dc8d74b..d0a7af1 100644 (file)
@@ -5,6 +5,7 @@ deps_litest = [
 libinput_haltests_srcs = [
     'test_main.cpp',
     'test_hal_libinput.cpp',
+    'test_hal_libinput_info.cpp',
 ]
 
 libinput_haltests_include_dirs = include_directories(
index bfbc237..b5bfd02 100644 (file)
@@ -43,7 +43,7 @@ TEST_F(LibInputHalTest, UdevCreateTest)
 
 TEST_F(LibInputHalTest, GetDeviceListsTest)
 {
-       EXPECT_TRUE(true);
+       EXPECT_TRUE(handle_libinput_add_event(li) != -1);
 }
 
 TEST_F(LibInputHalTest, MouseEventCheckTest)
index 7f00984..8e365c3 100644 (file)
@@ -11,6 +11,8 @@
 #include <unistd.h>
 #include <gtest/gtest.h>
 
+#include "test_hal_libinput_info.h"
+
 class LibInputHalTest : public ::testing::Test
 {
        public:
diff --git a/haltests/test_hal_libinput_info.cpp b/haltests/test_hal_libinput_info.cpp
new file mode 100644 (file)
index 0000000..f626461
--- /dev/null
@@ -0,0 +1,114 @@
+#include "test_hal_libinput_info.h"
+#include <limits.h>
+#include <libevdev/libevdev.h>
+#include <libevdev/libevdev-uinput.h>
+
+struct record_device devices[10];
+int devices_cnt = 0;
+
+#define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
+
+static bool evdev_bit_is_set(const unsigned long *array, int bit)
+{
+    return array[bit / LONG_BIT] & (1LL << (bit % LONG_BIT));
+}
+
+int handle_device_notify(struct libinput_event *ev)
+{
+       struct libinput_device *dev = libinput_event_get_device(ev);
+       struct udev_device *udev_device = libinput_device_get_udev_device(dev);
+       enum libinput_event_type type;
+       const char *devnode;
+
+       switch(libinput_event_get_type(ev)) {
+               case LIBINPUT_EVENT_DEVICE_ADDED:
+                       type = LIBINPUT_EVENT_DEVICE_ADDED;
+                       break;
+               case LIBINPUT_EVENT_DEVICE_REMOVED:
+                       type = LIBINPUT_EVENT_DEVICE_REMOVED;
+                       break;
+               default:
+                       abort();
+       }
+
+       if(strstr(udev_device_get_property_value(udev_device, "DEVPATH"), "virtual")){
+               LOG("It's contain virtual. No Check.\n");
+               return 0;
+       }
+
+       if (libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_KEYBOARD)) {
+               devnode = udev_device_get_devnode(libinput_device_get_udev_device(dev));
+               if(type == LIBINPUT_EVENT_DEVICE_ADDED) { // TODO. save using linked list
+                       const char *test = NULL;
+                       test = udev_device_get_property_value(udev_device, "ID_INPUT_KEYBOARD");
+                       LOG("devnode = %s\n", devnode);
+                       if(test && (test[0] == '1')){
+                               LOG("ID_INPUT_KEYBOARD test = %s\n", test);
+                               devices[devices_cnt].cap = LIBINPUT_DEVICE_CAP_KEYBOARD;
+                               devices[devices_cnt++].path = devnode;
+                               int fd = open(devnode, O_RDWR);
+                               unsigned long evbits[NLONGS(EV_CNT)] = { 0 };
+                               ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), evbits);
+                               if (!evdev_bit_is_set(evbits, EV_KEY))
+                                       LOG("no evdev bit is set\n");
+                               unsigned long keybits[NLONGS(KEY_CNT)] = { 0 };
+                               ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
+                               for(int i = KEY_RESERVED; i< KEY_MIN_INTERESTING; ++i)
+                                       if(evdev_bit_is_set(keybits, i)){
+                                               break;
+                                       }
+                       }
+               }
+       }
+       if (libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_POINTER)) {
+               devnode = udev_device_get_devnode(libinput_device_get_udev_device(dev));
+               if(type == LIBINPUT_EVENT_DEVICE_ADDED) { // TODO. save using linked list
+                       const char *test = NULL;
+                       test = udev_device_get_property_value(udev_device, "ID_INPUT_MOUSE");
+                       LOG("devnode = %s\n", devnode);
+                       if(test && (test[0] == '1')){
+                               LOG("ID_INPUT_MOUSE test = %s\n", test);
+                               devices[devices_cnt].cap = LIBINPUT_DEVICE_CAP_POINTER;
+                               devices[devices_cnt++].path = devnode;
+                       }
+               }
+       }
+       if (libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_TOUCH)) {
+               devnode = udev_device_get_devnode(libinput_device_get_udev_device(dev));
+               if(type == LIBINPUT_EVENT_DEVICE_ADDED) { // TODO. save using linked list
+                       const char *test = NULL;
+                       test = udev_device_get_property_value(udev_device, "ID_INPUT_TOUCHSCREEN");
+                       LOG("devnode = %s\n", devnode);
+                       if(test && (test[0] == '1')){
+                               LOG("ID_INPUT_TOUCHSCREEN test = %s\n", test);
+                               devices[devices_cnt].cap = LIBINPUT_DEVICE_CAP_TOUCH;
+                               devices[devices_cnt++].path = devnode;
+                       }
+               }
+       }
+       LOG("total device cnt = %d\n", devices_cnt);
+       return 0;
+}
+
+int handle_libinput_add_event(struct libinput *li)
+{
+       devices_cnt = 0;
+       struct libinput_event *ev;
+       libinput_dispatch(li);
+       while((ev = libinput_get_event(li))) {
+               switch (libinput_event_get_type(ev))
+               {
+                       case LIBINPUT_EVENT_NONE:
+                               abort();
+                       case LIBINPUT_EVENT_DEVICE_ADDED:
+                       case LIBINPUT_EVENT_DEVICE_REMOVED:
+                               handle_device_notify(ev);
+                               break;
+                       default:
+                               break;
+               }
+               libinput_event_destroy(ev);
+               libinput_dispatch(li);
+       }
+       return 0;
+}
diff --git a/haltests/test_hal_libinput_info.h b/haltests/test_hal_libinput_info.h
new file mode 100644 (file)
index 0000000..484989c
--- /dev/null
@@ -0,0 +1,52 @@
+
+#ifndef __LIBINPUT_TESTS_INFO_H__
+#define __LIBINPUT_TESTS_INFO_H__
+
+#include <libinput.h>
+#include <stdio.h>
+#include <gtest/gtest.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <linux/input.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <time.h>
+#include <poll.h>
+#include <dlog.h>
+#include <string.h>
+
+#define COLOR_RED "\x1b[31m" /* for error */
+#define COLOR_RESET "\x1b[0m"
+
+#define DEBUG_STDOUT
+#ifdef DEBUG_STDOUT
+#undef LOG_TAG
+#undef LOG
+#undef LOGD
+#undef LOGE
+#define LOG_TAG "TEST_HAL_LIBINPUT"
+
+#define LOG(fmt, args...) \
+                       LOGD(fmt, ##args)
+#define LOGD(fmt, args...) \
+                       dlog_print(DLOG_DEBUG, LOG_TAG, COLOR_RESET "%s : %s(%d) " fmt, __FILE__, __func__, __LINE__, ##args)
+#define LOGE(fmt, args...) \
+                       dlog_print(DLOG_ERROR, LOG_TAG, COLOR_RED "%s : %s(%d) " fmt, __FILE__, __func__, __LINE__, ##args)
+#endif
+
+int handle_libinput_add_event(struct libinput *li);
+
+struct record_device {
+       enum libinput_device_capability cap;
+       const char *path;
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // __LIBINPUT_TESTS_INFO_H__
\ No newline at end of file