Fix VST state-lock SNAFU (effSetChunk and process are exclusive)
[ardour.git] / libs / fst / vstwin.c
index 58ce0fd235e0419cc1bf1566b12bd1c06d441d89..1e053d1ae7198272c108669f2ffd7e31c4c158d9 100644 (file)
@@ -2,16 +2,16 @@
 #include <string.h>
 #include <windows.h>
 
+#define fst_error(...) fprintf(stderr, __VA_ARGS__)
+
 #ifdef PLATFORM_WINDOWS
 
-#include <ardourext/misc.h>
-#include <ardourext/pthread.h>
+#include <pthread.h>
 static UINT_PTR idle_timer_id   = 0;
 
 #else /* linux + wine */
 
 #include <linux/limits.h> // PATH_MAX
-#include <libgen.h> // basename
 #include <winnt.h>
 #include <wine/exception.h>
 #include <pthread.h>
@@ -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);
        }
 }
 
@@ -116,9 +121,9 @@ idle_hands(
                        }
                }
 
+               pthread_mutex_lock (&fst->lock);
 #ifndef PLATFORM_WINDOWS /* linux + wine */
                /* Dispatch messages to send keypresses to the plugin */
-               pthread_mutex_lock (&fst->lock);
                int i;
 
                for (i = 0; i < fst->n_pending_keys; ++i) {
@@ -141,6 +146,7 @@ idle_hands(
                }
 
                fst->n_pending_keys = 0;
+#endif
 
                /* See comment for maybe_set_program call below */
                maybe_set_program (fst);
@@ -158,7 +164,6 @@ idle_hands(
                        maybe_set_program (fst);
                        fst->program_set_without_editor = 1;
                }
-#endif
 
                pthread_mutex_unlock (&fst->lock);
        }
@@ -215,21 +220,14 @@ 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 = 36;
+       fst->voffset = 50;
        fst->hoffset = 0;
 #else /* linux + wine */
        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;
 
@@ -465,7 +467,7 @@ fst_move_window_into_view (VSTState* fst)
 {
        if (fst->windows_window) {
 #ifdef PLATFORM_WINDOWS
-               SetWindowPos ((HWND)(fst->windows_window), 0, fst->hoffset, fst->voffset, fst->width, fst->height, 0);
+               SetWindowPos ((HWND)(fst->windows_window), 0, fst->hoffset, fst->voffset, fst->width + fst->hoffset, fst->height + fst->voffset, 0);
 #else /* linux + wine */
                SetWindowPos ((HWND)(fst->windows_window), 0, 0, 0, fst->width + fst->hoffset, fst->height + fst->voffset, 0);
 #endif
@@ -489,9 +491,8 @@ fst_load (const char *path)
        if ((strlen(path)) && (NULL != (fhandle = fst_handle_new ())))
        {
                char* period;
-               fhandle->nameptr = strdup (path);
                fhandle->path = strdup (path);
-               fhandle->name = basename(fhandle->nameptr);
+               fhandle->name = g_path_get_basename(path);
                if ((period = strrchr (fhandle->name, '.'))) {
                        *period = '\0';
                }
@@ -502,10 +503,10 @@ fst_load (const char *path)
                        return NULL;
                }
 
-               fhandle->main_entry = (main_entry_t) GetProcAddress (fhandle->dll, "main");
+               fhandle->main_entry = (main_entry_t) GetProcAddress ((HMODULE)fhandle->dll, "main");
 
                if (fhandle->main_entry == 0) {
-                       if ((fhandle->main_entry = (main_entry_t) GetProcAddress (fhandle->dll, "VSTPluginMain"))) {
+                       if ((fhandle->main_entry = (main_entry_t) GetProcAddress ((HMODULE)fhandle->dll, "VSTPluginMain"))) {
                                fprintf(stderr, "VST >= 2.4 plugin '%s'\n", path);
                                //PBD::warning << path << _(": is a VST >= 2.4 - this plugin may or may not function correctly with this version of Ardour.") << endmsg;
                        }
@@ -540,9 +541,8 @@ fst_unload (VSTHandle** fhandle)
                (*fhandle)->path = NULL;
        }
 
-       if ((*fhandle)->nameptr) {
-               free ((*fhandle)->nameptr);
-               (*fhandle)->nameptr = NULL;
+       if ((*fhandle)->name) {
+               free ((*fhandle)->name);
                (*fhandle)->name = NULL;
        }
 
@@ -608,7 +608,7 @@ fst_close (VSTState* fst)
                        if (fst->handle->plugincnt && --fst->handle->plugincnt == 0) {
 
                                fst->handle->main_entry = NULL;
-                               fst_unload (&fst->handle);
+                               fst_unload (&fst->handle); // XXX
                        }
                }