1 /******************************************************************/
2 /** VSTFX - An engine based on FST for handling linuxVST plugins **/
3 /******************************************************************/
5 /*This is derived from the original FST (C code) with some tweaks*/
8 /** EDITOR tab stops at 4 **/
12 #include <jack/jack.h>
13 #include <jack/thread.h>
20 #include <ardour/vstfx.h>
37 static pthread_mutex_t plugin_mutex;
39 static VSTFX* vstfx_first = NULL;
41 const char magic[] = "VSTFX Plugin State v002";
43 int gui_thread_id = 0;
44 static int gui_quit = 0;
46 /*This will be our connection to X*/
48 Display* LXVST_XDisplay = NULL;
50 /*The thread handle for the GUI event loop*/
52 pthread_t LXVST_gui_event_thread;
54 #define DELAYED_WINDOW 1
56 /*Util functions to get the value of a property attached to an XWindow*/
60 int TempErrorHandler(Display *display, XErrorEvent *e)
69 int getXWindowProperty(Window window, Atom atom)
74 unsigned long userCount;
79 /*Use our own Xerror handler while we're in here - in an
80 attempt to stop the brain dead default Xerror behaviour of
81 qutting the entire application because of e.g. an invalid
84 XErrorHandler olderrorhandler = XSetErrorHandler(TempErrorHandler);
86 XGetWindowProperty( LXVST_XDisplay, //The display
89 0, //Offset into the data
90 1, //Number of 32Bit chunks of data
91 false, //false = don't delete the property
92 AnyPropertyType, //Required property type mask
93 &userType, //Actual type returned
94 &userSize, //Actual format returned
95 &userCount, //Actual number of items stored in the returned data
96 &bytes, //Number of bytes remaining if a partial read
97 &data); //The actual data read
99 if(LXVST_xerror == false && userCount == 1)
100 result = *(int*)data;
102 XSetErrorHandler(olderrorhandler);
104 /*Hopefully this will return zero if the property is not set*/
113 /********************************************************************/
114 /* This is untested - have no 64Bit plugins which use this */
115 /* system of passing an eventProc address */
116 /********************************************************************/
118 long getXWindowProperty(Window window, Atom atom)
123 unsigned long userCount;
126 LXVST_xerror = false;
128 /*Use our own Xerror handler while we're in here - in an
129 attempt to stop the brain dead default Xerror behaviour of
130 qutting the entire application because of e.g. an invalid
133 XErrorHandler olderrorhandler = XSetErrorHandler(TempErrorHandler);
135 XGetWindowProperty( LXVST_XDisplay,
148 if(LXVST_xerror == false && userCount == 1)
149 result = *(long*)data;
151 XSetErrorHandler(olderrorhandler);
153 /*Hopefully this will return zero if the property is not set*/
160 /*The event handler - called from within the main GUI thread to
161 dispatch events to any VST UIs which have callbacks stuck to them*/
163 static void dispatch_x_events(XEvent* event, VSTFX* vstfx)
165 /*Handle some of the Events we might be interested in*/
169 /*Configure event - when the window is resized or first drawn*/
171 case ConfigureNotify:
173 Window window = event->xconfigure.event;
175 int width = event->xconfigure.width;
176 int height = event->xconfigure.height;
178 /*If we get a config notify on the parent window XID then we need to see
179 if the size has been changed - some plugins re-size their UI window e.g.
180 when opening a preset manager (you might think that should be spawned as a new window...) */
182 /*if the size has changed, we flag this so that in lxvst_pluginui.cc we can make the
183 change to the GTK parent window in ardour, from its UI thread*/
185 if(window == (Window)(vstfx->window))
187 if((width!=vstfx->width) || (height!=vstfx->height))
189 vstfx->width = width;
190 vstfx->height = height;
191 vstfx->want_resize = 1;
193 /*QUIRK : Loomer plugins not only resize the UI but throw it into some random
194 position at the same time. We need to re-position the window at the origin of
197 if(vstfx->plugin_ui_window)
198 XMoveWindow(LXVST_XDisplay, vstfx->plugin_ui_window, 0, 0);
206 /*Reparent Notify - when the plugin UI is reparented into
207 our Host Window we will get an event here... probably... */
211 Window ParentWindow = event->xreparent.parent;
213 /*If the ParentWindow matches the window for the vstfx instance then
214 the Child window must be the XID of the pluginUI window created by the
215 plugin, so we need to see if it has a callback stuck to it, and if so
216 set that up in the vstfx */
218 /***********************************************************/
219 /* 64Bit --- This mechanism is not 64Bit compatible at the */
221 /***********************************************************/
223 if(ParentWindow == (Window)(vstfx->window))
225 Window PluginUIWindowID = event->xreparent.window;
227 vstfx->plugin_ui_window = PluginUIWindowID;
229 int result = getXWindowProperty(PluginUIWindowID, XInternAtom(LXVST_XDisplay, "_XEventProc", false));
232 vstfx->eventProc = NULL;
234 vstfx->eventProc = (void (*) (void* event))result;
237 long result = getXWindowProperty(PluginUIWindowID, XInternAtom(LXVST_XDisplay, "_XEventProc", false));
240 vstfx->eventProc = NULL;
242 vstfx->eventProc = (void (*) (void* event))result;
250 Window window = event->xany.window;
251 Atom message_type = event->xclient.message_type;
253 /*The only client message we are interested in is to signal
254 that the plugin parent window is now valid and can be passed
255 to effEditOpen when the editor is launched*/
257 if(window == (Window)(vstfx->window))
259 char* message = XGetAtomName(LXVST_XDisplay, message_type);
261 if(strcmp(message,"LaunchEditor") == 0)
264 if(event->xclient.data.l[0] == 0x0FEEDBAC)
265 vstfx_launch_editor(vstfx);
277 /* Some VSTs built with toolkits e.g. JUCE will manager their own UI
278 autonomously in the plugin, running the UI in its own thread, so once
279 we have created a parent window for the plugin, its UI takes care of
282 /*Other types register a callback as an Xwindow property on the plugin
283 UI window after they create it. If that is the case, we need to call it
284 here, passing the XEvent into it*/
286 if(vstfx->eventProc == NULL)
289 vstfx->eventProc((void*)event);
292 /** This is the main gui event loop for the plugin, we also need to pass
293 any Xevents to all the UI callbacks plugins 'may' have registered on their
294 windows, that is if they don't manage their own UIs **/
296 void* gui_event_loop (void* ptr)
300 int LXVST_sched_event_timer = 0;
301 int LXVST_sched_timer_interval = 50; //ms
304 /*The 'Forever' loop - runs the plugin UIs etc - based on the FST gui event loop*/
308 /* handle window creation requests, destroy requests,
309 and run idle callbacks */
311 /*Look at the XEvent queue - if there are any XEvents we need to handle them,
312 including passing them to all the plugin (eventProcs) we are currently managing*/
316 /*See if there are any events in the queue*/
318 int num_events = XPending(LXVST_XDisplay);
320 /*process them if there are any*/
324 XNextEvent(LXVST_XDisplay, &event);
326 /*Call dispatch events, with the event, for each plugin in the linked list*/
328 for (vstfx = vstfx_first; vstfx; vstfx = vstfx->next)
330 pthread_mutex_lock(&vstfx->lock);
332 dispatch_x_events(&event, vstfx);
334 pthread_mutex_unlock(&vstfx->lock);
341 /*We don't want to use all the CPU.. */
345 LXVST_sched_event_timer++;
347 LXVST_sched_event_timer = LXVST_sched_event_timer & 0x00FFFFFF;
349 /*See if its time for us to do a scheduled event pass on all the plugins*/
351 if((LXVST_sched_timer_interval!=0) && (!(LXVST_sched_event_timer% LXVST_sched_timer_interval)))
353 pthread_mutex_lock (&plugin_mutex);
356 /*Parse through the linked list of plugins*/
358 for (vstfx = vstfx_first; vstfx; vstfx = vstfx->next)
360 pthread_mutex_lock (&vstfx->lock);
362 /*Window scheduled for destruction*/
368 vstfx->plugin->dispatcher( vstfx->plugin, effEditClose, 0, 0, NULL, 0.0 );
370 XDestroyWindow (LXVST_XDisplay, vstfx->window);
371 vstfx->window = 0; //FIXME - probably safe to assume we never have an XID of 0 but not explicitly true
372 vstfx->destroy = FALSE;
375 vstfx_event_loop_remove_plugin (vstfx);
376 vstfx->been_activated = FALSE;
377 pthread_cond_signal (&vstfx->window_status_change);
378 pthread_mutex_unlock (&vstfx->lock);
383 /*Window does not yet exist - scheduled for creation*/
385 if (vstfx->window == 0) //FIXME - probably safe to assume 0 is not a valid XID but not explicitly true
387 if (vstfx_create_editor (vstfx))
389 vstfx_error ("** ERROR ** VSTFX : Cannot create editor for plugin %s", vstfx->handle->name);
390 vstfx_event_loop_remove_plugin (vstfx);
391 pthread_cond_signal (&vstfx->window_status_change);
392 pthread_mutex_unlock (&vstfx->lock);
397 /* condition/unlock: it was signalled & unlocked in fst_create_editor() */
401 /*Scheduled for setting a new program*/
403 if (vstfx->want_program != -1 )
405 if (vstfx->vst_version >= 2)
407 vstfx->plugin->dispatcher (vstfx->plugin, 67 /* effBeginSetProgram */, 0, 0, NULL, 0);
410 vstfx->plugin->dispatcher (vstfx->plugin, effSetProgram, 0, vstfx->want_program, NULL, 0);
412 if (vstfx->vst_version >= 2)
414 vstfx->plugin->dispatcher (vstfx->plugin, 68 /* effEndSetProgram */, 0, 0, NULL, 0);
419 vstfx->current_program = vstfx->plugin->dispatcher (vstfx->plugin, 3, /* effGetProgram */ 0, 0, NULL, 0);
420 vstfx->want_program = -1;
423 /*scheduled call to dispatcher*/
425 if (vstfx->dispatcher_wantcall) {
426 vstfx->dispatcher_retval = vstfx->plugin->dispatcher (
428 vstfx->dispatcher_opcode,
429 vstfx->dispatcher_index,
430 vstfx->dispatcher_val,
431 vstfx->dispatcher_ptr,
432 vstfx->dispatcher_opt
435 vstfx->dispatcher_wantcall = 0;
436 pthread_cond_signal (&vstfx->plugin_dispatcher_called);
439 /*Call the editor Idle function in the plugin*/
441 vstfx->plugin->dispatcher (vstfx->plugin, effEditIdle, 0, 0, NULL, 0);
444 vstfx->plugin->dispatcher (vstfx->plugin, 53, 0, 0, NULL, 0);
446 pthread_mutex_unlock (&vstfx->lock);
448 pthread_mutex_unlock (&plugin_mutex);
452 /*Drop out to here if we set gui_quit to 1 */
457 /*The VSTFX Init function - this needs to be called before the VSTFX engine
458 can be accessed, it gets the UI thread running, opens a connection to X etc
459 normally started in globals.cc*/
461 int vstfx_init (void* ptr)
464 int thread_create_result;
466 pthread_attr_t thread_attributes;
468 /*Init the attribs to defaults*/
470 pthread_attr_init(&thread_attributes);
472 /*Make sure the thread is joinable - this should be the default anyway -
473 so we can join to it on vstfx_exit*/
475 pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_JOINABLE);
478 /*This is where we need to open a connection to X, and start the GUI thread*/
480 /*Open our connection to X - all linuxVST plugin UIs handled by the LXVST engine
481 will talk to X down this connection - X cannot handle multi-threaded access via
484 if(LXVST_XDisplay==NULL)
485 LXVST_XDisplay = XOpenDisplay(NULL); //We might be able to make this open a specific screen etc
487 /*Drop out and report the error if we fail to connect to X */
489 if(LXVST_XDisplay==NULL)
491 vstfx_error ("** ERROR ** VSTFX: Failed opening connection to X");
496 /*We have a connection to X - so start the gui event loop*/
498 /*Create the thread - use default attrs for now, don't think we need anything special*/
500 thread_create_result = pthread_create(&LXVST_gui_event_thread, NULL, gui_event_loop, NULL);
502 if(thread_create_result!=0)
504 /*There was a problem starting the GUI event thread*/
506 vstfx_error ("** ERROR ** VSTFX: Failed starting GUI event thread");
508 XCloseDisplay(LXVST_XDisplay);
516 /*The vstfx Quit function*/
522 /*We need to pthread_join the gui_thread here so
523 we know when it has stopped*/
525 pthread_join(LXVST_gui_event_thread, NULL);
528 /*Adds a new plugin (VSTFX) instance to the linked list*/
530 int vstfx_run_editor (VSTFX* vstfx)
532 pthread_mutex_lock (&plugin_mutex);
534 /*Add the new VSTFX instance to the linked list*/
536 if (vstfx_first == NULL)
542 VSTFX* p = vstfx_first;
550 /*Mark the new end of the list*/
555 pthread_mutex_unlock (&plugin_mutex);
557 /* wait for the plugin editor window to be created (or not) */
559 pthread_mutex_lock (&vstfx->lock);
563 pthread_cond_wait (&vstfx->window_status_change, &vstfx->lock);
566 pthread_mutex_unlock (&vstfx->lock);
577 /*Creates an editor for the plugin - normally called from within the gui event loop
578 after run_editor has added the plugin (editor) to the linked list*/
580 int vstfx_create_editor (VSTFX* vstfx)
582 Window parent_window;
587 /* Note: vstfx->lock is held while this function is called */
589 if (!(vstfx->plugin->flags & effFlagsHasEditor))
591 vstfx_error ("** ERROR ** VSTFX: Plugin \"%s\" has no editor", vstfx->handle->name);
596 /*Create an XWindow for the plugin to inhabit*/
598 parent_window = XCreateSimpleWindow (
600 DefaultRootWindow(LXVST_XDisplay),
610 /*Select the events we are interested in receiving - we need Substructure notify so that
611 if the plugin resizes its window - e.g. Loomer Manifold then we get a message*/
613 XSelectInput(LXVST_XDisplay,
615 SubstructureNotifyMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | ExposureMask);
617 vstfx->window = parent_window;
619 vstfx->xid = parent_window; //vstfx->xid will be referenced to connect to GTK UI in ardour later
621 /*Because the plugin may be operating on a different Display* to us, and therefore
622 the two event queues can be asynchronous, although we have created the window on
623 our display, we can't guarantee it exists in the server yet, which will
624 cause BadWindow crashes if the plugin tries to use it.
626 It would be nice to use CreateNotify events here, but they don't get
627 through on all window managers, so instead we pass a client message
628 into out queue, after the XCreateWindow. When this message pops out
629 in our event handler, it will trigger the second stage of plugin
630 Editor instantiation, and by then the Window should be valid...*/
632 XClientMessageEvent event;
634 /*Create an atom to identify our message (only if it doesn't already exist)*/
636 Atom WindowActiveAtom = XInternAtom(LXVST_XDisplay, "LaunchEditor", false);
638 event.type = ClientMessage;
639 event.send_event = true;
640 event.window = parent_window;
641 event.message_type = WindowActiveAtom;
643 event.format = 32; //Data format
644 event.data.l[0] = 0x0FEEDBAC; //Something we can recognize later
646 /*Push the event into the queue on our Display*/
648 XSendEvent(LXVST_XDisplay, parent_window, FALSE, NoEventMask, (XEvent*)&event);
650 /*Unlock - and we are done for the first part of staring the Editor...*/
652 pthread_mutex_unlock (&vstfx->lock);
657 int vstfx_launch_editor(VSTFX* vstfx)
659 /*This is the second stage of launching the editor (see vstfx_create editor)
660 we get called here in response to receiving the ClientMessage on our Window,
661 therefore it's about as safe (as can be) to assume that the Window we created
662 is now valid in the XServer and can be passed to the plugin in effEditOpen
663 without generating BadWindow errors when the plugin reparents itself into our
666 if(vstfx->been_activated)
669 Window parent_window;
675 parent_window = vstfx->window;
677 /*Open the editor - Bah! we have to pass the int windowID as a void pointer - yuck
678 it gets cast back to an int as the parent window XID in the plugin - and we have to pass the
679 Display* as a long */
681 /**************************************************************/
682 /* 64Bit --- parent window is an int passed as a void* so */
683 /* that should be ok for 64Bit machines */
685 /* Display is passed in as a long - ok on arch's where sizeof */
688 /* Most linux VST plugins open a connection to X on their own */
689 /* Display anyway so it may not matter */
691 /* linuxDSP VSTs don't use the host Display* at all */
692 /**************************************************************/
694 vstfx->plugin->dispatcher (vstfx->plugin, effEditOpen, 0, (long)LXVST_XDisplay, (void*)(parent_window), 0 );
696 /*QUIRK - some plugins need a slight delay after opening the editor before you can
697 ask the window size or they might return zero - specifically discoDSP */
701 /*Now we can find out how big the parent window should be (and try) to resize it*/
703 vstfx->plugin->dispatcher (vstfx->plugin, effEditGetRect, 0, 0, &er, 0 );
705 x_size = er->right - er->left;
706 y_size = er->bottom - er->top;
708 vstfx->width = x_size;
709 vstfx->height = y_size;
711 XResizeWindow(LXVST_XDisplay, parent_window, x_size, y_size);
713 XFlush (LXVST_XDisplay);
715 /*Not sure if we need to map the window or if the plugin will do it for us
716 it should be ok because XReparentWindow generates a Map event*/
718 /*mark the editor as activated - mainly so that vstfx_get_XID
719 will know it is valid*/
721 vstfx->been_activated = TRUE;
723 pthread_cond_signal (&vstfx->window_status_change);
727 /*May not be needed in the XLib version*/
729 void vstfx_move_window_into_view (VSTFX* vstfx)
732 /*This is probably the equivalent of Mapping an XWindow
733 but we most likely don't need it because the window
734 will be Mapped by XReparentWindow*/
738 /*Destroy the editor window*/
740 void vstfx_destroy_editor (VSTFX* vstfx)
742 pthread_mutex_lock (&vstfx->lock);
745 vstfx->destroy = TRUE;
746 pthread_cond_wait (&vstfx->window_status_change, &vstfx->lock);
748 pthread_mutex_unlock (&vstfx->lock);
751 /*Remove a vstfx instance from the linked list parsed by the
754 void vstfx_event_loop_remove_plugin (VSTFX* vstfx)
756 /*This only ever gets called from within our GUI thread
757 so we don't need to lock here - if we did there would be
763 for(p = vstfx_first, prev = NULL; p; prev = p, p = p->next)
769 prev->next = p->next;
775 if (vstfx_first == vstfx)
776 vstfx_first = vstfx_first->next;
779 /*Get the XID of the plugin editor window*/
781 int vstfx_get_XID (VSTFX* vstfx)
785 /*Wait for the lock to become free - otherwise
786 the window might be in the process of being
787 created and we get bad Window errors when trying
788 to embed it in the GTK UI*/
790 pthread_mutex_lock(&vstfx->lock);
792 /*The Window may be scheduled for creation
793 but not actually created by the gui_event_loop yet -
795 spin here until it has been activated. Possible
796 deadlock if the window never gets activated but
797 should not be called here if the window doesn't
798 exist or will never exist*/
800 while(!(vstfx->been_activated))
805 pthread_mutex_unlock(&vstfx->lock);
807 /*Finally it might be safe to return the ID -
808 problems will arise if we return either a zero ID
809 and GTK tries to socket it or if we return an ID
810 which hasn't yet become real to the server*/
815 float htonf (float v)
818 char * fin = (char*)&v;
819 char * fout = (char*)&result;
828 /*load_state and save_state do not appear to be needed (yet) in ardour
829 - untested at the moment, these are just replicas of the fst code*/
833 int vstfx_load_state (VSTFX* vstfx, char * filename)
835 FILE* f = fopen (filename, "rb");
838 char testMagic[sizeof (magic)];
839 fread (&testMagic, sizeof (magic), 1, f);
840 if (strcmp (testMagic, magic))
842 printf ("File corrupt\n");
846 char productString[64];
847 char vendorString[64];
853 fread (&length, sizeof (unsigned), 1, f);
854 length = htonl (length);
855 fread (productString, length, 1, f);
856 productString[length] = 0;
857 printf ("Product string: %s\n", productString);
859 success = vstfx_call_dispatcher(vstfx, effGetProductString, 0, 0, testString, 0);
863 if (strcmp (testString, productString) != 0)
865 printf ("Product string mismatch! Plugin has: %s\n", testString);
870 else if (length != 0)
872 printf ("Product string mismatch! Plugin has none.\n", testString);
877 fread (&length, sizeof (unsigned), 1, f);
878 length = htonl (length);
879 fread (effectName, length, 1, f);
880 effectName[length] = 0;
881 printf ("Effect name: %s\n", effectName);
883 success = vstfx_call_dispatcher(vstfx, effGetEffectName, 0, 0, testString, 0);
887 if(strcmp(testString, effectName)!= 0)
889 printf ("Effect name mismatch! Plugin has: %s\n", testString);
896 printf ("Effect name mismatch! Plugin has none.\n", testString);
901 fread (&length, sizeof (unsigned), 1, f);
902 length = htonl (length);
903 fread (vendorString, length, 1, f);
904 vendorString[length] = 0;
906 printf ("Vendor string: %s\n", vendorString);
908 success = vstfx_call_dispatcher(vstfx, effGetVendorString, 0, 0, testString, 0);
911 if (strcmp(testString, vendorString)!= 0)
913 printf ("Vendor string mismatch! Plugin has: %s\n", testString);
920 printf ("Vendor string mismatch! Plugin has none.\n", testString);
927 fread (&numParam, sizeof (int), 1, f);
928 numParam = htonl (numParam);
930 for (i = 0; i < numParam; ++i)
933 fread (&val, sizeof (float), 1, f);
936 pthread_mutex_lock(&vstfx->lock );
937 vstfx->plugin->setParameter(vstfx->plugin, i, val);
938 pthread_mutex_unlock(&vstfx->lock );
943 fread (&bytelen, sizeof (int), 1, f);
944 bytelen = htonl (bytelen);
948 char * buf = malloc (bytelen);
949 fread (buf, bytelen, 1, f);
951 vstfx_call_dispatcher(vstfx, 24, 0, bytelen, buf, 0);
957 printf ("Could not open state file\n");