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