141 Commits

Author SHA1 Message Date
Ihor Dutchak
0ab6c14264 Add missing sanity checks (#727)
Based on #696

- Add missing checks for zero/null arguments;
- Better management of error strings in case of failures;

---------

Co-authored-by: Megamouse <studienricky89@googlemail.com>
2025-03-13 13:11:20 +02:00
Ihor Dutchak
95e6b98ce9 Add hid_read_error (#721)
hid_read_error is a separate error function, that returns error status of hid_read/hid_read_timeout.
hid_read/hid_read_timeout is no longer changes internal buffer used by hid_error and it makes it safe to use hid_read/hid_read_timeout from a separa thread, concurently with other device functions.
2025-03-11 19:26:36 +02:00
toxie
c5ee4d331e Fix WinAPI backend compilation as C++ source (#718)
Fix compilation with older WINVER.
2025-02-25 22:58:47 +02:00
JoergAtGithub
ff67c77dad Reconstruction logic for padding bytes at the end of Reports (only possible for devices without ReportID) (#707)
-Add reconstruction logic of const padding bytes at the end of a report for devices which don't use ReportIDs
-Enhanced padding logic to distinguish between byte and bit padding.
-Add a new test case for Razer Cobra mouse `1532_00A3_0002_0001` which has a completly con feature report of 90 bytes.
-Modified expected report descriptor reference file `045E_02FF_0005_0001_expected.rpt_desc` to include additional padding bytes.
2024-10-30 17:30:14 +02:00
Megamouse
750bf201ae Add missing checks for malloc/calloc/CloseHandle (#702) 2024-10-03 22:24:19 +03:00
Ihor Dutchak
c15392af9b winapi: add hid_winapi_set_write_timeout (#700)
Add API function to control hid_write timeout on WinAPI backend.

Resolves: #686
2024-10-03 14:00:43 +03:00
Megamouse
5c4acf88a8 Windows: return bytes_written if WriteFile returns synchronously (#697)
According to documentation, the system reserves the right to run
WriteFile synchronously, even if FILE_FLAG_OVERLAPPED is set,
so `bytes_written` needs to be taken from `WriteFile` directly in that case.
2024-09-29 12:09:13 +03:00
mbcinergy
4578ea2461 Add hid_send_output_report() (#677)
`hid_send_output_report` sends report over control pipe, unlike existing `hid_write` which would send the data on the first interrupt OUT endpoint, if one exists and only falls back to control pipe.
2024-06-12 19:40:09 +03:00
Sam Lantinga
d0732cda90 hidapi/windows: do not wait in GetOverlappedResult() in hid_read_timeout() (#577)
This is unsafe because the event is auto-reset, therefore the call to
WaitForSingleObject() resets the event which GetOverlappedResult() will
try to wait on.

Even though the overlapped operation is guaranteed to be completed at
the point we call GetOverlappedResult(), it will still wait on the event
handle for a short time to trigger the reset for auto-reset events. This
amounts to roughly a 100 ms sleep each time GetOverlappedResult() is called
for a completed I/O with a non-signalled event.

In the context of HIDAPI, this extra sleep means that callers that loop
on hid_read_timeout() with timeout=0 will loop forever, since the 100 ms
sleep each iteration ensures ReadFile() will always have new data.

Signed-off-by: Cameron Gutman <aicommander@gmail.com>
Signed-off-by: Sam Lantinga <slouken@libsdl.org>
2024-03-05 14:56:29 +02:00
Vladimir Gladkov
4f2e91bae8 windows: change MAX_STRING_WCHARS to 126 (#627)
Win32 HID API doc says: For USB devices, the maximum string length is 126 wide characters (not including the terminating NULL character).

For certain USB devices, using a buffer larger or equal to 127 wchars results in successful completion of HID API functions, but a broken string is stored in the output buffer. This behaviour persists even if HID API is bypassed and HID IOCTLs are passed to the HID driver directly (IOCTL_HID_GET_MANUFACTURER_STRING, IOCTL_HID_GET_PRODUCT_STRING, etc).

So, the buffer MUST NOT exceed 126 wchars.

windows: refactor ULONGLONG hid_internal_get_info(...) -> hid_internal_detect_bus_type_result hid_internal_detect_bus_type(...)

hid_internal_detect_bus_type is now only responsible for detection of the bus type; rename it accordingly. Also, mixing an internal flag and DEV_INST into an ULONGLONG retval feels kinda hackish; use a cleaner approach instead (add an internal flag to help distinguishing between BLUETOOTH and BLE devices, then clear it once we are done).
2024-03-04 13:08:57 +02:00
Moritz 'Morty' Strübe
4168d87521 Fix the windows build by fixing printf related issues (#641)
- Fix printf/string-related issues
- CI: Turn on -Wformat-signedness for GNU-cc
- Windows: Assert that struct has the correct size
2023-11-17 13:59:40 +02:00
JoergAtGithub
86b056fe70 Fix two C++-Builder 11 issues (#634)
* Invert preprocessor condition, as the Microsoft compiler seems to be the special case - not the other way around

* Removed redefinition of typedef 'PHIDP_PREPARSED_DATA' in hidapi_hidsdi.h to match the original hidsdi.h

As reported in #630
2023-10-27 02:19:38 +03:00
Sam Lantinga
98ab22a33d Suppress Visual C++ warning C4214: nonstandard extension used: bit field types other than int (#602) 2023-07-29 20:09:20 +03:00
luzpaz
849c8a9099 Fix various typo (#597)
Found via `codespell -q 3 -L childs,fo,xwindows`
Encountered in downstream https://github.com/libsdl-org/SDL
2023-07-03 17:18:54 +03:00
Sam Lantinga
b8ab253464 Fixed parameter documentation mismatch (#570) 2023-05-31 19:58:44 +03:00
Ihor Dutchak
1798adabb2 Fix "Explicit null dereferenced" (#561)
Fixes: #549
2023-05-22 13:08:19 +03:00
Ihor Dutchak
b595f3b063 Correct type (int) for printf width specifier (#562)
Fixes: #550
2023-05-22 13:08:01 +03:00
JoergAtGithub
5f5072f99e Fixed second occurence of CID1530054 (#554) 2023-05-15 00:43:06 +03:00
JoergAtGithub
f5c821e36f Fix Coverity issues CID1530052, CID1530054, CID1530055 (#552) 2023-05-14 23:43:44 +03:00
Ihor Dutchak
df12234504 ci: Docker build for Fedora/Mingw (#535)
Use GithubActions Docker build for Fedora/Mingw builds.
To avoid using `builds.sr.ht`.
Related: #476
2023-05-01 12:56:27 +03:00
Oleh Yudin
8588168980 Fix API definitions for clang/gcc flag -Wstrict-prototypes (#533) 2023-04-26 18:28:51 +03:00
Dimitriy Ryazantcev
bd6be4d83b windows: try to get USB device serial number if not provided by HidD_GetSerialNumberString (#464)
This is efficient for Xbox Common Controller class (XUSB) devices like Xbox 360 or Xbox One controllers that are missing serial number via usual HID API.
2023-03-14 00:50:57 +02:00
Ihor Dutchak
fc3eed3845 windows: Have a separate definition flag for export (#515)
On some compilers an empty definition (e.g. -DHID_API_EXPORT)
means the same as `=1` (i.e. -DHID_API_EXPORT=1), but the HID_API_EXPORT macro should be
empty in static library build case.

Having a separate compiler definition flag solves that issue.
2023-03-13 12:11:37 +02:00
Dimitriy Ryazantcev
e6caff3db3 windows: Get Bluetooth device Model Number String instead of Device Name into product string (#500)
Use `Device Name` only if `Model Number String` is not available. This should better match what we have in  `USB_DEVICE_DESCRIPTOR.iProduct`

See [Bluetooth Device Information Service](https://www.bluetooth.com/specifications/specs/device-information-service-1-1/) spec for more info.
2023-03-12 22:29:34 +02:00
Ihor Dutchak
26b5bb0ccf RFC: add hid_get_report_descriptor API function (#451)
- native implementations for hidraw/libusb/macOS;
- HID Report Descriptor reconstruction from HIDP_PREPARSED_DATA on Windows;
- unit-tests for some known devices for `hid_winapi_descriptor_reconstruct_pp_data`;
- support for ASAN builds (mainly to check the `hid_winapi_descriptor_reconstruct_pp_data`/its unit-tests;
2023-03-12 14:45:30 +02:00
Ihor Dutchak
438d06568d windows: don't mark with dllexport in static build (#507)
Don't mark API functions with __declspec(dllexport) when building a static library on Windows.
Enforced by CMake builds.
For other builds a compile definition is available.

Related: #480
2023-02-22 02:05:47 +02:00
Dimitriy Ryazantcev
0f2cf886e5 RFC: Add HID bus type in hid_device_info (#308) 2022-09-10 19:18:09 +03:00
Julian Waller
5c9f147a07 Add hid_get_device_info (#432)
- new API function: `struct hid_device_info * hid_get_device_info(hid_device *dev);` to get `hid_device_info` after the device already opened;
- reused existing implementation on Windows and macOS from enumeration routines to have the implementation;
- refactored libusb implementation to have a shared routine for `hid_enumerate` and `hid_get_device_info`;
- refactored hidraw implementation to have a shared routine for `hid_enumerate` and `hid_get_device_info`;

Resolves: #431
Closes: #164
Closes: #163
2022-08-13 19:01:57 +03:00
Ihor Dutchak
eaa5c7c6f1 hid_error improvements (#428)
- make the behaviour of methods which set hid_error more consistent;
- make the documentation and the implementation of hid_error consistent and safe;
- windows: add global error implementation;

Co-authored-by: Julian Waller <git@julusian.co.uk>
2022-06-22 01:34:39 +03:00
MeeSong
844e4682a9 Fix: memory write out of bounds. (#418) 2022-05-25 15:49:47 +03:00
Ihor Dutchak
93285f5246 windows: Add .rc (#415)
- Add resource file for DLL;
2022-05-11 17:47:38 +03:00
Ihor Dutchak
52fa87300b Windows build artifacts with Github Actions (#414)
- a straight-forward build matrix for Appveyor;
- MSBuild builds with Github Actions;
- Visual Studio 2022 support for `.sln` (toolset v143);
- gather Windows build artifacts with Github actions;
2022-05-08 19:59:39 +03:00
Ihor Dutchak
c95795ab5c Fix hidtest build
- fix including platform-specific includes when HIDAPI is a CMake subdirectory;
- check libusb and winapi platform-specific headers with hidtest;
2022-05-05 14:53:17 +03:00
Ihor Dutchak
52c728acf4 Use propper type for mach_port_t default param
- enable compilation warnings as errors for CI to catch such issues in the future;
- fix Mingw strict compilation warnings;
2022-05-05 14:53:17 +03:00
Jacob Shaffer
08563a0249 add better windows error handling (#388) 2022-05-02 14:08:44 +03:00
Ihor Dutchak
bca404520f Fire v0.12.0 (#398)
- add convenient macros to check HIDAPI version in runtime;
- mark in which version all recent HIAPI API functions where added;
2022-04-23 15:50:41 +03:00
Ihor Dutchak
75aafc3ac9 CI: check for new headers installation 2022-02-25 11:45:27 +02:00
Frederik Seiffert
2cfd63ac41 windows: Add Windows-specific API to get device container ID (#379) 2022-02-25 10:53:00 +02:00
Dimitriy Ryazantcev
c84e42ad91 Extract CM_Get_DevNode_PropertyW() and CM_Get_Device_Interface_PropertyW() calls to separate functions (#372) 2022-02-05 17:03:42 +02:00
Ihor Dutchak
2551a5d84b Reorganize Windows source structure
- separate HID declarations for non-DDK build into re-usable headers;
- refactor initialisation/deinitialisation of HIDAPI;
- make all the comments consistent (C-style only);
2022-01-29 18:33:15 +02:00
Ihor Dutchak
6f37897bef Remove stale Windows/macOS example/test 2022-01-29 18:33:15 +02:00
Ihor Dutchak
69dcdc1ff0 Update copyright strings 2022-01-29 18:33:15 +02:00
Megamouse
5f66a371ca Win32: Don't pass invalid pointers to HidD_FreePreparsedData in hid_open_path (#373)
Fixes a segfault that happens when hid_open_path is called on a previously disconnected device.
This is a regression caused by:
b600727 - Win32: Fix memory leak in `free_hid_device` (#361)

Co-authored-by: Megamouse <studienricky89@googlemail.de>
2022-01-29 18:32:25 +02:00
Dimitriy Ryazantcev
fe9a2a2e28 Ported code from SetupApi to CfgMgr32 (#362)
- https://docs.microsoft.com/windows-hardware/drivers/install/porting-from-setupapi-to-cfgmgr32
-  Use Unicode Windows API internally where possible.
2022-01-21 18:43:08 +02:00
Dimitriy Ryazantcev
af6c6012b6 Parse USB interface number from device Hardware IDs (#360)
Strickly speaking we cannot parse interface string at all and only allowed to parse Hardware ID string.
2021-12-23 00:01:41 +02:00
Dimitriy Ryazantcev
b600727200 Win32: Fix memory leak in free_hid_device (#361)
- Fix memory leak in `free_hid_device`;
- Simpllify `hid_open_path` code;
2021-12-22 23:59:56 +02:00
selsta
eb26015189 windows: add missing include for mingw32 (#348) 2021-11-11 01:00:25 +02:00
Ihor Dutchak
b923aeda68 Windows: hid_get_feature/input_report return size fix (#334)
- It appears when the numbered reports aren't used,
WinAPI returns size of the report excluding the data[0] which contains 0,
as an indication that numbered reports aren't used;
Explicity count that byte in the result.

Fixes: #328
2021-09-27 13:07:02 +03:00
Ihor Dutchak
d4888c5f65 Remove DDK support files. (#333)
Deleted DDK build scripts, support files and documentation for it,
as it wasn't working of-the-box with recent tool-sets from MS anyway.

Still keeping `HIDAPI_USE_DDK` macro in the code,
as it still _can_ be used if the build configuration is setup properly.

Resolves: #49
2021-09-22 13:30:23 +03:00
Ihor Dutchak
e050afb197 Fix sygwin compilation warnings (#331)
Fixes: #330
2021-09-18 18:31:26 +03:00