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>
36 #include <glibmm/timer.h>
38 #include "ardour/linux_vst_support.h"
55 static pthread_mutex_t plugin_mutex;
57 static VSTState * vstfx_first = NULL;
59 const char magic[] = "VSTFX Plugin State v002";
61 int gui_thread_id = 0;
62 static int gui_quit = 0;
64 /*This will be our connection to X*/
66 Display* LXVST_XDisplay = NULL;
68 /*The thread handle for the GUI event loop*/
70 pthread_t LXVST_gui_event_thread;
72 /*Util functions to get the value of a property attached to an XWindow*/
76 int TempErrorHandler(Display *display, XErrorEvent *e)
85 int getXWindowProperty(Window window, Atom atom)
90 unsigned long userCount;
95 /*Use our own Xerror handler while we're in here - in an
96 attempt to stop the brain dead default Xerror behaviour of
97 qutting the entire application because of e.g. an invalid
100 XErrorHandler olderrorhandler = XSetErrorHandler(TempErrorHandler);
102 XGetWindowProperty( LXVST_XDisplay, //The display
105 0, //Offset into the data
106 1, //Number of 32Bit chunks of data
107 false, //false = don't delete the property
108 AnyPropertyType, //Required property type mask
109 &userType, //Actual type returned
110 &userSize, //Actual format returned
111 &userCount, //Actual number of items stored in the returned data
112 &bytes, //Number of bytes remaining if a partial read
113 &data); //The actual data read
115 if(LXVST_xerror == false && userCount == 1)
116 result = *(int*)data;
118 XSetErrorHandler(olderrorhandler);
120 /*Hopefully this will return zero if the property is not set*/
129 /********************************************************************/
130 /* This is untested - have no 64Bit plugins which use this */
131 /* system of passing an eventProc address */
132 /********************************************************************/
134 long getXWindowProperty(Window window, Atom atom)
139 unsigned long userCount;
142 LXVST_xerror = false;
144 /*Use our own Xerror handler while we're in here - in an
145 attempt to stop the brain dead default Xerror behaviour of
146 qutting the entire application because of e.g. an invalid
149 XErrorHandler olderrorhandler = XSetErrorHandler(TempErrorHandler);
151 XGetWindowProperty( LXVST_XDisplay,
164 if(LXVST_xerror == false && userCount == 1)
165 result = *(long*)data;
167 XSetErrorHandler(olderrorhandler);
169 /*Hopefully this will return zero if the property is not set*/
176 /*The event handler - called from within the main GUI thread to
177 dispatch events to any VST UIs which have callbacks stuck to them*/
180 dispatch_x_events (XEvent* event, VSTState* vstfx)
182 /*Handle some of the Events we might be interested in*/
186 /*Configure event - when the window is resized or first drawn*/
188 case ConfigureNotify:
190 Window window = event->xconfigure.event;
192 int width = event->xconfigure.width;
193 int height = event->xconfigure.height;
195 /*If we get a config notify on the parent window XID then we need to see
196 if the size has been changed - some plugins re-size their UI window e.g.
197 when opening a preset manager (you might think that should be spawned as a new window...) */
199 /*if the size has changed, we flag this so that in lxvst_pluginui.cc we can make the
200 change to the GTK parent window in ardour, from its UI thread*/
202 if (window == (Window) (vstfx->linux_window)) {
203 if (width != vstfx->width || height!=vstfx->height) {
204 vstfx->width = width;
205 vstfx->height = height;
206 vstfx->want_resize = 1;
208 /*QUIRK : Loomer plugins not only resize the UI but throw it into some random
209 position at the same time. We need to re-position the window at the origin of
212 if (vstfx->linux_plugin_ui_window) {
213 XMoveWindow (LXVST_XDisplay, vstfx->linux_plugin_ui_window, 0, 0);
222 /*Reparent Notify - when the plugin UI is reparented into
223 our Host Window we will get an event here... probably... */
227 Window ParentWindow = event->xreparent.parent;
229 /*If the ParentWindow matches the window for the vstfx instance then
230 the Child window must be the XID of the pluginUI window created by the
231 plugin, so we need to see if it has a callback stuck to it, and if so
232 set that up in the vstfx */
234 /***********************************************************/
235 /* 64Bit --- This mechanism is not 64Bit compatible at the */
237 /***********************************************************/
239 if (ParentWindow == (Window) (vstfx->linux_window)) {
241 Window PluginUIWindowID = event->xreparent.window;
243 vstfx->linux_plugin_ui_window = PluginUIWindowID;
245 int result = getXWindowProperty(PluginUIWindowID, XInternAtom(LXVST_XDisplay, "_XEventProc", false));
248 vstfx->eventProc = NULL;
250 vstfx->eventProc = (void (*) (void* event))result;
254 long result = getXWindowProperty(PluginUIWindowID, XInternAtom(LXVST_XDisplay, "_XEventProc", false));
257 vstfx->eventProc = NULL;
259 vstfx->eventProc = (void (*) (void* event))result;
267 Window window = event->xany.window;
268 Atom message_type = event->xclient.message_type;
270 /*The only client message we are interested in is to signal
271 that the plugin parent window is now valid and can be passed
272 to effEditOpen when the editor is launched*/
274 if (window == (Window) (vstfx->linux_window)) {
275 char* message = XGetAtomName(LXVST_XDisplay, message_type);
277 if (strcmp(message,"LaunchEditor") == 0) {
278 if (event->xclient.data.l[0] == 0x0FEEDBAC) {
279 vstfx_launch_editor (vstfx);
292 /* Some VSTs built with toolkits e.g. JUCE will manager their own UI
293 autonomously in the plugin, running the UI in its own thread, so once
294 we have created a parent window for the plugin, its UI takes care of
297 /*Other types register a callback as an Xwindow property on the plugin
298 UI window after they create it. If that is the case, we need to call it
299 here, passing the XEvent into it*/
301 if (vstfx->eventProc == NULL) {
305 vstfx->eventProc((void*)event);
309 maybe_set_program (VSTState* vstfx)
311 if (vstfx->want_program != -1) {
312 if (vstfx->vst_version >= 2) {
313 vstfx->plugin->dispatcher (vstfx->plugin, 67 /* effBeginSetProgram */, 0, 0, NULL, 0);
316 vstfx->plugin->dispatcher (vstfx->plugin, effSetProgram, 0, vstfx->want_program, NULL, 0);
318 if (vstfx->vst_version >= 2) {
319 vstfx->plugin->dispatcher (vstfx->plugin, 68 /* effEndSetProgram */, 0, 0, NULL, 0);
322 vstfx->want_program = -1;
325 if (vstfx->want_chunk == 1) {
326 vstfx->plugin->dispatcher (vstfx->plugin, 24 /* effSetChunk */, 1, vstfx->wanted_chunk_size, vstfx->wanted_chunk, 0);
327 vstfx->want_chunk = 0;
331 /** This is the main gui event loop for the plugin, we also need to pass
332 any Xevents to all the UI callbacks plugins 'may' have registered on their
333 windows, that is if they don't manage their own UIs **/
335 void* gui_event_loop (void* ptr)
338 int LXVST_sched_event_timer = 0;
339 int LXVST_sched_timer_interval = 50; //ms
342 /*The 'Forever' loop - runs the plugin UIs etc - based on the FST gui event loop*/
346 /* handle window creation requests, destroy requests,
347 and run idle callbacks */
349 /*Look at the XEvent queue - if there are any XEvents we need to handle them,
350 including passing them to all the plugin (eventProcs) we are currently managing*/
354 /*See if there are any events in the queue*/
356 int num_events = XPending(LXVST_XDisplay);
358 /*process them if there are any*/
362 XNextEvent(LXVST_XDisplay, &event);
364 /*Call dispatch events, with the event, for each plugin in the linked list*/
366 for (vstfx = vstfx_first; vstfx; vstfx = vstfx->next)
368 pthread_mutex_lock(&vstfx->lock);
370 dispatch_x_events(&event, vstfx);
372 pthread_mutex_unlock(&vstfx->lock);
379 /*We don't want to use all the CPU.. */
383 LXVST_sched_event_timer++;
385 LXVST_sched_event_timer = LXVST_sched_event_timer & 0x00FFFFFF;
387 /*See if its time for us to do a scheduled event pass on all the plugins*/
389 if((LXVST_sched_timer_interval!=0) && (!(LXVST_sched_event_timer% LXVST_sched_timer_interval)))
391 pthread_mutex_lock (&plugin_mutex);
394 /*Parse through the linked list of plugins*/
396 for (vstfx = vstfx_first; vstfx; vstfx = vstfx->next)
398 pthread_mutex_lock (&vstfx->lock);
400 /*Window scheduled for destruction*/
402 if (vstfx->destroy) {
403 if (vstfx->linux_window) {
404 vstfx->plugin->dispatcher (vstfx->plugin, effEditClose, 0, 0, NULL, 0.0);
406 XDestroyWindow (LXVST_XDisplay, vstfx->linux_window);
407 /* FIXME - probably safe to assume we never have an XID of 0 but not explicitly true */
408 vstfx->linux_window = 0;
409 vstfx->destroy = FALSE;
412 vstfx_event_loop_remove_plugin (vstfx);
413 vstfx->been_activated = FALSE;
414 pthread_cond_signal (&vstfx->window_status_change);
415 pthread_mutex_unlock (&vstfx->lock);
420 /*Window does not yet exist - scheduled for creation*/
422 /* FIXME - probably safe to assume 0 is not a valid XID but not explicitly true */
423 if (vstfx->linux_window == 0) {
424 if (vstfx_create_editor (vstfx)) {
425 vstfx_error ("** ERROR ** VSTFX : Cannot create editor for plugin %s", vstfx->handle->name);
426 vstfx_event_loop_remove_plugin (vstfx);
427 pthread_cond_signal (&vstfx->window_status_change);
428 pthread_mutex_unlock (&vstfx->lock);
431 /* condition/unlock: it was signalled & unlocked in fst_create_editor() */
435 maybe_set_program (vstfx);
436 vstfx->want_program = -1;
437 vstfx->want_chunk = 0;
439 /*scheduled call to dispatcher*/
441 if (vstfx->dispatcher_wantcall) {
442 vstfx->dispatcher_retval = vstfx->plugin->dispatcher (
444 vstfx->dispatcher_opcode,
445 vstfx->dispatcher_index,
446 vstfx->dispatcher_val,
447 vstfx->dispatcher_ptr,
448 vstfx->dispatcher_opt
451 vstfx->dispatcher_wantcall = 0;
452 pthread_cond_signal (&vstfx->plugin_dispatcher_called);
455 /*Call the editor Idle function in the plugin*/
457 vstfx->plugin->dispatcher (vstfx->plugin, effEditIdle, 0, 0, NULL, 0);
460 vstfx->plugin->dispatcher (vstfx->plugin, 53, 0, 0, NULL, 0);
462 pthread_mutex_unlock (&vstfx->lock);
464 pthread_mutex_unlock (&plugin_mutex);
468 /*Drop out to here if we set gui_quit to 1 */
473 /*The VSTFX Init function - this needs to be called before the VSTFX engine
474 can be accessed, it gets the UI thread running, opens a connection to X etc
475 normally started in globals.cc*/
477 int vstfx_init (void* ptr)
480 int thread_create_result;
482 pthread_attr_t thread_attributes;
484 /*Init the attribs to defaults*/
486 pthread_attr_init(&thread_attributes);
488 /*Make sure the thread is joinable - this should be the default anyway -
489 so we can join to it on vstfx_exit*/
491 pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_JOINABLE);
494 /*This is where we need to open a connection to X, and start the GUI thread*/
496 /*Open our connection to X - all linuxVST plugin UIs handled by the LXVST engine
497 will talk to X down this connection - X cannot handle multi-threaded access via
500 if(LXVST_XDisplay==NULL)
501 LXVST_XDisplay = XOpenDisplay(NULL); //We might be able to make this open a specific screen etc
503 /*Drop out and report the error if we fail to connect to X */
505 if(LXVST_XDisplay==NULL)
507 vstfx_error ("** ERROR ** VSTFX: Failed opening connection to X");
512 /*We have a connection to X - so start the gui event loop*/
514 /*Create the thread - use default attrs for now, don't think we need anything special*/
516 thread_create_result = pthread_create(&LXVST_gui_event_thread, NULL, gui_event_loop, NULL);
518 if(thread_create_result!=0)
520 /*There was a problem starting the GUI event thread*/
522 vstfx_error ("** ERROR ** VSTFX: Failed starting GUI event thread");
524 XCloseDisplay(LXVST_XDisplay);
532 /*The vstfx Quit function*/
538 /*We need to pthread_join the gui_thread here so
539 we know when it has stopped*/
541 pthread_join(LXVST_gui_event_thread, NULL);
544 /*Adds a new plugin (VSTFX) instance to the linked list*/
546 int vstfx_run_editor (VSTState* vstfx)
548 pthread_mutex_lock (&plugin_mutex);
550 /* Add the new VSTFX instance to the linked list */
552 if (vstfx_first == NULL) {
555 VSTState* p = vstfx_first;
562 /* Mark the new end of the list */
567 pthread_mutex_unlock (&plugin_mutex);
569 /* wait for the plugin editor window to be created (or not) */
571 pthread_mutex_lock (&vstfx->lock);
573 if (!vstfx->linux_window) {
574 pthread_cond_wait (&vstfx->window_status_change, &vstfx->lock);
577 pthread_mutex_unlock (&vstfx->lock);
579 if (!vstfx->linux_window) {
587 /*Creates an editor for the plugin - normally called from within the gui event loop
588 after run_editor has added the plugin (editor) to the linked list*/
590 int vstfx_create_editor (VSTState* vstfx)
592 Window parent_window;
597 /* Note: vstfx->lock is held while this function is called */
599 if (!(vstfx->plugin->flags & effFlagsHasEditor))
601 vstfx_error ("** ERROR ** VSTFX: Plugin \"%s\" has no editor", vstfx->handle->name);
606 /*Create an XWindow for the plugin to inhabit*/
608 parent_window = XCreateSimpleWindow (
610 DefaultRootWindow(LXVST_XDisplay),
620 /*Select the events we are interested in receiving - we need Substructure notify so that
621 if the plugin resizes its window - e.g. Loomer Manifold then we get a message*/
623 XSelectInput(LXVST_XDisplay,
625 SubstructureNotifyMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | ExposureMask);
627 vstfx->linux_window = parent_window;
629 vstfx->xid = parent_window; //vstfx->xid will be referenced to connect to GTK UI in ardour later
631 /*Because the plugin may be operating on a different Display* to us, and therefore
632 the two event queues can be asynchronous, although we have created the window on
633 our display, we can't guarantee it exists in the server yet, which will
634 cause BadWindow crashes if the plugin tries to use it.
636 It would be nice to use CreateNotify events here, but they don't get
637 through on all window managers, so instead we pass a client message
638 into out queue, after the XCreateWindow. When this message pops out
639 in our event handler, it will trigger the second stage of plugin
640 Editor instantiation, and by then the Window should be valid...*/
642 XClientMessageEvent event;
644 /*Create an atom to identify our message (only if it doesn't already exist)*/
646 Atom WindowActiveAtom = XInternAtom(LXVST_XDisplay, "LaunchEditor", false);
648 event.type = ClientMessage;
649 event.send_event = true;
650 event.window = parent_window;
651 event.message_type = WindowActiveAtom;
653 event.format = 32; //Data format
654 event.data.l[0] = 0x0FEEDBAC; //Something we can recognize later
656 /*Push the event into the queue on our Display*/
658 XSendEvent(LXVST_XDisplay, parent_window, FALSE, NoEventMask, (XEvent*)&event);
660 /*Unlock - and we are done for the first part of staring the Editor...*/
662 pthread_mutex_unlock (&vstfx->lock);
668 vstfx_launch_editor (VSTState* vstfx)
670 /*This is the second stage of launching the editor (see vstfx_create editor)
671 we get called here in response to receiving the ClientMessage on our Window,
672 therefore it's about as safe (as can be) to assume that the Window we created
673 is now valid in the XServer and can be passed to the plugin in effEditOpen
674 without generating BadWindow errors when the plugin reparents itself into our
677 if(vstfx->been_activated)
680 Window parent_window;
686 parent_window = vstfx->linux_window;
688 /*Open the editor - Bah! we have to pass the int windowID as a void pointer - yuck
689 it gets cast back to an int as the parent window XID in the plugin - and we have to pass the
690 Display* as a long */
692 /**************************************************************/
693 /* 64Bit --- parent window is an int passed as a void* so */
694 /* that should be ok for 64Bit machines */
696 /* Display is passed in as a long - ok on arch's where sizeof */
699 /* Most linux VST plugins open a connection to X on their own */
700 /* Display anyway so it may not matter */
702 /* linuxDSP VSTs don't use the host Display* at all */
703 /**************************************************************/
705 vstfx->plugin->dispatcher (vstfx->plugin, effEditOpen, 0, (long)LXVST_XDisplay, (void*)(parent_window), 0 );
707 /*QUIRK - some plugins need a slight delay after opening the editor before you can
708 ask the window size or they might return zero - specifically discoDSP */
710 Glib::usleep(100000);
712 /*Now we can find out how big the parent window should be (and try) to resize it*/
714 vstfx->plugin->dispatcher (vstfx->plugin, effEditGetRect, 0, 0, &er, 0 );
716 x_size = er->right - er->left;
717 y_size = er->bottom - er->top;
719 vstfx->width = x_size;
720 vstfx->height = y_size;
722 XResizeWindow(LXVST_XDisplay, parent_window, x_size, y_size);
724 XFlush (LXVST_XDisplay);
726 /*Not sure if we need to map the window or if the plugin will do it for us
727 it should be ok because XReparentWindow generates a Map event*/
729 /*mark the editor as activated - mainly so that vstfx_get_XID
730 will know it is valid*/
732 vstfx->been_activated = TRUE;
734 pthread_cond_signal (&vstfx->window_status_change);
738 /** Destroy the editor window */
740 vstfx_destroy_editor (VSTState* vstfx)
742 pthread_mutex_lock (&vstfx->lock);
743 if (vstfx->linux_window) {
744 vstfx->destroy = TRUE;
745 pthread_cond_wait (&vstfx->window_status_change, &vstfx->lock);
747 pthread_mutex_unlock (&vstfx->lock);
750 /** Remove a vstfx instance from the linked list parsed by the
754 vstfx_event_loop_remove_plugin (VSTState* 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
764 for (p = vstfx_first, prev = NULL; p; prev = p, p = p->next) {
767 prev->next = p->next;
773 if (vstfx_first == vstfx) {
774 vstfx_first = vstfx_first->next;