fix missing colon in extension of LXVST search path
[ardour.git] / libs / fst / vstwin.c
index f1553821ad45d263c08dac494e1e9a28217de8cd..67ae03986ee3202c17cf8af63b4e4d5c2c04af55 100644 (file)
@@ -14,6 +14,8 @@
 #include <X11/X.h>
 #include <X11/Xlib.h>
 
+extern char * strdup (const char *);
+
 struct ERect{
     short top;
     short left;
@@ -22,15 +24,15 @@ struct ERect{
 };
 
 static pthread_mutex_t plugin_mutex;
-static FST* fst_first = NULL;
+
+/** Head of linked list of all FSTs */
+static VSTState* fst_first = NULL;
+
 const char magic[] = "FST Plugin State v002";
 
 DWORD  gui_thread_id = 0;
 static int gui_quit = 0;
 
-#define DELAYED_WINDOW 1
-
-
 static LRESULT WINAPI 
 my_window_proc (HWND w, UINT msg, WPARAM wp, LPARAM lp)
 {
@@ -63,33 +65,57 @@ my_window_proc (HWND w, UINT msg, WPARAM wp, LPARAM lp)
        return DefWindowProcA (w, msg, wp, lp );
 }
 
-static FST
+static VSTState 
 fst_new ()
 {
-       FST* fst = (FST*) calloc (1, sizeof (FST));
+       VSTState* fst = (VSTState *) calloc (1, sizeof (VSTState));
        pthread_mutex_init (&fst->lock, NULL);
        pthread_cond_init (&fst->window_status_change, NULL);
        pthread_cond_init (&fst->plugin_dispatcher_called, NULL);
        fst->want_program = -1;
        fst->want_chunk = 0;
-       fst->current_program = -1;
-       fst->pending_key = 0;
+       fst->n_pending_keys = 0;
+       fst->has_editor = 0;
+       fst->program_set_without_editor = 0;
        return fst;
 }
 
-static FSTHandle* 
+static VSTHandle* 
 fst_handle_new ()
 {
-       FSTHandle* fst = (FSTHandle*) calloc (1, sizeof (FSTHandle));
+       VSTHandle* fst = (VSTHandle*) calloc (1, sizeof (VSTHandle));
        return fst;
 }
 
+void
+maybe_set_program (VSTState* fst)
+{
+       if (fst->want_program != -1) {
+               if (fst->vst_version >= 2) {
+                       fst->plugin->dispatcher (fst->plugin, 67 /* 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, 68 /* effEndSetProgram */, 0, 0, NULL, 0);
+               }
+               fst->want_program = -1; 
+       }
+       
+       if (fst->want_chunk == 1) {
+               fst->plugin->dispatcher (fst->plugin, 24 /* effSetChunk */, 1, fst->wanted_chunk_size, fst->wanted_chunk, 0);
+               fst->want_chunk = 0;
+       }
+}
+
 DWORD WINAPI gui_event_loop (LPVOID param)
 {
        MSG msg;
-       FST* fst;
+       VSTState* fst;
        HMODULE hInst;
        HWND window;
+        int i;
 
        gui_thread_id = GetCurrentThreadId ();
 
@@ -128,24 +154,30 @@ DWORD WINAPI gui_event_loop (LPVOID param)
                TranslateMessage( &msg );
                DispatchMessageA (&msg);
 
-               /* handle window creation requests, destroy requests, 
+               if (msg.message != WM_TIMER) {
+                       continue;
+               }
+
+               pthread_mutex_lock (&plugin_mutex);
+
+               /* Do things that are appropriate for plugins which have open editor windows:
+                  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) {
+               for (fst = fst_first; fst; fst = fst->next) {
+                       
+                       pthread_mutex_lock (&fst->lock);
+                       
+                       if (fst->has_editor == 1) {
                                
-                               pthread_mutex_lock (&fst->lock);
-
                                if (fst->destroy) {
                                        fprintf (stderr, "%s scheduled for destroy\n", fst->handle->name);
-                                       if (fst->window) {
+                                       if (fst->windows_window) {
                                                fst->plugin->dispatcher( fst->plugin, effEditClose, 0, 0, NULL, 0.0 );
-                                               CloseWindow (fst->window);
-                                               fst->window = NULL;
+                                               CloseWindow (fst->windows_window);
+                                               fst->windows_window = NULL;
                                                fst->destroy = FALSE;
                                        }
                                        fst_event_loop_remove_plugin (fst);
@@ -155,7 +187,7 @@ again:
                                        goto again;
                                } 
                                
-                               if (fst->window == NULL) {
+                               if (fst->windows_window == NULL) {
                                        if (fst_create_editor (fst)) {
                                                fst_error ("cannot create editor for plugin %s", fst->handle->name);
                                                fst_event_loop_remove_plugin (fst);
@@ -166,28 +198,8 @@ again:
                                                /* condition/unlock: it was signalled & unlocked in fst_create_editor()   */
                                        }
                                }
