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