Skip to content

Commit

Permalink
macos: Add Mac-specific API to get device Location ID (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
triplef authored Feb 25, 2022
1 parent 2cfd63a commit 1ce31c3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mac/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 12 additions & 1 deletion mac/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <unistd.h>
#include <dlfcn.h>

#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;
Expand Down Expand Up @@ -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)
{
Expand Down
50 changes: 50 additions & 0 deletions mac/hidapi_darwin.h
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>

#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

0 comments on commit 1ce31c3

Please sign in to comment.