2 Copyright (C) 2012 Paul Davis
3 Based on code by Paul Davis, Torben Hohn as part of FST
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /******************************************************************/
22 /** VSTFX - An engine based on FST for handling linuxVST plugins **/
23 /******************************************************************/
25 /** EDITOR tab stops at 4 **/
29 #include <jack/jack.h>
30 #include <jack/thread.h>
37 #include "ardour/linux_vst_support.h"
54 static pthread_mutex_t plugin_mutex;
56 static VSTState * vstfx_first = NULL;
58 const char magic[] = "VSTFX Plugin State v002";
60 int gui_thread_id = 0;
61 static int gui_quit = 0;
63 /*This will be our connection to X*/
65 Display* LXVST_XDisplay = NULL;
67 /*The thread handle for the GUI event loop*/
69 pthread_t LXVST_gui_event_thread;
71 /*Util functions to get the value of a property attached to an XWindow*/
75 int TempErrorHandler(Display *display, XErrorEvent *e)
84 int getXWindowProperty(Window window, Atom atom)
89 unsigned long userCount;
94 /*Use our own Xerror handler while we're in here - in an
95 attempt to stop the brain dead default Xerror behaviour of
96 qutting the entire application because of e.g. an invalid
99 XErrorHandler olderrorhandler = XSetErrorHandler(TempErrorHandler);
101 XGetWindowProperty( LXVST_XDisplay, //The display
104 0, //Offset into the data
105 1, //Number of 32Bit chunks of data
106 false, //false = don't delete the property
107 AnyPropertyType, //Required property type mask
108 &userType, //Actual type returned
109 &userSize, //Actual format returned
110 &userCount, //Actual number of items stored in the returned data
111 &bytes, //Number of bytes remaining if a partial read
112 &data); //The actual data read
114 if(LXVST_xerror == false && userCount == 1)
115 result = *(int*)data;
117 XSetErrorHandler(olderrorhandler);
119 /*Hopefully this will return zero if the property is not set*/
128 /********************************************************************/
129 /* This is untested - have no 64Bit plugins which use this */
130 /* system of passing an eventProc address */
131 /********************************************************************/
133 long getXWindowProperty(Window window, Atom atom)
138 unsigned long userCount;
141 LXVST_xerror = false;
143 /*Use our own Xerror handler while we're in here - in an
144 attempt to stop the brain dead default Xerror behaviour of
145 qutting the entire application because of e.g. an invalid
148 XErrorHandler olderrorhandler = XSetErrorHandler(TempErrorHandler);
150 XGetWindowProperty( LXVST_XDisplay,
163 if(LXVST_xerror == false && userCount == 1)
164 result = *(long*)data;
166 XSetErrorHandler(olderrorhandler);
168 /*Hopefully this will return zero if the property is not set*/
175 /*The event handler - called from within the main GUI thread to
176 dispatch events to any VST UIs which have callbacks stuck to them*/
179 dispatch_x_events (XEvent* event, VSTState* vstfx)
181 /*Handle some of the Events we might be interested in*/
185 /*Configure event - when the window is resized or first drawn*/
187 case ConfigureNotify:
189 Window window = event->xconfigure.event;
191 int width = event->xconfigure.width;
192 int height = event->xconfigure.height;
194 /*If we get a config notify on the parent window XID then we need to see
195 if the size has been changed - some plugins re-size their UI window e.g.
196 when opening a preset manager (you might think that should be spawned as a new window...) */
198 /*if the size has changed, we flag this so that in lxvst_pluginui.cc we can make the
199 change to the GTK parent window in ardour, from its UI thread*/
201 if (window == (Window) (vstfx->linux_window)) {
202 if (width != vstfx->width || height!=vstfx->height) {
203 vstfx->width = width;
204 vstfx->height = height;
205 vstfx->want_resize = 1;
207 /*QUIRK : Loomer plugins not only resize the UI but throw it into some random
208 position at the same time. We need to re-position the window at the origin of
211 if (vstfx->linux_plugin_ui_window) {
212 XMoveWindow (LXVST_XDisplay, vstfx->linux_plugin_ui_window, 0, 0);
221 /*Reparent Notify - when the plugin UI is reparented into
222 our Host Window we will get an event here... probably... */
226 Window ParentWindow = event->xreparent.parent;
228 /*If the ParentWindow matches the window for the vstfx instance then
229 the Child window must be the XID of the pluginUI window created by the
230 plugin, so we need to see if it has a callback stuck to it, and if so
231 set that up in the vstfx */
233 /***********************************************************/
234 /* 64Bit --- This mechanism is not 64Bit compatible at the */
236 /***********************************************************/
238 if (ParentWindow == (Window) (vstfx->linux_window)) {
240 Window PluginUIWindowID = event->xreparent.window;
242 vstfx->linux_plugin_ui_window = PluginUIWindowID;
244 int result = getXWindowProperty(PluginUIWindowID, XInternAtom(LXVST_XDisplay, "_XEventProc", false));
247 vstfx->eventProc = NULL;
249 vstfx->eventProc = (void (*) (void* event))result;
253 long result = getXWindowProperty(PluginUIWindowID, XInternAtom(LXVST_XDisplay, "_XEventProc", false));
256 vstfx->eventProc = NULL;
258 vstfx->eventProc = (void (*) (void* event))result;
266 Window window = event->xany.window;
267 Atom message_type = event->xclient.message_type;
269 /*The only client message we are interested in is to signal
270 that the plugin parent window is now valid and can be passed
271 to effEditOpen when the editor is launched*/
273 if (window == (Window) (vstfx->linux_window)) {
274 char* message = XGetAtomName(LXVST_XDisplay, message_type);
276 if (strcmp(message,"LaunchEditor") == 0) {
277 if (event->xclient.data.l[0] == 0x0FEEDBAC) {
278 vstfx_launch_editor (vstfx);
291 /* Some VSTs built with toolkits e.g. JUCE will manager their own UI
292 autonomously in the plugin, running the UI in its own thread, so once
293 we have created a parent window for the plugin, its UI takes care of
296 /*Other types register a callback as an Xwindow property on the plugin
297 UI window after they create it. If that is the case, we need to call it
298 here, passing the XEvent into it*/
300 if (vstfx->eventProc == NULL) {
304 vstfx->eventProc((void*)event);
308 maybe_set_program (VSTState* vstfx)
310 if (vstfx->want_program != -1) {
311 if (vstfx->vst_version >= 2) {
312 vstfx->plugin->dispatcher (vstfx->plugin, 67 /* effBeginSetProgram */, 0, 0, NULL, 0);
315 vstfx->plugin->dispatcher (vstfx->plugin, effSetProgram, 0, vstfx->want_program, NULL, 0);
317 if (vstfx->vst_version >= 2) {
318 vstfx->plugin->dispatcher (vstfx->plugin, 68 /* effEndSetProgram */, 0, 0, NULL, 0);
321 vstfx->want_program = -1;
324 if (vstfx->want_chunk == 1) {
325 vstfx->plugin->dispatcher (vstfx->plugin, 24 /* effSetChunk */, 1, vstfx->wanted_chunk_size, vstfx->wanted_chunk, 0);
326 vstfx->want_chunk = 0;
330 /** This is the main gui event loop for the plugin, we also need to pass
331 any Xevents to all the UI callbacks plugins 'may' have registered on their
332 windows, that is if they don't manage their own UIs **/
334 void* gui_event_loop (void* ptr)
337 int LXVST_sched_event_timer = 0;
338 int LXVST_sched_timer_interval = 50; //ms
341 /*The 'Forever' loop - runs the plugin UIs etc - based on the FST gui event loop*/
345 /* handle window creation requests, destroy requests,
346 and run idle callbacks */
348 /*Look at the XEvent queue - if there are any XEvents we need to handle them,
349 including passing them to all the plugin (eventProcs) we are currently managing*/
353 /*See if there are any events in the queue*/
355 int num_events = XPending(LXVST_XDisplay);
357 /*process them if there are any*/
361 XNextEvent(LXVST_XDisplay, &event);
363 /*Call dispatch events, with the event, for each plugin in the linked list*/
365 for (vstfx = vstfx_first; vstfx; vstfx = vstfx->next)
367 pthread_mutex_lock(&vstfx->lock);
369 dispatch_x_events(&event, vstfx);
371 pthread_mutex_unlock(&vstfx->lock);
378 /*We don't want to use all the CPU.. */
382 LXVST_sched_event_timer++;
384 LXVST_sched_event_timer = LXVST_sched_event_timer & 0x00FFFFFF;
386 /*See if its time for us to do a scheduled event pass on all the plugins*/
388 if((LXVST_sched_timer_interval!=0) && (!(LXVST_sched_event_timer% LXVST_sched_timer_interval)))
390 pthread_mutex_lock (&plugin_mutex);
393 /*Parse through the linked list of plugins*/
395 for (vstfx = vstfx_first; vstfx; vstfx = vstfx->next)
397 pthread_mutex_lock (&vstfx->lock);
399 /*Window scheduled for destruction*/
401 if (vstfx->destroy) {
402 if (vstfx->linux_window) {
403 vstfx->plugin->dispatcher (vstfx->plugin, effEditClose, 0, 0, NULL, 0.0);
405 XDestroyWindow (LXVST_XDisplay, vstfx->linux_window);
406 /* FIXME - probably safe to assume we never have an XID of 0 but not explicitly true */
407 vstfx->linux_window = 0;
408 vstfx->destroy = FALSE;
411 vstfx_event_loop_remove_plugin (vstfx);
412 vstfx->been_activated = FALSE;
413 pthread_cond_signal (&vstfx->window_status_change);
414 pthread_mutex_unlock (&vstfx->lock);
419 /*Window does not yet exist - scheduled for creation*/
421 /* FIXME - probably safe to assume 0 is not a valid XID but not explicitly true */
422 if (vstfx->linux_window == 0) {
423 if (vstfx_create_editor (vstfx)) {
424 vstfx_error ("** ERROR ** VSTFX : Cannot create editor for plugin %s", vstfx->handle->name);
425 vstfx_event_loop_remove_plugin (vstfx);
426 pthread_cond_signal (&vstfx->window_status_change);
427 pthread_mutex_unlock (&vstfx->lock);
430 /* condition/unlock: it was signalled & unlocked in fst_create_editor() */
434 maybe_set_program (vstfx);
435 vstfx->want_program = -1;
436 vstfx->want_chunk = 0;
438 /*scheduled call to dispatcher*/
440 if (vstfx->dispatcher_wantcall) {
441 vstfx->dispatcher_retval = vstfx->plugin->dispatcher (
443 vstfx->dispatcher_opcode,
444 vstfx->dispatcher_index,
445 vstfx->dispatcher_val,
446 vstfx->dispatcher_ptr,
447 vstfx->dispatcher_opt
450 vstfx->dispatcher_wantcall = 0;
451 pthread_cond_signal (&vstfx->plugin_dispatcher_called);
454 /*Call the editor Idle function in the plugin*/
456 vstfx->plugin->dispatcher (vstfx->plugin, effEditIdle, 0, 0, NULL, 0);
459 vstfx->plugin->dispatcher (vstfx->plugin, 53, 0, 0, NULL, 0);
461 pthread_mutex_unlock (&vstfx->lock);
463 pthread_mutex_unlock (&plugin_mutex);
467 /*Drop out to here if we set gui_quit to 1 */
472 /*The VSTFX Init function - this needs to be called before the VSTFX engine
473 can be accessed, it gets the UI thread running, opens a connection to X etc
474 normally started in globals.cc*/
476 int vstfx_init (void* ptr)
479 int thread_create_result;
481 pthread_attr_t thread_attributes;
483 /*Init the attribs to defaults*/
485 pthread_attr_init(&thread_attributes);
487 /*Make sure the thread is joinable - this should be the default anyway -
488 so we can join to it on vstfx_exit*/
490 pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_JOINABLE);
493 /*This is where we need to open a connection to X, and start the GUI thread*/
495 /*Open our connection to X - all linuxVST plugin UIs handled by the LXVST engine
496 will talk to X down this connection - X cannot handle multi-threaded access via
499 if(LXVST_XDisplay==NULL)
500 LXVST_XDisplay = XOpenDisplay(NULL); //We might be able to make this open a specific screen etc
502 /*Drop out and report the error if we fail to connect to X */
504 if(LXVST_XDisplay==NULL)
506 vstfx_error ("** ERROR ** VSTFX: Failed opening connection to X");
511 /*We have a connection to X - so start the gui event loop*/
513 /*Create the thread - use default attrs for now, don't think we need anything special*/
515 thread_create_result = pthread_create(&LXVST_gui_event_thread, NULL, gui_event_loop, NULL);
517 if(thread_create_result!=0)
519 /*There was a problem starting the GUI event thread*/
521 vstfx_error ("** ERROR ** VSTFX: Failed starting GUI event thread");
523 XCloseDisplay(LXVST_XDisplay);
531 /*The vstfx Quit function*/
537 /*We need to pthread_join the gui_thread here so
538 we know when it has stopped*/
540 pthread_join(LXVST_gui_event_thread, NULL);
543 /*Adds a new plugin (VSTFX) instance to the linked list*/
545 int vstfx_run_editor (VSTState* vstfx)
547 pthread_mutex_lock (&plugin_mutex);
549 /* Add the new VSTFX instance to the linked list */
551 if (vstfx_first == NULL) {
554 VSTState* p = vstfx_first;
561 /* Mark the new end of the list */
566 pthread_mutex_unlock (&plugin_mutex);
568 /* wait for the plugin editor window to be created (or not) */
570 pthread_mutex_lock (&vstfx->lock);
572 if (!vstfx->linux_window) {
573 pthread_cond_wait (&vstfx->window_status_change, &vstfx->lock);
576 pthread_mutex_unlock (&vstfx->lock);
578 if (!vstfx->linux_window) {
586 /*Creates an editor for the plugin - normally called from within the gui event loop
587 after run_editor has added the plugin (editor) to the linked list*/
589 int vstfx_create_editor (VSTState* vstfx)
591 Window parent_window;
596 /* Note: vstfx->lock is held while this function is called */
598 if (!(vstfx->plugin->flags & effFlagsHasEditor))
600 vstfx_error ("** ERROR ** VSTFX: Plugin \"%s\" has no editor", vstfx->handle->name);
605 /*Create an XWindow for the plugin to inhabit*/
607 parent_window = XCreateSimpleWindow (
609 DefaultRootWindow(LXVST_XDisplay),
619 /*Select the events we are interested in receiving - we need Substructure notify so that
620 if the plugin resizes its window - e.g. Loomer Manifold then we get a message*/
622 XSelectInput(LXVST_XDisplay,
624 SubstructureNotifyMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | ExposureMask);
626 vstfx->linux_window = parent_window;
628 vstfx->xid = parent_window; //vstfx->xid will be referenced to connect to GTK UI in ardour later
630 /*Because the plugin may be operating on a different Display* to us, and therefore
631 the two event queues can be asynchronous, although we have created the window on
632 our display, we can't guarantee it exists in the server yet, which will
633 cause BadWindow crashes if the plugin tries to use it.
635 It would be nice to use CreateNotify events here, but they don't get
636 through on all window managers, so instead we pass a client message
637 into out queue, after the XCreateWindow. When this message pops out
638 in our event handler, it will trigger the second stage of plugin
639 Editor instantiation, and by then the Window should be valid...*/
641 XClientMessageEvent event;
643 /*Create an atom to identify our message (only if it doesn't already exist)*/
645 Atom WindowActiveAtom = XInternAtom(LXVST_XDisplay, "LaunchEditor", false);
647 event.type = ClientMessage;
648 event.send_event = true;
649 event.window = parent_window;
650 event.message_type = WindowActiveAtom;
652 event.format = 32; //Data format
653 event.data.l[0] = 0x0FEEDBAC; //Something we can recognize later
655 /*Push the event into the queue on our Display*/
657 XSendEvent(LXVST_XDisplay, parent_window, FALSE, NoEventMask, (XEvent*)&event);
659 /*Unlock - and we are done for the first part of staring the Editor...*/
661 pthread_mutex_unlock (&vstfx->lock);
667 vstfx_launch_editor (VSTState* vstfx)
669 /*This is the second stage of launching the editor (see vstfx_create editor)
670 we get called here in response to receiving the ClientMessage on our Window,
671 therefore it's about as safe (as can be) to assume that the Window we created
672 is now valid in the XServer and can be passed to the plugin in effEditOpen
673 without generating BadWindow errors when the plugin reparents itself into our
676 if(vstfx->been_activated)
679 Window parent_window;
685 parent_window = vstfx->linux_window;
687 /*Open the editor - Bah! we have to pass the int windowID as a void pointer - yuck
688 it gets cast back to an int as the parent window XID in the plugin - and we have to pass the
689 Display* as a long */
691 /**************************************************************/
692 /* 64Bit --- parent window is an int passed as a void* so */
693 /* that should be ok for 64Bit machines */
695 /* Display is passed in as a long - ok on arch's where sizeof */
698 /* Most linux VST plugins open a connection to X on their own */
699 /* Display anyway so it may not matter */
701 /* linuxDSP VSTs don't use the host Display* at all */
702 /**************************************************************/
704 vstfx->plugin->dispatcher (vstfx->plugin, effEditOpen, 0, (long)LXVST_XDisplay, (void*)(parent_window), 0 );
706 /*QUIRK - some plugins need a slight delay after opening the editor before you can
707 ask the window size or they might return zero - specifically discoDSP */
711 /*Now we can find out how big the parent window should be (and try) to resize it*/
713 vstfx->plugin->dispatcher (vstfx->plugin, effEditGetRect, 0, 0, &er, 0 );
715 x_size = er->right - er->left;
716 y_size = er->bottom - er->top;
718 vstfx->width = x_size;
719 vstfx->height = y_size;
721 XResizeWindow(LXVST_XDisplay, parent_window, x_size, y_size);
723 XFlush (LXVST_XDisplay);
725 /*Not sure if we need to map the window or if the plugin will do it for us
726 it should be ok because XReparentWindow generates a Map event*/
728 /*mark the editor as activated - mainly so that vstfx_get_XID
729 will know it is valid*/
731 vstfx->been_activated = TRUE;
733 pthread_cond_signal (&vstfx->window_status_change);
737 /** Destroy the editor window */
739 vstfx_destroy_editor (VSTState* vstfx)
741 pthread_mutex_lock (&vstfx->lock);
742 if (vstfx->linux_window) {
743 vstfx->destroy = TRUE;
744 pthread_cond_wait (&vstfx->window_status_change, &vstfx->lock);
746 pthread_mutex_unlock (&vstfx->lock);
749 /** Remove a vstfx instance from the linked list parsed by the
753 vstfx_event_loop_remove_plugin (VSTState* vstfx)
755 /* This only ever gets called from within our GUI thread
756 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) {
766 prev->next = p->next;
772 if (vstfx_first == vstfx) {
773 vstfx_first = vstfx_first->next;