From e7ad49292766ccf815dae35606199a2c4410904d Mon Sep 17 00:00:00 2001
From: phil <me@filou.se>
Date: Mon, 22 Mar 2021 16:19:10 +0100
Subject: [PATCH] removed unnecessary submodules and makefile cleanup, macos
 claim bugfix

---
 .gitmodules                |  6 ------
 Makefile.linux             |  6 +++---
 Makefile.macos             | 41 +++-----------------------------------
 README.md                  |  8 +++++---
 deps/USB-HID-Report-Parser |  2 +-
 deps/hidapi                |  1 -
 deps/libusb                |  1 -
 src/hid/hid.c              | 11 +++++++---
 8 files changed, 20 insertions(+), 56 deletions(-)
 delete mode 160000 deps/hidapi
 delete mode 160000 deps/libusb

diff --git a/.gitmodules b/.gitmodules
index 42bd8d2..dcd1e13 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,12 +1,6 @@
 [submodule "deps/pd-lib-builder"]
 	path = deps/pd-lib-builder
 	url = https://github.com/pure-data/pd-lib-builder/
-[submodule "deps/libusb"]
-	path = deps/libusb
-	url = https://github.com/libusb/libusb
-[submodule "deps/hidapi"]
-	path = deps/hidapi
-	url = https://github.com/libusb/hidapi
 [submodule "deps/USB-HID-Report-Parser"]
 	path = deps/USB-HID-Report-Parser
 	url = https://github.com/tschiemer/USB-HID-Report-Parser
diff --git a/Makefile.linux b/Makefile.linux
index 61e02df..dc56002 100644
--- a/Makefile.linux
+++ b/Makefile.linux
@@ -1,11 +1,11 @@
 PDLIBBUILDER_DIR=${CURDIR}/deps/pd-lib-builder
-LIBUSB_DIR=/usr/include/libusb-1.0
-HIDAPI_DIR=/usr/include/hidapi
+LIBUSB_DIR=/usr/include
+HIDAPI_DIR=/usr/include
 HIDMAP_DIR=${CURDIR}/deps/USB-HID-Report-Parser
 
 # using invasive detaching.... ahem.
 
-cflags += -I${LIBUSB_DIR} -I${HIDAPI_DIR} -I${HIDMAP_DIR} -DDETACH_KERNEL_DRIVER
+cflags += -I${HIDMAP_DIR} -DDETACH_KERNEL_DRIVER
 ldflags += -lusb-1.0 -lhidapi-libusb -L${HIDMAP_DIR} -lusbhid_map
 
 export PDDIR
diff --git a/Makefile.macos b/Makefile.macos
index ae5fee8..3fe535f 100644
--- a/Makefile.macos
+++ b/Makefile.macos
@@ -1,22 +1,17 @@
 
 PDLIBBUILDER_DIR=${CURDIR}/deps/pd-lib-builder
-LIBUSB_DIR=${CURDIR}/deps/libusb
-HIDAPI_DIR=${CURDIR}/deps/hidapi
 HIDMAP_DIR=${CURDIR}/deps/USB-HID-Report-Parser
 
-cflags += -I${LIBUSB_DIR}/libusb -I${HIDAPI_DIR}/hidapi -I${HIDMAP_DIR}
-ldflags += -L${LIBUSB_DIR}/libusb -lusb -L${HIDAPI_DIR}/local-install/lib #-lhidapi -L${HIDMAP_DIR} #-lusbhid_map
+cflags +=  -I${HIDMAP_DIR}
+ldflags += -lusb-1.0 -lhidapi
 
-export PDDIR
 export PDLIBBUILDER_DIR
-export LIBUSB_DIR
-export HIDAPI_DIR
 export HIDMAP_DIR
 export cflags
 export ldflags
 
 
-all: libusb hidapi #usbhid_map
+all:
 	$(MAKE) -C src/hid
 
 install:
@@ -24,33 +19,3 @@ install:
 
 clean:
 	$(MAKE) -C src/hid clean
