consolidate VSTState functions
[ardour.git] / libs / fst / vstwin.c
index 8911ff4a0117f45fabca0e6c900ec6318f4c4506..fcfa8368935baaa8c87dc41c00c8817aac05df4c 100644 (file)
 #include <stdio.h>
-#include <libgen.h>
+#include <string.h>
 #include <windows.h>
-#include <winnt.h>
-#include <wine/exception.h>
-#include <pthread.h>
-#include <signal.h>
-
-//#include <x11/xlib.h>
-//#include <x11/xresource.h>
-//#include <x11/xutil.h>
-//#include <x11/xatom.h>
-
-#include "fst.h"
-
-
-struct ERect{
-    short top;
-    short left;
-    short bottom;
-    short right;
-};
-
-static pthread_mutex_t plugin_mutex = PTHREAD_MUTEX_INITIALIZER;
-static FST* fst_first = NULL;
-
-DWORD  gui_thread_id = 0;
-
-static char* message_name (int message)
-{
-       switch (message) {
-       case 0x0000:
-               return "WM_NULL";
-
-       case 0x0001:
-               return "WM_CREATE";
 
-       case 0x0002:
-               return "WM_DESTROY";
+#define fst_error(...) fprintf(stderr, __VA_ARGS__)
 
-       case 0x0003:
-               return "WM_MOVE";
+#ifdef PLATFORM_WINDOWS
 
-       case 0x0004:
-               return "WM_SIZEWAIT";
-
-       case 0x0005:
-               return "WM_SIZE";
-
-       case 0x0006:
-               return "WM_ACTIVATE";
-
-       case 0x0007:
-               return "WM_SETFOCUS";
-
-       case 0x0008:
-               return "WM_KILLFOCUS";
-
-       case 0x0009:
-               return "WM_SETVISIBLE";
-
-       case 0x000a:
-               return "WM_ENABLE";
-
-       case 0x000b:
-               return "WM_SETREDRAW";
-
-       case 0x000c:
-               return "WM_SETTEXT";
-
-       case 0x000d:
-               return "WM_GETTEXT";
-
-       case 0x000e:
-               return "WM_GETTEXTLENGTH";
-
-       case 0x000f:
-               return "WM_PAINT";
-
-       case 0x0010:
-               return "WM_CLOSE";
+#include <pthread.h>
+static UINT_PTR idle_timer_id   = 0;
 
-       case 0x0011:
-               return "WM_QUERYENDSESSION";
+#else /* linux + wine */
 
-       case 0x0012:
-               return "WM_QUIT";
+#include <linux/limits.h> // PATH_MAX
+#include <winnt.h>
+#include <wine/exception.h>
+#include <pthread.h>
+static int gui_quit = 0;
+static unsigned int idle_id = 0;
 
-       case 0x0013:
-               return "WM_QUERYOPEN";
+#endif
 
-       case 0x0014:
-               return "WM_ERASEBKGND";
+#ifndef COMPILER_MSVC
+extern char * strdup (const char *);
+#endif
 
-       case 0x0015:
-               return "WM_SYSCOLORCHANGE";
+#include <glib.h>
+#include "fst.h"
 
-       case 0x0016:
-               return "WM_ENDSESSION";
+struct ERect {
+       short top;
+       short left;
+       short bottom;
+       short right;
+};
 
-       case 0x0017:
-               return "WM_SYSTEMERROR";
+static pthread_mutex_t  plugin_mutex;
+static VSTState*        fst_first        = NULL; /**< Head of linked list of all FSTs */
+static int              host_initialized = 0;
+static const char       magic[]          =  "FST Plugin State v002";
 
-       case 0x0018:
-               return "WM_SHOWWINDOW";
 
-       case 0x0019:
-               return "WM_CTLCOLOR";
+static LRESULT WINAPI
+vstedit_wndproc (HWND w, UINT msg, WPARAM wp, LPARAM lp)
+{
+       switch (msg) {
+               case WM_KEYUP:
+               case WM_KEYDOWN:
+                       break;
+
+               case WM_CLOSE:
+                       /* we don't care about windows closing ...
+                        * WM_CLOSE is used for minimizing the window.
+                        * Our window has no frame so it shouldn't ever
+                        * get sent - but if it does, we don't want our
+                        * window to get minimized!
+                        */
+                       return 0;
+                       break;
+
+               case WM_DESTROY:
+               case WM_NCDESTROY:
+                       /* we don't care about windows being destroyed ... */
+                       return 0;
+                       break;
+
+               default:
+                       break;
+       }
 
-       case 0x001a:
-               return "WM_WININICHANGE";
+       return DefWindowProcA (w, msg, wp, lp );
+}
 
-       case 0x001b:
-               return "WM_DEVMODECHANGE";
 
-       case 0x001c:
-               return "WM_ACTIVATEAPP";
+static VOID CALLBACK
+idle_hands(
+               HWND hwnd,        // handle to window for timer messages
+               UINT message,     // WM_TIMER message
+               UINT idTimer,     // timer identifier
+               DWORD dwTime)     // current system time
+{
+       VSTState* fst;
 
-       case 0x001d:
-               return "WM_FONTCHANGE";
+       pthread_mutex_lock (&plugin_mutex);
 
-       case 0x001e:
-               return "WM_TIMECHANGE";
+       for (fst = fst_first; fst; fst = fst->next) {
+               if (fst->gui_shown) {
+                       // this seems insane, but some plugins will not draw their meters if you don't
+                       // call this every time.  Example Ambience by Magnus @ Smartelectron:x
+                       fst->plugin->dispatcher (fst->plugin, effEditIdle, 0, 0, NULL, 0);
 
-       case 0x001f:
-               return "WM_CANCELMODE";
+                       if (fst->wantIdle) {
+                               fst->wantIdle = fst->plugin->dispatcher (fst->plugin, effIdle, 0, 0, NULL, 0);
+                       }
+               }
 
-       case 0x0020:
-               return "WM_SETCURSOR";
+               pthread_mutex_lock (&fst->lock);
+#ifndef PLATFORM_WINDOWS /* linux + wine */
+               /* Dispatch messages to send keypresses to the plugin */
+               int i;
+
+               for (i = 0; i < fst->n_pending_keys; ++i) {
+                       MSG msg;
+                       /* I'm not quite sure what is going on here; it seems
+                        * `special' keys must be delivered with WM_KEYDOWN,
+                        * but that alphanumerics etc. must use WM_CHAR or
+                        * they will be ignored.  Ours is not to reason why ...
+                        */
+                       if (fst->pending_keys[i].special != 0) {
+                               msg.message = WM_KEYDOWN;
+                               msg.wParam = fst->pending_keys[i].special;
+                       } else {
+                               msg.message = WM_CHAR;
+                               msg.wParam = fst->pending_keys[i].character;
+                       }
+                       msg.hwnd = GetFocus ();
+                       msg.lParam = 0;
+                       DispatchMessageA (&msg);
+               }
 
-       case 0x0021:
-               return "WM_MOUSEACTIVATE";
+               fst->n_pending_keys = 0;
+#endif
+
+               /* 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
+                * set up the program, otherwise when we load a plugin without
+                * opening its window it will sound wrong.  However, it seems
+                * that if you don't also load the program after opening the GUI,
+                * the GUI does not reflect the program properly.  So we'll not
+                * mark that we've done this (ie we won't set want_program to -1)
+                * and so it will be done again if and when the GUI arrives.
+                */
+               if (fst->program_set_without_editor == 0) {
+                       vststate_maybe_set_program (fst);
+                       fst->program_set_without_editor = 1;
+               }
 
-       case 0x0022:
-               return "WM_CHILDACTIVATE";
+               pthread_mutex_unlock (&fst->lock);
+       }
 
-       case 0x0023:
-               return "WM_QUEUESYNC";
+       pthread_mutex_unlock (&plugin_mutex);
+}
 
-       case 0x0024:
-               return "WM_GETMINMAXINFO";
+static void
+fst_idle_timer_add_plugin (VSTState* fst)
+{
+       pthread_mutex_lock (&plugin_mutex);
 
-       default:
-               break;
+       if (fst_first == NULL) {
+               fst_first = fst;
+       } else {
+               VSTState* p = fst_first;
+               while (p->next) {
+                       p = p->next;
+               }
+               p->next = fst;
        }
-       return "--- OTHER ---";
-}
-       
-static LRESULT WINAPI 
-my_window_proc (HWND w, UINT msg, WPARAM wp, LPARAM lp)
-{
-       FST* fst;
 
-//     if (msg != WM_TIMER) {
-//             fst_error ("window callback handler, msg = 0x%x (%s) win=%p\n", msg, message_name (msg), w);
-//     }
+       pthread_mutex_unlock (&plugin_mutex);
+}
 
-       switch (msg) {
-       case WM_KEYUP:
-       case WM_KEYDOWN:
-               break;
+static void
+fst_idle_timer_remove_plugin (VSTState* fst)
+{
+       VSTState* p;
+       VSTState* prev;
 
-       case WM_CLOSE:
-               PostQuitMessage (0);
+       pthread_mutex_lock (&plugin_mutex);
 
-       case WM_DESTROY:
-       case WM_NCDESTROY:
-               /* we should never get these */
-               //return 0;
-               break;
-
-       case WM_PAINT:
-               if ((fst = GetPropA (w, "fst_ptr")) != NULL) {
-                       if (fst->window && !fst->been_activated) {
-                               fst->been_activated = TRUE;
-                               pthread_cond_signal (&fst->window_status_change);
-                               pthread_mutex_unlock (&fst->lock);
+       for (p = fst_first, prev = NULL; p; prev = p, p = p->next) {
+               if (p == fst) {
+                       if (prev) {
+                               prev->next = p->next;
                        }
+                       break;
+               }
+               if (!p->next) {
+                       break;
                }
-               break;
+       }
 
-       default:
-               break;
+       if (fst_first == fst) {
+               fst_first = fst_first->next;
        }
 
-       return DefWindowProcA (w, msg, wp, lp );
+       pthread_mutex_unlock (&plugin_mutex);
 }
 
-static FST* 
-fst_new ()
+static VSTState*
+fst_new (void)
 {
-       FST* fst = (FST*) calloc (1, sizeof (FST));
-
-       pthread_mutex_init (&fst->lock, NULL);
-       pthread_cond_init (&fst->window_status_change, NULL);
-
+       VSTState* fst = (VSTState*) calloc (1, sizeof (VSTState));
+       vststate_init (fst);
+
+#ifdef PLATFORM_WINDOWS
+       fst->voffset = 50;
+       fst->hoffset = 0;
+#else /* linux + wine */
+       fst->voffset = 24;
+       fst->hoffset = 6;
+#endif
        return fst;
 }
 
-static FSTHandle* 
-fst_handle_new ()
+static void
+fst_delete (VSTState* fst)
 {
-       FSTHandle* fst = (FSTHandle*) calloc (1, sizeof (FSTHandle));
+       if (fst) {
+               free((void*)fst);
+               fst = NULL;
+       }
+}
+
+static VSTHandle*
+fst_handle_new (void)
+{
+       VSTHandle* fst = (VSTHandle*) calloc (1, sizeof (VSTHandle));
        return fst;
 }
 
+#ifndef PLATFORM_WINDOWS /* linux + wine */
+static gboolean
+g_idle_call (gpointer ignored) {
+       if (gui_quit) return FALSE;
+       MSG msg;
+       if (PeekMessageA (&msg, NULL, 0, 0, 1)) {
+               TranslateMessage (&msg);
+               DispatchMessageA (&msg);
+       }
+       idle_hands(NULL, 0, 0, 0);
+       g_main_context_iteration(NULL, FALSE);
+       return gui_quit ? FALSE : TRUE;
+}
+#endif
+
+
 int
-fst_create_editor (FST* fst)
+fst_init (void* possible_hmodule)
 {
+       if (host_initialized) return 0;
        HMODULE hInst;
-       HWND window;
-
-       /* "guard point" to trap errors that occur during plugin loading */
 
-       /* Note: fst->lock is held while this function is called */
-
-       if (!(fst->plugin->flags & effFlagsHasEditor)) {
-               fst_error ("Plugin \"%s\" has no editor", fst->handle->name);
+       if (possible_hmodule) {
+#ifdef PLATFORM_WINDOWS
+               fst_error ("Error in fst_init(): (module handle is unnecessary for Win32 build)");
                return -1;
-       }
-
-       if ((hInst = GetModuleHandleA (NULL)) == NULL) {
+#else /* linux + wine */
+               hInst = (HMODULE) possible_hmodule;
+#endif
+       } else if ((hInst = GetModuleHandleA (NULL)) == NULL) {
                fst_error ("can't get module handle");
-               return 1;
-       }
-       
-//     if ((window = CreateWindowExA (WS_EX_TOOLWINDOW | WS_EX_TRAYWINDOW, "FST", fst->handle->name,
-       if ((window = CreateWindowExA (0, "FST", fst->handle->name,
-                                      (WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX),
-                                      0, 0, 1, 1,
-                                      NULL, NULL,
-                                      hInst,
-                                      NULL)) == NULL) {
-               fst_error ("cannot create editor window");
-               return 1;
+               return -1;
        }
 
-       if (!SetPropA (window, "fst_ptr", fst)) {
-               fst_error ("cannot set fst_ptr on window");
+       if (!hInst) {
+               fst_error ("Cannot initialise VST host");
+               return -1;
        }
 
-       fst->window = window;
-       fst->xid = (int) GetPropA (window, "__wine_x11_whole_window");
-
-       {
-               struct ERect* er;
-
-               ShowWindow (fst->window, SW_SHOW);
-       
-               fst->plugin->dispatcher (fst->plugin, effEditOpen, 0, 0, fst->window, 0 );
-               fst->plugin->dispatcher (fst->plugin, effEditGetRect, 0, 0, &er, 0 );
-               
-               fst->width =  er->right-er->left;
-               fst->height =  er->bottom-er->top;
-               
-               SetWindowPos (fst->window, 0, 0, 0, er->right-er->left+8, er->bottom-er->top+26, SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOZORDER);
+       WNDCLASSEX wclass;
+
+       wclass.cbSize = sizeof(WNDCLASSEX);
+#ifdef PLATFORM_WINDOWS
+       wclass.style = (CS_HREDRAW | CS_VREDRAW);
+       wclass.hIcon = NULL;
+       wclass.hCursor = LoadCursor(0, IDC_ARROW);
+#else /* linux + wine */
+       wclass.style = 0;
+       wclass.hIcon = LoadIcon(hInst, "FST");
+       wclass.hCursor = LoadCursor(0, IDI_APPLICATION);
+#endif
+       wclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
+       wclass.lpfnWndProc = vstedit_wndproc;
+       wclass.cbClsExtra = 0;
+       wclass.cbWndExtra = 0;
+       wclass.hInstance = hInst;
+       wclass.lpszMenuName = "MENU_FST";
+       wclass.lpszClassName = "FST";
+       wclass.hIconSm = 0;
+
+       pthread_mutex_init (&plugin_mutex, NULL);
+       host_initialized = -1;
+
+       if (!RegisterClassExA(&wclass)){
+               fst_error ("Error in fst_init(): (class registration failed");
+               return -1;
        }
-
        return 0;
 }
 
 void
-fst_destroy_editor (FST* fst)
+fst_start_threading(void)
 {
-       pthread_mutex_lock (&fst->lock);
-       if (fst->window) {
-               fst->destroy = TRUE;
-               if (!PostThreadMessageA (gui_thread_id, WM_USER, 0, 0)) {
-                       fst_error ("could not post message to gui thread");
-               }
-               pthread_cond_wait (&fst->window_status_change, &fst->lock);
+#ifndef PLATFORM_WINDOWS /* linux + wine */
+       if (idle_id == 0) {
+               gui_quit = 0;
+               idle_id = g_idle_add (g_idle_call, NULL);
+       }
+#endif
+}
 
+void
+fst_stop_threading(void) {
+#ifndef PLATFORM_WINDOWS /* linux + wine */
+       if (idle_id != 0) {
+               gui_quit = 1;
+               PostQuitMessage (0);
+               g_main_context_iteration(NULL, FALSE);
+               //g_source_remove(idle_id);
+               idle_id = 0;
        }
-       pthread_mutex_unlock (&fst->lock);
+#endif
 }
 
 void
-fst_event_loop_remove_plugin (FST* fst)
+fst_exit (void)
 {
-       FST* p;
-       FST* prev;
-
-       for (p = fst_first, prev = NULL; p->next; prev = p, p = p->next) {
-               if (p == fst) {
-                       if (prev) {
-                               prev->next = p->next;
-                       }
-               }
+       if (!host_initialized) return;
+       VSTState* fst;
+       // If any plugins are still open at this point, close them!
+       while ((fst = fst_first))
+               fst_close (fst);
+
+#ifdef PLATFORM_WINDOWS
+       if (idle_timer_id != 0) {
+               KillTimer(NULL, idle_timer_id);
        }
-
-       if (fst_first == fst) {
-               fst_first = fst_first->next;
+#else /* linux + wine */
+       if (idle_id) {
+               gui_quit = 1;
+               PostQuitMessage (0);
        }
+#endif
 
+       host_initialized = FALSE;
+       pthread_mutex_destroy (&plugin_mutex);
 }
 
-void debreak( void ) { printf( "debreak\n" ); }
 
-DWORD WINAPI gui_event_loop (LPVOID param)
+int
+fst_run_editor (VSTState* fst, void* window_parent)
 {
-       MSG msg;
-       FST* fst;
-       HMODULE hInst;
-       HWND window;
+       /* For safety, remove any pre-existing editor window */ 
+       fst_destroy_editor (fst);
+       
+       if (fst->windows_window == NULL) {
+               HMODULE hInst;
+               HWND window;
+               struct ERect* er = NULL;
+
+               if (!(fst->plugin->flags & effFlagsHasEditor)) {
+                       fst_error ("Plugin \"%s\" has no editor", fst->handle->name);
+                       return -1;
+               }
 
-       gui_thread_id = GetCurrentThreadId ();
+               if ((hInst = GetModuleHandleA (NULL)) == NULL) {
+                       fst_error ("fst_create_editor() can't get module handle");
+                       return 1;
+               }
 
-       /* create a dummy window for timer events */
+               if ((window = CreateWindowExA (0, "FST", fst->handle->name,
+                                               window_parent ? WS_CHILD : (WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX),
+                                               CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
+                                               (HWND)window_parent, NULL,
+                                               hInst,
+                                               NULL) ) == NULL) {
+                       fst_error ("fst_create_editor() cannot create editor window");
+                       return 1;
+               }
 
-       if ((hInst = GetModuleHandleA (NULL)) == NULL) {
-               fst_error ("can't get module handle");
-               return 1;
-       }
-       
-       if ((window = CreateWindowExA (0, "FST", "dummy",
-                                      WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,
-                                      CW_USEDEFAULT, CW_USEDEFAULT,
-                                      CW_USEDEFAULT, CW_USEDEFAULT,
-                                      NULL, NULL,
-                                      hInst,
-                                      NULL )) == NULL) {
-               fst_error ("cannot create dummy timer window");
-       }
+               if (!SetPropA (window, "fst_ptr", fst)) {
+                       fst_error ("fst_create_editor() cannot set fst_ptr on window");
+               }
 
-       if (!SetTimer (window, 1000, 100, NULL)) {
-               fst_error ("cannot set timer on dummy window");
-       }
+               fst->windows_window = window;
 
-       while (GetMessageA (&msg, NULL, 0,0)) {
-               
-           if( msg.message == WM_KEYDOWN ) debreak();
-               TranslateMessage( &msg );
-               DispatchMessageA (&msg);
+               if (window_parent) {
+                       // This is requiredv for some reason. Note the parent is set above when the window
+                       // is created. Without this extra call the actual plugin window will draw outside
+                       // of our plugin window.
+                       SetParent((HWND)fst->windows_window, (HWND)window_parent);
+                       fst->xid = 0;
+#ifndef PLATFORM_WINDOWS /* linux + wine */
+               } else {
+                       SetWindowPos (fst->windows_window, 0, 9999, 9999, 2, 2, 0);
+                       ShowWindow (fst->windows_window, SW_SHOWNA);
+                       fst->xid = (int) GetPropA (fst->windows_window, "__wine_x11_whole_window");
+#endif
+               }
 
-               /* handle window creation requests, destroy requests, 
-                  and run idle callbacks 
-               */
-               
-
-               if( msg.message == WM_TIMER ) {
-                   pthread_mutex_lock (&plugin_mutex);
-again:
-                   for (fst = fst_first; fst; fst = fst->next) {
-
-                       if (fst->destroy) {
-                           if (fst->window) {
-                               fst->plugin->dispatcher( fst->plugin, effEditClose, 0, 0, NULL, 0.0 );
-                               CloseWindow (fst->window);
-                               fst->window = NULL;
-                               fst->destroy = FALSE;
-                           }
-                           fst_event_loop_remove_plugin (fst);
-                           fst->been_activated = FALSE;
-                           pthread_mutex_lock (&fst->lock);
-                           pthread_cond_signal (&fst->window_status_change);
-                           pthread_mutex_unlock (&fst->lock);
-                           goto again;
-                       } 
-
-                       if (fst->window == NULL) {
-                           pthread_mutex_lock (&fst->lock);
-                           if (fst_create_editor (fst)) {
-                               fst_error ("cannot create editor for plugin %s", fst->handle->name);
-                               fst_event_loop_remove_plugin (fst);
-                               pthread_cond_signal (&fst->window_status_change);
-                               pthread_mutex_unlock (&fst->lock);
-                               goto again;
-                           }
-                           /* condition/unlock handled when we receive WM_ACTIVATE */
-                       }
+               // This is the suggested order of calls.
+               fst->plugin->dispatcher (fst->plugin, effEditGetRect, 0, 0, &er, 0 );
+               fst->plugin->dispatcher (fst->plugin, effEditOpen, 0, 0, fst->windows_window, 0 );
+               fst->plugin->dispatcher (fst->plugin, effEditGetRect, 0, 0, &er, 0 );
 
-                       fst->plugin->dispatcher (fst->plugin, effEditIdle, 0, 0, NULL, 0);
-                   }
-                   pthread_mutex_unlock (&plugin_mutex);
+               if (er != NULL) {
+                       fst->width = er->right - er->left;
+                       fst->height = er->bottom - er->top;
                }
-       }
-       fst_error ("FST GUI event loop has quit!");
-       return 0;
-}
 
-int
-fst_init ()
-{
-       WNDCLASSA wc;
-       HMODULE hInst;
+               fst->been_activated = TRUE;
 
-       if ((hInst = GetModuleHandleA (NULL)) == NULL) {
-               fst_error ("can't get module handle");
-               return -1;
-       }
-       wc.style = 0;
-       wc.lpfnWndProc = my_window_proc;
-       wc.cbClsExtra = 0;
-       wc.cbWndExtra = 0;
-       wc.hInstance = hInst;
-       wc.hIcon = LoadIconA( hInst, "FST");
-       wc.hCursor = LoadCursorA( NULL, IDI_APPLICATION );
-       wc.hbrBackground = GetStockObject( BLACK_BRUSH );
-       wc.lpszMenuName = "MENU_FST";
-       wc.lpszClassName = "FST";
-
-       if (!RegisterClassA(&wc)){
-               return 1;
        }
 
-       if (CreateThread (NULL, 0, gui_event_loop, NULL, 0, NULL) == NULL) {
-               fst_error ("could not create new thread proxy");
-               return -1;
+       if (fst->windows_window) {
+#ifdef PLATFORM_WINDOWS
+               if (idle_timer_id == 0) {
+                       // Init the idle timer if needed, so that the main window calls us.
+                       idle_timer_id = SetTimer(NULL, idle_timer_id, 50, (TIMERPROC) idle_hands);
+               }
+#endif
+
+               fst_idle_timer_add_plugin (fst);
        }
 
-       return 0;
+       return fst->windows_window == NULL ? -1 : 0;
 }
 
-int
-fst_run_editor (FST* fst)
+void
+fst_destroy_editor (VSTState* fst)
 {
-       /* Add the FST to the list of all that should be handled by the GUI thread */
+       if (fst->windows_window) {
+               fprintf (stderr, "%s destroying edit window\n", fst->handle->name);
 
-       pthread_mutex_lock (&plugin_mutex);
+               fst_idle_timer_remove_plugin (fst);
+               fst->plugin->dispatcher( fst->plugin, effEditClose, 0, 0, NULL, 0.0 );
 
-       if (fst_first == NULL) {
-               fst_first = fst;
-       } else {
-               FST* p = fst_first;
-               while (p->next) {
-                       p = p->next;
-               }
-               p->next = fst;
-       }
+               DestroyWindow ((HWND)(fst->windows_window));
 
-       if (!PostThreadMessageA (gui_thread_id, WM_USER, 0, 0)) {
-               fst_error ("could not post message to gui thread");
-               return -1;
+               fst->windows_window = NULL;
        }
 
-       pthread_mutex_unlock (&plugin_mutex);
-
-       /* wait for the plugin editor window to be created (or not) */
-
-       pthread_mutex_lock (&fst->lock);
-       if (!fst->window) {
-               pthread_cond_wait (&fst->window_status_change, &fst->lock);
-       } 
-       pthread_mutex_unlock (&fst->lock);
+       fst->been_activated = FALSE;
+}
 
-       if (!fst->window) {
-               fst_error ("no window created for VST plugin editor");
-               return -1;
+void
+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->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
+               ShowWindow ((HWND)(fst->windows_window), SW_SHOWNA);
        }
+}
 
-       return 0;
+static HMODULE
+fst_load_vst_library(const char * path)
+{
+       char legalized_path[PATH_MAX];
+       strcpy (legalized_path, g_locale_from_utf8(path, -1, NULL, NULL, NULL));
+       return ( LoadLibraryA (legalized_path) );
 }
 
-FSTHandle*
+VSTHandle *
 fst_load (const char *path)
 {
-       char* buf;
-       FSTHandle* fhandle;
-       char* period;
+       VSTHandle* fhandle = NULL;
 
-       fhandle = fst_handle_new ();
-       
-       // XXX: Would be nice to find the correct call for this.
-       //      if the user does not configure Z: to be / we are doomed :(
-
-       if (strstr (path, ".dll") == NULL) {
-
-               buf = (char *) malloc (strlen (path) + 7);
-
-               if( path[0] == '/' ) {
-                   sprintf (buf, "Z:%s.dll", path);
-               } else {
-                   sprintf (buf, "%s.dll", path);
+       if ((strlen(path)) && (NULL != (fhandle = fst_handle_new ())))
+       {
+               char* period;
+               fhandle->path = strdup (path);
+               fhandle->name = g_path_get_basename(path);
+               if ((period = strrchr (fhandle->name, '.'))) {
+                       *period = '\0';
                }
 
-               fhandle->nameptr = strdup (path);
-
-       } else {
-
-               buf = (char *) malloc (strlen (path) + 3);
-
-               if( path[0] == '/' ) {
-                   sprintf (buf, "Z:%s", path);
-               } else {
-                   sprintf (buf, "%s", path);
+               // See if we can load the plugin DLL
+               if ((fhandle->dll = (HMODULE)fst_load_vst_library (path)) == NULL) {
+                       fst_unload (&fhandle);
+                       return NULL;
                }
 
-               fhandle->nameptr = strdup (path);
-       }
-       
-       fhandle->name = basename (fhandle->nameptr);
-
-       /* strip off .dll */
+               fhandle->main_entry = (main_entry_t) GetProcAddress ((HMODULE)fhandle->dll, "main");
 
-       if ((period = strrchr (fhandle->name, '.')) != NULL) {
-               *period = '\0';
-       }
-
-       if ((fhandle->dll = LoadLibraryA (buf)) == NULL) {
-               fst_unload (fhandle);
-               return NULL;
-       }
+               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;
+                       }
+               }
 
-       if ((fhandle->main_entry = GetProcAddress (fhandle->dll, "main")) == NULL) {
-               fst_unload (fhandle);
-               return NULL;
+               if (fhandle->main_entry == 0) {
+                       fst_unload (&fhandle);
+                       return NULL;
+               }
        }
-
        return fhandle;
 }
 
 int
-fst_unload (FSTHandle* fhandle)
+fst_unload (VSTHandle** fhandle)
 {
-       if (fhandle->plugincnt) {
+       if (!(*fhandle)) {
                return -1;
        }
 
-       if (fhandle->dll) {
-               FreeLibrary (fhandle->dll);
-               fhandle->dll = NULL;
+       if ((*fhandle)->plugincnt) {
+               return -1;
        }
 
-       if (fhandle->nameptr) {
-               free (fhandle->nameptr);
-               fhandle->name = NULL;
+       if ((*fhandle)->dll) {
+               FreeLibrary ((HMODULE)(*fhandle)->dll);
+               (*fhandle)->dll = NULL;
        }
-       
-       free (fhandle);
+
+       if ((*fhandle)->path) {
+               free ((*fhandle)->path);
+               (*fhandle)->path = NULL;
+       }
+
+       if ((*fhandle)->name) {
+               free ((*fhandle)->name);
+               (*fhandle)->name = NULL;
+       }
+
+       free (*fhandle);
+       *fhandle = NULL;
+
        return 0;
 }
 
-FST*
-fst_instantiate (FSTHandle* fhandle, audioMasterCallback amc, void* userptr)
+VSTState*
+fst_instantiate (VSTHandle* fhandle, audioMasterCallback amc, void* userptr)
 {
-       FST* fst = fst_new ();
+       VSTState* fst = NULL;
 
        if( fhandle == NULL ) {
-           fst_error( "the handle was NULL\n" );
-           return NULL;
+               fst_error( "fst_instantiate(): (the handle was NULL)\n" );
+               return NULL;
        }
 
+       fst = fst_new ();
+
        if ((fst->plugin = fhandle->main_entry (amc)) == NULL)  {
-               fst_error ("%s could not be instantiated\n", fhandle->name);
+               fst_error ("fst_instantiate: %s could not be instantiated\n", fhandle->name);
                free (fst);
                return NULL;
        }
-       
+
        fst->handle = fhandle;
        fst->plugin->user = userptr;
-               
+
        if (fst->plugin->magic != kEffectMagic) {
-               fst_error ("%s is not a VST plugin\n", fhandle->name);
-               free (fst);
+               fst_error ("fst_instantiate: %s is not a vst plugin\n", fhandle->name);
+               fst_close(fst);
                return NULL;
        }
-       
+
        fst->plugin->dispatcher (fst->plugin, effOpen, 0, 0, 0, 0);
-       //fst->plugin->dispatcher (fst->plugin, effMainsChanged, 0, 0, NULL, 0);
+       fst->vst_version = fst->plugin->dispatcher (fst->plugin, effGetVstVersion, 0, 0, 0, 0);
 
        fst->handle->plugincnt++;
+       fst->wantIdle = 0;
 
        return fst;
 }
 
+void fst_audio_master_idle(void) {
+       while(g_main_context_iteration(NULL, FALSE)) ;
+}
+
 void
-fst_close (FST* fst)
+fst_close (VSTState* fst)
 {
-       fst_destroy_editor (fst);
+       if (fst != NULL) {
+               fst_destroy_editor (fst);
 
-       fst->plugin->dispatcher (fst->plugin, effMainsChanged, 0, 0, NULL, 0);
-       fst->plugin->dispatcher (fst->plugin, effClose, 0, 0, 0, 0);
+               if (fst->plugin) {
+                       fst->plugin->dispatcher (fst->plugin, effMainsChanged, 0, 0, NULL, 0);
+                       fst->plugin->dispatcher (fst->plugin, effClose, 0, 0, 0, 0);
+                       fst->plugin = NULL;
+               }
+
+               if (fst->handle) {
+                       if (fst->handle->plugincnt && --fst->handle->plugincnt == 0) {
+
+                               fst->handle->main_entry = NULL;
+                               fst_unload (&fst->handle); // XXX
+                       }
+               }
 
-       if (fst->handle->plugincnt) {
-               --fst->handle->plugincnt;
+               /* It might be good for this to be in it's own cleanup function
+                       since it will free the memory for the fst leaving the caller
+                       with an invalid pointer.  Caller beware */
+               fst_delete(fst);
        }
 }
 
-int
-fst_get_XID (FST* fst)
+#if 0 // ?? who needs this, where?
+float htonf (float v)
 {
-       return fst->xid;
+       float result;
+       char * fin = (char*)&v;
+       char * fout = (char*)&result;
+       fout[0] = fin[3];
+       fout[1] = fin[2];
+       fout[2] = fin[1];
+       fout[3] = fin[0];
+       return result;
 }
+#endif