-
-                               if (fst->want_program != -1 ) {
-                                       if (fst->vst_version >= 2) {
-                                               fst->plugin->dispatcher (fst->plugin, 67 /* 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, 68 /* effEndSetProgram */, 0, 0, NULL, 0);
-                                       }
-                                       /* did it work? */
-                                       fst->current_program = fst->plugin->dispatcher (fst->plugin, 3, /* effGetProgram */ 0, 0, NULL, 0);
-                                       fst->want_program = -1; 
-                               }
-
-                               if (fst->want_chunk == 1) {
-                                       fst->plugin->dispatcher (fst->plugin, 24 /* effSetChunk */, 1, fst->wanted_chunk_size, fst->wanted_chunk, 0);
-                                       fst->want_chunk = 0;
-                               }
                                
-                               if(fst->dispatcher_wantcall) {
+                               if (fst->dispatcher_wantcall) {
                                        fst->dispatcher_retval = fst->plugin->dispatcher( fst->plugin, 
                                                                                          fst->dispatcher_opcode,
                                                                                          fst->dispatcher_index,
@@ -199,32 +211,56 @@ again:
                                }
                                
                                fst->plugin->dispatcher (fst->plugin, effEditIdle, 0, 0, NULL, 0);
-
-                               if( fst->wantIdle ) {
+                               
+                               if (fst->wantIdle) {
                                        fst->plugin->dispatcher (fst->plugin, 53, 0, 0, NULL, 0);
                                }
-
-                               pthread_mutex_unlock (&fst->lock);
                                
-                       }
-                       pthread_mutex_unlock (&plugin_mutex);
-
-               }
+                               /* Dispatch messages to send keypresses to the plugin */
+                               
+                               for (i = 0; i < fst->n_pending_keys; ++i) {
+                                       /* 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);
+                               }
+                               
+                               fst->n_pending_keys = 0;
 
+                               /* See comment for maybe_set_program call below */
+                               maybe_set_program (fst);
+                               fst->want_program = -1;
+                               fst->want_chunk = 0;
+                       }
 
-               for (fst = fst_first; fst; fst = fst->next) {
-                       
-                       if (fst->pending_key) {
-                               msg.message = WM_CHAR;
-                               msg.hwnd = GetFocus ();
-                               msg.wParam = fst->pending_key;
-                               msg.lParam = 0;
-                               DispatchMessageA (&msg);
-                               fst->pending_key = 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) {
+                               maybe_set_program (fst);
+                               fst->program_set_without_editor = 1;
                        }
 
+                       pthread_mutex_unlock (&fst->lock);
                }
 
+               pthread_mutex_unlock (&plugin_mutex);
        }
 
        return 0;
@@ -284,31 +320,20 @@ fst_exit ()
 }
 
 int
