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 VSTState * 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 /*Util functions to get the value of a property attached to an XWindow*/
58 int TempErrorHandler(Display *display, XErrorEvent *e)
67 int getXWindowProperty(Window window, Atom atom)
72 unsigned long userCount;
77 /*Use our own Xerror handler while we're in here - in an
78 attempt to stop the brain dead default Xerror behaviour of
79 qutting the entire application because of e.g. an invalid
82 XErrorHandler olderrorhandler = XSetErrorHandler(TempErrorHandler);
84 XGetWindowProperty( LXVST_XDisplay, //The display
87 0, //Offset into the data
88 1, //Number of 32Bit chunks of data
89 false, //false = don't delete the property
90 AnyPropertyType, //Required property type mask
91 &userType, //Actual type returned
92 &userSize, //Actual format returned
93 &userCount, //Actual number of items stored in the returned data
94 &bytes, //Number of bytes remaining if a partial read
95 &data); //The actual data read
97 if(LXVST_xerror == false && userCount == 1)
100 XSetErrorHandler(olderrorhandler);
102 /*Hopefully this will return zero if the property is not set*/
111 /********************************************************************/
112 /* This is untested - have no 64Bit plugins which use this */
113 /* system of passing an eventProc address */
114 /********************************************************************/
116 long getXWindowProperty(Window window, Atom atom)
121 unsigned long userCount;
124 LXVST_xerror = false;
126 /*Use our own Xerror handler while we're in here - in an
127 attempt to stop the brain dead default Xerror behaviour of
128 qutting the entire application because of e.g. an invalid
131 XErrorHandler olderrorhandler = XSetErrorHandler(TempErrorHandler);
133 XGetWindowProperty( LXVST_XDisplay,
146 if(LXVST_xerror == false && userCount == 1)
147 result = *(long*)data;
149 XSetErrorHandler(olderrorhandler);
151 /*Hopefully this will return zero if the property is not set*/
158 /*The event handler - called from within the main GUI thread to
159 dispatch events to any VST UIs which have callbacks stuck to them*/
162 dispatch_x_events (XEvent* event, VSTState* vstfx)
164 /*Handle some of the Events we might be interested in*/
168 /*Configure event - when the window is resized or first drawn*/
170 case ConfigureNotify:
172 Window window = event->xconfigure.event;
174 int width = event->xconfigure.width;
175 int height = event->xconfigure.height;
177 /*If we get a config notify on the parent window XID then we need to see
178 if the size has been changed - some plugins re-size their UI window e.g.
179 when opening a preset manager (you might think that should be spawned as a new window...) */
181 /*if the size has changed, we flag this so that in lxvst_pluginui.cc we can make the
182 change to the GTK parent window in ardour, from its UI thread*/
184 if (window == (Window) (vstfx->linux_window)) {
185 if (width != vstfx->width || height!=vstfx->height) {
186 vstfx->width = width;
187 vstfx->height = height;
188 vstfx->want_resize = 1;
190 /*QUIRK : Loomer plugins not only resize the UI but throw it into some random
191 position at the same time. We need to re-position the window at the origin of
194 if (vstfx->linux_plugin_ui_window) {
195 XMoveWindow (LXVST_XDisplay, vstfx->linux_plugin_ui_window, 0, 0);
204 /*Reparent Notify - when the plugin UI is reparented into
205 our Host Window we will get an event here... probably... */
209 Window ParentWindow = event->xreparent.parent;
211 /*If the ParentWindow matches the window for the vstfx instance then
212 the Child window must be the XID of the pluginUI window created by the
213 plugin, so we need to see if it has a callback stuck to it, and if so
214 set that up in the vstfx */
216 /***********************************************************/
217 /* 64Bit --- This mechanism is not 64Bit compatible at the */
219 /***********************************************************/
221 if (ParentWindow == (Window) (vstfx->linux_window)) {
223 Window PluginUIWindowID = event->xreparent.window;
225 vstfx->linux_plugin_ui_window = PluginUIWindowID;
227 int result = getXWindowProperty(PluginUIWindowID, XInternAtom(LXVST_XDisplay, "_XEventProc", false));
230 vstfx->eventProc = NULL;
232 vstfx->eventProc = (void (*) (void* event))result;
236 long result = getXWindowProperty(PluginUIWindowID, XInternAtom(LXVST_XDisplay, "_XEventProc", false));
239 vstfx->eventProc = NULL;
241 vstfx->eventProc = (void (*) (void* event))result;
249 Window window = event->xany.window;
250 Atom message_type = event->xclient.message_type;
252 /*The only client message we are interested in is to signal
253 that the plugin parent window is now valid and can be passed
254 to effEditOpen when the editor is launched*/
256 if (window == (Window) (vstfx->linux_window)) {
257 char* message = XGetAtomName(LXVST_XDisplay, message_type);
259 if (strcmp(message,"LaunchEditor") == 0) {
260 if (event->xclient.data.l[0] == 0x0FEEDBAC) {
261 vstfx_launch_editor (vstfx);
274 /* Some VSTs built with toolkits e.g. JUCE will manager their own UI
275 autonomously in the plugin, running the UI in its own thread, so once
276 we have created a parent window for the plugin, its UI takes care of
279 /*Other types register a callback as an Xwindow property on the plugin
280 UI window after they create it. If that is the case, we need to call it
281 here, passing the XEvent into it*/
283 if (vstfx->eventProc == NULL) {
287 vstfx->eventProc((void*)event);
290 /** This is the main gui event loop for the plugin, we also need to pass
291 any Xevents to all the UI callbacks plugins 'may' have registered on their
292 windows, that is if they don't manage their own UIs **/
294 void* gui_event_loop (void* ptr)
297 int LXVST_sched_event_timer = 0;
298 int LXVST_sched_timer_interval = 50; //ms
301 /*The 'Forever' loop - runs the plugin UIs etc - based on the FST gui event loop*/
305 /* handle window creation requests, destroy requests,
306 and run idle callbacks */
308 /*Look at the XEvent queue - if there are any XEvents we need to handle them,
309 including passing them to all the plugin (eventProcs) we are currently managing*/
313 /*See if there are any events in the queue*/
315 int num_events = XPending(LXVST_XDisplay);
317 /*process them if there are any*/
321 XNextEvent(LXVST_XDisplay, &event);
323 /*Call dispatch events, with the event, for each plugin in the linked list*/
325 for (vstfx = vstfx_first; vstfx; vstfx = vstfx->next)
327 pthread_mutex_lock(&vstfx->lock);
329 dispatch_x_events(&event, vstfx);
331 pthread_mutex_unlock(&vstfx->lock);
338 /*We don't want to use all the CPU.. */
342 LXVST_sched_event_timer++;
344 LXVST_sched_event_timer = LXVST_sched_event_timer & 0x00FFFFFF;
346 /*See if its time for us to do a scheduled event pass on all the plugins*/
348 if((LXVST_sched_timer_interval!=0) && (!(LXVST_sched_event_timer% LXVST_sched_timer_interval)))
350 pthread_mutex_lock (&plugin_mutex);
353 /*Parse through the linked list of plugins*/
355 for (vstfx = vstfx_first; vstfx; vstfx = vstfx->next)
357 pthread_mutex_lock (&vstfx->lock);
359 /*Window scheduled for destruction*/
361 if (vstfx->destroy) {
362 if (vstfx->linux_window) {
363 vstfx->plugin->dispatcher (vstfx->plugin, effEditClose, 0, 0, NULL, 0.0);
365 XDestroyWindow (LXVST_XDisplay, vstfx->linux_window);
366 /* FIXME - probably safe to assume we never have an XID of 0 but not explicitly true */
367 vstfx->linux_window = 0;
368 vstfx->destroy = FALSE;
371 vstfx_event_loop_remove_plugin (vstfx);
372 vstfx->been_activated = FALSE;
373 pthread_cond_signal (&vstfx->window_status_change);
374 pthread_mutex_unlock (&vstfx->lock);
379 /*Window does not yet exist - scheduled for creation*/
381 /* FIXME - probably safe to assume 0 is not a valid XID but not explicitly true */
382 if (vstfx->linux_window == 0) {
383 if (vstfx_create_editor (vstfx)) {
384 vstfx_error ("** ERROR ** VSTFX : Cannot create editor for plugin %s", vstfx->handle->name);
385 vstfx_event_loop_remove_plugin (vstfx);
386 pthread_cond_signal (&vstfx->window_status_change);
387 pthread_mutex_unlock (&vstfx->lock);
390 /* condition/unlock: it was signalled & unlocked in fst_create_editor() */
394 /*Scheduled for setting a new program*/
396 if (vstfx->want_program != -1 )
398 if (vstfx->vst_version >= 2)
400 vstfx->plugin->dispatcher (vstfx->plugin, 67 /* effBeginSetProgram */, 0, 0, NULL, 0);
403 vstfx->plugin->dispatcher (vstfx->plugin, effSetProgram, 0, vstfx->want_program, NULL, 0);
405 if (vstfx->vst_version >= 2)
407 vstfx->plugin->dispatcher (vstfx->plugin, 68 /* effEndSetProgram */, 0, 0, NULL, 0);
410 vstfx->want_program = -1;
413 /*scheduled call to dispatcher*/
415 if (vstfx->dispatcher_wantcall) {
416 vstfx->dispatcher_retval = vstfx->plugin->dispatcher (
418 vstfx->dispatcher_opcode,
419 vstfx->dispatcher_index,
420 vstfx->dispatcher_val,
421 vstfx->dispatcher_ptr,
422 vstfx->dispatcher_opt
425 vstfx->dispatcher_wantcall = 0;
426 pthread_cond_signal (&vstfx->plugin_dispatcher_called);
429 /*Call the editor Idle function in the plugin*/
431 vstfx->plugin->dispatcher (vstfx->plugin, effEditIdle, 0, 0, NULL, 0);
434 vstfx->plugin->dispatcher (vstfx->plugin, 53, 0, 0, NULL, 0);
436 pthread_mutex_unlock (&vstfx->lock);
438 pthread_mutex_unlock (&plugin_mutex);
442 /*Drop out to here if we set gui_quit to 1 */
447 /*The VSTFX Init function - this needs to be called before the VSTFX engine
448 can be accessed, it gets the UI thread running, opens a connection to X etc
449 normally started in globals.cc*/
451 int vstfx_init (void* ptr)
454 int thread_create_result;
456 pthread_attr_t thread_attributes;
458 /*Init the attribs to defaults*/
460 pthread_attr_init(&thread_attributes);
462 /*Make sure the thread is joinable - this should be the default anyway -
463 so we can join to it on vstfx_exit*/
465 pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_JOINABLE);
468 /*This is where we need to open a connection to X, and start the GUI thread*/
470 /*Open our connection to X - all linuxVST plugin UIs handled by the LXVST engine
471 will talk to X down this connection - X cannot handle multi-threaded access via
474 if(LXVST_XDisplay==NULL)
475 LXVST_XDisplay = XOpenDisplay(NULL); //We might be able to make this open a specific screen etc
477 /*Drop out and report the error if we fail to connect to X */
479 if(LXVST_XDisplay==NULL)
481 vstfx_error ("** ERROR ** VSTFX: Failed opening connection to X");
486 /*We have a connection to X - so start the gui event loop*/
488 /*Create the thread - use default attrs for now, don't think we need anything special*/
490 thread_create_result = pthread_create(&LXVST_gui_event_thread, NULL, gui_event_loop, NULL);
492 if(thread_create_result!=0)
494 /*There was a problem starting the GUI event thread*/
496 vstfx_error ("** ERROR ** VSTFX: Failed starting GUI event thread");
498 XCloseDisplay(LXVST_XDisplay);
506 /*The vstfx Quit function*/
512 /*We need to pthread_join the gui_thread here so
513 we know when it has stopped*/
515 pthread_join(LXVST_gui_event_thread, NULL);
518 /*Adds a new plugin (VSTFX) instance to the linked list*/
520 int vstfx_run_editor (VSTState* vstfx)
522 pthread_mutex_lock (&plugin_mutex);
524 /* Add the new VSTFX instance to the linked list */
526 if (vstfx_first == NULL) {
529 VSTState* p = vstfx_first;
536 /* Mark the new end of the list */
541 pthread_mutex_unlock (&plugin_mutex);
543 /* wait for the plugin editor window to be created (or not) */
545 pthread_mutex_lock (&vstfx->lock);
547 if (!vstfx->linux_window) {
548 pthread_cond_wait (&vstfx->window_status_change, &vstfx->lock);
551 pthread_mutex_unlock (&vstfx->lock);
553 if (!vstfx->linux_window) {
561 /*Creates an editor for the plugin - normally called from within the gui event loop
562 after run_editor has added the plugin (editor) to the linked list*/
564 int vstfx_create_editor (VSTState* vstfx)
566 Window parent_window;
571 /* Note: vstfx->lock is held while this function is called */
573 if (!(vstfx->plugin->flags & effFlagsHasEditor))
575 vstfx_error ("** ERROR ** VSTFX: Plugin \"%s\" has no editor", vstfx->handle->name);
580 /*Create an XWindow for the plugin to inhabit*/
582 parent_window = XCreateSimpleWindow (
584 DefaultRootWindow(LXVST_XDisplay),
594 /*Select the events we are interested in receiving - we need Substructure notify so that
595 if the plugin resizes its window - e.g. Loomer Manifold then we get a message*/
597 XSelectInput(LXVST_XDisplay,
599 SubstructureNotifyMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | ExposureMask);
601 vstfx->linux_window = parent_window;
603 vstfx->xid = parent_window; //vstfx->xid will be referenced to connect to GTK UI in ardour later
605 /*Because the plugin may be operating on a different Display* to us, and therefore
606 the two event queues can be asynchronous, although we have created the window on
607 our display, we can't guarantee it exists in the server yet, which will
608 cause BadWindow crashes if the plugin tries to use it.
610 It would be nice to use CreateNotify events here, but they don't get
611 through on all window managers, so instead we pass a client message
612 into out queue, after the XCreateWindow. When this message pops out
613 in our event handler, it will trigger the second stage of plugin
614 Editor instantiation, and by then the Window should be valid...*/
616 XClientMessageEvent event;
618 /*Create an atom to identify our message (only if it doesn't already exist)*/
620 Atom WindowActiveAtom = XInternAtom(LXVST_XDisplay, "LaunchEditor", false);
622 event.type = ClientMessage;
623 event.send_event = true;
624 event.window = parent_window;
625 event.message_type = WindowActiveAtom;
627 event.format = 32; //Data format
628 event.data.l[0] = 0x0FEEDBAC; //Something we can recognize later
630 /*Push the event into the queue on our Display*/
632 XSendEvent(LXVST_XDisplay, parent_window, FALSE, NoEventMask, (XEvent*)&event);
634 /*Unlock - and we are done for the first part of staring the Editor...*/
636 pthread_mutex_unlock (&vstfx->lock);
642 vstfx_launch_editor (VSTState* vstfx)
644 /*This is the second stage of launching the editor (see vstfx_create editor)
645 we get called here in response to receiving the ClientMessage on our Window,
646 therefore it's about as safe (as can be) to assume that the Window we created
647 is now valid in the XServer and can be passed to the plugin in effEditOpen
648 without generating BadWindow errors when the plugin reparents itself into our
651 if(vstfx->been_activated)
654 Window parent_window;
660 parent_window = vstfx->linux_window;
662 /*Open the editor - Bah! we have to pass the int windowID as a void pointer - yuck
663 it gets cast back to an int as the parent window XID in the plugin - and we have to pass the
664 Display* as a long */
666 /**************************************************************/
667 /* 64Bit --- parent window is an int passed as a void* so */
668 /* that should be ok for 64Bit machines */
670 /* Display is passed in as a long - ok on arch's where sizeof */
673 /* Most linux VST plugins open a connection to X on their own */
674 /* Display anyway so it may not matter */
676 /* linuxDSP VSTs don't use the host Display* at all */
677 /**************************************************************/
679 vstfx->plugin->dispatcher (vstfx->plugin, effEditOpen, 0, (long)LXVST_XDisplay, (void*)(parent_window), 0 );
681 /*QUIRK - some plugins need a slight delay after opening the editor before you can
682 ask the window size or they might return zero - specifically discoDSP */
686 /*Now we can find out how big the parent window should be (and try) to resize it*/
688 vstfx->plugin->dispatcher (vstfx->plugin, effEditGetRect, 0, 0, &er, 0 );
690 x_size = er->right - er->left;
691 y_size = er->bottom - er->top;
693 vstfx->width = x_size;
694 vstfx->height = y_size;
696 XResizeWindow(LXVST_XDisplay, parent_window, x_size, y_size);
698 XFlush (LXVST_XDisplay);
700 /*Not sure if we need to map the window or if the plugin will do it for us
701 it should be ok because XReparentWindow generates a Map event*/
703 /*mark the editor as activated - mainly so that vstfx_get_XID
704 will know it is valid*/
706 vstfx->been_activated = TRUE;
708 pthread_cond_signal (&vstfx->window_status_change);
712 /** Destroy the editor window */
714 vstfx_destroy_editor (VSTState* vstfx)
716 pthread_mutex_lock (&vstfx->lock);
717 if (vstfx->linux_window) {
718 vstfx->destroy = TRUE;
719 pthread_cond_wait (&vstfx->window_status_change, &vstfx->lock);
721 pthread_mutex_unlock (&vstfx->lock);
724 /** Remove a vstfx instance from the linked list parsed by the
728 vstfx_event_loop_remove_plugin (VSTState* vstfx)
730 /* This only ever gets called from within our GUI thread
731 so we don't need to lock here - if we did there would be
738 for (p = vstfx_first, prev = NULL; p; prev = p, p = p->next) {
741 prev->next = p->next;
747 if (vstfx_first == vstfx) {
748 vstfx_first = vstfx_first->next;
756 char * fin = (char*)&v;
757 char * fout = (char*)&result;
766 /*load_state and save_state do not appear to be needed (yet) in ardour
767 - untested at the moment, these are just replicas of the fst code*/
771 int vstfx_load_state (VSTState* vstfx, char * filename)
773 FILE* f = fopen (filename, "rb");
776 char testMagic[sizeof (magic)];
777 fread (&testMagic, sizeof (magic), 1, f);
778 if (strcmp (testMagic, magic))
780 printf ("File corrupt\n");
784 char productString[64];
785 char vendorString[64];
791 fread (&length, sizeof (unsigned), 1, f);
792 length = htonl (length);
793 fread (productString, length, 1, f);
794 productString[length] = 0;
795 printf ("Product string: %s\n", productString);
797 success = vstfx_call_dispatcher(vstfx, effGetProductString, 0, 0, testString, 0);
801 if (strcmp (testString, productString) != 0)
803 printf ("Product string mismatch! Plugin has: %s\n", testString);
808 else if (length != 0)
810 printf ("Product string mismatch! Plugin has none.\n", testString);
815 fread (&length, sizeof (unsigned), 1, f);
816 length = htonl (length);
817 fread (effectName, length, 1, f);
818 effectName[length] = 0;
819 printf ("Effect name: %s\n", effectName);
821 success = vstfx_call_dispatcher(vstfx, effGetEffectName, 0, 0, testString, 0);
825 if(strcmp(testString, effectName)!= 0)
827 printf ("Effect name mismatch! Plugin has: %s\n", testString);
834 printf ("Effect name mismatch! Plugin has none.\n", testString);
839 fread (&length, sizeof (unsigned), 1, f);
840 length = htonl (length);
841 fread (vendorString, length, 1, f);
842 vendorString[length] = 0;
844 printf ("Vendor string: %s\n", vendorString);
846 success = vstfx_call_dispatcher(vstfx, effGetVendorString, 0, 0, testString, 0);
849 if (strcmp(testString, vendorString)!= 0)
851 printf ("Vendor string mismatch! Plugin has: %s\n", testString);
858 printf ("Vendor string mismatch! Plugin has none.\n", testString);
865 fread (&numParam, sizeof (int), 1, f);
866 numParam = htonl (numParam);
868 for (i = 0; i < numParam; ++i)
871 fread (&val, sizeof (float), 1, f);
874 pthread_mutex_lock(&vstfx->lock );
875 vstfx->plugin->setParameter(vstfx->plugin, i, val);
876 pthread_mutex_unlock(&vstfx->lock );
881 fread (&bytelen, sizeof (int), 1, f);
882 bytelen = htonl (bytelen);
886 char * buf = malloc (bytelen);
887 fread (buf, bytelen, 1, f);
889 vstfx_call_dispatcher(vstfx, 24, 0, bytelen, buf, 0);
895 printf ("Could not open state file\n");