-	$(MAKE) -C ${LIBUSB_DIR} clean
-	$(MAKE) -C ${HIDAPI_DIR} clean
-
-
-### libusbb
-
-libusb: ${LIBUSB_DIR}/libusb/.libs/libusb-1.0.a
-
-${LIBUSB_DIR}/libusb/.libs/libusb-1.0.a: ${LIBUSB_DIR}/Makefile
-	$(MAKE) -C ${LIBUSB_DIR}
-
-${LIBUSB_DIR}/Makefile: ${LIBUSB_DIR}/configure
-	cd ${LIBUSB_DIR}; ./configure
-
-${LIBUSB_DIR}/configure:
-	cd ${LIBUSB_DIR}; ./autogen.sh
-
-### hidapi
-
-hidapi: ${HIDAPI_DIR}/local-install/libhidapi.la
-
-${HIDAPI_DIR}/local-install/libhidapi.la: ${HIDAPI_DIR}/Makefile
-	$(MAKE) -C ${HIDAPI_DIR}
-	$(MAKE) -C ${HIDAPI_DIR} install
-
-${HIDAPI_DIR}/Makefile: ${HIDAPI_DIR}/configure
-	cd ${HIDAPI_DIR}; ./configure --prefix=${HIDAPI_DIR}/local-install
-
-${HIDAPI_DIR}/configure:
-	cd ${HIDAPI_DIR}; ./bootstrap
diff --git a/README.md b/README.md
index f227a58..383fbf4 100644
--- a/README.md
+++ b/README.md
@@ -21,8 +21,12 @@ Tested RPi 4 (5.10.17-v7l+), in principle works but is suboptimal.
 
 ### macos
 
-*Should* work as is..
+Install dependencies:
+```bash
+brew install libusb hidapi
+```
 
+Compile:
 ```bash 
 git clone --recursive https://github.com/tschiemer/hid-pd-external
 cd hid-pd-external/
@@ -31,8 +35,6 @@ make -f Makefile.macos && make -f Makefile.macos install
 
 ### linux (Raspberry Pi)
 
-A bit more troublesome.. these instructions might not get you all the way, but should be a start.
-
 *NOTE* as it is, using libusb (in this external) on linux is somewhat invasive on USB devices as kernel
 drivers might be detached for this to work. Using hidraw (as offered by hidapi) this seems to work without
 further problem, but this is something that remains to be done.
diff --git a/deps/USB-HID-Report-Parser b/deps/USB-HID-Report-Parser
index e3ba717..b663772 160000
--- a/deps/USB-HID-Report-Parser
+++ b/deps/USB-HID-Report-Parser
@@ -1 +1 @@
-Subproject commit e3ba71789fe84e5cd841f0abfd321b846bd41975
+Subproject commit b6637729edcd833c3c9a54970ff10017f7f1782c
diff --git a/deps/hidapi b/deps/hidapi
deleted file mode 160000
index 6a01f3b..0000000
--- a/deps/hidapi
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit 6a01f3b4a8862b19a7ec768752ebcfc1a412f4b1
diff --git a/deps/libusb b/deps/libusb
deleted file mode 160000
index bda2344..0000000
--- a/deps/libusb
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit bda2344f5e7d460decd3968492faa7a5fb2797cd
diff --git a/src/hid/hid.c b/src/hid/hid.c
index 0a620f3..7797fc4 100644
--- a/src/hid/hid.c
+++ b/src/hid/hid.c
@@ -3,11 +3,12 @@
 #include "m_pd.h"
 #include "usbhid_map.h"
 
-#include <libusb.h>
-#include <hidapi.h>
 //#include "report_item.h"
 //#include "report_usage.h"
 
+#include <libusb-1.0/libusb.h>
+#include <hidapi/hidapi.h>
+
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
@@ -391,10 +392,13 @@ static int hid_filter_device_list(libusb_device **devs, ssize_t count, hid_devic
                 }
 #endif
 
+#if !__APPLE__
                 r = libusb_claim_interface(handle, interface_num);
                 if (r < 0) {
                     error("claim_interface(): %d", r);
-                } else {
+                } else
+#endif
+                {
 
                     uint8_t report_desc[256];
                     r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN | LIBUSB_RECIPIENT_INTERFACE,
@@ -490,6 +494,7 @@ static int hid_filter_device_list(libusb_device **devs, ssize_t count, hid_devic
 //                    post("usage (page) = %d (%d)", report_desc[3], report_desc[1]);
                         }
                     }
+
                 } // claimed
 
 #ifdef DETACH_KERNEL_DRIVER
-- 
GitLab