src/hidapi/android/hid.cpp
changeset 12765 80586f864508
parent 12346 b4c98c84a0b2
     1.1 --- a/src/hidapi/android/hid.cpp	Fri Jun 07 09:00:24 2019 -0700
     1.2 +++ b/src/hidapi/android/hid.cpp	Fri Jun 07 09:00:26 2019 -0700
     1.3 @@ -1027,11 +1027,14 @@
     1.4  
     1.5  int  HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length)
     1.6  {
     1.7 -	LOGV( "hid_write id=%d length=%u", device->m_nId, length );
     1.8 -	hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
     1.9 -	if ( pDevice )
    1.10 +	if ( device )
    1.11  	{
    1.12 -		return pDevice->SendOutputReport( data, length );
    1.13 +		LOGV( "hid_write id=%d length=%u", device->m_nId, length );
    1.14 +		hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
    1.15 +		if ( pDevice )
    1.16 +		{
    1.17 +			return pDevice->SendOutputReport( data, length );
    1.18 +		}
    1.19  	}
    1.20  	return -1; // Controller was disconnected
    1.21  }
    1.22 @@ -1039,13 +1042,16 @@
    1.23  // TODO: Implement timeout?
    1.24  int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *device, unsigned char *data, size_t length, int milliseconds)
    1.25  {
    1.26 -//	LOGV( "hid_read_timeout id=%d length=%u timeout=%d", device->m_nId, length, milliseconds );
    1.27 -	hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
    1.28 -	if ( pDevice )
    1.29 +	if ( device )
    1.30  	{
    1.31 -		return pDevice->GetInput( data, length );
    1.32 +//		LOGV( "hid_read_timeout id=%d length=%u timeout=%d", device->m_nId, length, milliseconds );
    1.33 +		hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
    1.34 +		if ( pDevice )
    1.35 +		{
    1.36 +			return pDevice->GetInput( data, length );
    1.37 +		}
    1.38 +		LOGV( "controller was disconnected" );
    1.39  	}
    1.40 -	LOGV( "controller was disconnected" );
    1.41  	return -1; // Controller was disconnected
    1.42  }
    1.43  
    1.44 @@ -1064,11 +1070,14 @@
    1.45  
    1.46  int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length)
    1.47  {
    1.48 -	LOGV( "hid_send_feature_report id=%d length=%u", device->m_nId, length );
    1.49 -	hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
    1.50 -	if ( pDevice )
    1.51 +	if ( device )
    1.52  	{
    1.53 -		return pDevice->SendFeatureReport( data, length );
    1.54 +		LOGV( "hid_send_feature_report id=%d length=%u", device->m_nId, length );
    1.55 +		hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
    1.56 +		if ( pDevice )
    1.57 +		{
    1.58 +			return pDevice->SendFeatureReport( data, length );
    1.59 +		}
    1.60  	}
    1.61  	return -1; // Controller was disconnected
    1.62  }
    1.63 @@ -1077,11 +1086,14 @@
    1.64  // Synchronous operation. Will block until completed.
    1.65  int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length)
    1.66  {
    1.67 -	LOGV( "hid_get_feature_report id=%d length=%u", device->m_nId, length );
    1.68 -	hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
    1.69 -	if ( pDevice )
    1.70 +	if ( device )
    1.71  	{
    1.72 -		return pDevice->GetFeatureReport( data, length );
    1.73 +		LOGV( "hid_get_feature_report id=%d length=%u", device->m_nId, length );
    1.74 +		hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
    1.75 +		if ( pDevice )
    1.76 +		{
    1.77 +			return pDevice->GetFeatureReport( data, length );
    1.78 +		}
    1.79  	}
    1.80  	return -1; // Controller was disconnected
    1.81  }
    1.82 @@ -1089,54 +1101,65 @@
    1.83  
    1.84  void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device)
    1.85  {
    1.86 -	LOGV( "hid_close id=%d", device->m_nId );
    1.87 -	hid_mutex_guard r( &g_DevicesRefCountMutex );
    1.88 -	LOGD("Decrementing device %d (%p), refCount = %d\n", device->m_nId, device, device->m_nDeviceRefCount - 1);
    1.89 -	if ( --device->m_nDeviceRefCount == 0 )
    1.90 +	if ( device )
    1.91 +	{
    1.92 +		LOGV( "hid_close id=%d", device->m_nId );
    1.93 +		hid_mutex_guard r( &g_DevicesRefCountMutex );
    1.94 +		LOGD("Decrementing device %d (%p), refCount = %d\n", device->m_nId, device, device->m_nDeviceRefCount - 1);
    1.95 +		if ( --device->m_nDeviceRefCount == 0 )
    1.96 +		{
    1.97 +			hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
    1.98 +			if ( pDevice )
    1.99 +			{
   1.100 +				pDevice->Close( true );
   1.101 +			}
   1.102 +			else
   1.103 +			{
   1.104 +				delete device;
   1.105 +			}
   1.106 +			LOGD("Deleted device %p\n", device);
   1.107 +		}
   1.108 +	}
   1.109 +}
   1.110 +
   1.111 +int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen)
   1.112 +{
   1.113 +	if ( device )
   1.114  	{
   1.115  		hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
   1.116  		if ( pDevice )
   1.117  		{
   1.118 -			pDevice->Close( true );
   1.119 +			wcsncpy( string, pDevice->GetDeviceInfo()->manufacturer_string, maxlen );
   1.120 +			return 0;
   1.121  		}
   1.122 -		else
   1.123 -		{
   1.124 -			delete device;
   1.125 -		}
   1.126 -		LOGD("Deleted device %p\n", device);
   1.127 -	}
   1.128 -
   1.129 -}
   1.130 -
   1.131 -int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen)
   1.132 -{
   1.133 -	hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
   1.134 -	if ( pDevice )
   1.135 -	{
   1.136 -		wcsncpy( string, pDevice->GetDeviceInfo()->manufacturer_string, maxlen );
   1.137 -		return 0;
   1.138  	}
   1.139  	return -1;
   1.140  }
   1.141  
   1.142  int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen)
   1.143  {
   1.144 -	hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
   1.145 -	if ( pDevice )
   1.146 +	if ( device )
   1.147  	{
   1.148 -		wcsncpy( string, pDevice->GetDeviceInfo()->product_string, maxlen );
   1.149 -		return 0;
   1.150 +		hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
   1.151 +		if ( pDevice )
   1.152 +		{
   1.153 +			wcsncpy( string, pDevice->GetDeviceInfo()->product_string, maxlen );
   1.154 +			return 0;
   1.155 +		}
   1.156  	}
   1.157  	return -1;
   1.158  }
   1.159  
   1.160  int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen)
   1.161  {
   1.162 -	hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
   1.163 -	if ( pDevice )
   1.164 +	if ( device )
   1.165  	{
   1.166 -		wcsncpy( string, pDevice->GetDeviceInfo()->serial_number, maxlen );
   1.167 -		return 0;
   1.168 +		hid_device_ref<CHIDDevice> pDevice = FindDevice( device->m_nId );
   1.169 +		if ( pDevice )
   1.170 +		{
   1.171 +			wcsncpy( string, pDevice->GetDeviceInfo()->serial_number, maxlen );
   1.172 +			return 0;
   1.173 +		}
   1.174  	}
   1.175  	return -1;
   1.176  }