MCPcopy Create free account
hub / github.com/RavEngine/RavEngine / hid_read_timeout

Function hid_read_timeout

deps/SDL2/src/hidapi/windows/hid.c:761–830  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

759}
760
761int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
762{
763 DWORD bytes_read = 0;
764 size_t copy_len = 0;
765 BOOL res;
766
767 /* Copy the handle for convenience. */
768 HANDLE ev = dev->ol.hEvent;
769
770 if (!dev->read_pending) {
771 /* Start an Overlapped I/O read. */
772 dev->read_pending = TRUE;
773 memset(dev->read_buf, 0, dev->input_report_length);
774 ResetEvent(ev);
775 res = ReadFile(dev->device_handle, dev->read_buf, (DWORD)dev->input_report_length, &bytes_read, &dev->ol);
776
777 if (!res) {
778 if (GetLastError() != ERROR_IO_PENDING) {
779 /* ReadFile() has failed.
780 Clean up and return error. */
781 CancelIo(dev->device_handle);
782 dev->read_pending = FALSE;
783 goto end_of_function;
784 }
785 }
786 }
787
788 if (milliseconds >= 0) {
789 /* See if there is any data yet. */
790 res = WaitForSingleObject(ev, milliseconds);
791 if (res != WAIT_OBJECT_0) {
792 /* There was no data this time. Return zero bytes available,
793 but leave the Overlapped I/O running. */
794 return 0;
795 }
796 }
797
798 /* Either WaitForSingleObject() told us that ReadFile has completed, or
799 we are in non-blocking mode. Get the number of bytes read. The actual
800 data has been copied to the data[] array which was passed to ReadFile(). */
801 res = GetOverlappedResult(dev->device_handle, &dev->ol, &bytes_read, TRUE/*wait*/);
802
803 /* Set pending back to false, even if GetOverlappedResult() returned error. */
804 dev->read_pending = FALSE;
805
806 if (res && bytes_read > 0) {
807 if (dev->read_buf[0] == 0x0) {
808 /* If report numbers aren't being used, but Windows sticks a report
809 number (0x0) on the beginning of the report anyway. To make this
810 work like the other platforms, and to make it work more like the
811 HID spec, we'll skip over this byte. */
812 bytes_read--;
813 copy_len = length > bytes_read ? bytes_read : length;
814 memcpy(data, dev->read_buf+1, copy_len);
815 }
816 else {
817 /* Copy the whole buffer, report number and all. */
818 copy_len = length > bytes_read ? bytes_read : length;

Callers 1

hid_readFunction · 0.70

Calls 3

ReadFileFunction · 0.85
memcpyFunction · 0.85
register_errorFunction · 0.70

Tested by

no test coverage detected