-fst_run_editor (FST* fst)
+fst_run_editor (VSTState* fst)
 {
-       pthread_mutex_lock (&plugin_mutex);
-
-       if (fst_first == NULL) {
-               fst_first = fst;
-       } else {
-               FST* p = fst_first;
-               while (p->next) {
-                       p = p->next;
-               }
-               p->next = fst;
-       }
-
-       pthread_mutex_unlock (&plugin_mutex);
-
        /* wait for the plugin editor window to be created (or not) */
 
        pthread_mutex_lock (&fst->lock);
-       if (!fst->window) {
+
+       fst->has_editor = 1;
+       
+       if (!fst->windows_window) {
                pthread_cond_wait (&fst->window_status_change, &fst->lock);
-       } 
+       }
        pthread_mutex_unlock (&fst->lock);
 
-       if (!fst->window) {
+       if (!fst->windows_window) {
                return -1;
        }
 
@@ -316,7 +341,7 @@ fst_run_editor (FST* fst)
 }
 
 int
-fst_call_dispatcher (FST *fst, int opcode, int index, int val, void *ptr, float opt) 
+fst_call_dispatcher (VSTState* fst, int opcode, int index, int val, void *ptr, float opt) 
 {
        pthread_mutex_lock (&fst->lock);
        fst->dispatcher_opcode = opcode;
@@ -333,7 +358,7 @@ fst_call_dispatcher (FST *fst, int opcode, int index, int val, void *ptr, float
 }
 
 int
-fst_create_editor (FST* fst)
+fst_create_editor (VSTState * fst)
 {
        HMODULE hInst;
        HWND window;
@@ -370,12 +395,12 @@ fst_create_editor (FST* fst)
                fst_error ("cannot set fst_ptr on window");
        }
 
-       fst->window = window;
+       fst->windows_window = window;
 //     fst->xid = (int) GetPropA (window, "__wine_x11_whole_window");
 
 
        //printf( "effEditOpen......\n" );
-       fst->plugin->dispatcher (fst->plugin, effEditOpen, 0, 0, fst->window, 0 );
+       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;
@@ -383,8 +408,8 @@ fst_create_editor (FST* fst)
        //printf( "get rect ses... %d,%d\n", fst->width, fst->height );
 
        //SetWindowPos (fst->window, 0, 9999, 9999, er->right-er->left+8, er->bottom-er->top+26, 0);
-       SetWindowPos (fst->window, 0, 9999, 9999, 2, 2, 0);
-       ShowWindow (fst->window, SW_SHOWNA);
+       SetWindowPos (fst->windows_window, 0, 9999, 9999, 2, 2, 0);
+       ShowWindow (fst->windows_window, SW_SHOWNA);
        //SetWindowPos (fst->window, 0, 0, 0, er->right-er->left+8, er->bottom-er->top+26, SWP_NOMOVE|SWP_NOZORDER);
        
        fst->xid = (int) GetPropA (window, "__wine_x11_whole_window");
@@ -396,19 +421,19 @@ fst_create_editor (FST* fst)
 }
 
 void
-fst_move_window_into_view (FST* fst)
+fst_move_window_into_view (VSTState* fst)
 {
-        if (fst->window) {
-               SetWindowPos (fst->window, 0, 0, 0, fst->width, fst->height+24, 0);
-               ShowWindow (fst->window, SW_SHOWNA);
+        if (fst->windows_window) {
+               SetWindowPos (fst->windows_window, 0, 0, 0, fst->width, fst->height + 24, 0);
+               ShowWindow (fst->windows_window, SW_SHOWNA);
        }
 }
 
 void
-fst_destroy_editor (FST* fst)
+fst_destroy_editor (VSTState* fst)
 {
        pthread_mutex_lock (&fst->lock);
-       if (fst->window) {
+       if (fst->windows_window) {
                fprintf (stderr, "mark %s for destroy\n", fst->handle->name);
                fst->destroy = TRUE;
                //if (!PostThreadMessageA (gui_thread_id, WM_USER, 0, 0)) {
@@ -417,16 +442,16 @@ fst_destroy_editor (FST* fst)
                //}
                pthread_cond_wait (&fst->window_status_change, &fst->lock);
                fprintf (stderr, "%s editor destroyed\n", fst->handle->name);
-
+               fst->has_editor = 0;
        }
        pthread_mutex_unlock (&fst->lock);
 }
 
 void
-fst_event_loop_remove_plugin (FST* fst)
+fst_event_loop_remove_plugin (VSTState* fst)
 {
-       FST* p;
-       FST* prev;
+       VSTState* p;
+       VSTState* prev;
 
        for (p = fst_first, prev = NULL; p->next; prev = p, p = p->next) {
                if (p == fst) {
@@ -491,11 +516,11 @@ fst_load_vst_library(const char * path)
        return dll;
 }
 
-FSTHandle*
+VSTHandle *
 fst_load (const char *path)
 {
        char* buf;
-       FSTHandle* fhandle;
+       VSTHandle* fhandle;
        char* period;
 
        fhandle = fst_handle_new ();
@@ -550,7 +575,7 @@ fst_load (const char *path)
 }
 
 int
-fst_unload (FSTHandle* fhandle)
+fst_unload (VSTHandle* fhandle)
 {
        if (fhandle->plugincnt) {
                return -1;
@@ -570,11 +595,25 @@ fst_unload (FSTHandle* fhandle)
        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 = fst_new ();
+
+       pthread_mutex_lock (&plugin_mutex);
 
+       if (fst_first == NULL) {
+               fst_first = fst;
+       } else {
+               VSTState* p = fst_first;
+               while (p->next) {
+                       p = p->next;
+               }
+               p->next = fst;
+       }
+
+       pthread_mutex_unlock (&plugin_mutex);
+       
        if( fhandle == NULL ) {
            fst_error( "the handle was NULL\n" );
            return NULL;
@@ -607,7 +646,7 @@ fst_instantiate (FSTHandle* fhandle, audioMasterCallback amc, void* userptr)
 }
 
 void
-fst_close (FST* fst)
+fst_close (VSTState* fst)
 {
        fst_destroy_editor (fst);
 
@@ -619,12 +658,6 @@ fst_close (FST* fst)
        }
 }
 
-int
-fst_get_XID (FST* fst)
-{
-       return fst->xid;
-}
-
 float htonf (float v)
 {
       float result;
@@ -746,13 +779,15 @@ int fst_load_state (FST * fst, char * filename)
 }
 #endif
 
-int fst_save_state (FST * fst, char * filename)
+int
+fst_save_state (VSTState * fst, char * filename)
 {
        FILE * f = fopen (filename, "wb");
+        int j;
+
        if (f) {
                int bytelen;
                int numParams = fst->plugin->numParams;
-               unsigned i;
                char productString[64];
                char effectName[64];
                char vendorString[64];
@@ -789,13 +824,13 @@ int fst_save_state (FST * fst, char * filename)
                        numParams = 0;
                }
 
-               for( i=0; i<numParams; i++ ) {
+               for (j = 0; j < numParams; ++j) {
                        float val;
                        
                        pthread_mutex_lock( &fst->lock );
-                       val = fst->plugin->getParameter( fst->plugin, i );
+                       val = fst->plugin->getParameter (fst->plugin, j);
                        pthread_mutex_unlock( &fst->lock );
-                       fprintf( f, "  <param index=\"%d\" value=\"%f\"/>\n", i, val );
+                       fprintf( f, "  <param index=\"%d\" value=\"%f\"/>\n", j, val );
                }
 
                if( fst->plugin->flags & 32 ) {