From 23f1b40dbf6223b4b8af3845efe64b076998b2d7 Mon Sep 17 00:00:00 2001
From: nomike <nomike@nomike.com>
Date: Sat, 12 Jul 2025 03:14:16 +0200
Subject: [PATCH] Add CMake module for finding HidAPI library

---
 cmake/modules/FindHidAPI.cmake | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100644 cmake/modules/FindHidAPI.cmake

diff --git a/cmake/modules/FindHidAPI.cmake b/cmake/modules/FindHidAPI.cmake
new file mode 100644
index 000000000..0fdcecf88
--- /dev/null
+++ b/cmake/modules/FindHidAPI.cmake
@@ -0,0 +1,30 @@
+find_package(PkgConfig)
+pkg_search_module(PC_HIDAPI QUIET hidapi hidapi-libusb)
+
+find_path(HIDAPI_INCLUDE_DIR NAMES hidapi.h
+	HINTS
+	${PC_HIDAPI_INCLUDEDIR}
+	${PC_HIDAPI_INCLUDE_DIRS})
+
+find_library(HIDAPI_LIBRARY NAMES hidapi hidapi-libusb
+	HINTS
+	${PC_HIDAPI_LIBDIR}
+	${PC_HIDAPI_LIBRARY_DIRS})
+
+if(HIDAPI_INCLUDE_DIR AND EXISTS "${HIDAPI_INCLUDE_DIR}/hidapi.h")
+  file(STRINGS "${HIDAPI_INCLUDE_DIR}/hidapi.h" hidapi_version_major
+      REGEX "^#define[\t ]+HID_API_VERSION_MAJOR[\t ]+[0-9]+")
+  file(STRINGS "${HIDAPI_INCLUDE_DIR}/hidapi.h" hidapi_version_minor
+      REGEX "^#define[\t ]+HID_API_VERSION_MINOR[\t ]+[0-9]+")
+  file(STRINGS "${HIDAPI_INCLUDE_DIR}/hidapi.h" hidapi_version_patch
+      REGEX "^#define[\t ]+HID_API_VERSION_PATCH[\t ]+[0-9]+")
+  string(REGEX REPLACE "[^0-9.]" "" HIDAPI_VERSION_STRING "${hidapi_version_major}.${hidapi_version_minor}.${hidapi_version_patch}")
+  unset(hidapi_version_major)
+  unset(hidapi_version_minor)
+  unset(hidapi_version_patch)
+endif()
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(HidAPI REQUIRED_VARS HIDAPI_LIBRARY HIDAPI_INCLUDE_DIR)
+
+mark_as_advanced(HIDAPI_INCLUDE_DIR HIDAPI_LIBRARY)
-- 
2.50.0

