Fix VST state-lock SNAFU (effSetChunk and process are exclusive)
[ardour.git] / libs / fst / vstwin.c
index 27fb3cbead21af949eb87ed6361e4d21df356f61..1e053d1ae7198272c108669f2ffd7e31c4c158d9 100644 (file)
@@ -20,7 +20,10 @@ static unsigned int idle_id = 0;
 
 #endif
 
+#ifndef COMPILER_MSVC
 extern char * strdup (const char *);
+#endif
+
 #include <glib.h>
 #include "fst.h"
 
@@ -89,8 +92,10 @@ maybe_set_program (VSTState* fst)
                // XXX check
                // 24 == audioMasterGetAutomationState,
                // 48 == audioMasterGetChunkFile
+               pthread_mutex_lock (&vstfx->state_lock);
                fst->plugin->dispatcher (fst->plugin, 24 /* effSetChunk */, 1, fst->wanted_chunk_size, fst->wanted_chunk, 0);
                fst->want_chunk = 0;
+               pthread_mutex_unlock (&vstfx->state_lock);
        }
 }
 
@@ -215,13 +220,7 @@ static VSTState*
 fst_new (void)
 {
        VSTState* fst = (VSTState*) calloc (1, sizeof (VSTState));
-       pthread_mutex_init (&fst->lock, NULL);
-       pthread_cond_init (&fst->window_status_change, NULL); // unused ?? -> TODO check gtk2ardour
-       pthread_cond_init (&fst->plugin_dispatcher_called, NULL); // unused ??
-       fst->want_program = -1;
-       fst->want_chunk = 0;
-       fst->n_pending_keys = 0;
-       fst->has_editor = 0;
+       fst->init();
 #ifdef PLATFORM_WINDOWS
        fst->voffset = 50;
        fst->hoffset = 0;
@@ -229,7 +228,6 @@ fst_new (void)
        fst->voffset = 24;
        fst->hoffset = 6;
 #endif
-       fst->program_set_without_editor = 0;
        return fst;
 }
 
@@ -371,10 +369,13 @@ fst_exit (void)
 int
 fst_run_editor (VSTState* fst, void* window_parent)
 {
+       /* For safety, remove any pre-existing editor window */ 
+       fst_destroy_editor (fst);
+       
        if (fst->windows_window == NULL) {
                HMODULE hInst;
                HWND window;
-               struct ERect* er;
+               struct ERect* er = NULL;
 
                if (!(fst->plugin->flags & effFlagsHasEditor)) {
                        fst_error ("Plugin \"%s\" has no editor", fst->handle->name);
@@ -421,9 +422,10 @@ fst_run_editor (VSTState* fst, void* window_parent)
                fst->plugin->dispatcher (fst->plugin, effEditOpen, 0, 0, fst->windows_window, 0 );
                fst->plugin->dispatcher (fst->plugin, effEditGetRect, 0, 0, &er, 0 );
 
-               fst->width =  er->right-er->left;
-               fst->height =  er->bottom-er->top;
-
+               if (er != NULL) {
+                       fst->width = er->right - er->left;
+                       fst->height = er->bottom - er->top;
+               }
 
                fst->been_activated = TRUE;