Skip to content

Instantly share code, notes, and snippets.

@punesemu
Created October 26, 2022 23:50
Show Gist options
  • Save punesemu/823502d381db771dae9dd562426fbae1 to your computer and use it in GitHub Desktop.
Save punesemu/823502d381db771dae9dd562426fbae1 to your computer and use it in GitHub Desktop.
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c981f90..4b1f9e4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -72,4 +72,9 @@
endif()
+if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
+ find_package(Sndio REQUIRED)
+ find_package(UsbHid REQUIRED)
+endif()
+
if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_SYSTEM_NAME STREQUAL "OpenBSD" OR CMAKE_SYSTEM_NAME STREQUAL "Linux")
find_package(X11)
diff --git a/cmake/FindSndio.cmake b/cmake/FindSndio.cmake
new file mode 100644
index 0000000..5418677
--- /dev/null
+++ b/cmake/FindSndio.cmake
@@ -0,0 +1,16 @@
+check_include_file(sndio.h HAVE_SNDIO_H)
+if (NOT HAVE_SNDIO_H)
+ message(FATAL_ERROR "sndio.h not found")
+endif()
+
+find_library(SNDIO_LIB sndio)
+if (NOT SNDIO_LIB)
+ message(FATAL_ERROR "libsndio library not found")
+endif()
+
+if(NOT TARGET Sndio::Sndio)
+ add_library(Sndio::Sndio UNKNOWN IMPORTED)
+ set_target_properties(Sndio::Sndio PROPERTIES
+ IMPORTED_LOCATION "${SNDIO_LIB}"
+ )
+endif()
diff --git a/cmake/FindUsbHid.cmake b/cmake/FindUsbHid.cmake
new file mode 100644
index 0000000..606814f
--- /dev/null
+++ b/cmake/FindUsbHid.cmake
@@ -0,0 +1,156 @@
+include(CheckLibraryExists)
+include(CheckCSourceCompiles)
+
+set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
+set(ORIG_CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
+
+set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -include stdint.h")
+
+check_library_exists(usbhid hid_init "" LIBUSBHID)
+
+if(LIBUSBHID)
+ check_include_file(usbhid.h HAVE_USBHID_H)
+ if(HAVE_USBHID_H)
+ add_definitions(-DHAVE_USBHID_H)
+ set(USB_CFLAGS -DHAVE_USBHID_H)
+ endif()
+
+ check_include_file(libusbhid.h HAVE_LIBUSBHID_H)
+ if(HAVE_LIBUSBHID_H)
+ add_definitions(-DHAVE_LIBUSBHID_H)
+ set(USB_CFLAGS "${USB_CFLAGS} -DHAVE_LIBUSBHID_H")
+ endif()
+ set(LIBUSBHID usbhid)
+else()
+ check_include_file(usb.h HAVE_USB_H)
+ if(HAVE_USB_H)
+ set(USB_CFLAGS -DHAVE_USB_H)
+ endif()
+ check_include_file(libusb.h HAVE_LIBUSB_H)
+ if(HAVE_LIBUSB_H)
+ set(USB_CFLAGS "${USB_CFLAGS} -DHAVE_LIBUSB_H")
+ endif()
+ check_library_exists(usb hid_init "" LIBUSBHID)
+ if(LIBUSBHID)
+ set(LIBUSBHID usb)
+ endif()
+endif()
+
+if (NOT LIBUSBHID)
+ message(FATAL_ERROR "usb library not_found")
+endif()
+
+find_library(USBHID_LIB ${LIBUSBHID})
+if(NOT USBHID_LIB)
+ message(FATAL_ERROR "usb library not_found")
+endif()
+set(USB_LIBS ${USBHID_LIB})
+
+set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS} ${USB_CFLAGS}")
+set(CMAKE_REQUIRED_LIBRARIES "${USB_LIBS}")
+
+check_c_source_compiles("
+ #include <sys/types.h>
+ #if defined(HAVE_USB_H)
+ #include <usb.h>
+ #endif
+ #ifdef __DragonFly__
+ # include <bus/u4b/usb.h>
+ # include <bus/u4b/usbhid.h>
+ #else
+ # include <dev/usb/usb.h>
+ # include <dev/usb/usbhid.h>
+ #endif
+ #if defined(HAVE_USBHID_H)
+ #include <usbhid.h>
+ #elif defined(HAVE_LIBUSB_H)
+ #include <libusb.h>
+ #elif defined(HAVE_LIBUSBHID_H)
+ #include <libusbhid.h>
+ #endif
+ int main(int argc, char **argv) {
+ struct report_desc *repdesc;
+ struct usb_ctl_report *repbuf;
+ hid_kind_t hidkind;
+ return 0;
+ }" HAVE_USBHID)
+if(HAVE_USBHID)
+ check_c_source_compiles("
+ #include <sys/types.h>
+ #if defined(HAVE_USB_H)
+ #include <usb.h>
+ #endif
+ #ifdef __DragonFly__
+ # include <bus/u4b/usb.h>
+ # include <bus/u4b/usbhid.h>
+ #else
+ # include <dev/usb/usb.h>
+ # include <dev/usb/usbhid.h>
+ #endif
+ #if defined(HAVE_USBHID_H)
+ #include <usbhid.h>
+ #elif defined(HAVE_LIBUSB_H)
+ #include <libusb.h>
+ #elif defined(HAVE_LIBUSBHID_H)
+ #include <libusbhid.h>
+ #endif
+ int main(int argc, char** argv) {
+ struct usb_ctl_report buf;
+ if (buf.ucr_data) { }
+ return 0;
+ }" HAVE_USBHID_UCR_DATA)
+ if(HAVE_USBHID_UCR_DATA)
+ add_definitions(-DUSBHID_UCR_DATA)
+ set(USB_CFLAGS "${USB_CFLAGS} -DUSBHID_UCR_DATA")
+ endif()
+
+ check_c_source_compiles("
+ #include <sys/types.h>
+ #if defined(HAVE_USB_H)
+ #include <usb.h>
+ #endif
+ #ifdef __DragonFly__
+ #include <bus/u4b/usb.h>
+ #include <bus/u4b/usbhid.h>
+ #else
+ #include <dev/usb/usb.h>
+ #include <dev/usb/usbhid.h>
+ #endif
+ #if defined(HAVE_USBHID_H)
+ #include <usbhid.h>
+ #elif defined(HAVE_LIBUSB_H)
+ #include <libusb.h>
+ #elif defined(HAVE_LIBUSBHID_H)
+ #include <libusbhid.h>
+ #endif
+ int main(int argc, char **argv) {
+ report_desc_t d;
+ hid_start_parse(d, 1, 1);
+ return 0;
+ }" HAVE_USBHID_NEW)
+ if(HAVE_USBHID_NEW)
+ add_definitions(-DUSBHID_NEW)
+ set(USB_CFLAGS "${USB_CFLAGS} -DUSBHID_NEW")
+ endif()
+
+ check_c_source_compiles("
+ #include <machine/joystick.h>
+ int main(int argc, char** argv) {
+ struct joystick t;
+ return 0;
+ }" HAVE_MACHINE_JOYSTICK)
+ if(HAVE_MACHINE_JOYSTICK)
+ add_definitions(-DUSBHID_MACHINE_JOYSTICK)
+ set(USB_CFLAGS "${USB_CFLAGS} -DUSBHID_MACHINE_JOYSTICK")
+ endif()
+
+ set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS}")
+ set(CMAKE_REQUIRED_LIBRARIES "${ORIG_CMAKE_REQUIRED_LIBRARIES}")
+
+ if(NOT TARGET UsbHid::UsbHid)
+ add_library(UsbHid::UsbHid UNKNOWN IMPORTED)
+ set_target_properties(UsbHid::UsbHid PROPERTIES
+ IMPORTED_LOCATION "${USB_LIBS}"
+ )
+ endif()
+endif()
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index f922cd8..54a698f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -564,7 +564,10 @@
$<$<PLATFORM_ID:Linux>:
- gui/linux/monitor.c
gui/linux/os_jstick.c
>
+
+ $<$<AND:$<PLATFORM_ID:FreeBSD,OpenBSD,Linux>,$<BOOL:${X11_FOUND}>>:
+ gui/linux/monitor.c
+ >
)
@@ -619,5 +622,5 @@
"${PROJECT_BINARY_DIR}"
- $<$<PLATFORM_ID:FreeBSD>:${CMAKE_CURRENT_SOURCE_DIR}/gui/bsd>
+ $<$<PLATFORM_ID:FreeBSD,OpenBSD>:${CMAKE_CURRENT_SOURCE_DIR}/gui/bsd>
$<$<PLATFORM_ID:Linux>:${CMAKE_CURRENT_SOURCE_DIR}/gui/linux>
$<$<PLATFORM_ID:Windows>:${CMAKE_CURRENT_SOURCE_DIR}/gui/windows>
@@ -632,11 +635,11 @@
Threads::Threads
$<$<BOOL:${ENABLE_FFMPEG}>:PkgConfig::LIBAV>
+ $<$<PLATFORM_ID:FreeBSD,OpenBSD>:Sndio::Sndio>
+ $<$<PLATFORM_ID:FreeBSD,OpenBSD>:UsbHid::UsbHid>
$<$<PLATFORM_ID:Linux>:ALSA::ALSA>
$<$<PLATFORM_ID:Linux>:PkgConfig::UDEV>
$<$<BOOL:${X11_FOUND}>:X11::Xrandr>
- $<$<BOOL:${ENABLE_OPENGL}>:
- OpenGL::OpenGL
- OpenGL::GLX
- >
+ $<$<BOOL:${ENABLE_OPENGL}>:OpenGL::GL>
+ $<$<BOOL:${ENABLE_OPENGL}>:$<$<PLATFORM_ID:Linux>:OpenGL::GLX>>
$<$<BOOL:${ENABLE_OPENGL_CG}>:Cg::CgGL>
lib7zip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment