]> Repos - portaudio/commitdiff
hotplug: Clean up structure of Pa_RefreshDeviceList(). Fix to report error result...
authorRoss Bencina <rossb@audiomulch.com>
Sun, 18 Sep 2016 14:39:26 +0000 (00:39 +1000)
committerRoss Bencina <rossb@audiomulch.com>
Sun, 18 Sep 2016 14:39:26 +0000 (00:39 +1000)
src/common/pa_front.c

index 5c2eecbbf0f7fe9d8f83d49e91956dce1039b460..fd2c95ae726a620215eeb0da614b7744551a6465 100644 (file)
@@ -758,7 +758,8 @@ PaError Pa_RefreshDeviceList( void )
     PaError result     = paNoError;
     void **scanResults = NULL;
     int  *deviceCounts = NULL;
-    int i = 0;
+    int i;
+    int baseDeviceIndex;
 
     PA_LOGAPI_ENTER( "Pa_UpdateAvailableDeviceList" );
     if( !PA_IS_INITIALISED_ )
@@ -790,67 +791,65 @@ PaError Pa_RefreshDeviceList( void )
             continue;
 
         PA_DEBUG(( "Scanning new device list for host api %d.\n",i));
-        if( hostApi->ScanDeviceInfos( hostApi, i, &scanResults[ i ], &deviceCounts[ i ] ) != paNoError )
-            break;
+        result = hostApi->ScanDeviceInfos( hostApi, i, &scanResults[ i ], &deviceCounts[ i ] );
+        if( result != paNoError )
+        {
+            /* On error, dispose scan results already obtained, then return. */
+            int j = 0;
+            for( j = 0 ; j < i ; ++j )
+            {
+                PaUtilHostApiRepresentation *hostApi = hostApis_[j];
+                if( hostApi->DisposeDeviceInfos == NULL )
+                    continue;
+
+                PA_DEBUG(( "Performing rollback for device list scan for host api %d.\n",i));
+                hostApi->DisposeDeviceInfos( hostApi, scanResults[ j ], deviceCounts[ j ] );
+            }
 
+            goto done;
+        }
     }
 
-    /* Check the result of the scan operation */
-    if( i < hostApisCount_ )
+    /* -------------- Can't fail from here on -------------- */
+
+    /* Phase 2: Commit the scan changes to each back-end */
+
+    baseDeviceIndex = 0;
+    deviceCount_ = 0;
+    for( i = 0 ; i < hostApisCount_ ; ++i )
     {
-        /* If failure, rollback the scan changes back to original state */
-        int j = 0;
-        for( j = 0 ; j < i ; ++j )
+        PaUtilHostApiRepresentation *hostApi = hostApis_[i];
+        if( hostApi->CommitDeviceInfos == NULL )
         {
-            PaUtilHostApiRepresentation *hostApi = hostApis_[j];
-            if( hostApi->DisposeDeviceInfos == NULL )
-                continue;
-
-            PA_DEBUG(( "Performing rollback for device list scan for host api %d.\n",i));
-            hostApi->DisposeDeviceInfos( hostApi, scanResults[ j ], deviceCounts[ j ] );
+            /* Not yet implemented for this backend. Just
+                assume that the baseDeviceIndex and the deviceCount_ are
+                incremented according to the values in the info */
+            baseDeviceIndex += hostApi->info.deviceCount;
+            deviceCount_ += hostApi->info.deviceCount;
+            continue;
         }
-    }
-    else
-    {
-        int baseDeviceIndex = 0;
-        deviceCount_ = 0;
 
-        /* Otherwise, commit the scan changes to each back-end */
-        for( i = 0 ; i < hostApisCount_ ; ++i )
+        PA_DEBUG(( "Committing device list scan for host api %d.\n",i));
+        if( hostApi->CommitDeviceInfos( hostApi, i, scanResults[ i ], deviceCounts[ i ] ) != paNoError )
         {
-            PaUtilHostApiRepresentation *hostApi = hostApis_[i];
-            if( hostApi->CommitDeviceInfos == NULL )
-            {
-                /* Not yet implemented for this backend. Just
-                   assume that the baseDeviceIndex and the deviceCount_ are
-                   incremented according to the values in the info */
-                baseDeviceIndex += hostApi->info.deviceCount;
-                deviceCount_ += hostApi->info.deviceCount;
-                continue;
-            }
-
-            PA_DEBUG(( "Committing device list scan for host api %d.\n",i));
-            if( hostApi->CommitDeviceInfos( hostApi, i, scanResults[ i ], deviceCounts[ i ] ) != paNoError )
-            {
-                PA_DEBUG(( "Committing failed (shouldn't happen) %d.\n",i));
-                result = paInternalError;
-                goto done;
-            }
+            PA_DEBUG(( "Committing failed (shouldn't happen) %d.\n",i));
+            result = paInternalError;
+            goto done;
+        }
 
-            assert( hostApi->info.defaultInputDevice < hostApi->info.deviceCount );
-            assert( hostApi->info.defaultOutputDevice < hostApi->info.deviceCount );
+        assert( hostApi->info.defaultInputDevice < hostApi->info.deviceCount );
+        assert( hostApi->info.defaultOutputDevice < hostApi->info.deviceCount );
 
-            hostApi->privatePaFrontInfo.baseDeviceIndex = baseDeviceIndex;
+        hostApi->privatePaFrontInfo.baseDeviceIndex = baseDeviceIndex;
 
-            if( hostApi->info.defaultInputDevice != paNoDevice )
-                hostApi->info.defaultInputDevice += baseDeviceIndex;
+        if( hostApi->info.defaultInputDevice != paNoDevice )
+            hostApi->info.defaultInputDevice += baseDeviceIndex;
 
-            if( hostApi->info.defaultOutputDevice != paNoDevice )
-                hostApi->info.defaultOutputDevice += baseDeviceIndex;
+        if( hostApi->info.defaultOutputDevice != paNoDevice )
+            hostApi->info.defaultOutputDevice += baseDeviceIndex;
 
-            baseDeviceIndex += hostApi->info.deviceCount;
-            deviceCount_ += hostApi->info.deviceCount;
-        }
+        baseDeviceIndex += hostApi->info.deviceCount;
+        deviceCount_ += hostApi->info.deviceCount;
     }
 
 done: