NO-OP: whitespace
[ardour.git] / libs / fst / vstwin.c
index 3c1cbcce8fed7d768c8d3748d795b3295b38d729..b799d5179d881d770dcf77d4739e9ab4573d3558 100644 (file)
@@ -8,7 +8,6 @@
 
 #include <pthread.h>
 static UINT_PTR idle_timer_id   = 0;
-//extern char* basename(char *path);  //
 
 #else /* linux + wine */
 
@@ -21,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"
 
@@ -46,6 +48,27 @@ vstedit_wndproc (HWND w, UINT msg, WPARAM wp, LPARAM lp)
                case WM_KEYDOWN:
                        break;
 
+               case WM_SIZE:
+#ifdef PLATFORM_WINDOWS
+                       {
+                               LRESULT rv = DefWindowProcA (w, msg, wp, lp);
+                               RECT rect;
+                               GetClientRect(w, &rect);
+#ifndef NDEBUG
+                               printf("VST WM_SIZE.. %ld %ld %ld %ld\n", rect.top, rect.left, (rect.right - rect.left), (rect.bottom - rect.top));
+#endif
+                               VSTState* fst = (VSTState*) GetProp (w, "fst_ptr");
+                               if (fst) {
+                                       int32_t width = (rect.right - rect.left);
+                                       int32_t height = (rect.bottom - rect.top);
+                                       if (width > 0 && height > 0) {
+                                               fst->amc (fst->plugin, 15 /*audioMasterSizeWindow */, width, height, NULL, 0);
+                                       }
+                               }
+                               return rv;
+                       }
+#endif
+                       break;
                case WM_CLOSE:
                        /* we don't care about windows closing ...
                         * WM_CLOSE is used for minimizing the window.
@@ -66,35 +89,10 @@ vstedit_wndproc (HWND w, UINT msg, WPARAM wp, LPARAM lp)
                        break;
        }
 
-       return DefWindowProcA (w, msg, wp, lp );
+       return DefWindowProcA (w, msg, wp, lp);
 }
 
 
-static void
-maybe_set_program (VSTState* fst)
-{
-       if (fst->want_program != -1) {
-               if (fst->vst_version >= 2) {
-                       fst->plugin->dispatcher (fst->plugin, effBeginSetProgram, 0, 0, NULL, 0);
-               }
-
-               fst->plugin->dispatcher (fst->plugin, effSetProgram, 0, fst->want_program, NULL, 0);
-
-               if (fst->vst_version >= 2) {
-                       fst->plugin->dispatcher (fst->plugin, effEndSetProgram, 0, 0, NULL, 0);
-               }
-               fst->want_program = -1;
-       }
-
-       if (fst->want_chunk == 1) {
-               // XXX check
-               // 24 == audioMasterGetAutomationState,
-               // 48 == audioMasterGetChunkFile
-               fst->plugin->dispatcher (fst->plugin, 24 /* effSetChunk */, 1, fst->wanted_chunk_size, fst->wanted_chunk, 0);
-               fst->want_chunk = 0;
-       }
-}
-
 static VOID CALLBACK
 idle_hands(
                HWND hwnd,        // handle to window for timer messages
@@ -144,8 +142,8 @@ idle_hands(
                fst->n_pending_keys = 0;
 #endif
 
-               /* See comment for maybe_set_program call below */
-               maybe_set_program (fst);
+               /* See comment for call below */
+               vststate_maybe_set_program (fst);
                fst->want_program = -1;
                fst->want_chunk = 0;
                /* If we don't have an editor window yet, we still need to
@@ -157,7 +155,7 @@ idle_hands(
                 * and so it will be done again if and when the GUI arrives.
                 */
                if (fst->program_set_without_editor == 0) {
-                       maybe_set_program (fst);
+                       vststate_maybe_set_program (fst);
                        fst->program_set_without_editor = 1;
                }
 
@@ -216,21 +214,15 @@ 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;
+       vststate_init (fst);
+
 #ifdef PLATFORM_WINDOWS
-       fst->voffset = 36;
+       fst->voffset = 45;
        fst->hoffset = 0;
 #else /* linux + wine */
        fst->voffset = 24;
        fst->hoffset = 6;
 #endif
-       fst->program_set_without_editor = 0;
        return fst;
 }
 
@@ -320,20 +312,20 @@ fst_init (void* possible_hmodule)
        return 0;
 }
 
-#ifndef PLATFORM_WINDOWS /* linux + wine */
 void
 fst_start_threading(void)
 {
+#ifndef PLATFORM_WINDOWS /* linux + wine */
        if (idle_id == 0) {
                gui_quit = 0;
                idle_id = g_idle_add (g_idle_call, NULL);
        }
-}
 #endif
+}
 
-#ifndef PLATFORM_WINDOWS /* linux + wine */
 void
 fst_stop_threading(void) {
+#ifndef PLATFORM_WINDOWS /* linux + wine */
        if (idle_id != 0) {
                gui_quit = 1;
                PostQuitMessage (0);
@@ -341,8 +333,8 @@ fst_stop_threading(void) {
                //g_source_remove(idle_id);
                idle_id = 0;
        }
-}
 #endif
+}
 
 void
 fst_exit (void)
@@ -372,10 +364,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);
@@ -422,9 +417,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;
 
@@ -466,11 +462,16 @@ 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),
+                               HWND_TOP /*0*/,
+                               fst->hoffset, fst->voffset,
+                               fst->width, fst->height,
+                               SWP_NOACTIVATE|SWP_NOOWNERZORDER);
 #else /* linux + wine */
                SetWindowPos ((HWND)(fst->windows_window), 0, 0, 0, fst->width + fst->hoffset, fst->height + fst->voffset, 0);
 #endif
                ShowWindow ((HWND)(fst->windows_window), SW_SHOWNA);
+               UpdateWindow ((HWND)(fst->windows_window));
        }
 }
 
@@ -490,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 = strdup (g_path_get_basename(fhandle->nameptr));
+               fhandle->name = g_path_get_basename(path);
                if ((period = strrchr (fhandle->name, '.'))) {
                        *period = '\0';
                }
@@ -503,13 +503,10 @@ fst_load (const char *path)
                        return NULL;
                }
 
-               fhandle->main_entry = (main_entry_t) GetProcAddress ((HMODULE)fhandle->dll, "main");
+               fhandle->main_entry = (main_entry_t) GetProcAddress ((HMODULE)fhandle->dll, "VSTPluginMain");
 
                if (fhandle->main_entry == 0) {
-                       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;
-                       }
+                       fhandle->main_entry = (main_entry_t) GetProcAddress ((HMODULE)fhandle->dll, "main");
                }
 
                if (fhandle->main_entry == 0) {
@@ -541,9 +538,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;
        }
 
@@ -564,6 +560,7 @@ fst_instantiate (VSTHandle* fhandle, audioMasterCallback amc, void* userptr)
        }
 
        fst = fst_new ();
+       fst->amc = amc;
 
        if ((fst->plugin = fhandle->main_entry (amc)) == NULL)  {
                fst_error ("fst_instantiate: %s could not be instantiated\n", fhandle->name);
@@ -572,7 +569,7 @@ fst_instantiate (VSTHandle* fhandle, audioMasterCallback amc, void* userptr)
        }
 
        fst->handle = fhandle;
-       fst->plugin->user = userptr;
+       fst->plugin->ptr1 = userptr;
 
        if (fst->plugin->magic != kEffectMagic) {
                fst_error ("fst_instantiate: %s is not a vst plugin\n", fhandle->name);
@@ -580,8 +577,16 @@ fst_instantiate (VSTHandle* fhandle, audioMasterCallback amc, void* userptr)
                return NULL;
        }
 
-       fst->plugin->dispatcher (fst->plugin, effOpen, 0, 0, 0, 0);
-       fst->vst_version = fst->plugin->dispatcher (fst->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
+                */
+               fst->plugin->dispatcher (fst->plugin, effOpen, 0, 0, 0, 0);
+               fst->vst_version = fst->plugin->dispatcher (fst->plugin, effGetVstVersion, 0, 0, 0, 0);
+       }
 
        fst->handle->plugincnt++;
        fst->wantIdle = 0;