From 67899b7388620879749a893e07886c1a455fdee8 Mon Sep 17 00:00:00 2001 From: Frederik Seiffert Date: Tue, 8 Feb 2022 17:28:41 +0100 Subject: [PATCH] Add Mac-specific API to get device location ID --- mac/CMakeLists.txt | 2 ++ mac/hid.c | 13 +++++++++++- mac/hidapi_darwin.h | 50 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 mac/hidapi_darwin.h diff --git a/mac/CMakeLists.txt b/mac/CMakeLists.txt index 9b202d737..a83664aae 100644 --- a/mac/CMakeLists.txt +++ b/mac/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.4.3 FATAL_ERROR) +list(APPEND HIDAPI_PUBLIC_HEADERS "hidapi_darwin.h") + add_library(hidapi_darwin ${HIDAPI_PUBLIC_HEADERS} hid.c diff --git a/mac/hid.c b/mac/hid.c index cd5b71118..eadd74552 100644 --- a/mac/hid.c +++ b/mac/hid.c @@ -34,7 +34,7 @@ #include #include -#include "hidapi.h" +#include "hidapi_darwin.h" /* As defined in AppKit.h, but we don't need the entire AppKit for a single constant. */ extern const double NSAppKitVersionNumber; @@ -1188,6 +1188,17 @@ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index return 0; } +int HID_API_EXPORT_CALL hid_darwin_get_location_id(hid_device *dev, uint32_t *location_id) +{ + int res = get_int_property(dev->device_handle, CFSTR(kIOHIDLocationIDKey)); + if (res != 0) { + *location_id = (uint32_t) res; + return 0; + } else { + return -1; + } +} + HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev) { diff --git a/mac/hidapi_darwin.h b/mac/hidapi_darwin.h new file mode 100644 index 000000000..cd3331c41 --- /dev/null +++ b/mac/hidapi_darwin.h @@ -0,0 +1,50 @@ +/******************************************************* + HIDAPI - Multi-Platform library for + communication with HID devices. + + libusb/hidapi Team + + Copyright 2022, All Rights Reserved. + + At the discretion of the user of this library, + this software may be licensed under the terms of the + GNU General Public License v3, a BSD-Style license, or the + original HIDAPI license as outlined in the LICENSE.txt, + LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt + files located at the root of the source distribution. + These files may also be found in the public source + code repository located at: + https://github.com/libusb/hidapi . +********************************************************/ + +/** @file + * @defgroup API hidapi API + */ + +#ifndef HIDAPI_DARWIN_H__ +#define HIDAPI_DARWIN_H__ + +#include + +#include "hidapi.h" + +#ifdef __cplusplus +extern "C" { +#endif + + /** @brief Get the location ID for a HID device. + + @ingroup API + @param dev A device handle returned from hid_open(). + @param location_id The device's location ID on return. + + @returns + This function returns 0 on success and -1 on error. + */ + int HID_API_EXPORT_CALL hid_darwin_get_location_id(hid_device *dev, uint32_t *location_id); + +#ifdef __cplusplus +} +#endif + +#endif