fix mistaken "do not roll" conclusion in TransportFSM::compute_should_roll()
[ardour.git] / gtk2_ardour / linux_vst_gui_support.cc
1 /*
2  * Copyright (C) 2013-2015 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
4  *
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.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 /******************************************************************/
21 /** VSTFX - An engine based on FST for handling linuxVST plugins **/
22 /******************************************************************/
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <libgen.h>
27 #include <assert.h>
28
29 #include <pthread.h>
30 #include <signal.h>
31 #include <glib.h>
32 #include <glibmm/timer.h>
33
34 #include "ardour/linux_vst_support.h"
35 #include "ardour/vst_plugin.h"
36
37 #include <X11/X.h>
38 #include <X11/Xlib.h>
39 #include <dlfcn.h>
40 #include <string.h>
41 #include <time.h>
42 #include <unistd.h>
43 #include <pthread.h>
44 #include <sys/time.h>
45
46 struct ERect{
47     short top;
48     short left;
49     short bottom;
50     short right;
51 };
52
53 static pthread_mutex_t plugin_mutex;
54
55 static VSTState * vstfx_first = NULL;
56
57 const char magic[] = "VSTFX Plugin State v002";
58
59 static volatile int gui_quit = 0;
60
61 /*This will be our connection to X*/
62
63 static Display* LXVST_XDisplay = NULL;
64
65 /*The thread handle for the GUI event loop*/
66
67 static pthread_t LXVST_gui_event_thread;
68
69 /*Util functions to get the value of a property attached to an XWindow*/
70
71 static bool LXVST_xerror;
72
73 int TempErrorHandler (Display *display, XErrorEvent *e)
74 {
75         LXVST_xerror = true;
76
77         return 0;
78 }
79
80 #ifdef LXVST_32BIT
81
82 int getXWindowProperty (Window window, Atom atom)
83 {
84         int result = 0;
85         int userSize;
86         unsigned long bytes;
87         unsigned long userCount;
88         unsigned char *data;
89         Atom userType;
90         LXVST_xerror = false;
91
92         /*Use our own Xerror handler while we're in here - in an
93         attempt to stop the brain dead default Xerror behaviour of
94         qutting the entire application because of e.g. an invalid
95         window ID*/
96
97         XErrorHandler olderrorhandler = XSetErrorHandler (TempErrorHandler);
98
99         XGetWindowProperty (LXVST_XDisplay,  // The display
100                             window,          // The Window
101                             atom,            // The property
102                             0,               // Offset into the data
103                             1,               // Number of 32Bit chunks of data
104                             false,           // false = don't delete the property
105                             AnyPropertyType, // Required property type mask
106                             &userType,       // Actual type returned
107                             &userSize,       // Actual format returned
108                             &userCount,      // Actual number of items stored in the returned data
109                             &bytes,          // Number of bytes remaining if a partial read
110                             &data);          // The actual data read
111
112         if (LXVST_xerror == false && userCount == 1) {
113                 result = *(int*)data;
114         }
115
116         XSetErrorHandler (olderrorhandler);
117
118         /*Hopefully this will return zero if the property is not set*/
119
120         return result;
121 }
122
123 #endif
124
125 #ifdef LXVST_64BIT
126
127 /********************************************************************/
128 /* This is untested - have no 64Bit plugins which use this          */
129 /* system of passing an eventProc address                           */
130 /********************************************************************/
131
132 long getXWindowProperty (Window window, Atom atom)
133 {
134         long result = 0;
135         int userSize;
136         unsigned long bytes;
137         unsigned long userCount;
138         unsigned char *data;
139         Atom userType;
140         LXVST_xerror = false;
141
142         /*Use our own Xerror handler while we're in here - in an
143          * attempt to stop the brain dead default Xerror behaviour of
144          * qutting the entire application because of e.g. an invalid
145          * window ID
146          */
147
148         XErrorHandler olderrorhandler = XSetErrorHandler (TempErrorHandler);
149
150         XGetWindowProperty (LXVST_XDisplay,
151                             window,
152                             atom,
153                             0,
154                             2,
155                             false,
156                             AnyPropertyType,
157                             &userType,
158                             &userSize,
159                             &userCount,
160                             &bytes,
161                             &data);
162
163         if (LXVST_xerror == false && userCount == 1) {
164                 result = *(long*)data;
165         }
166
167         XSetErrorHandler (olderrorhandler);
168
169         /*Hopefully this will return zero if the property is not set*/
170
171         return result;
172 }
173
174 #endif
175
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*/
178
179 static void
180 dispatch_x_events (XEvent* event, VSTState* vstfx)
181 {
182         /*Handle some of the Events we might be interested in*/
183
184         switch (event->type)
185         {
186                 /*Configure event - when the window is resized or first drawn*/
187
188                 case ConfigureNotify:
189                 {
190                         Window window = event->xconfigure.event;
191
192                         int width = event->xconfigure.width;
193                         int height = event->xconfigure.height;
194
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.
198                          *
199                          * if the size has changed, we flag this so that in lxvst_pluginui.cc
200                          * we can make the change to the GTK parent window in ardour, from its UI thread */
201
202                         if (window == (Window) (vstfx->linux_window)) {
203 #ifndef NDEBUG
204                                 printf ("dispatch_x_events: ConfigureNotify cfg: (%d %d) plugin: (%d %d)\n",
205                                                 width, height,
206                                                 vstfx->width, vstfx->height
207                                                 );
208 #endif
209                                 if (width != vstfx->width || height != vstfx->height) {
210                                         vstfx->width = width;
211                                         vstfx->height = height;
212                                         ARDOUR::VSTPlugin* plug = (ARDOUR::VSTPlugin *)(vstfx->plugin->ptr1);
213                                         plug->VSTSizeWindow (); /* EMIT SIGNAL */
214                                 }
215
216                                 /* QUIRK : Loomer plugins not only resize the UI but throw it into some random
217                                  * position at the same time. We need to re-position the window at the origin of
218                                  * the parent window*/
219
220                                 if (vstfx->linux_plugin_ui_window) {
221                                         XMoveWindow (LXVST_XDisplay, vstfx->linux_plugin_ui_window, 0, 0);
222                                 }
223                         }
224
225                         break;
226                 }
227
228                 /*Reparent Notify - when the plugin UI is reparented into
229                 our Host Window we will get an event here... probably... */
230
231                 case ReparentNotify:
232                 {
233                         Window ParentWindow = event->xreparent.parent;
234
235                         /*If the ParentWindow matches the window for the vstfx instance then
236                         the Child window must be the XID of the pluginUI window created by the
237                         plugin, so we need to see if it has a callback stuck to it, and if so
238                         set that up in the vstfx */
239
240                         /***********************************************************/
241                         /* 64Bit --- This mechanism is not 64Bit compatible at the */
242                         /* present time                                            */
243                         /***********************************************************/
244
245                         if (ParentWindow == (Window) (vstfx->linux_window)) {
246
247                                 Window PluginUIWindowID = event->xreparent.window;
248
249                                 vstfx->linux_plugin_ui_window = PluginUIWindowID;
250 #ifdef LXVST_32BIT
251                                 int result = getXWindowProperty (PluginUIWindowID, XInternAtom (LXVST_XDisplay, "_XEventProc", false));
252
253                                 if (result == 0) {
254                                         vstfx->eventProc = NULL;
255                                 } else {
256                                         vstfx->eventProc = (void (*) (void* event))result;
257                                 }
258 #endif
259 #ifdef LXVST_64BIT
260                                 long result = getXWindowProperty (PluginUIWindowID, XInternAtom (LXVST_XDisplay, "_XEventProc", false));
261
262                                 if (result == 0) {
263                                         vstfx->eventProc = NULL;
264                                 } else {
265                                         vstfx->eventProc = (void (*) (void* event))result;
266                                 }
267 #endif
268                         }
269                         break;
270                 }
271
272                 case ClientMessage:
273                 {
274                         Window window = event->xany.window;
275                         Atom message_type = event->xclient.message_type;
276
277                         /*The only client message we are interested in is to signal
278                         that the plugin parent window is now valid and can be passed
279                         to effEditOpen when the editor is launched*/
280
281                         if (window == (Window) (vstfx->linux_window)) {
282                                 char* message = XGetAtomName (LXVST_XDisplay, message_type);
283
284                                 if (strcmp (message,"LaunchEditor") == 0) {
285                                         if (event->xclient.data.l[0] == 0x0FEEDBAC) {
286                                                 vstfx_launch_editor (vstfx);
287                                         }
288                                 }
289
290                                 XFree (message);
291                         }
292                         break;
293                 }
294
295                 default:
296                         break;
297         }
298
299         /* Some VSTs built with toolkits e.g. JUCE will manager their own UI
300         autonomously in the plugin, running the UI in its own thread, so once
301         we have created a parent window for the plugin, its UI takes care of
302         itself.*/
303
304         /*Other types register a callback as an Xwindow property on the plugin
305         UI window after they create it.  If that is the case, we need to call it
306         here, passing the XEvent into it*/
307
308         if (vstfx->eventProc == NULL) {
309                 return;
310         }
311
312         vstfx->eventProc ((void*)event);
313 }
314
315 /** This is the main gui event loop for the plugin, we also need to pass
316 any Xevents to all the UI callbacks plugins 'may' have registered on their
317 windows, that is if they don't manage their own UIs **/
318
319 static void*
320 gui_event_loop (void* ptr)
321 {
322         VSTState* vstfx;
323         int LXVST_sched_timer_interval = 40; //ms, 25fps
324         XEvent event;
325         uint64_t clock1, clock2;
326
327         clock1 = g_get_monotonic_time();
328         /*The 'Forever' loop - runs the plugin UIs etc - based on the FST gui event loop*/
329
330         while (!gui_quit)
331         {
332                 /* handle window creation requests, destroy requests,
333                    and run idle callbacks */
334
335                 /*Look at the XEvent queue - if there are any XEvents we need to handle them,
336                 including passing them to all the plugin (eventProcs) we are currently managing*/
337
338                 bool may_sleep = true;
339
340                 if (LXVST_XDisplay) {
341                         /*See if there are any events in the queue*/
342
343                         int num_events = XPending (LXVST_XDisplay);
344
345                         if (num_events > 0) {
346                                 // keep dispatching events as fast as possible
347                                 may_sleep = false;
348                         }
349
350                         /*process them if there are any*/
351
352                         while (num_events) {
353                                 XNextEvent (LXVST_XDisplay, &event);
354
355                                 /*Call dispatch events, with the event, for each plugin in the linked list*/
356
357                                 for (vstfx = vstfx_first; vstfx; vstfx = vstfx->next) {
358                                         pthread_mutex_lock (&vstfx->lock);
359
360                                         dispatch_x_events (&event, vstfx);
361
362                                         pthread_mutex_unlock (&vstfx->lock);
363                                 }
364
365                                 num_events--;
366                         }
367                 }
368
369                 /*We don't want to use all the CPU.. */
370
371                 Glib::usleep (1000);
372
373                 /*See if its time for us to do a scheduled event pass on all the plugins*/
374
375                 clock2 = g_get_monotonic_time();
376                 const int64_t elapsed_time_ms = (clock2 - clock1) / 1000;
377
378                 if ((LXVST_sched_timer_interval != 0) && elapsed_time_ms >= LXVST_sched_timer_interval) {
379                         //printf ("elapsed %d ms ^= %.2f Hz\n", elapsed_time_ms, 1000.0/ (double)elapsed_time_ms); // DEBUG
380                         pthread_mutex_lock (&plugin_mutex);
381
382 again:
383                         /*Parse through the linked list of plugins*/
384
385                         for (vstfx = vstfx_first; vstfx; vstfx = vstfx->next)
386                         {
387                                 pthread_mutex_lock (&vstfx->lock);
388
389                                 /*Window scheduled for destruction*/
390
391                                 if (vstfx->destroy) {
392                                         if (vstfx->linux_window) {
393                                                 vstfx->plugin->dispatcher (vstfx->plugin, effEditClose, 0, 0, NULL, 0.0);
394
395                                                 XDestroyWindow (LXVST_XDisplay, vstfx->linux_window);
396                                                 /* FIXME - probably safe to assume we never have an XID of 0 but not explicitly true */
397                                                 vstfx->linux_window = 0;
398                                                 vstfx->destroy = FALSE;
399                                         }
400
401                                         vstfx_event_loop_remove_plugin (vstfx);
402                                         vstfx->been_activated = FALSE;
403                                         pthread_cond_signal (&vstfx->window_status_change);
404                                         pthread_mutex_unlock (&vstfx->lock);
405
406                                         goto again;
407                                 }
408
409                                 /*Window does not yet exist - scheduled for creation*/
410
411                                 /* FIXME - probably safe to assume 0 is not a valid XID but not explicitly true */
412                                 if (vstfx->linux_window == 0) {
413                                         if (vstfx_create_editor (vstfx)) {
414                                                 vstfx_error ("** ERROR ** VSTFX : Cannot create editor for plugin %s", vstfx->handle->name);
415                                                 vstfx_event_loop_remove_plugin (vstfx);
416                                                 pthread_cond_signal (&vstfx->window_status_change);
417                                                 pthread_mutex_unlock (&vstfx->lock);
418                                                 goto again;
419                                         } else {
420                                                 /* condition/unlock: it was signalled & unlocked in fst_create_editor() */
421                                         }
422                                 }
423
424                                 vststate_maybe_set_program (vstfx);
425                                 vstfx->want_program = -1;
426                                 vstfx->want_chunk = 0;
427
428                                 /*scheduled call to dispatcher*/
429
430                                 if (vstfx->dispatcher_wantcall) {
431                                         vstfx->dispatcher_retval = vstfx->plugin->dispatcher (
432                                                 vstfx->plugin,
433                                                 vstfx->dispatcher_opcode,
434                                                 vstfx->dispatcher_index,
435                                                 vstfx->dispatcher_val,
436                                                 vstfx->dispatcher_ptr,
437                                                 vstfx->dispatcher_opt
438                                                 );
439
440                                         vstfx->dispatcher_wantcall = 0;
441                                         pthread_cond_signal (&vstfx->plugin_dispatcher_called);
442                                 }
443
444                                 /*Call the editor Idle function in the plugin*/
445
446                                 vstfx->plugin->dispatcher (vstfx->plugin, effEditIdle, 0, 0, NULL, 0);
447
448                                 if (vstfx->wantIdle) {
449                                         vstfx->plugin->dispatcher (vstfx->plugin, 53, 0, 0, NULL, 0);
450                                 }
451
452                                 pthread_mutex_unlock (&vstfx->lock);
453                         }
454                         pthread_mutex_unlock (&plugin_mutex);
455
456                         clock1 = g_get_monotonic_time();
457                 }
458
459                 if (!gui_quit && may_sleep && elapsed_time_ms + 1 < LXVST_sched_timer_interval) {
460                         Glib::usleep (1000 * (LXVST_sched_timer_interval - elapsed_time_ms - 1));
461                 }
462         }
463
464         if (LXVST_XDisplay) {
465                 XCloseDisplay (LXVST_XDisplay);
466                 LXVST_XDisplay = 0;
467         }
468
469         /* some plugin UIs (looking at you, u-he^abique), do set thread-keys
470          * and free, but not unset them.
471          *
472          * This leads to a double-free in __nptl_deallocate_tsd
473          * nptl/pthread_create.c:175  __pthread_keys[idx].destr (data);
474          * when the event-loop thread is joined.
475          *
476          * This workaround is dedicated to all the plugin-UI-devs
477          * who think their UI owns the complete process memory-space.
478          *
479          * NB. ardour itself does not use thread-keys for the
480          * VST event-loop thread, and anyway, this thread is joined
481          * only when ardour exit()s. If this would result in a leak,
482          * nobody will care.
483          */
484         if (!getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
485                 for (pthread_key_t i = 0; i < PTHREAD_KEYS_MAX; ++i) {
486                         if (pthread_getspecific (i)) {
487                                 pthread_setspecific (i, NULL);
488                         }
489                 }
490         }
491
492         return NULL;
493 }
494
495 /*The VSTFX Init function - this needs to be called before the VSTFX engine
496 can be accessed, it gets the UI thread running, opens a connection to X etc
497 normally started in globals.cc*/
498
499 int vstfx_init (void* ptr)
500 {
501         assert (gui_quit == 0);
502         pthread_mutex_init (&plugin_mutex, NULL);
503
504         int thread_create_result;
505
506         pthread_attr_t thread_attributes;
507
508         /*Init the attribs to defaults*/
509
510         pthread_attr_init (&thread_attributes);
511
512         /*Make sure the thread is joinable - this should be the default anyway -
513         so we can join to it on vstfx_exit*/
514
515         pthread_attr_setdetachstate (&thread_attributes, PTHREAD_CREATE_JOINABLE);
516
517
518         /*This is where we need to open a connection to X, and start the GUI thread*/
519
520         /*Open our connection to X - all linuxVST plugin UIs handled by the LXVST engine
521         will talk to X down this connection - X cannot handle multi-threaded access via
522         the same Display* */
523
524         if (LXVST_XDisplay == NULL) {
525                 LXVST_XDisplay = XOpenDisplay (NULL); //We might be able to make this open a specific screen etc
526         }
527
528         /*Drop out and report the error if we fail to connect to X */
529
530         if (LXVST_XDisplay == NULL) {
531                 vstfx_error ("** ERROR ** VSTFX: Failed opening connection to X");
532
533                 return -1;
534         }
535
536         /*We have a connection to X - so start the gui event loop*/
537
538         /*Create the thread - use default attrs for now, don't think we need anything special*/
539
540         thread_create_result = pthread_create (&LXVST_gui_event_thread, &thread_attributes, gui_event_loop, NULL);
541
542         if (thread_create_result != 0)
543         {
544                 /*There was a problem starting the GUI event thread*/
545
546                 vstfx_error ("** ERROR ** VSTFX: Failed starting GUI event thread");
547
548                 XCloseDisplay (LXVST_XDisplay);
549                 LXVST_XDisplay = 0;
550                 gui_quit = 1;
551
552                 return -1;
553         }
554
555         return 0;
556 }
557
558 /*The vstfx Quit function*/
559
560 void vstfx_exit ()
561 {
562         if (gui_quit) {
563                 return;
564         }
565         gui_quit = 1;
566
567         /*We need to pthread_join the gui_thread here so
568         we know when it has stopped*/
569
570         pthread_join(LXVST_gui_event_thread, NULL);
571         pthread_mutex_destroy (&plugin_mutex);
572 }
573
574 /*Adds a new plugin (VSTFX) instance to the linked list*/
575
576 int vstfx_run_editor (VSTState* vstfx)
577 {
578         pthread_mutex_lock (&plugin_mutex);
579
580         /* Add the new VSTFX instance to the linked list */
581
582         if (vstfx_first == NULL) {
583                 vstfx_first = vstfx;
584         } else {
585                 VSTState* p = vstfx_first;
586
587                 while (p->next) {
588                         p = p->next;
589                 }
590                 p->next = vstfx;
591
592                 /* Mark the new end of the list */
593
594                 vstfx->next = NULL;
595         }
596
597         pthread_mutex_unlock (&plugin_mutex);
598
599         /* wait for the plugin editor window to be created (or not) */
600
601         pthread_mutex_lock (&vstfx->lock);
602
603         if (!vstfx->linux_window) {
604                 pthread_cond_wait (&vstfx->window_status_change, &vstfx->lock);
605         }
606
607         pthread_mutex_unlock (&vstfx->lock);
608
609         if (!vstfx->linux_window) {
610                 return -1;
611         }
612
613         return 0;
614 }
615
616
617 /*Creates an editor for the plugin - normally called from within the gui event loop
618 after run_editor has added the plugin (editor) to the linked list*/
619
620 int vstfx_create_editor (VSTState* vstfx)
621 {
622         Window parent_window;
623
624         int x_size = 1;
625         int y_size = 1;
626
627         if (!LXVST_XDisplay) {
628                 vstfx_error ("** ERROR ** VSTFX: No X11 Display available for plugin UI");
629                 return -1;
630         }
631
632         /* Note: vstfx->lock is held while this function is called */
633
634         if (!(vstfx->plugin->flags & effFlagsHasEditor))
635         {
636                 vstfx_error ("** ERROR ** VSTFX: Plugin \"%s\" has no editor", vstfx->handle->name);
637                 return -1;
638         }
639
640
641         /*Create an XWindow for the plugin to inhabit*/
642
643         parent_window = XCreateSimpleWindow (
644                 LXVST_XDisplay,
645                 DefaultRootWindow(LXVST_XDisplay),
646                 0,
647                 0,
648                 x_size,
649                 y_size,
650                 0,
651                 0,
652                 0
653                 );
654
655         /*Select the events we are interested in receiving - we need Substructure notify so that
656         if the plugin resizes its window - e.g. Loomer Manifold then we get a message*/
657
658         XSelectInput(LXVST_XDisplay,
659                                 parent_window,
660                                 SubstructureNotifyMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | ExposureMask);
661
662         vstfx->linux_window = parent_window;
663
664         vstfx->xid = parent_window;  //vstfx->xid will be referenced to connect to GTK UI in ardour later
665
666         /*Because the plugin may be operating on a different Display* to us, and therefore
667         the two event queues can be asynchronous, although we have created the window on
668         our display, we can't guarantee it exists in the server yet, which will
669         cause BadWindow crashes if the plugin tries to use it.
670
671         It would be nice to use CreateNotify events here, but they don't get
672         through on all window managers, so instead we pass a client message
673         into out queue, after the XCreateWindow.  When this message pops out
674         in our event handler, it will trigger the second stage of plugin
675         Editor instantiation, and by then the Window should be valid...*/
676
677         XClientMessageEvent event;
678
679         /*Create an atom to identify our message (only if it doesn't already exist)*/
680
681         Atom WindowActiveAtom = XInternAtom(LXVST_XDisplay, "LaunchEditor", false);
682
683         event.type = ClientMessage;
684         event.send_event = true;
685         event.window = parent_window;
686         event.message_type = WindowActiveAtom;
687
688         event.format    = 32;         //Data format
689         event.data.l[0] = 0x0FEEDBAC; //Something we can recognize later
690
691         /*Push the event into the queue on our Display*/
692
693         XSendEvent(LXVST_XDisplay, parent_window, FALSE, NoEventMask, (XEvent*)&event);
694
695         return 0;
696 }
697
698 int
699 vstfx_launch_editor (VSTState* vstfx)
700 {
701         /*This is the second stage of launching the editor (see vstfx_create editor)
702         we get called here in response to receiving the ClientMessage on our Window,
703         therefore it's about as safe (as can be) to assume that the Window we created
704         is now valid in the XServer and can be passed to the plugin in effEditOpen
705         without generating BadWindow errors when the plugin reparents itself into our
706         parent window*/
707
708         if(vstfx->been_activated)
709                 return 0;
710
711         Window parent_window;
712         struct ERect* er = NULL;
713
714         int x_size = 1;
715         int y_size = 1;
716
717         parent_window = vstfx->linux_window;
718
719         /*Open the editor - Bah! we have to pass the int windowID as a void pointer - yuck
720         it gets cast back to an int as the parent window XID in the plugin - and we have to pass the
721         Display* as a long */
722
723         /**************************************************************/
724         /* 64Bit --- parent window is an int passed as a void* so     */
725         /* that should be ok for 64Bit machines                       */
726         /*                                                            */
727         /* Display is passed in as a long - ok on arch's where sizeof */
728         /* long = 8                                                   */
729         /*                                                            */
730         /* Most linux VST plugins open a connection to X on their own */
731         /* Display anyway so it may not matter                        */
732         /*                                                            */
733         /* linuxDSP VSTs don't use the host Display* at all           */
734         /**************************************************************/
735
736         vstfx->plugin->dispatcher (vstfx->plugin, effEditOpen, 0, (long)LXVST_XDisplay, (void*)(parent_window), 0);
737
738         /*QUIRK - some plugins need a slight delay after opening the editor before you can
739         ask the window size or they might return zero - specifically discoDSP */
740
741         Glib::usleep(100000);
742
743         /*Now we can find out how big the parent window should be (and try) to resize it*/
744
745         vstfx->plugin->dispatcher (vstfx->plugin, effEditGetRect, 0, 0, &er, 0);
746
747         if (er) {
748                 // Don't crash is plugin does not implement effEditGetRect
749                 // it'll result in 1x1 px window but that's not our problem :)
750                 x_size = er->right - er->left;
751                 y_size = er->bottom - er->top;
752         }
753
754         vstfx->width = x_size;
755         vstfx->height = y_size;
756
757         XResizeWindow (LXVST_XDisplay, parent_window, x_size, y_size);
758
759         XFlush (LXVST_XDisplay);
760
761         /*Not sure if we need to map the window or if the plugin will do it for us
762         it should be ok because XReparentWindow generates a Map event*/
763
764         /*mark the editor as activated - mainly so that vstfx_get_XID
765         will know it is valid*/
766
767         vstfx->been_activated = TRUE;
768
769         pthread_cond_signal (&vstfx->window_status_change);
770         return 0;
771 }
772
773 /** Destroy the editor window */
774 void
775 vstfx_destroy_editor (VSTState* vstfx)
776 {
777         pthread_mutex_lock (&vstfx->lock);
778         if (vstfx->linux_window) {
779                 vstfx->destroy = TRUE;
780                 pthread_cond_wait (&vstfx->window_status_change, &vstfx->lock);
781         }
782         pthread_mutex_unlock (&vstfx->lock);
783 }
784
785 /** Remove a vstfx instance from the linked list parsed by the
786     event loop
787 */
788 void
789 vstfx_event_loop_remove_plugin (VSTState* vstfx)
790 {
791         /* This only ever gets called from within our GUI thread
792            so we don't need to lock here - if we did there would be
793            a deadlock anyway
794         */
795
796         VSTState* p;
797         VSTState* prev;
798
799         for (p = vstfx_first, prev = NULL; p; prev = p, p = p->next) {
800                 if (p == vstfx) {
801                         if (prev) {
802                                 prev->next = p->next;
803                                 break;
804                         }
805                 }
806         }
807
808         // if this function is called, there must be
809         // at least one plugin in the linked list
810         assert (vstfx_first);
811
812         if (vstfx_first == vstfx) {
813                 vstfx_first = vstfx_first->next;
814         }
815 }
816