Repository: libsdl-org/SDL
File: src/hidapi/windows/hid.c (C)

Code before:

	if (cr != CR_SUCCESS) {
		goto end_of_function;
	}

	/* Iterate over each device interface in the HID class, looking for the right one. */
	for (wchar_t* device_interface = device_interface_list; *device_interface; device_interface += wcslen(device_interface) + 1) {
		HANDLE device_handle = INVALID_HANDLE_VALUE;
		HIDD_ATTRIBUTES attrib;

        /* XInput devices don't get real HID reports and are better handled by the raw input driver */
        if (wcsstr(device_interface, L"&IG_") != NULL) {
            continue;
        }

		/* Open read-only handle to the device */
		device_handle = open_device(device_interface, FALSE);

		/* Check validity of device_handle. */
		if (device_handle == INVALID_HANDLE_VALUE) {
			/* Unable to open the device. */
			continue;
		}

#ifdef HIDAPI_IGNORE_DEVICE
		hid_bus_type bus_type = get_bus_type(device_interface);
		PHIDP_PREPARSED_DATA pp_data = NULL;
		HIDP_CAPS caps = { 0 };
#endif

		/* Get the Vendor ID and Product ID for this device. */
		attrib.Size = sizeof(HIDD_ATTRIBUTES);
		if (!HidD_GetAttributes(device_handle, &attrib)) {
			goto cont_close;
		}

#ifdef HIDAPI_IGNORE_DEVICE
		/* See if there are any devices we should skip in enumeration */
		if (HidD_GetPreparsedData(device_handle, &pp_data)) {
			HidP_GetCaps(pp_data, &caps);
			HidD_FreePreparsedData(pp_data);
		}
		if (HIDAPI_IGNORE_DEVICE(bus_type, attrib.VendorID, attrib.ProductID, caps.UsagePage, caps.Usage, false)) {
			goto cont_close;
		}
#endif

		/* Check the VID/PID to see if we should add this
		   device to the enumeration list. */
		if ((vendor_id == 0x0 || attrib.VendorID == vendor_id) &&
		    (product_id == 0x0 || attrib.ProductID == product_id) &&

Review comment (suggestion):
`get_bus_type` is not a trivial function.
I think it's best to keep fetching the vid/pid first as a smoke test.


Code after:

	if (cr != CR_SUCCESS) {
		goto end_of_function;
	}

	/* Iterate over each device interface in the HID class, looking for the right one. */
	for (wchar_t* device_interface = device_interface_list; *device_interface; device_interface += wcslen(device_interface) + 1) {
		HANDLE device_handle = INVALID_HANDLE_VALUE;
		HIDD_ATTRIBUTES attrib;

        /* XInput devices don't get real HID reports and are better handled by the raw input driver */
        if (wcsstr(device_interface, L"&IG_") != NULL) {
            continue;
        }

		/* Open read-only handle to the device */
		device_handle = open_device(device_interface, FALSE);

		/* Check validity of device_handle. */
		if (device_handle == INVALID_HANDLE_VALUE) {
			/* Unable to open the device. */
			continue;
		}

#ifdef HIDAPI_IGNORE_DEVICE
		hid_bus_type bus_type = HID_API_BUS_UNKNOWN;
		PHIDP_PREPARSED_DATA pp_data = NULL;
		HIDP_CAPS caps = { 0 };
#endif

		/* Get the Vendor ID and Product ID for this device. */
		attrib.Size = sizeof(HIDD_ATTRIBUTES);
		if (!HidD_GetAttributes(device_handle, &attrib)) {
			goto cont_close;
		}

#ifdef HIDAPI_IGNORE_DEVICE
		/* See if there are any devices we should skip in enumeration */
		bus_type = get_bus_type(device_interface);
		if (HidD_GetPreparsedData(device_handle, &pp_data)) {
			HidP_GetCaps(pp_data, &caps);
			HidD_FreePreparsedData(pp_data);
		}
		if (HIDAPI_IGNORE_DEVICE(bus_type, attrib.VendorID, attrib.ProductID, caps.UsagePage, caps.Usage, false)) {
			goto cont_close;
		}
#endif

		/* Check the VID/PID to see if we should add this
		   device to the enumeration list. */
		if ((vendor_id == 0x0 || attrib.VendorID == vendor_id) &&