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/linux_vst_support.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);
291 maybe_set_program (VSTState* vstfx)
293 if (vstfx->want_program != -1) {
294 if (vstfx->vst_version >= 2) {
295 vstfx->plugin->dispatcher (vstfx->plugin, 67 /* effBeginSetProgram */, 0, 0, NULL, 0);
298 vstfx->plugin->dispatcher (vstfx->plugin, effSetProgram, 0, vstfx->want_program, NULL, 0);
300 if (vstfx->vst_version >= 2) {
301 vstfx->plugin->dispatcher (vstfx->plugin, 68 /* effEndSetProgram */, 0, 0, NULL, 0);
304 vstfx->want_program = -1;
307 if (vstfx->want_chunk == 1) {
308 vstfx->plugin->dispatcher (vstfx->plugin, 24 /* effSetChunk */, 1, vstfx->wanted_chunk_size, vstfx->wanted_chunk, 0);
309 vstfx->want_chunk = 0;
313 /** This is the main gui event loop for the plugin, we also need to pass
314 any Xevents to all the UI callbacks plugins 'may' have registered on their
315 windows, that is if they don't manage their own UIs **/
317 void* gui_event_loop (void* ptr)
320 int LXVST_sched_event_timer = 0;
321 int LXVST_sched_timer_interval = 50; //ms
324 /*The 'Forever' loop - runs the plugin UIs etc - based on the FST gui event loop*/
328 /* handle window creation requests, destroy requests,
329 and run idle callbacks */
331 /*Look at the XEvent queue - if there are any XEvents we need to handle them,
332 including passing them to all the plugin (eventProcs) we are currently managing*/
336 /*See if there are any events in the queue*/
338 int num_events = XPending(LXVST_XDisplay);
340 /*process them if there are any*/
344 XNextEvent(LXVST_XDisplay, &event);
346 /*Call dispatch events, with the event, for each plugin in the linked list*/
348 for (vstfx = vstfx_first; vstfx; vstfx = vstfx->next)
350 pthread_mutex_lock(&vstfx->lock);
352 dispatch_x_events(&event, vstfx);
354 pthread_mutex_unlock(&vstfx->lock);
361 /*We don't want to use all the CPU.. */
365 LXVST_sched_event_timer++;
367 LXVST_sched_event_timer = LXVST_sched_event_timer & 0x00FFFFFF;
369 /*See if its time for us to do a scheduled event pass on all the plugins*/
371 if((LXVST_sched_timer_interval!=0) && (!(LXVST_sched_event_timer% LXVST_sched_timer_interval)))
373 pthread_mutex_lock (&plugin_mutex);
376 /*Parse through the linked list of plugins*/
378 for (vstfx = vstfx_first; vstfx; vstfx = vstfx->next)
380 pthread_mutex_lock (&vstfx->lock);
382 /*Window scheduled for destruction*/
384 if (vstfx->destroy) {
385 if (vstfx->linux_window) {
386 vstfx->plugin->dispatcher (vstfx->plugin, effEditClose, 0, 0, NULL, 0.0);
388 XDestroyWindow (LXVST_XDisplay, vstfx->linux_window);
389 /* FIXME - probably safe to assume we never have an XID of 0 but not explicitly true */
390 vstfx->linux_window = 0;
391 vstfx->destroy = FALSE;
394 vstfx_event_loop_remove_plugin (vstfx);
395 vstfx->been_activated = FALSE;
396 pthread_cond_signal (&vstfx->window_status_change);
397 pthread_mutex_unlock (&vstfx->lock);
402 /*Window does not yet exist - scheduled for creation*/
404 /* FIXME - probably safe to assume 0 is not a valid XID but not explicitly true */
405 if (vstfx->linux_window == 0) {
406 if (vstfx_create_editor (vstfx)) {
407 vstfx_error ("** ERROR ** VSTFX : Cannot create editor for plugin %s", vstfx->handle->name);
408 vstfx_event_loop_remove_plugin (vstfx);
409 pthread_cond_signal (&vstfx->window_status_change);
410 pthread_mutex_unlock (&vstfx->lock);
413 /* condition/unlock: it was signalled & unlocked in fst_create_editor() */
417 maybe_set_program (vstfx);
418 vstfx->want_program = -1;
419 vstfx->want_chunk = 0;
421 /*scheduled call to dispatcher*/
423 if (vstfx->dispatcher_wantcall) {
424 vstfx->dispatcher_retval = vstfx->plugin->dispatcher (
426 vstfx->dispatcher_opcode,
427 vstfx->dispatcher_index,
428 vstfx->dispatcher_val,
429 vstfx->dispatcher_ptr,
430 vstfx->dispatcher_opt
433 vstfx->dispatcher_wantcall = 0;
434 pthread_cond_signal (&vstfx->plugin_dispatcher_called);
437 /*Call the editor Idle function in the plugin*/
439 vstfx->plugin->dispatcher (vstfx->plugin, effEditIdle, 0, 0, NULL, 0);
442 vstfx->plugin->dispatcher (vstfx->plugin, 53, 0, 0, NULL, 0);
444 pthread_mutex_unlock (&vstfx->lock);
446 pthread_mutex_unlock (&plugin_mutex);
450 /*Drop out to here if we set gui_quit to 1 */
455 /*The VSTFX Init function - this needs to be called before the VSTFX engine
456 can be accessed, it gets the UI thread running, opens a connection to X etc
457 normally started in globals.cc*/
459 int vstfx_init (void* ptr)
462 int thread_create_result;
464 pthread_attr_t thread_attributes;
466 /*Init the attribs to defaults*/
468 pthread_attr_init(&thread_attributes);
470 /*Make sure the thread is joinable - this should be the default anyway -
471 so we can join to it on vstfx_exit*/
473 pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_JOINABLE);
476 /*This is where we need to open a connection to X, and start the GUI thread*/
478 /*Open our connection to X - all linuxVST plugin UIs handled by the LXVST engine
479 will talk to X down this connection - X cannot handle multi-threaded access via
482 if(LXVST_XDisplay==NULL)
483 LXVST_XDisplay = XOpenDisplay(NULL); //We might be able to make this open a specific screen etc
485 /*Drop out and report the error if we fail to connect to X */
487 if(LXVST_XDisplay==NULL)
489 vstfx_error ("** ERROR ** VSTFX: Failed opening connection to X");
494 /*We have a connection to X - so start the gui event loop*/
496 /*Create the thread - use default attrs for now, don't think we need anything special*/
498 thread_create_result = pthread_create(&LXVST_gui_event_thread, NULL, gui_event_loop, NULL);
500 if(thread_create_result!=0)
502 /*There was a problem starting the GUI event thread*/
504 vstfx_error ("** ERROR ** VSTFX: Failed starting GUI event thread");
506 XCloseDisplay(LXVST_XDisplay);
514 /*The vstfx Quit function*/
520 /*We need to pthread_join the gui_thread here so
521 we know when it has stopped*/
523 pthread_join(LXVST_gui_event_thread, NULL);
526 /*Adds a new plugin (VSTFX) instance to the linked list*/
528 int vstfx_run_editor (VSTState* vstfx)
530 pthread_mutex_lock (&plugin_mutex);
532 /* Add the new VSTFX instance to the linked list */
534 if (vstfx_first == NULL) {
537 VSTState* p = vstfx_first;
544 /* Mark the new end of the list */
549 pthread_mutex_unlock (&plugin_mutex);
551 /* wait for the plugin editor window to be created (or not) */
553 pthread_mutex_lock (&vstfx->lock);
555 if (!vstfx->linux_window) {
556 pthread_cond_wait (&vstfx->window_status_change, &vstfx->lock);
559 pthread_mutex_unlock (&vstfx->lock);
561 if (!vstfx->linux_window) {
569 /*Creates an editor for the plugin - normally called from within the gui event loop
570 after run_editor has added the plugin (editor) to the linked list*/
572 int vstfx_create_editor (VSTState* vstfx)
574 Window parent_window;
579 /* Note: vstfx->lock is held while this function is called */
581 if (!(vstfx->plugin->flags & effFlagsHasEditor))
583 vstfx_error ("** ERROR ** VSTFX: Plugin \"%s\" has no editor", vstfx->handle->name);
588 /*Create an XWindow for the plugin to inhabit*/
590 parent_window = XCreateSimpleWindow (
592 DefaultRootWindow(LXVST_XDisplay),
602 /*Select the events we are interested in receiving - we need Substructure notify so that
603 if the plugin resizes its window - e.g. Loomer Manifold then we get a message*/
605 XSelectInput(LXVST_XDisplay,
607 SubstructureNotifyMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | ExposureMask);
609 vstfx->linux_window = parent_window;
611 vstfx->xid = parent_window; //vstfx->xid will be referenced to connect to GTK UI in ardour later
613 /*Because the plugin may be operating on a different Display* to us, and therefore
614 the two event queues can be asynchronous, although we have created the window on
615 our display, we can't guarantee it exists in the server yet, which will
616 cause BadWindow crashes if the plugin tries to use it.
618 It would be nice to use CreateNotify events here, but they don't get
619 through on all window managers, so instead we pass a client message
620 into out queue, after the XCreateWindow. When this message pops out
621 in our event handler, it will trigger the second stage of plugin
622 Editor instantiation, and by then the Window should be valid...*/
624 XClientMessageEvent event;
626 /*Create an atom to identify our message (only if it doesn't already exist)*/
628 Atom WindowActiveAtom = XInternAtom(LXVST_XDisplay, "LaunchEditor", false);
630 event.type = ClientMessage;
631 event.send_event = true;
632 event.window = parent_window;
633 event.message_type = WindowActiveAtom;
635 event.format = 32; //Data format
636 event.data.l[0] = 0x0FEEDBAC; //Something we can recognize later
638 /*Push the event into the queue on our Display*/
640 XSendEvent(LXVST_XDisplay, parent_window, FALSE, NoEventMask, (XEvent*)&event);
642 /*Unlock - and we are done for the first part of staring the Editor...*/
644 pthread_mutex_unlock (&vstfx->lock);
650 vstfx_launch_editor (VSTState* vstfx)
652 /*This is the second stage of launching the editor (see vstfx_create editor)
653 we get called here in response to receiving the ClientMessage on our Window,
654 therefore it's about as safe (as can be) to assume that the Window we created
655 is now valid in the XServer and can be passed to the plugin in effEditOpen
656 without generating BadWindow errors when the plugin reparents itself into our
659 if(vstfx->been_activated)
662 Window parent_window;
668 parent_window = vstfx->linux_window;
670 /*Open the editor - Bah! we have to pass the int windowID as a void pointer - yuck
671 it gets cast back to an int as the parent window XID in the plugin - and we have to pass the
672 Display* as a long */
674 /**************************************************************/
675 /* 64Bit --- parent window is an int passed as a void* so */
676 /* that should be ok for 64Bit machines */
678 /* Display is passed in as a long - ok on arch's where sizeof */
681 /* Most linux VST plugins open a connection to X on their own */
682 /* Display anyway so it may not matter */
684 /* linuxDSP VSTs don't use the host Display* at all */
685 /**************************************************************/
687 vstfx->plugin->dispatcher (vstfx->plugin, effEditOpen, 0, (long)LXVST_XDisplay, (void*)(parent_window), 0 );
689 /*QUIRK - some plugins need a slight delay after opening the editor before you can
690 ask the window size or they might return zero - specifically discoDSP */
694 /*Now we can find out how big the parent window should be (and try) to resize it*/
696 vstfx->plugin->dispatcher (vstfx->plugin, effEditGetRect, 0, 0, &er, 0 );
698 x_size = er->right - er->left;
699 y_size = er->bottom - er->top;
701 vstfx->width = x_size;
702 vstfx->height = y_size;
704 XResizeWindow(LXVST_XDisplay, parent_window, x_size, y_size);
706 XFlush (LXVST_XDisplay);
708 /*Not sure if we need to map the window or if the plugin will do it for us
709 it should be ok because XReparentWindow generates a Map event*/
711 /*mark the editor as activated - mainly so that vstfx_get_XID
712 will know it is valid*/
714 vstfx->been_activated = TRUE;
716 pthread_cond_signal (&vstfx->window_status_change);
720 /** Destroy the editor window */
722 vstfx_destroy_editor (VSTState* vstfx)
724 pthread_mutex_lock (&vstfx->lock);
725 if (vstfx->linux_window) {
726 vstfx->destroy = TRUE;
727 pthread_cond_wait (&vstfx->window_status_change, &vstfx->lock);
729 pthread_mutex_unlock (&vstfx->lock);
732 /** Remove a vstfx instance from the linked list parsed by the
736 vstfx_event_loop_remove_plugin (VSTState* vstfx)
738 /* This only ever gets called from within our GUI thread
739 so we don't need to lock here - if we did there would be
746 for (p = vstfx_first, prev = NULL; p; prev = p, p = p->next) {
749 prev->next = p->next;
755 if (vstfx_first == vstfx) {
756 vstfx_first = vstfx_first->next;