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