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