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