Fix a potential memory-corruption
[ardour.git] / libs / ardour / mac_vst_support.cc
index 42eb3f70e502b085ca640ad07f7dd1e17c38fa75..b1f27eef31702d7a3d9a865f3b8465434c41d84e 100644 (file)
@@ -24,6 +24,7 @@
 #include <signal.h>
 #include <dlfcn.h>
 #include <string.h>
+#include <strings.h>
 #include <time.h>
 #include <unistd.h>
 #include <pthread.h>
@@ -80,25 +81,7 @@ static VSTState *
 mac_vst_new ()
 {
        VSTState* mac_vst = (VSTState *) calloc (1, sizeof (VSTState));
-
-       /*Mutexes*/
-       pthread_mutex_init (&mac_vst->lock, 0);
-       pthread_cond_init (&mac_vst->window_status_change, 0); // XXX  unused
-       pthread_cond_init (&mac_vst->plugin_dispatcher_called, 0); // XXX unused
-       pthread_cond_init (&mac_vst->window_created, 0); // XXX unused
-
-       /*Safe values*/
-       mac_vst->want_program = -1;
-       mac_vst->want_chunk = 0;
-       mac_vst->n_pending_keys = 0;
-       mac_vst->has_editor = 0;
-       mac_vst->program_set_without_editor = 0;
-       mac_vst->linux_window = 0;
-       mac_vst->linux_plugin_ui_window = 0;
-       mac_vst->eventProc = 0;
-       mac_vst->extra_data = 0;
-       mac_vst->want_resize = 0;
-
+       vststate_init (mac_vst);
        return mac_vst;
 }
 
@@ -211,7 +194,7 @@ mac_vst_instantiate (VSTHandle* fhandle, audioMasterCallback amc, void* userptr)
        }
 
        mac_vst->handle = fhandle;
-       mac_vst->plugin->user = userptr;
+       mac_vst->plugin->ptr1 = userptr;
 
        if (mac_vst->plugin->magic != kEffectMagic)
        {
@@ -220,17 +203,19 @@ mac_vst_instantiate (VSTHandle* fhandle, audioMasterCallback amc, void* userptr)
                return 0;
        }
 
-       mac_vst->plugin->dispatcher (mac_vst->plugin, effOpen, 0, 0, 0, 0);
-
-       /*May or May not need to 'switch the plugin on' here - unlikely
-       since FST doesn't and most plugins start up 'On' by default - I think this is the least of our worries*/
-
-       //mac_vst->plugin->dispatcher (mac_vst->plugin, effMainsChanged, 0, 1, 0, 0);
-
-       /* configure plugin to use Cocoa View */
-       mac_vst->plugin->dispatcher (mac_vst->plugin, effCanDo, 0, 0, const_cast<char*> ("hasCockosViewAsConfig"), 0.0f);
-
-       mac_vst->vst_version = mac_vst->plugin->dispatcher (mac_vst->plugin, effGetVstVersion, 0, 0, 0, 0);
+       if (!userptr) {
+               /* scanning.. or w/o master-callback userptr == 0, open now.
+                *
+                * Session::vst_callback needs a pointer to the AEffect
+                *     ((VSTPlugin*)userptr)->_plugin = vstfx->plugin
+                * before calling effOpen, because effOpen may call back
+                */
+               mac_vst->plugin->dispatcher (mac_vst->plugin, effOpen, 0, 0, 0, 0);
+               mac_vst->vst_version = mac_vst->plugin->dispatcher (mac_vst->plugin, effGetVstVersion, 0, 0, 0, 0);
+
+               /* configure plugin to use Cocoa View */
+               mac_vst->plugin->dispatcher (mac_vst->plugin, effCanDo, 0, 0, const_cast<char*> ("hasCockosViewAsConfig"), 0.0f);
+       }
 
        mac_vst->handle->plugincnt++;
        mac_vst->wantIdle = 0;