@@ -142,6 +142,7 @@ struct hid_device_ {
142142 pthread_barrier_t shutdown_barrier ; /* Ensures correct shutdown sequence */
143143 int shutdown_thread ;
144144 wchar_t * last_error_str ;
145+ wchar_t * last_read_error_str ;
145146};
146147
147148static hid_device * new_hid_device (void )
@@ -163,6 +164,7 @@ static hid_device *new_hid_device(void)
163164 dev -> device_info = NULL ;
164165 dev -> shutdown_thread = 0 ;
165166 dev -> last_error_str = NULL ;
167+ dev -> last_read_error_str = NULL ;
166168
167169 /* Thread objects */
168170 pthread_mutex_init (& dev -> mutex , NULL );
@@ -195,6 +197,8 @@ static void free_hid_device(hid_device *dev)
195197 if (dev -> source )
196198 CFRelease (dev -> source );
197199 free (dev -> input_report_buf );
200+ free (dev -> last_error_str );
201+ free (dev -> last_read_error_str );
198202 hid_free_enumeration (dev -> device_info );
199203
200204 /* Clean up the thread objects */
@@ -1278,10 +1282,7 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
12781282{
12791283 int bytes_read = -1 ;
12801284
1281- if (!dev ) {
1282- register_global_error ("Device is NULL" );
1283- return bytes_read ;
1284- }
1285+ register_error_str (& dev -> last_read_error_str , NULL );
12851286
12861287 /* Lock the access to the report list. */
12871288 pthread_mutex_lock (& dev -> mutex );
@@ -1296,7 +1297,7 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
12961297 /* Return if the device has been disconnected. */
12971298 if (dev -> disconnected ) {
12981299 bytes_read = -1 ;
1299- register_device_error ( dev , "hid_read_timeout: device disconnected" );
1300+ register_error_str ( & dev -> last_read_error_str , "hid_read_timeout: device disconnected" );
13001301 goto ret ;
13011302 }
13021303
@@ -1305,7 +1306,7 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
13051306 has been an error. An error code of -1 should
13061307 be returned. */
13071308 bytes_read = -1 ;
1308- register_device_error ( dev , "hid_read_timeout: thread shutdown" );
1309+ register_error_str ( & dev -> last_read_error_str , "hid_read_timeout: thread shutdown" );
13091310 goto ret ;
13101311 }
13111312
@@ -1319,7 +1320,7 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
13191320 bytes_read = return_data (dev , data , length );
13201321 else {
13211322 /* There was an error, or a device disconnection. */
1322- register_device_error ( dev , "hid_read_timeout: error waiting for more data" );
1323+ register_error_str ( & dev -> last_read_error_str , "hid_read_timeout: error waiting for more data" );
13231324 bytes_read = -1 ;
13241325 }
13251326 }
@@ -1343,7 +1344,7 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
13431344 } else if (res == ETIMEDOUT ) {
13441345 bytes_read = 0 ;
13451346 } else {
1346- register_device_error ( dev , "hid_read_timeout: error waiting for more data" );
1347+ register_error_str ( & dev -> last_read_error_str , "hid_read_timeout: error waiting for more data" );
13471348 bytes_read = -1 ;
13481349 }
13491350 }
@@ -1368,6 +1369,13 @@ int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
13681369 return hid_read_timeout (dev , data , length , (dev -> blocking )? -1 : 0 );
13691370}
13701371
1372+ HID_API_EXPORT const wchar_t * HID_API_CALL hid_read_error (hid_device * dev )
1373+ {
1374+ if (dev -> last_read_error_str == NULL )
1375+ return L"Success" ;
1376+ return dev -> last_read_error_str ;
1377+ }
1378+
13711379int HID_API_EXPORT hid_set_nonblocking (hid_device * dev , int nonblock )
13721380{
13731381 if (!dev ) {
0 commit comments