Skip to content

Commit 9e92233

Browse files
committed
Added SDL_hid_get_properties()
1 parent 507d31d commit 9e92233

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
@@ -951,13 +951,14 @@ struct SDL_hid_device
951951
void *device;
952952
const struct hidapi_backend *backend;
953953
SDL_hid_device_info info;
954+
SDL_PropertiesID props;
954955
};
955956

956957
#if defined(HAVE_PLATFORM_BACKEND) || defined(HAVE_DRIVER_BACKEND) || defined(HAVE_LIBUSB)
957958

958959
static SDL_hid_device *CreateHIDDeviceWrapper(void *device, const struct hidapi_backend *backend)
959960
{
960-
SDL_hid_device *wrapper = (SDL_hid_device *)SDL_malloc(sizeof(*wrapper));
961+
SDL_hid_device *wrapper = (SDL_hid_device *)SDL_calloc(1, sizeof(*wrapper));
961962
SDL_SetObjectValid(wrapper, SDL_OBJECT_TYPE_HIDAPI_DEVICE, true);
962963
wrapper->device = device;
963964
wrapper->backend = backend;
@@ -1424,7 +1425,9 @@ SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id
14241425
if (libusb_ctx) {
14251426
pDevice = LIBUSB_hid_open(vendor_id, product_id, serial_number);
14261427
if (pDevice != NULL) {
1427-
return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
1428+
SDL_hid_device *dev = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
1429+
SDL_SetPointerProperty(SDL_hid_get_properties(dev), SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER, ((LIBUSB_hid_device *)pDevice)->device_handle);
1430+
return dev;
14281431
}
14291432
}
14301433
#endif // HAVE_LIBUSB
@@ -1463,7 +1466,9 @@ SDL_hid_device *SDL_hid_open_path(const char *path)
14631466
if (libusb_ctx) {
14641467
pDevice = LIBUSB_hid_open_path(path);
14651468
if (pDevice != NULL) {
1466-
return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
1469+
SDL_hid_device *dev = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
1470+
SDL_SetPointerProperty(SDL_hid_get_properties(dev), SDL_PROP_HIDAPI_LIBUSB_DEVICE_HANDLE_POINTER, ((LIBUSB_hid_device *)pDevice)->device_handle);
1471+
return dev;
14671472
}
14681473
}
14691474
#endif // HAVE_LIBUSB
@@ -1473,6 +1478,16 @@ SDL_hid_device *SDL_hid_open_path(const char *path)
14731478
return NULL;
14741479
}
14751480

1481+
SDL_PropertiesID SDL_hid_get_properties(SDL_hid_device *device)
1482+
{
1483+
CHECK_DEVICE_MAGIC(device, 0);
1484+
1485+
if (!device->props) {
1486+
device->props = SDL_CreateProperties();
1487+
}
1488+
return device->props;
1489+
}
1490+
14761491
int SDL_hid_write(SDL_hid_device *device, const unsigned char *data, size_t length)
14771492
{
14781493
CHECK_DEVICE_MAGIC(device, -1);
@@ -1527,6 +1542,7 @@ int SDL_hid_close(SDL_hid_device *device)
15271542
CHECK_DEVICE_MAGIC(device, -1);
15281543

15291544
device->backend->hid_close(device->device);
1545+
SDL_DestroyProperties(device->props);
15301546
DeleteHIDDeviceWrapper(device);
15311547
return 0;
15321548
}

0 commit comments

Comments
 (0)