From 79aefe1cbede5b460b716147e780ce7f94deb11c Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <vital.had@gmail.com>
Date: Sat, 20 Jan 2024 04:22:39 +0800
Subject: [PATCH] Restore support for 10.5

Reverts breaking changes introduced in https://github.com/PortAudio/portaudio/commit/aa053464eb261337c2c763f2a3df1e5ef8f4797e

--- src/hostapi/coreaudio/pa_mac_core.c
+++ src/hostapi/coreaudio/pa_mac_core.c
@@ -728,11 +728,19 @@ PaError PaMacCore_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIn
 
     VVDBUG(("PaMacCore_Initialize(): hostApiIndex=%d\n", hostApiIndex));
 
-    CFRunLoopRef theRunLoop = NULL;
-    AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
-    OSStatus osErr = AudioObjectSetPropertyData (kAudioObjectSystemObject, &theAddress, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop);
-    if (osErr != noErr) {
-        goto error;
+    SInt32 major;
+    SInt32 minor;
+    Gestalt(gestaltSystemVersionMajor, &major);
+    Gestalt(gestaltSystemVersionMinor, &minor);
+
+    // Starting with 10.6 systems, the HAL notification thread is created internally
+    if ( major > 10 || (major == 10 && minor >= 6) ) {
+        CFRunLoopRef theRunLoop = NULL;
+        AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
+        OSStatus osErr = AudioObjectSetPropertyData (kAudioObjectSystemObject, &theAddress, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop);
+        if (osErr != noErr) {
+            goto error;
+        }
     }
 
     unixErr = initializeXRunListenerList();
@@ -1174,8 +1182,13 @@ static PaError OpenAndSetupOneAudioUnit(
         const double sampleRate,
         void *refCon )
 {
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
     AudioComponentDescription desc;
     AudioComponent comp;
+#else
+    ComponentDescription desc;
+    Component comp;
+#endif
     /*An Apple TN suggests using CAStreamBasicDescription, but that is C++*/
     AudioStreamBasicDescription desiredFormat;
     OSStatus result = noErr;
@@ -1242,7 +1255,11 @@ static PaError OpenAndSetupOneAudioUnit(
     desc.componentFlags        = 0;
     desc.componentFlagsMask    = 0;
     /* -- find the component -- */
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
     comp = AudioComponentFindNext( NULL, &desc );
+#else
+    comp = FindNextComponent( NULL, &desc );
+#endif
     if( !comp )
     {
         DBUG( ( "AUHAL component not found." ) );
@@ -1251,7 +1268,11 @@ static PaError OpenAndSetupOneAudioUnit(
         return paUnanticipatedHostError;
     }
     /* -- open it -- */
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
     result = AudioComponentInstanceNew( comp, audioUnit );
+#else
+    result = OpenAComponent( comp, audioUnit );
+#endif
     if( result )
     {
         DBUG( ( "Failed to open AUHAL component." ) );
@@ -1600,7 +1621,11 @@ static PaError OpenAndSetupOneAudioUnit(
 
 error:
 
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
     AudioComponentInstanceDispose( *audioUnit );
+#else
+    CloseComponent( *audioUnit );
+#endif
     *audioUnit = NULL;
     if( result )
         return PaMacCore_SetError( result, line, 1 );
@@ -2655,13 +2680,21 @@ static PaError CloseStream( PaStream* s )
         }
         if( stream->outputUnit && stream->outputUnit != stream->inputUnit ) {
             AudioUnitUninitialize( stream->outputUnit );
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
             AudioComponentInstanceDispose( stream->outputUnit );
+#else
+            CloseComponent( stream->outputUnit );
+#endif
         }
         stream->outputUnit = NULL;
         if( stream->inputUnit )
         {
             AudioUnitUninitialize( stream->inputUnit );
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
             AudioComponentInstanceDispose( stream->inputUnit );
+#else
+            CloseComponent( stream->inputUnit );
+#endif
             stream->inputUnit = NULL;
         }
         if( stream->inputRingBuffer.buffer )
