Skip to content

Commit 65c1fc1

Browse files
committed
Added SDL_hid_get_properties()
1 parent 09ca7e8 commit 65c1fc1

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

include/SDL3/SDL_hidapi.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,23 @@ extern SDL_DECLSPEC SDL_hid_device * SDLCALL SDL_hid_open(unsigned short vendor_
283283
*/
284284
extern SDL_DECLSPEC SDL_hid_device * SDLCALL SDL_hid_open_path(const char *path);
285285

286+
/**
287+
* Get the properties associated with an SDL_hid_device.
288+
*
289+
* The following read-only properties are provided by SDL:
290+
*
291+
* - `SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER`: the libusb_device_handle associated with the device, if it was opened using libusb.
292+
*
293+
* \param dev a device handle returned from SDL_hid_open().
294+
* \returns a valid property ID on success or 0 on failure; call
295+
* SDL_GetError() for more information.
296+
*
297+
* \since This function is available since SDL 3.4.0.
298+
*/
299+
extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_hid_get_properties(SDL_hid_device *dev);
300+
301+
#define SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER "SDL.hidapi.libusb.device.handle"
302+
286303
/**
287304
* Write an Output report to a HID device.
288305
*

src/hidapi/SDL_hidapi.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -960,13 +960,14 @@ struct SDL_hid_device
960960
void *device;
961961
const struct hidapi_backend *backend;
962962
SDL_hid_device_info info;
963+
SDL_PropertiesID props;
963964
};
964965

965966
#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB)
966967

967968
static SDL_hid_device *CreateHIDDeviceWrapper(void *device, const struct hidapi_backend *backend)
968969
{
969-
SDL_hid_device *wrapper = (SDL_hid_device *)SDL_malloc(sizeof(*wrapper));
970+
SDL_hid_device *wrapper = (SDL_hid_device *)SDL_calloc(1, sizeof(*wrapper));
970971
SDL_SetObjectValid(wrapper, SDL_OBJECT_TYPE_HIDAPI_DEVICE, true);
971972
wrapper->device = device;
972973
wrapper->backend = backend;
@@ -1433,7 +1434,9 @@ SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id
14331434
if (libusb_ctx) {
14341435
pDevice = LIBUSB_hid_open(vendor_id, product_id, serial_number);
14351436
if (pDevice != NULL) {
1436-
return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
1437+
SDL_hid_device *dev = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
1438+
SDL_SetPointerProperty(SDL_hid_get_properties(dev), SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER, ((LIBUSB_hid_device *)pDevice)->device_handle);
1439+
return dev;
14371440
}
14381441
}
14391442
#endif // HAVE_LIBUSB
@@ -1472,7 +1475,9 @@ SDL_hid_device *SDL_hid_open_path(const char *path)
14721475
if (libusb_ctx) {
14731476
pDevice = LIBUSB_hid_open_path(path);
14741477
if (pDevice != NULL) {
1475-
return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
1478+
SDL_hid_device *dev = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
1479+
SDL_SetPointerProperty(SDL_hid_get_properties(dev), SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER, ((LIBUSB_hid_device *)pDevice)->device_handle);
1480+
return dev;
14761481
}
14771482
}
14781483
#endif // HAVE_LIBUSB
@@ -1482,6 +1487,16 @@ SDL_hid_device *SDL_hid_open_path(const char *path)
14821487
return NULL;
14831488
}
14841489

1490+
SDL_PropertiesID SDL_hid_get_properties(SDL_hid_device *device)
1491+
{
1492+
CHECK_DEVICE_MAGIC(device, 0);
1493+
1494+
if (!device->props) {
1495+
device->props = SDL_CreateProperties();
1496+
}
1497+
return device->props;
1498+
}
1499+
14851500
int SDL_hid_write(SDL_hid_device *device, const unsigned char *data, size_t length)
14861501
{
14871502
CHECK_DEVICE_MAGIC(device, -1);
@@ -1536,6 +1551,7 @@ int SDL_hid_close(SDL_hid_device *device)
15361551
CHECK_DEVICE_MAGIC(device, -1);
15371552

15381553
device->backend->hid_close(device->device);
1554+
SDL_DestroyProperties(device->props);
15391555
DeleteHIDDeviceWrapper(device);
15401556
return 0;
15411557
}

0 commit comments

Comments
 (0)