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