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->n_pending_keys = 0;
79 fst->program_set_without_editor = 0;
86 VSTHandle* fst = (VSTHandle*) calloc (1, sizeof (VSTHandle));
91 maybe_set_program (VSTState* fst)
93 if (fst->want_program != -1) {
94 if (fst->vst_version >= 2) {
95 fst->plugin->dispatcher (fst->plugin, 67 /* effBeginSetProgram */, 0, 0, NULL, 0);
98 fst->plugin->dispatcher (fst->plugin, effSetProgram, 0, fst->want_program, NULL, 0);
100 if (fst->vst_version >= 2) {
101 fst->plugin->dispatcher (fst->plugin, 68 /* effEndSetProgram */, 0, 0, NULL, 0);
103 fst->want_program = -1;
106 if (fst->want_chunk == 1) {
107 fst->plugin->dispatcher (fst->plugin, 24 /* effSetChunk */, 1, fst->wanted_chunk_size, fst->wanted_chunk, 0);
112 DWORD WINAPI gui_event_loop (LPVOID param)
120 gui_thread_id = GetCurrentThreadId ();
122 /* create a dummy window for timer events */
124 if ((hInst = GetModuleHandleA (NULL)) == NULL) {
125 fst_error ("can't get module handle");
129 if ((window = CreateWindowExA (0, "FST", "dummy",
130 WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,
136 fst_error ("cannot create dummy timer window");
139 if (!SetTimer (window, 1000, 20, NULL)) {
140 fst_error ("cannot set timer on dummy window");
145 if (!GetMessageA (&msg, NULL, 0,0)) {
147 fprintf (stderr, "QUIT message received by Windows GUI thread - ignored\n");
154 TranslateMessage( &msg );
155 DispatchMessageA (&msg);
157 if (msg.message != WM_TIMER) {
161 pthread_mutex_lock (&plugin_mutex);
163 /* Do things that are appropriate for plugins which have open editor windows:
164 handle window creation requests, destroy requests,
165 and run idle callbacks
169 for (fst = fst_first; fst; fst = fst->next) {
171 pthread_mutex_lock (&fst->lock);
173 if (fst->has_editor == 1) {
176 fprintf (stderr, "%s scheduled for destroy\n", fst->handle->name);
177 if (fst->windows_window) {
178 fst->plugin->dispatcher( fst->plugin, effEditClose, 0, 0, NULL, 0.0 );
179 CloseWindow (fst->windows_window);
180 fst->windows_window = NULL;
181 fst->destroy = FALSE;
183 fst_event_loop_remove_plugin (fst);
184 fst->been_activated = FALSE;
185 pthread_cond_signal (&fst->window_status_change);
186 pthread_mutex_unlock (&fst->lock);
190 if (fst->windows_window == NULL) {
191 if (fst_create_editor (fst)) {
192 fst_error ("cannot create editor for plugin %s", fst->handle->name);
193 fst_event_loop_remove_plugin (fst);
194 pthread_cond_signal (&fst->window_status_change);
195 pthread_mutex_unlock (&fst->lock);
198 /* condition/unlock: it was signalled & unlocked in fst_create_editor() */
202 if (fst->dispatcher_wantcall) {
203 fst->dispatcher_retval = fst->plugin->dispatcher( fst->plugin,
204 fst->dispatcher_opcode,
205 fst->dispatcher_index,
208 fst->dispatcher_opt );
209 fst->dispatcher_wantcall = 0;
210 pthread_cond_signal (&fst->plugin_dispatcher_called);
213 fst->plugin->dispatcher (fst->plugin, effEditIdle, 0, 0, NULL, 0);
216 fst->plugin->dispatcher (fst->plugin, 53, 0, 0, NULL, 0);
219 /* Dispatch messages to send keypresses to the plugin */
221 for (i = 0; i < fst->n_pending_keys; ++i) {
222 /* I'm not quite sure what is going on here; it seems
223 `special' keys must be delivered with WM_KEYDOWN,
224 but that alphanumerics etc. must use WM_CHAR or
225 they will be ignored. Ours is not to reason why ...
227 if (fst->pending_keys[i].special != 0) {
228 msg.message = WM_KEYDOWN;
229 msg.wParam = fst->pending_keys[i].special;
231 msg.message = WM_CHAR;
232 msg.wParam = fst->pending_keys[i].character;
234 msg.hwnd = GetFocus ();
236 DispatchMessageA (&msg);
239 fst->n_pending_keys = 0;
241 /* See comment for maybe_set_program call below */
242 maybe_set_program (fst);
243 fst->want_program = -1;
247 /* If we don't have an editor window yet, we still need to
248 * set up the program, otherwise when we load a plugin without
249 * opening its window it will sound wrong. However, it seems
250 * that if you don't also load the program after opening the GUI,
251 * the GUI does not reflect the program properly. So we'll not
252 * mark that we've done this (ie we won't set want_program to -1)
253 * and so it will be done again if and when the GUI arrives.
255 if (fst->program_set_without_editor == 0) {
256 maybe_set_program (fst);
257 fst->program_set_without_editor = 1;
260 pthread_mutex_unlock (&fst->lock);
263 pthread_mutex_unlock (&plugin_mutex);
270 fst_init (void* possible_hmodule)
275 if (possible_hmodule) {
276 hInst = (HMODULE) possible_hmodule;
277 } else if ((hInst = GetModuleHandleA (NULL)) == NULL) {
278 fst_error ("can't get module handle");
282 wclass.cbSize = sizeof(WNDCLASSEX);
284 wclass.lpfnWndProc = my_window_proc;
285 wclass.cbClsExtra = 0;
286 wclass.cbWndExtra = 0;
287 wclass.hInstance = hInst;
288 wclass.hIcon = LoadIcon(hInst, "FST");
289 wclass.hCursor = LoadCursor(0, IDI_APPLICATION);
290 // wclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
291 wclass.lpszMenuName = "MENU_FST";
292 wclass.lpszClassName = "FST";
296 if (!RegisterClassExA(&wclass)){
297 printf( "Class register failed :(\n" );
301 fst_error ("Startup win32 GUI thread\n");
303 if (CreateThread (NULL, 0, gui_event_loop, NULL, 0, NULL) == NULL) {
304 fst_error ("could not create new thread proxy");
308 #ifdef HAVE_JACK_SET_THREAD_CREATOR
309 jack_set_thread_creator (wine_pthread_create);
323 fst_run_editor (VSTState* fst)
325 /* wait for the plugin editor window to be created (or not) */
327 pthread_mutex_lock (&fst->lock);
331 if (!fst->windows_window) {
332 pthread_cond_wait (&fst->window_status_change, &fst->lock);
334 pthread_mutex_unlock (&fst->lock);
336 if (!fst->windows_window) {
344 fst_call_dispatcher (VSTState* fst, int opcode, int index, int val, void *ptr, float opt)
346 pthread_mutex_lock (&fst->lock);
347 fst->dispatcher_opcode = opcode;
348 fst->dispatcher_index = index;
349 fst->dispatcher_val = val;
350 fst->dispatcher_ptr = ptr;
351 fst->dispatcher_opt = opt;
352 fst->dispatcher_wantcall = 1;
354 pthread_cond_wait (&fst->plugin_dispatcher_called, &fst->lock);
355 pthread_mutex_unlock (&fst->lock);
357 return fst->dispatcher_retval;
361 fst_create_editor (VSTState * fst)
367 /* "guard point" to trap errors that occur during plugin loading */
369 /* Note: fst->lock is held while this function is called */
371 if (!(fst->plugin->flags & effFlagsHasEditor)) {
372 fst_error ("Plugin \"%s\" has no editor", fst->handle->name);
376 if ((hInst = GetModuleHandleA (NULL)) == NULL) {
377 fst_error ("can't get module handle");
381 // if ((window = CreateWindowExA (WS_EX_TOOLWINDOW | WS_EX_TRAYWINDOW, "FST", fst->handle->name,
382 if ((window = CreateWindowExA (0, "FST", fst->handle->name,
383 (WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX),
384 // (WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX),
386 // CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
390 fst_error ("cannot create editor window");
394 if (!SetPropA (window, "fst_ptr", fst)) {
395 fst_error ("cannot set fst_ptr on window");
398 fst->windows_window = window;
399 // fst->xid = (int) GetPropA (window, "__wine_x11_whole_window");
402 //printf( "effEditOpen......\n" );
403 fst->plugin->dispatcher (fst->plugin, effEditOpen, 0, 0, fst->windows_window, 0);
404 fst->plugin->dispatcher (fst->plugin, effEditGetRect, 0, 0, &er, 0 );
406 fst->width = er->right-er->left;
407 fst->height = er->bottom-er->top;
408 //printf( "get rect ses... %d,%d\n", fst->width, fst->height );
410 //SetWindowPos (fst->window, 0, 9999, 9999, er->right-er->left+8, er->bottom-er->top+26, 0);
411 SetWindowPos (fst->windows_window, 0, 9999, 9999, 2, 2, 0);
412 ShowWindow (fst->windows_window, SW_SHOWNA);
413 //SetWindowPos (fst->window, 0, 0, 0, er->right-er->left+8, er->bottom-er->top+26, SWP_NOMOVE|SWP_NOZORDER);
415 fst->xid = (int) GetPropA (window, "__wine_x11_whole_window");
416 fst->been_activated = TRUE;
417 pthread_cond_signal (&fst->window_status_change);
418 pthread_mutex_unlock (&fst->lock);
424 fst_move_window_into_view (VSTState* fst)
426 if (fst->windows_window) {
427 SetWindowPos (fst->windows_window, 0, 0, 0, fst->width, fst->height + 24, 0);
428 ShowWindow (fst->windows_window, SW_SHOWNA);
433 fst_destroy_editor (VSTState* fst)
435 pthread_mutex_lock (&fst->lock);
436 if (fst->windows_window) {
437 fprintf (stderr, "mark %s for destroy\n", fst->handle->name);
439 //if (!PostThreadMessageA (gui_thread_id, WM_USER, 0, 0)) {
440 //if (!PostThreadMessageA (gui_thread_id, WM_QUIT, 0, 0)) {
441 // fst_error ("could not post message to gui thread");
443 pthread_cond_wait (&fst->window_status_change, &fst->lock);
444 fprintf (stderr, "%s editor destroyed\n", fst->handle->name);
447 pthread_mutex_unlock (&fst->lock);
451 fst_event_loop_remove_plugin (VSTState* fst)
456 for (p = fst_first, prev = NULL; p->next; prev = p, p = p->next) {
459 prev->next = p->next;
464 if (fst_first == fst) {
465 fst_first = fst_first->next;
471 fst_load_vst_library(const char * path)
480 if ((dll = LoadLibraryA (path)) != NULL) {
484 envdup = getenv ("VST_PATH");
485 if (envdup == NULL) {
489 envdup = strdup (envdup);
490 if (envdup == NULL) {
491 fst_error ("strdup failed");
497 vst_path = strtok (envdup, ":");
498 while (vst_path != NULL) {
499 fst_error ("\"%s\"", vst_path);
500 len1 = strlen(vst_path);
501 full_path = malloc (len1 + 1 + len2 + 1);
502 memcpy(full_path, vst_path, len1);
503 full_path[len1] = '/';
504 memcpy(full_path + len1 + 1, path, len2);
505 full_path[len1 + 1 + len2] = '\0';
507 if ((dll = LoadLibraryA (full_path)) != NULL) {
511 vst_path = strtok (NULL, ":");
520 fst_load (const char *path)
526 fhandle = fst_handle_new ();
528 // XXX: Would be nice to find the correct call for this.
529 // if the user does not configure Z: to be / we are doomed :(
531 if (strstr (path, ".dll") == NULL) {
533 buf = (char *) malloc (strlen (path) + 7);
535 if( path[0] == '/' ) {
536 sprintf (buf, "Z:%s.dll", path);
538 sprintf (buf, "%s.dll", path);
541 fhandle->nameptr = strdup (path);
545 buf = (char *) malloc (strlen (path) + 3);
547 if( path[0] == '/' ) {
548 sprintf (buf, "Z:%s", path);
550 sprintf (buf, "%s", path);
553 fhandle->nameptr = strdup (path);
556 fhandle->name = basename (fhandle->nameptr);
560 if ((period = strrchr (fhandle->name, '.')) != NULL) {
564 if ((fhandle->dll = fst_load_vst_library (buf)) == NULL) {
565 fst_unload (fhandle);
569 if ((fhandle->main_entry = (main_entry_t) GetProcAddress (fhandle->dll, "main")) == NULL) {
570 fst_unload (fhandle);
578 fst_unload (VSTHandle* fhandle)
580 if (fhandle->plugincnt) {
585 FreeLibrary (fhandle->dll);
589 if (fhandle->nameptr) {
590 free (fhandle->nameptr);
591 fhandle->name = NULL;
599 fst_instantiate (VSTHandle* fhandle, audioMasterCallback amc, void* userptr)
601 VSTState* fst = fst_new ();
603 pthread_mutex_lock (&plugin_mutex);
605 if (fst_first == NULL) {
608 VSTState* p = fst_first;
615 pthread_mutex_unlock (&plugin_mutex);
617 if( fhandle == NULL ) {
618 fst_error( "the handle was NULL\n" );
622 if ((fst->plugin = fhandle->main_entry (amc)) == NULL) {
623 fst_error ("%s could not be instantiated\n", fhandle->name);
628 fst->handle = fhandle;
629 fst->plugin->user = userptr;
631 if (fst->plugin->magic != kEffectMagic) {
632 fst_error ("%s is not a VST plugin\n", fhandle->name);
637 fst->plugin->dispatcher (fst->plugin, effOpen, 0, 0, 0, 0);
638 //fst->plugin->dispatcher (fst->plugin, effMainsChanged, 0, 0, NULL, 0);
640 fst->vst_version = fst->plugin->dispatcher (fst->plugin, effGetVstVersion, 0, 0, 0, 0);
642 fst->handle->plugincnt++;
649 fst_close (VSTState* fst)
651 fst_destroy_editor (fst);
653 fst->plugin->dispatcher (fst->plugin, effMainsChanged, 0, 0, NULL, 0);
654 fst->plugin->dispatcher (fst->plugin, effClose, 0, 0, 0, 0);
656 if (fst->handle->plugincnt) {
657 --fst->handle->plugincnt;
661 float htonf (float v)
664 char * fin = (char*)&v;
665 char * fout = (char*)&result;
674 int fst_load_state (FST * fst, char * filename)
676 FILE * f = fopen (filename, "rb");
678 char testMagic[sizeof (magic)];
679 fread (&testMagic, sizeof (magic), 1, f);
680 if (strcmp (testMagic, magic)) {
681 printf ("File corrupt\n");
685 char productString[64];
686 char vendorString[64];
692 fread (&length, sizeof (unsigned), 1, f);
693 length = htonl (length);
694 fread (productString, length, 1, f);
695 productString[length] = 0;
696 printf ("Product string: %s\n", productString);
698 success = fst_call_dispatcher( fst, effGetProductString, 0, 0, testString, 0 );
700 if (strcmp (testString, productString) != 0) {
701 printf ("Product string mismatch! Plugin has: %s\n", testString);
705 } else if (length != 0) {
706 printf ("Product string mismatch! Plugin has none.\n", testString);
711 fread (&length, sizeof (unsigned), 1, f);
712 length = htonl (length);
713 fread (effectName, length, 1, f);
714 effectName[length] = 0;
715 printf ("Effect name: %s\n", effectName);
717 success = fst_call_dispatcher( fst, effGetEffectName, 0, 0, testString, 0 );
719 if (strcmp (testString, effectName) != 0) {
720 printf ("Effect name mismatch! Plugin has: %s\n", testString);
724 } else if (length != 0) {
725 printf ("Effect name mismatch! Plugin has none.\n", testString);
730 fread (&length, sizeof (unsigned), 1, f);
731 length = htonl (length);
732 fread (vendorString, length, 1, f);
733 vendorString[length] = 0;
734 printf ("Vendor string: %s\n", vendorString);
736 success = fst_call_dispatcher( fst, effGetVendorString, 0, 0, testString, 0 );
738 if (strcmp (testString, vendorString) != 0) {
739 printf ("Vendor string mismatch! Plugin has: %s\n", testString);
743 } else if (length != 0) {
744 printf ("Vendor string mismatch! Plugin has none.\n", testString);
751 fread (&numParam, sizeof (int), 1, f);
752 numParam = htonl (numParam);
753 for (i = 0; i < numParam; ++i) {
755 fread (&val, sizeof (float), 1, f);
758 pthread_mutex_lock( &fst->lock );
759 fst->plugin->setParameter( fst->plugin, i, val );
760 pthread_mutex_unlock( &fst->lock );
764 fread (&bytelen, sizeof (int), 1, f);
765 bytelen = htonl (bytelen);
767 char * buf = malloc (bytelen);
768 fread (buf, bytelen, 1, f);
770 fst_call_dispatcher( fst, 24, 0, bytelen, buf, 0 );
774 printf ("Could not open state file\n");
783 fst_save_state (VSTState * fst, char * filename)
785 FILE * f = fopen (filename, "wb");
790 int numParams = fst->plugin->numParams;
791 char productString[64];
793 char vendorString[64];
797 fprintf( f, "<plugin_state>\n" );
799 success = fst_call_dispatcher( fst, effGetProductString, 0, 0, productString, 0 );
801 fprintf (f, " <check field=\"productString\" value=\"%s\"/>\n", productString);
803 printf ("No product string\n");
806 success = fst_call_dispatcher( fst, effGetEffectName, 0, 0, effectName, 0 );
808 fprintf (f, " <check field=\"effectName\" value=\"%s\"/>\n", effectName);
809 printf ("Effect name: %s\n", effectName);
811 printf ("No effect name\n");
814 success = fst_call_dispatcher( fst, effGetVendorString, 0, 0, vendorString, 0 );
816 fprintf (f, " <check field=\"vendorString\" value=\"%s\"/>\n", vendorString);
817 printf ("Vendor string: %s\n", vendorString);
819 printf ("No vendor string\n");
823 if( fst->plugin->flags & 32 ) {
827 for (j = 0; j < numParams; ++j) {
830 pthread_mutex_lock( &fst->lock );
831 val = fst->plugin->getParameter (fst->plugin, j);
832 pthread_mutex_unlock( &fst->lock );
833 fprintf( f, " <param index=\"%d\" value=\"%f\"/>\n", j, val );
836 if( fst->plugin->flags & 32 ) {
837 printf( "getting chunk...\n" );
839 bytelen = fst_call_dispatcher( fst, 23, 0, 0, &chunk, 0 );
840 printf( "got tha chunk..\n" );
843 printf( "Chunke len < 0 !!! Not saving chunk.\n" );
845 char *encoded = g_base64_encode( chunk, bytelen );
846 fprintf( f, " <chunk size=\"%d\">\n %s\n </chunk>\n", bytelen, encoded );
852 fprintf( f, "</plugin_state>\n" );
855 printf ("Could not open state file\n");