3 #include <jack/thread.h>
7 #include <wine/exception.h>
17 extern char * strdup (const char *);
26 static pthread_mutex_t plugin_mutex;
28 /** Head of linked list of all FSTs */
29 static VSTState* fst_first = NULL;
31 const char magic[] = "FST Plugin State v002";
33 DWORD gui_thread_id = 0;
34 static int gui_quit = 0;
37 my_window_proc (HWND w, UINT msg, WPARAM wp, LPARAM lp)
40 if (msg != WM_TIMER) {
41 fst_error ("window callback handler, msg = 0x%x win=%p\n", msg, w);
51 /* we don't care about windows closing ... */
57 /* we don't care about windows being destroyed ... */
65 return DefWindowProcA (w, msg, wp, lp );
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;
77 fst->current_program = -1;
78 fst->n_pending_keys = 0;
80 fst->program_set_without_editor = 0;
87 VSTHandle* fst = (VSTHandle*) calloc (1, sizeof (VSTHandle));
92 maybe_set_program (VSTState* fst)
94 if (fst->want_program != -1) {
95 if (fst->vst_version >= 2) {
96 fst->plugin->dispatcher (fst->plugin, 67 /* effBeginSetProgram */, 0, 0, NULL, 0);
99 fst->plugin->dispatcher (fst->plugin, effSetProgram, 0, fst->want_program, NULL, 0);
101 if (fst->vst_version >= 2) {
102 fst->plugin->dispatcher (fst->plugin, 68 /* effEndSetProgram */, 0, 0, NULL, 0);
105 fst->current_program = fst->plugin->dispatcher (fst->plugin, 3, /* effGetProgram */ 0, 0, NULL, 0);
106 fst->want_program = -1;
109 if (fst->want_chunk == 1) {
110 fst->plugin->dispatcher (fst->plugin, 24 /* effSetChunk */, 1, fst->wanted_chunk_size, fst->wanted_chunk, 0);
115 DWORD WINAPI gui_event_loop (LPVOID param)
123 gui_thread_id = GetCurrentThreadId ();
125 /* create a dummy window for timer events */
127 if ((hInst = GetModuleHandleA (NULL)) == NULL) {
128 fst_error ("can't get module handle");
132 if ((window = CreateWindowExA (0, "FST", "dummy",
133 WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,
139 fst_error ("cannot create dummy timer window");
142 if (!SetTimer (window, 1000, 20, NULL)) {
143 fst_error ("cannot set timer on dummy window");
148 if (!GetMessageA (&msg, NULL, 0,0)) {
150 fprintf (stderr, "QUIT message received by Windows GUI thread - ignored\n");
157 TranslateMessage( &msg );
158 DispatchMessageA (&msg);
160 if (msg.message != WM_TIMER) {
164 pthread_mutex_lock (&plugin_mutex);
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
172 for (fst = fst_first; fst; fst = fst->next) {
174 pthread_mutex_lock (&fst->lock);
176 if (fst->has_editor == 1) {
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;
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);
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);
201 /* condition/unlock: it was signalled & unlocked in fst_create_editor() */
205 if (fst->dispatcher_wantcall) {
206 fst->dispatcher_retval = fst->plugin->dispatcher( fst->plugin,
207 fst->dispatcher_opcode,
208 fst->dispatcher_index,
211 fst->dispatcher_opt );
212 fst->dispatcher_wantcall = 0;
213 pthread_cond_signal (&fst->plugin_dispatcher_called);
216 fst->plugin->dispatcher (fst->plugin, effEditIdle, 0, 0, NULL, 0);
219 fst->plugin->dispatcher (fst->plugin, 53, 0, 0, NULL, 0);
222 /* Dispatch messages to send keypresses to the plugin */
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 ...
230 if (fst->pending_keys[i].special != 0) {
231 msg.message = WM_KEYDOWN;
232 msg.wParam = fst->pending_keys[i].special;
234 msg.message = WM_CHAR;
235 msg.wParam = fst->pending_keys[i].character;
237 msg.hwnd = GetFocus ();
239 DispatchMessageA (&msg);
242 fst->n_pending_keys = 0;
244 /* See comment for maybe_set_program call below */
245 maybe_set_program (fst);
246 fst->want_program = -1;
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.
258 if (fst->program_set_without_editor == 0) {
259 maybe_set_program (fst);
260 fst->program_set_without_editor = 1;
263 pthread_mutex_unlock (&fst->lock);
266 pthread_mutex_unlock (&plugin_mutex);
273 fst_init (void* possible_hmodule)
278 if (possible_hmodule) {
279 hInst = (HMODULE) possible_hmodule;
280 } else if ((hInst = GetModuleHandleA (NULL)) == NULL) {
281 fst_error ("can't get module handle");
285 wclass.cbSize = sizeof(WNDCLASSEX);
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";
299 if (!RegisterClassExA(&wclass)){
300 printf( "Class register failed :(\n" );
304 fst_error ("Startup win32 GUI thread\n");
306 if (CreateThread (NULL, 0, gui_event_loop, NULL, 0, NULL) == NULL) {
307 fst_error ("could not create new thread proxy");
311 #ifdef HAVE_JACK_SET_THREAD_CREATOR
312 jack_set_thread_creator (wine_pthread_create);
326 fst_run_editor (VSTState* fst)
328 /* wait for the plugin editor window to be created (or not) */
330 pthread_mutex_lock (&fst->lock);
334 if (!fst->windows_window) {
335 pthread_cond_wait (&fst->window_status_change, &fst->lock);
337 pthread_mutex_unlock (&fst->lock);
339 if (!fst->windows_window) {
347 fst_call_dispatcher (VSTState* fst, int opcode, int index, int val, void *ptr, float opt)
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;
357 pthread_cond_wait (&fst->plugin_dispatcher_called, &fst->lock);
358 pthread_mutex_unlock (&fst->lock);
360 return fst->dispatcher_retval;
364 fst_create_editor (VSTState * fst)
370 /* "guard point" to trap errors that occur during plugin loading */
372 /* Note: fst->lock is held while this function is called */
374 if (!(fst->plugin->flags & effFlagsHasEditor)) {
375 fst_error ("Plugin \"%s\" has no editor", fst->handle->name);
379 if ((hInst = GetModuleHandleA (NULL)) == NULL) {
380 fst_error ("can't get module handle");
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),
389 // CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
393 fst_error ("cannot create editor window");
397 if (!SetPropA (window, "fst_ptr", fst)) {
398 fst_error ("cannot set fst_ptr on window");
401 fst->windows_window = window;
402 // fst->xid = (int) GetPropA (window, "__wine_x11_whole_window");
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 );
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 );
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);
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);
427 fst_move_window_into_view (VSTState* fst)
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);
436 fst_destroy_editor (VSTState* fst)
438 pthread_mutex_lock (&fst->lock);
439 if (fst->windows_window) {
440 fprintf (stderr, "mark %s for destroy\n", fst->handle->name);
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");
446 pthread_cond_wait (&fst->window_status_change, &fst->lock);
447 fprintf (stderr, "%s editor destroyed\n", fst->handle->name);
450 pthread_mutex_unlock (&fst->lock);
454 fst_event_loop_remove_plugin (VSTState* fst)
459 for (p = fst_first, prev = NULL; p->next; prev = p, p = p->next) {
462 prev->next = p->next;
467 if (fst_first == fst) {
468 fst_first = fst_first->next;
474 fst_load_vst_library(const char * path)
483 if ((dll = LoadLibraryA (path)) != NULL) {
487 envdup = getenv ("VST_PATH");
488 if (envdup == NULL) {
492 envdup = strdup (envdup);
493 if (envdup == NULL) {
494 fst_error ("strdup failed");
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';
510 if ((dll = LoadLibraryA (full_path)) != NULL) {
514 vst_path = strtok (NULL, ":");
523 fst_load (const char *path)
529 fhandle = fst_handle_new ();
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 :(
534 if (strstr (path, ".dll") == NULL) {
536 buf = (char *) malloc (strlen (path) + 7);
538 if( path[0] == '/' ) {
539 sprintf (buf, "Z:%s.dll", path);
541 sprintf (buf, "%s.dll", path);
544 fhandle->nameptr = strdup (path);
548 buf = (char *) malloc (strlen (path) + 3);
550 if( path[0] == '/' ) {
551 sprintf (buf, "Z:%s", path);
553 sprintf (buf, "%s", path);
556 fhandle->nameptr = strdup (path);
559 fhandle->name = basename (fhandle->nameptr);
563 if ((period = strrchr (fhandle->name, '.')) != NULL) {
567 if ((fhandle->dll = fst_load_vst_library (buf)) == NULL) {
568 fst_unload (fhandle);
572 if ((fhandle->main_entry = (main_entry_t) GetProcAddress (fhandle->dll, "main")) == NULL) {
573 fst_unload (fhandle);
581 fst_unload (VSTHandle* fhandle)
583 if (fhandle->plugincnt) {
588 FreeLibrary (fhandle->dll);
592 if (fhandle->nameptr) {
593 free (fhandle->nameptr);
594 fhandle->name = NULL;
602 fst_instantiate (VSTHandle* fhandle, audioMasterCallback amc, void* userptr)
604 VSTState* fst = fst_new ();
606 pthread_mutex_lock (&plugin_mutex);
608 if (fst_first == NULL) {
611 VSTState* p = fst_first;
618 pthread_mutex_unlock (&plugin_mutex);
620 if( fhandle == NULL ) {
621 fst_error( "the handle was NULL\n" );
625 if ((fst->plugin = fhandle->main_entry (amc)) == NULL) {
626 fst_error ("%s could not be instantiated\n", fhandle->name);
631 fst->handle = fhandle;
632 fst->plugin->user = userptr;
634 if (fst->plugin->magic != kEffectMagic) {
635 fst_error ("%s is not a VST plugin\n", fhandle->name);
640 fst->plugin->dispatcher (fst->plugin, effOpen, 0, 0, 0, 0);
641 //fst->plugin->dispatcher (fst->plugin, effMainsChanged, 0, 0, NULL, 0);
643 fst->vst_version = fst->plugin->dispatcher (fst->plugin, effGetVstVersion, 0, 0, 0, 0);
645 fst->handle->plugincnt++;
652 fst_close (VSTState* fst)
654 fst_destroy_editor (fst);
656 fst->plugin->dispatcher (fst->plugin, effMainsChanged, 0, 0, NULL, 0);
657 fst->plugin->dispatcher (fst->plugin, effClose, 0, 0, 0, 0);
659 if (fst->handle->plugincnt) {
660 --fst->handle->plugincnt;
665 fst_get_XID (VSTState* fst)
670 float htonf (float v)
673 char * fin = (char*)&v;
674 char * fout = (char*)&result;
683 int fst_load_state (FST * fst, char * filename)
685 FILE * f = fopen (filename, "rb");
687 char testMagic[sizeof (magic)];
688 fread (&testMagic, sizeof (magic), 1, f);
689 if (strcmp (testMagic, magic)) {
690 printf ("File corrupt\n");
694 char productString[64];
695 char vendorString[64];
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);
707 success = fst_call_dispatcher( fst, effGetProductString, 0, 0, testString, 0 );
709 if (strcmp (testString, productString) != 0) {
710 printf ("Product string mismatch! Plugin has: %s\n", testString);
714 } else if (length != 0) {
715 printf ("Product string mismatch! Plugin has none.\n", testString);
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);
726 success = fst_call_dispatcher( fst, effGetEffectName, 0, 0, testString, 0 );
728 if (strcmp (testString, effectName) != 0) {
729 printf ("Effect name mismatch! Plugin has: %s\n", testString);
733 } else if (length != 0) {
734 printf ("Effect name mismatch! Plugin has none.\n", testString);
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);
745 success = fst_call_dispatcher( fst, effGetVendorString, 0, 0, testString, 0 );
747 if (strcmp (testString, vendorString) != 0) {
748 printf ("Vendor string mismatch! Plugin has: %s\n", testString);
752 } else if (length != 0) {
753 printf ("Vendor string mismatch! Plugin has none.\n", testString);
760 fread (&numParam, sizeof (int), 1, f);
761 numParam = htonl (numParam);
762 for (i = 0; i < numParam; ++i) {
764 fread (&val, sizeof (float), 1, f);
767 pthread_mutex_lock( &fst->lock );
768 fst->plugin->setParameter( fst->plugin, i, val );
769 pthread_mutex_unlock( &fst->lock );
773 fread (&bytelen, sizeof (int), 1, f);
774 bytelen = htonl (bytelen);
776 char * buf = malloc (bytelen);
777 fread (buf, bytelen, 1, f);
779 fst_call_dispatcher( fst, 24, 0, bytelen, buf, 0 );
783 printf ("Could not open state file\n");
792 fst_save_state (VSTState * fst, char * filename)
794 FILE * f = fopen (filename, "wb");
799 int numParams = fst->plugin->numParams;
800 char productString[64];
802 char vendorString[64];
806 fprintf( f, "<plugin_state>\n" );
808 success = fst_call_dispatcher( fst, effGetProductString, 0, 0, productString, 0 );
810 fprintf (f, " <check field=\"productString\" value=\"%s\"/>\n", productString);
812 printf ("No product string\n");
815 success = fst_call_dispatcher( fst, effGetEffectName, 0, 0, effectName, 0 );
817 fprintf (f, " <check field=\"effectName\" value=\"%s\"/>\n", effectName);
818 printf ("Effect name: %s\n", effectName);
820 printf ("No effect name\n");
823 success = fst_call_dispatcher( fst, effGetVendorString, 0, 0, vendorString, 0 );
825 fprintf (f, " <check field=\"vendorString\" value=\"%s\"/>\n", vendorString);
826 printf ("Vendor string: %s\n", vendorString);
828 printf ("No vendor string\n");
832 if( fst->plugin->flags & 32 ) {
836 for (j = 0; j < numParams; ++j) {
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 );
845 if( fst->plugin->flags & 32 ) {
846 printf( "getting chunk...\n" );
848 bytelen = fst_call_dispatcher( fst, 23, 0, 0, &chunk, 0 );
849 printf( "got tha chunk..\n" );
852 printf( "Chunke len < 0 !!! Not saving chunk.\n" );
854 char *encoded = g_base64_encode( chunk, bytelen );
855 fprintf( f, " <chunk size=\"%d\">\n %s\n </chunk>\n", bytelen, encoded );
861 fprintf( f, "</plugin_state>\n" );
864 printf ("Could not open state file\n");