Remove unused define.
[ardour.git] / libs / fst / vstwin.c
1 #include <stdio.h>
2 #include <jack/jack.h>
3 #include <jack/thread.h>
4 #include <libgen.h>
5 #include <windows.h>
6 #include <winnt.h>
7 #include <wine/exception.h>
8 #include <pthread.h>
9 #include <signal.h>
10 #include <glib.h>
11
12 #include "fst.h"
13
14 #include <X11/X.h>
15 #include <X11/Xlib.h>
16
17 extern char * strdup (const char *);
18
19 struct ERect{
20     short top;
21     short left;
22     short bottom;
23     short right;
24 };
25
26 static pthread_mutex_t plugin_mutex;
27
28 /** Head of linked list of all FSTs */
29 static VSTState* fst_first = NULL;
30
31 const char magic[] = "FST Plugin State v002";
32
33 DWORD  gui_thread_id = 0;
34 static int gui_quit = 0;
35
36 static LRESULT WINAPI 
37 my_window_proc (HWND w, UINT msg, WPARAM wp, LPARAM lp)
38 {
39 #if 0   
40         if (msg != WM_TIMER) {
41                 fst_error ("window callback handler, msg = 0x%x win=%p\n", msg, w);
42         }
43 #endif  
44
45         switch (msg) {
46         case WM_KEYUP:
47         case WM_KEYDOWN:
48                 break;
49
50         case WM_CLOSE:
51                 /* we don't care about windows closing ... */
52                 return 0;
53                 break;
54
55         case WM_DESTROY:
56         case WM_NCDESTROY:
57                 /* we don't care about windows being destroyed ... */
58                 return 0;
59                 break;
60
61         default:
62                 break;
63         }
64
65         return DefWindowProcA (w, msg, wp, lp );
66 }
67
68 static VSTState * 
69 fst_new ()
70 {
71         VSTState* fst = (VSTState *) calloc (1, sizeof (VSTState));
72         pthread_mutex_init (&fst->lock, NULL);
73         pthread_cond_init (&fst->window_status_change, NULL);
74         pthread_cond_init (&fst->plugin_dispatcher_called, NULL);
75         fst->want_program = -1;
76         fst->want_chunk = 0;
77         fst->current_program = -1;
78         fst->n_pending_keys = 0;
79         fst->has_editor = 0;
80         fst->program_set_without_editor = 0;
81         return fst;
82 }
83
84 static VSTHandle* 
85 fst_handle_new ()
86 {
87         VSTHandle* fst = (VSTHandle*) calloc (1, sizeof (VSTHandle));
88         return fst;
89 }
90
91 void
92 maybe_set_program (VSTState* fst)
93 {
94         if (fst->want_program != -1) {
95                 if (fst->vst_version >= 2) {
96                         fst->plugin->dispatcher (fst->plugin, 67 /* effBeginSetProgram */, 0, 0, NULL, 0);
97                 }
98                 
99                 fst->plugin->dispatcher (fst->plugin, effSetProgram, 0, fst->want_program, NULL, 0);
100                 
101                 if (fst->vst_version >= 2) {
102                         fst->plugin->dispatcher (fst->plugin, 68 /* effEndSetProgram */, 0, 0, NULL, 0);
103                 }
104                 /* did it work? */
105                 fst->current_program = fst->plugin->dispatcher (fst->plugin, 3, /* effGetProgram */ 0, 0, NULL, 0);
106                 fst->want_program = -1; 
107         }
108         
109         if (fst->want_chunk == 1) {
110                 fst->plugin->dispatcher (fst->plugin, 24 /* effSetChunk */, 1, fst->wanted_chunk_size, fst->wanted_chunk, 0);
111                 fst->want_chunk = 0;
112         }
113 }
114
115 DWORD WINAPI gui_event_loop (LPVOID param)
116 {
117         MSG msg;
118         VSTState* fst;
119         HMODULE hInst;
120         HWND window;
121         int i;
122
123         gui_thread_id = GetCurrentThreadId ();
124
125         /* create a dummy window for timer events */
126
127         if ((hInst = GetModuleHandleA (NULL)) == NULL) {
128                 fst_error ("can't get module handle");
129                 return 1;
130         }
131         
132         if ((window = CreateWindowExA (0, "FST", "dummy",
133                                        WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,
134                                        9999, 9999,
135                                        1, 1,
136                                        NULL, NULL,
137                                        hInst,
138                                        NULL )) == NULL) {
139                 fst_error ("cannot create dummy timer window");
140         }
141
142         if (!SetTimer (window, 1000, 20, NULL)) {
143                 fst_error ("cannot set timer on dummy window");
144         }
145
146         while (!gui_quit) {
147
148                 if (!GetMessageA (&msg, NULL, 0,0)) {
149                         if (!gui_quit) {
150                                 fprintf (stderr, "QUIT message received by Windows GUI thread - ignored\n");
151                                 continue;
152                         } else {
153                                 break;
154                         }
155                 }
156
157                 TranslateMessage( &msg );
158                 DispatchMessageA (&msg);
159
160                 if (msg.message != WM_TIMER) {
161                         continue;
162                 }
163
164                 pthread_mutex_lock (&plugin_mutex);
165
166                 /* Do things that are appropriate for plugins which have open editor windows:
167                    handle window creation requests, destroy requests, 
168                    and run idle callbacks 
169                 */
170                 
171 again:
172                 for (fst = fst_first; fst; fst = fst->next) {
173                         
174                         pthread_mutex_lock (&fst->lock);
175                         
176                         if (fst->has_editor == 1) {
177                                 
178                                 if (fst->destroy) {
179                                         fprintf (stderr, "%s scheduled for destroy\n", fst->handle->name);
180                                         if (fst->windows_window) {
181                                                 fst->plugin->dispatcher( fst->plugin, effEditClose, 0, 0, NULL, 0.0 );
182                                                 CloseWindow (fst->windows_window);
183                                                 fst->windows_window = NULL;
184                                                 fst->destroy = FALSE;
185                                         }
186                                         fst_event_loop_remove_plugin (fst);
187                                         fst->been_activated = FALSE;
188                                         pthread_cond_signal (&fst->window_status_change);
189                                         pthread_mutex_unlock (&fst->lock);
190                                         goto again;
191                                 } 
192                                 
193                                 if (fst->windows_window == NULL) {
194                                         if (fst_create_editor (fst)) {
195                                                 fst_error ("cannot create editor for plugin %s", fst->handle->name);
196                                                 fst_event_loop_remove_plugin (fst);
197                                                 pthread_cond_signal (&fst->window_status_change);
198                                                 pthread_mutex_unlock (&fst->lock);
199                                                 goto again;
200                                         } else {
201                                                 /* condition/unlock: it was signalled & unlocked in fst_create_editor()   */
202                                         }
203                                 }
204                                 
205                                 if (fst->dispatcher_wantcall) {
206                                         fst->dispatcher_retval = fst->plugin->dispatcher( fst->plugin, 
207                                                                                           fst->dispatcher_opcode,
208                                                                                           fst->dispatcher_index,
209                                                                                           fst->dispatcher_val,
210                                                                                           fst->dispatcher_ptr,
211                                                                                           fst->dispatcher_opt );
212                                         fst->dispatcher_wantcall = 0;
213                                         pthread_cond_signal (&fst->plugin_dispatcher_called);
214                                 }
215                                 
216                                 fst->plugin->dispatcher (fst->plugin, effEditIdle, 0, 0, NULL, 0);
217                                 
218                                 if (fst->wantIdle) {
219                                         fst->plugin->dispatcher (fst->plugin, 53, 0, 0, NULL, 0);
220                                 }
221                                 
222                                 /* Dispatch messages to send keypresses to the plugin */
223                                 
224                                 for (i = 0; i < fst->n_pending_keys; ++i) {
225                                         /* I'm not quite sure what is going on here; it seems
226                                            `special' keys must be delivered with WM_KEYDOWN,
227                                            but that alphanumerics etc. must use WM_CHAR or
228                                            they will be ignored.  Ours is not to reason why ...
229                                         */
230                                         if (fst->pending_keys[i].special != 0) {
231                                                 msg.message = WM_KEYDOWN;
232                                                 msg.wParam = fst->pending_keys[i].special;
233                                         } else {
234                                                 msg.message = WM_CHAR;
235                                                 msg.wParam = fst->pending_keys[i].character;
236                                         }
237                                         msg.hwnd = GetFocus ();
238                                         msg.lParam = 0;
239                                         DispatchMessageA (&msg);
240                                 }
241                                 
242                                 fst->n_pending_keys = 0;
243
244                                 /* See comment for maybe_set_program call below */
245                                 maybe_set_program (fst);
246                                 fst->want_program = -1;
247                                 fst->want_chunk = 0;
248                         }
249
250                         /* If we don't have an editor window yet, we still need to
251                          * set up the program, otherwise when we load a plugin without
252                          * opening its window it will sound wrong.  However, it seems
253                          * that if you don't also load the program after opening the GUI,
254                          * the GUI does not reflect the program properly.  So we'll not
255                          * mark that we've done this (ie we won't set want_program to -1)
256                          * and so it will be done again if and when the GUI arrives.
257                          */
258                         if (fst->program_set_without_editor == 0) {
259                                 maybe_set_program (fst);
260                                 fst->program_set_without_editor = 1;
261                         }
262
263                         pthread_mutex_unlock (&fst->lock);
264                 }
265
266                 pthread_mutex_unlock (&plugin_mutex);
267         }
268
269         return 0;
270 }
271
272 int
273 fst_init (void* possible_hmodule)
274 {
275         WNDCLASSEX wclass;
276         HMODULE hInst;
277         
278         if (possible_hmodule) {
279                 hInst = (HMODULE) possible_hmodule;
280         } else if ((hInst = GetModuleHandleA (NULL)) == NULL) {
281                 fst_error ("can't get module handle");
282                 return -1;
283         }
284
285         wclass.cbSize = sizeof(WNDCLASSEX);
286         wclass.style = 0;
287         wclass.lpfnWndProc = my_window_proc;
288         wclass.cbClsExtra = 0;
289         wclass.cbWndExtra = 0;
290         wclass.hInstance = hInst;
291         wclass.hIcon = LoadIcon(hInst, "FST");
292         wclass.hCursor = LoadCursor(0, IDI_APPLICATION);
293 //    wclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
294         wclass.lpszMenuName = "MENU_FST";
295         wclass.lpszClassName = "FST";
296         wclass.hIconSm = 0;
297
298
299         if (!RegisterClassExA(&wclass)){
300                 printf( "Class register failed :(\n" );
301                 return -1;
302         }
303
304         fst_error ("Startup win32 GUI thread\n");
305
306         if (CreateThread (NULL, 0, gui_event_loop, NULL, 0, NULL) == NULL) {
307                 fst_error ("could not create new thread proxy");
308                 return -1;
309         }
310
311 #ifdef HAVE_JACK_SET_THREAD_CREATOR
312         jack_set_thread_creator (wine_pthread_create);
313 #endif
314
315         return 0;
316 }
317
318 void
319 fst_exit ()
320 {
321         gui_quit = 1;
322         PostQuitMessage (0);
323 }
324
325 int
326 fst_run_editor (VSTState* fst)
327 {
328         /* wait for the plugin editor window to be created (or not) */
329
330         pthread_mutex_lock (&fst->lock);
331
332         fst->has_editor = 1;
333         
334         if (!fst->windows_window) {
335                 pthread_cond_wait (&fst->window_status_change, &fst->lock);
336         }
337         pthread_mutex_unlock (&fst->lock);
338
339         if (!fst->windows_window) {
340                 return -1;
341         }
342
343         return 0;
344 }
345
346 int
347 fst_call_dispatcher (VSTState* fst, int opcode, int index, int val, void *ptr, float opt) 
348 {
349         pthread_mutex_lock (&fst->lock);
350         fst->dispatcher_opcode = opcode;
351         fst->dispatcher_index = index;
352         fst->dispatcher_val = val;
353         fst->dispatcher_ptr = ptr;
354         fst->dispatcher_opt = opt;
355         fst->dispatcher_wantcall = 1;
356
357         pthread_cond_wait (&fst->plugin_dispatcher_called, &fst->lock);
358         pthread_mutex_unlock (&fst->lock);
359
360         return fst->dispatcher_retval;
361 }
362
363 int
364 fst_create_editor (VSTState * fst)
365 {
366         HMODULE hInst;
367         HWND window;
368         struct ERect* er;
369
370         /* "guard point" to trap errors that occur during plugin loading */
371
372         /* Note: fst->lock is held while this function is called */
373
374         if (!(fst->plugin->flags & effFlagsHasEditor)) {
375                 fst_error ("Plugin \"%s\" has no editor", fst->handle->name);
376                 return -1;
377         }
378
379         if ((hInst = GetModuleHandleA (NULL)) == NULL) {
380                 fst_error ("can't get module handle");
381                 return 1;
382         }
383         
384 //      if ((window = CreateWindowExA (WS_EX_TOOLWINDOW | WS_EX_TRAYWINDOW, "FST", fst->handle->name,
385         if ((window = CreateWindowExA (0, "FST", fst->handle->name,
386                                        (WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX),
387 //                                     (WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX),
388                                        9999,9999,1,1,
389 //                                     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
390                                        NULL, NULL,
391                                        hInst,
392                                        NULL)) == NULL) {
393                 fst_error ("cannot create editor window");
394                 return 1;
395         }
396
397         if (!SetPropA (window, "fst_ptr", fst)) {
398                 fst_error ("cannot set fst_ptr on window");
399         }
400
401         fst->windows_window = window;
402 //      fst->xid = (int) GetPropA (window, "__wine_x11_whole_window");
403
404
405         //printf( "effEditOpen......\n" );
406         fst->plugin->dispatcher (fst->plugin, effEditOpen, 0, 0, fst->windows_window, 0);
407         fst->plugin->dispatcher (fst->plugin, effEditGetRect, 0, 0, &er, 0 );
408
409         fst->width =  er->right-er->left;
410         fst->height =  er->bottom-er->top;
411         //printf( "get rect ses... %d,%d\n", fst->width, fst->height );
412
413         //SetWindowPos (fst->window, 0, 9999, 9999, er->right-er->left+8, er->bottom-er->top+26, 0);
414         SetWindowPos (fst->windows_window, 0, 9999, 9999, 2, 2, 0);
415         ShowWindow (fst->windows_window, SW_SHOWNA);
416         //SetWindowPos (fst->window, 0, 0, 0, er->right-er->left+8, er->bottom-er->top+26, SWP_NOMOVE|SWP_NOZORDER);
417         
418         fst->xid = (int) GetPropA (window, "__wine_x11_whole_window");
419         fst->been_activated = TRUE;
420         pthread_cond_signal (&fst->window_status_change);
421         pthread_mutex_unlock (&fst->lock);
422
423         return 0;
424 }
425
426 void
427 fst_move_window_into_view (VSTState* fst)
428 {
429         if (fst->windows_window) {
430                 SetWindowPos (fst->windows_window, 0, 0, 0, fst->width, fst->height + 24, 0);
431                 ShowWindow (fst->windows_window, SW_SHOWNA);
432         }
433 }
434
435 void
436 fst_destroy_editor (VSTState* fst)
437 {
438         pthread_mutex_lock (&fst->lock);
439         if (fst->windows_window) {
440                 fprintf (stderr, "mark %s for destroy\n", fst->handle->name);
441                 fst->destroy = TRUE;
442                 //if (!PostThreadMessageA (gui_thread_id, WM_USER, 0, 0)) {
443                 //if (!PostThreadMessageA (gui_thread_id, WM_QUIT, 0, 0)) {
444                 //      fst_error ("could not post message to gui thread");
445                 //}
446                 pthread_cond_wait (&fst->window_status_change, &fst->lock);
447                 fprintf (stderr, "%s editor destroyed\n", fst->handle->name);
448                 fst->has_editor = 0;
449         }
450         pthread_mutex_unlock (&fst->lock);
451 }
452
453 void
454 fst_event_loop_remove_plugin (VSTState* fst)
455 {
456         VSTState* p;
457         VSTState* prev;
458
459         for (p = fst_first, prev = NULL; p->next; prev = p, p = p->next) {
460                 if (p == fst) {
461                         if (prev) {
462                                 prev->next = p->next;
463                         }
464                 }
465         }
466
467         if (fst_first == fst) {
468                 fst_first = fst_first->next;
469         }
470
471 }
472
473 HMODULE
474 fst_load_vst_library(const char * path)
475 {
476         HMODULE dll;
477         char * full_path;
478         char * envdup;
479         char * vst_path;
480         size_t len1;
481         size_t len2;
482
483         if ((dll = LoadLibraryA (path)) != NULL) {
484                 return dll;
485         }
486
487         envdup = getenv ("VST_PATH");
488         if (envdup == NULL) {
489                 return NULL;
490         }
491
492         envdup = strdup (envdup);
493         if (envdup == NULL) {
494                 fst_error ("strdup failed");
495                 return NULL;
496         }
497
498         len2 = strlen(path);
499
500         vst_path = strtok (envdup, ":");
501         while (vst_path != NULL) {
502                 fst_error ("\"%s\"", vst_path);
503                 len1 = strlen(vst_path);
504                 full_path = malloc (len1 + 1 + len2 + 1);
505                 memcpy(full_path, vst_path, len1);
506                 full_path[len1] = '/';
507                 memcpy(full_path + len1 + 1, path, len2);
508                 full_path[len1 + 1 + len2] = '\0';
509
510                 if ((dll = LoadLibraryA (full_path)) != NULL) {
511                         break;
512                 }
513
514                 vst_path = strtok (NULL, ":");
515         }
516
517         free(envdup);
518
519         return dll;
520 }
521
522 VSTHandle *
523 fst_load (const char *path)
524 {
525         char* buf;
526         VSTHandle* fhandle;
527         char* period;
528
529         fhandle = fst_handle_new ();
530         
531         // XXX: Would be nice to find the correct call for this.
532         //      if the user does not configure Z: to be / we are doomed :(
533
534         if (strstr (path, ".dll") == NULL) {
535
536                 buf = (char *) malloc (strlen (path) + 7);
537
538                 if( path[0] == '/' ) {
539                     sprintf (buf, "Z:%s.dll", path);
540                 } else {
541                     sprintf (buf, "%s.dll", path);
542                 }
543
544                 fhandle->nameptr = strdup (path);
545
546         } else {
547
548                 buf = (char *) malloc (strlen (path) + 3);
549
550                 if( path[0] == '/' ) {
551                     sprintf (buf, "Z:%s", path);
552                 } else {
553                     sprintf (buf, "%s", path);
554                 }
555
556                 fhandle->nameptr = strdup (path);
557         }
558         
559         fhandle->name = basename (fhandle->nameptr);
560
561         /* strip off .dll */
562
563         if ((period = strrchr (fhandle->name, '.')) != NULL) {
564                 *period = '\0';
565         }
566
567         if ((fhandle->dll = fst_load_vst_library (buf)) == NULL) {
568                 fst_unload (fhandle);
569                 return NULL;
570         }
571
572         if ((fhandle->main_entry = (main_entry_t) GetProcAddress (fhandle->dll, "main")) == NULL) {
573                 fst_unload (fhandle);
574                 return NULL;
575         }
576
577         return fhandle;
578 }
579
580 int
581 fst_unload (VSTHandle* fhandle)
582 {
583         if (fhandle->plugincnt) {
584                 return -1;
585         }
586
587         if (fhandle->dll) {
588                 FreeLibrary (fhandle->dll);
589                 fhandle->dll = NULL;
590         }
591
592         if (fhandle->nameptr) {
593                 free (fhandle->nameptr);
594                 fhandle->name = NULL;
595         }
596         
597         free (fhandle);
598         return 0;
599 }
600
601 VSTState*
602 fst_instantiate (VSTHandle* fhandle, audioMasterCallback amc, void* userptr)
603 {
604         VSTState* fst = fst_new ();
605
606         pthread_mutex_lock (&plugin_mutex);
607
608         if (fst_first == NULL) {
609                 fst_first = fst;
610         } else {
611                 VSTState* p = fst_first;
612                 while (p->next) {
613                         p = p->next;
614                 }
615                 p->next = fst;
616         }
617
618         pthread_mutex_unlock (&plugin_mutex);
619         
620         if( fhandle == NULL ) {
621             fst_error( "the handle was NULL\n" );
622             return NULL;
623         }
624
625         if ((fst->plugin = fhandle->main_entry (amc)) == NULL)  {
626                 fst_error ("%s could not be instantiated\n", fhandle->name);
627                 free (fst);
628                 return NULL;
629         }
630         
631         fst->handle = fhandle;
632         fst->plugin->user = userptr;
633                 
634         if (fst->plugin->magic != kEffectMagic) {
635                 fst_error ("%s is not a VST plugin\n", fhandle->name);
636                 free (fst);
637                 return NULL;
638         }
639         
640         fst->plugin->dispatcher (fst->plugin, effOpen, 0, 0, 0, 0);
641         //fst->plugin->dispatcher (fst->plugin, effMainsChanged, 0, 0, NULL, 0);
642
643         fst->vst_version = fst->plugin->dispatcher (fst->plugin, effGetVstVersion, 0, 0, 0, 0);
644         
645         fst->handle->plugincnt++;
646         fst->wantIdle = 0;
647
648         return fst;
649 }
650
651 void
652 fst_close (VSTState* fst)
653 {
654         fst_destroy_editor (fst);
655
656         fst->plugin->dispatcher (fst->plugin, effMainsChanged, 0, 0, NULL, 0);
657         fst->plugin->dispatcher (fst->plugin, effClose, 0, 0, 0, 0);
658
659         if (fst->handle->plugincnt) {
660                 --fst->handle->plugincnt;
661         }
662 }
663
664 int
665 fst_get_XID (VSTState* fst)
666 {
667         return fst->xid;
668 }
669
670 float htonf (float v)
671 {
672       float result;
673       char * fin = (char*)&v;
674       char * fout = (char*)&result;
675       fout[0] = fin[3];
676       fout[1] = fin[2];
677       fout[2] = fin[1];
678       fout[3] = fin[0];
679       return result;
680 }
681
682 #if 0
683 int fst_load_state (FST * fst, char * filename)
684 {
685         FILE * f = fopen (filename, "rb");
686         if (f) {
687                 char testMagic[sizeof (magic)];
688                 fread (&testMagic, sizeof (magic), 1, f);
689                 if (strcmp (testMagic, magic)) {
690                         printf ("File corrupt\n");
691                         return FALSE;
692                 }
693
694                 char productString[64];
695                 char vendorString[64];
696                 char effectName[64];
697                 char testString[64];
698                 unsigned length;
699                 int success;
700
701                 fread (&length, sizeof (unsigned), 1, f);
702                 length = htonl (length);
703                 fread (productString, length, 1, f);
704                 productString[length] = 0;
705                 printf ("Product string: %s\n", productString);
706
707                 success = fst_call_dispatcher( fst, effGetProductString, 0, 0, testString, 0 );
708                 if (success == 1) {
709                         if (strcmp (testString, productString) != 0) {
710                                 printf ("Product string mismatch! Plugin has: %s\n", testString);
711                                 fclose (f);
712                                 return FALSE;
713                         }
714                 } else if (length != 0) {
715                         printf ("Product string mismatch! Plugin has none.\n", testString);
716                         fclose (f);
717                         return FALSE;
718                 }
719
720                 fread (&length, sizeof (unsigned), 1, f);
721                 length = htonl (length);
722                 fread (effectName, length, 1, f);
723                 effectName[length] = 0;
724                 printf ("Effect name: %s\n", effectName);
725
726                 success = fst_call_dispatcher( fst, effGetEffectName, 0, 0, testString, 0 );
727                 if (success == 1) {
728                         if (strcmp (testString, effectName) != 0) {
729                                 printf ("Effect name mismatch! Plugin has: %s\n", testString);
730                                 fclose (f);
731                                 return FALSE;
732                         }
733                 } else if (length != 0) {
734                         printf ("Effect name mismatch! Plugin has none.\n", testString);
735                         fclose (f);
736                         return FALSE;
737                 }
738
739                 fread (&length, sizeof (unsigned), 1, f);
740                 length = htonl (length);
741                 fread (vendorString, length, 1, f);
742                 vendorString[length] = 0;
743                 printf ("Vendor string: %s\n", vendorString);
744
745                 success = fst_call_dispatcher( fst, effGetVendorString, 0, 0, testString, 0 );
746                 if (success == 1) {
747                         if (strcmp (testString, vendorString) != 0) {
748                                 printf ("Vendor string mismatch! Plugin has: %s\n", testString);
749                                 fclose (f);
750                                 return FALSE;
751                         }
752                 } else if (length != 0) {
753                         printf ("Vendor string mismatch! Plugin has none.\n", testString);
754                         fclose (f);
755                         return FALSE;
756                 }
757
758                 int numParam;
759                 unsigned i;
760                 fread (&numParam, sizeof (int), 1, f);
761                 numParam = htonl (numParam);
762                 for (i = 0; i < numParam; ++i) {
763                         float val;
764                         fread (&val, sizeof (float), 1, f);
765                         val = htonf (val);
766
767                         pthread_mutex_lock( &fst->lock );
768                         fst->plugin->setParameter( fst->plugin, i, val );
769                         pthread_mutex_unlock( &fst->lock );
770                 }
771
772                 int bytelen;
773                 fread (&bytelen, sizeof (int), 1, f);
774                 bytelen = htonl (bytelen);
775                 if (bytelen) {
776                         char * buf = malloc (bytelen);
777                         fread (buf, bytelen, 1, f);
778
779                         fst_call_dispatcher( fst, 24, 0, bytelen, buf, 0 );
780                         free (buf);
781                 }
782         } else {
783                 printf ("Could not open state file\n");
784                 return FALSE;
785         }
786         return TRUE;
787
788 }
789 #endif
790
791 int
792 fst_save_state (VSTState * fst, char * filename)
793 {
794         FILE * f = fopen (filename, "wb");
795         int j;
796
797         if (f) {
798                 int bytelen;
799                 int numParams = fst->plugin->numParams;
800                 char productString[64];
801                 char effectName[64];
802                 char vendorString[64];
803                 int success;
804
805                 // write header
806                 fprintf( f, "<plugin_state>\n" );
807
808                 success = fst_call_dispatcher( fst, effGetProductString, 0, 0, productString, 0 );
809                 if( success == 1 ) {
810                         fprintf (f, "  <check field=\"productString\" value=\"%s\"/>\n", productString);
811                 } else {
812                         printf ("No product string\n");
813                 }
814
815                 success = fst_call_dispatcher( fst, effGetEffectName, 0, 0, effectName, 0 );
816                 if( success == 1 ) {
817                         fprintf (f, "  <check field=\"effectName\" value=\"%s\"/>\n", effectName);
818                         printf ("Effect name: %s\n", effectName);
819                 } else {
820                         printf ("No effect name\n");
821                 }
822
823                 success = fst_call_dispatcher( fst, effGetVendorString, 0, 0, vendorString, 0 );
824                 if( success == 1 ) {
825                         fprintf (f, "  <check field=\"vendorString\" value=\"%s\"/>\n", vendorString);
826                         printf ("Vendor string: %s\n", vendorString);
827                 } else {
828                         printf ("No vendor string\n");
829                 }
830
831
832                 if( fst->plugin->flags & 32 ) {
833                         numParams = 0;
834                 }
835
836                 for (j = 0; j < numParams; ++j) {
837                         float val;
838                         
839                         pthread_mutex_lock( &fst->lock );
840                         val = fst->plugin->getParameter (fst->plugin, j);
841                         pthread_mutex_unlock( &fst->lock );
842                         fprintf( f, "  <param index=\"%d\" value=\"%f\"/>\n", j, val );
843                 }
844
845                 if( fst->plugin->flags & 32 ) {
846                         printf( "getting chunk...\n" );
847                         void * chunk;
848                         bytelen = fst_call_dispatcher( fst, 23, 0, 0, &chunk, 0 );
849                         printf( "got tha chunk..\n" );
850                         if( bytelen ) {
851                                 if( bytelen < 0 ) {
852                                         printf( "Chunke len < 0 !!! Not saving chunk.\n" );
853                                 } else {
854                                         char *encoded = g_base64_encode( chunk, bytelen );
855                                         fprintf( f, "  <chunk size=\"%d\">\n    %s\n  </chunk>\n", bytelen, encoded );
856                                         g_free( encoded );
857                                 }
858                         }
859                 } 
860
861                 fprintf( f, "</plugin_state>\n" );
862                 fclose( f );
863         } else {
864                 printf ("Could not open state file\n");
865                 return FALSE;
866         }
867         return TRUE;
868 }
869