2 #define Marker FuckYouAppleAndYourLackOfNameSpaces
4 #include "pbd/convert.h"
7 #include "ardour/audio_unit.h"
8 #include "ardour/debug.h"
9 #include "ardour/plugin_insert.h"
11 #undef check // stupid gtk, stupid apple
13 #include <gtkmm/button.h>
14 #include <gdk/gdkquartz.h>
16 #include <gtkmm2ext/utils.h>
18 #include "au_pluginui.h"
19 #include "gui_thread.h"
21 #include "appleutility/CAAudioUnit.h"
22 #include "appleutility/CAComponent.h"
24 #import <AudioUnit/AUCocoaUIView.h>
25 #import <CoreAudioKit/AUGenericView.h>
31 #include "public_editor.h"
34 using namespace ARDOUR;
36 using namespace Gtkmm2ext;
41 vector<string> AUPluginUI::automation_mode_strings;
43 static const gchar* _automation_mode_strings[] = {
51 @implementation NotificationObject
53 - (NotificationObject*) initWithPluginUI: (AUPluginUI*) apluginui andCocoaParent: (NSWindow*) cp andTopLevelParent: (NSWindow*) tlp
55 self = [ super init ];
58 plugin_ui = apluginui;
59 top_level_parent = tlp;
64 [[NSNotificationCenter defaultCenter] addObserver:self
65 selector:@selector(cocoaParentActivationHandler:)
66 name:NSWindowDidBecomeMainNotification
69 [[NSNotificationCenter defaultCenter] addObserver:self
70 selector:@selector(cocoaParentBecameKeyHandler:)
71 name:NSWindowDidBecomeKeyNotification
79 - (void)cocoaParentActivationHandler:(NSNotification *)notification
81 NSWindow* notification_window = (NSWindow *)[notification object];
83 if (top_level_parent == notification_window || cocoa_parent == notification_window) {
84 if ([notification_window isMainWindow]) {
85 plugin_ui->activate();
87 plugin_ui->deactivate();
92 - (void)cocoaParentBecameKeyHandler:(NSNotification *)notification
94 NSWindow* notification_window = (NSWindow *)[notification object];
96 if (top_level_parent == notification_window || cocoa_parent == notification_window) {
97 if ([notification_window isKeyWindow]) {
98 plugin_ui->activate();
100 plugin_ui->deactivate();
105 - (void)auViewResized:(NSNotification *)notification;
107 (void) notification; // stop complaints about unusued argument
108 plugin_ui->cocoa_view_resized();
113 AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
114 : PlugUIBase (insert)
115 , automation_mode_label (_("Automation"))
116 , preset_label (_("Presets"))
119 if (automation_mode_strings.empty()) {
120 automation_mode_strings = I18N (_automation_mode_strings);
123 set_popdown_strings (automation_mode_selector, automation_mode_strings);
124 automation_mode_selector.set_active_text (automation_mode_strings.front());
126 if ((au = boost::dynamic_pointer_cast<AUPlugin> (insert->plugin())) == 0) {
127 error << _("unknown type of editor-supplying plugin (note: no AudioUnit support in this version of ardour)") << endmsg;
128 throw failed_constructor ();
131 /* stuff some stuff into the top of the window */
133 HBox* smaller_hbox = manage (new HBox);
135 smaller_hbox->set_spacing (6);
136 smaller_hbox->pack_start (preset_label, false, false, 4);
137 smaller_hbox->pack_start (_preset_combo, false, false);
138 smaller_hbox->pack_start (save_button, false, false);
140 /* one day these might be useful with an AU plugin, but not yet */
141 smaller_hbox->pack_start (automation_mode_label, false, false);
142 smaller_hbox->pack_start (automation_mode_selector, false, false);
144 smaller_hbox->pack_start (bypass_button, false, true);
146 VBox* v1_box = manage (new VBox);
147 VBox* v2_box = manage (new VBox);
149 v1_box->pack_start (*smaller_hbox, false, true);
150 v2_box->pack_start (focus_button, false, true);
152 top_box.set_homogeneous (false);
153 top_box.set_spacing (6);
154 top_box.set_border_width (6);
156 top_box.pack_end (*v2_box, false, false);
157 top_box.pack_end (*v1_box, false, false);
160 pack_start (top_box, false, false);
161 pack_start (low_box, false, false);
163 preset_label.show ();
164 _preset_combo.show ();
165 automation_mode_label.show ();
166 automation_mode_selector.show ();
167 bypass_button.show ();
175 _activating_from_app = false;
182 /* prefer cocoa, fall back to cocoa, but use carbon if its there */
184 if (test_cocoa_view_support()) {
185 create_cocoa_view ();
187 } else if (test_carbon_view_support()) {
188 create_carbon_view ();
191 create_cocoa_view ();
194 low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
197 AUPluginUI::~AUPluginUI ()
200 [[NSNotificationCenter defaultCenter] removeObserver:_notify];
204 NSWindow* win = get_nswindow();
205 [win removeChildWindow:cocoa_parent];
210 /* not parented, just overlaid on top of our window */
211 DisposeWindow (carbon_window);
216 CloseComponent (editView);
220 /* remove whatever we packed into low_box so that GTK doesn't
224 [au_view removeFromSuperview];
229 AUPluginUI::test_carbon_view_support ()
234 carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
235 carbon_descriptor.componentSubType = 'gnrc';
236 carbon_descriptor.componentManufacturer = 'appl';
237 carbon_descriptor.componentFlags = 0;
238 carbon_descriptor.componentFlagsMask = 0;
242 // ask the AU for its first editor component
244 err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
246 int nEditors = propertySize / sizeof(ComponentDescription);
247 ComponentDescription *editors = new ComponentDescription[nEditors];
248 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
250 // just pick the first one for now
251 carbon_descriptor = editors[0];
264 AUPluginUI::test_cocoa_view_support ()
267 Boolean isWritable = 0;
268 OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
269 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
270 0, &dataSize, &isWritable);
272 return dataSize > 0 && err == noErr;
276 AUPluginUI::plugin_class_valid (Class pluginClass)
278 if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
279 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
280 [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
288 AUPluginUI::create_cocoa_view ()
290 bool wasAbleToLoadCustomView = false;
291 AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
292 UInt32 numberOfClasses = 0;
295 NSString* factoryClassName = 0;
296 NSURL* CocoaViewBundlePath = NULL;
298 OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
299 kAudioUnitProperty_CocoaUI,
300 kAudioUnitScope_Global,
305 numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
307 // Does view have custom Cocoa UI?
309 if ((result == noErr) && (numberOfClasses > 0) ) {
311 DEBUG_TRACE(DEBUG::AudioUnits,
312 string_compose ( "based on %1, there are %2 cocoa UI classes\n", dataSize, numberOfClasses));
314 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
316 if(AudioUnitGetProperty(*au->get_au(),
317 kAudioUnitProperty_CocoaUI,
318 kAudioUnitScope_Global,
321 &dataSize) == noErr) {
323 CocoaViewBundlePath = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
325 // we only take the first view in this example.
326 factoryClassName = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
328 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("the factory name is %1 bundle is %2\n",
329 [factoryClassName UTF8String], CocoaViewBundlePath));
333 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("No cocoaUI property cocoaViewInfo = %1\n", cocoaViewInfo));
335 if (cocoaViewInfo != NULL) {
336 free (cocoaViewInfo);
337 cocoaViewInfo = NULL;
342 // [A] Show custom UI if view has it
344 if (CocoaViewBundlePath && factoryClassName) {
345 NSBundle *viewBundle = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
347 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("tried to create bundle, result = %1\n", viewBundle));
349 if (viewBundle == nil) {
350 error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
353 Class factoryClass = [viewBundle classNamed:factoryClassName];
354 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("tried to create factory class, result = %1\n", factoryClass));
356 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
360 // make sure 'factoryClass' implements the AUCocoaUIBase protocol
361 if (!plugin_class_valid (factoryClass)) {
362 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
366 id factory = [[[factoryClass alloc] init] autorelease];
367 if (factory == nil) {
368 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
372 DEBUG_TRACE (DEBUG::AudioUnits, "got a factory instance\n");
375 au_view = [factory uiViewForAudioUnit:*au->get_au() withSize:NSZeroSize];
377 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("view created @ %1\n", au_view));
380 [CocoaViewBundlePath release];
383 for (i = 0; i < numberOfClasses; i++)
384 CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
386 free (cocoaViewInfo);
388 wasAbleToLoadCustomView = true;
392 if (!wasAbleToLoadCustomView) {
393 // load generic Cocoa view
394 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("Loading generic view using %1 -> %2\n", au,
396 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
397 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("view created @ %1\n", au_view));
398 [(AUGenericView *)au_view setShowsExpertParameters:1];
401 // Get the initial size of the new AU View's frame
403 NSRect rect = [au_view frame];
404 prefheight = rect.size.height;
405 prefwidth = rect.size.width;
406 low_box.set_size_request (rect.size.width, rect.size.height);
412 AUPluginUI::cocoa_view_resized ()
414 GtkRequisition topsize = top_box.size_request ();
415 NSWindow* window = get_nswindow ();
416 NSSize oldContentSize= [window contentRectForFrameRect:[window frame]].size;
417 NSSize newContentSize= [au_view frame].size;
418 NSRect windowFrame= [window frame];
420 oldContentSize.height -= topsize.height;
422 float dy = oldContentSize.height - newContentSize.height;
423 float dx = oldContentSize.width - newContentSize.width;
425 windowFrame.origin.y += dy;
426 windowFrame.origin.x += dx;
427 windowFrame.size.height -= dy;
428 windowFrame.size.width -= dx;
430 [[NSNotificationCenter defaultCenter] removeObserver:_notify
431 name:NSViewFrameDidChangeNotification
434 NSUInteger old_auto_resize = [au_view autoresizingMask];
436 [au_view setAutoresizingMask:NSViewNotSizable];
437 [window setFrame:windowFrame display:1];
438 [au_view setAutoresizingMask:old_auto_resize];
440 [[NSNotificationCenter defaultCenter] addObserver:_notify
441 selector:@selector(auViewResized:) name:NSViewFrameDidChangeNotification
446 AUPluginUI::create_carbon_view ()
450 ControlRef root_control;
452 Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
454 OpenAComponent(editComponent, &editView);
456 error << _("AU Carbon view: cannot open AU Component") << endmsg;
460 Rect r = { 100, 100, 100, 100 };
461 WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
462 kWindowCompositingAttribute|
463 kWindowNoShadowAttribute|
464 kWindowNoTitleBarAttribute);
466 if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
467 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
468 CloseComponent (editView);
472 if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
473 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
474 DisposeWindow (carbon_window);
475 CloseComponent (editView);
480 Float32Point location = { 0.0, 0.0 };
481 Float32Point size = { 0.0, 0.0 } ;
483 if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
484 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
485 DisposeWindow (carbon_window);
486 CloseComponent (editView);
493 GetControlBounds(viewPane, &bounds);
494 size.x = bounds.right-bounds.left;
495 size.y = bounds.bottom-bounds.top;
497 prefwidth = (int) (size.x + 0.5);
498 prefheight = (int) (size.y + 0.5);
500 SizeWindow (carbon_window, prefwidth, prefheight, true);
501 low_box.set_size_request (prefwidth, prefheight);
505 error << _("AU Carbon GUI is not supported.") << endmsg;
511 AUPluginUI::get_nswindow ()
513 Gtk::Container* toplevel = get_toplevel();
515 if (!toplevel || !toplevel->is_toplevel()) {
516 error << _("AUPluginUI: no top level window!") << endmsg;
520 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
523 error << _("AUPluginUI: no top level window!") << endmsg;
531 AUPluginUI::activate ()
534 ActivateWindow (carbon_window, TRUE);
536 // [cocoa_parent makeKeyAndOrderFront:nil];
540 AUPluginUI::deactivate ()
543 ActivateWindow (carbon_window, FALSE);
548 AUPluginUI::parent_carbon_window ()
551 NSWindow* win = get_nswindow ();
552 Rect windowStructureBoundsRect;
558 Gtk::Container* toplevel = get_toplevel();
560 if (!toplevel || !toplevel->is_toplevel()) {
561 error << _("AUPluginUI: no top level window!") << endmsg;
565 /* figure out where the cocoa parent window is in carbon-coordinate space, which
566 differs from both cocoa-coordinate space and GTK-coordinate space
569 GetWindowBounds((WindowRef) [win windowRef], kWindowStructureRgn, &windowStructureBoundsRect);
571 /* compute how tall the title bar is, because we have to offset the position of the carbon window
575 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
576 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
578 int titlebar_height = wm_frame.size.height - content_frame.size.height;
580 int packing_extra = 6; // this is the total vertical packing in our top level window
582 /* move into position, based on parent window position */
583 MoveWindow (carbon_window,
584 windowStructureBoundsRect.left,
585 windowStructureBoundsRect.top + titlebar_height + top_box.get_height() + packing_extra,
587 ShowWindow (carbon_window);
589 // create the cocoa window for the carbon one and make it visible
590 cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
592 SetWindowActivationScope (carbon_window, kWindowActivationScopeNone);
594 _notify = [ [NotificationObject alloc] initWithPluginUI:this andCocoaParent:cocoa_parent andTopLevelParent:win ];
596 [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
605 AUPluginUI::parent_cocoa_window ()
607 NSWindow* win = get_nswindow ();
613 [win setAutodisplay:1]; // turn of GTK stuff for this window
615 Gtk::Container* toplevel = get_toplevel();
617 if (!toplevel || !toplevel->is_toplevel()) {
618 error << _("AUPluginUI: no top level window!") << endmsg;
622 NSView* view = gdk_quartz_window_get_nsview (get_toplevel()->get_window()->gobj());
623 GtkRequisition a = top_box.size_request ();
625 /* move the au_view down so that it doesn't overlap the top_box contents */
627 NSPoint origin = { 0, a.height };
629 [au_view setFrameOrigin:origin];
630 [view addSubview:au_view positioned:NSWindowBelow relativeTo:nil];
632 // watch for size changes of the view
634 _notify = [ [NotificationObject alloc] initWithPluginUI:this andCocoaParent:nil andTopLevelParent:win ];
636 [[NSNotificationCenter defaultCenter] addObserver:_notify
637 selector:@selector(auViewResized:) name:NSViewFrameDidChangeNotification
644 dump_view_tree (NSView* view, int depth)
646 NSArray* subviews = [view subviews];
647 unsigned long cnt = [subviews count];
649 for (int d = 0; d < depth; d++) {
652 cerr << " view @ " << view << endl;
654 for (unsigned long i = 0; i < cnt; ++i) {
655 NSView* subview = [subviews objectAtIndex:i];
656 dump_view_tree (subview, depth+1);
661 AUPluginUI::forward_key_event (GdkEventKey* ev)
663 NSEvent* nsevent = gdk_quartz_event_get_nsevent ((GdkEvent*)ev);
665 if (au_view && nsevent) {
667 /* filter on nsevent type here because GDK massages FlagsChanged
668 messages into GDK_KEY_{PRESS,RELEASE} but Cocoa won't
669 handle a FlagsChanged message as a keyDown or keyUp
672 if ([nsevent type] == NSKeyDown) {
673 [[[au_view window] firstResponder] keyDown:nsevent];
674 } else if ([nsevent type] == NSKeyUp) {
675 [[[au_view window] firstResponder] keyUp:nsevent];
676 } else if ([nsevent type] == NSFlagsChanged) {
677 [[[au_view window] firstResponder] flagsChanged:nsevent];
683 AUPluginUI::on_realize ()
687 /* our windows should not have that resize indicator */
689 NSWindow* win = get_nswindow ();
691 [win setShowsResizeIndicator:0];
696 AUPluginUI::lower_box_realized ()
699 parent_cocoa_window ();
700 } else if (carbon_window) {
701 parent_carbon_window ();
706 AUPluginUI::on_map_event (GdkEventAny*)
712 AUPluginUI::on_window_hide ()
716 HideWindow (carbon_window);
717 ActivateWindow (carbon_window, FALSE);
725 AUPluginUI::on_window_show (const string& /*title*/)
727 /* this is idempotent so just call it every time we show the window */
729 gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
735 ShowWindow (carbon_window);
736 ActivateWindow (carbon_window, TRUE);
744 AUPluginUI::start_updating (GdkEventAny*)
750 AUPluginUI::stop_updating (GdkEventAny*)
756 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
758 AUPluginUI* aup = new AUPluginUI (plugin_insert);
764 AUPluginUI::on_focus_in_event (GdkEventFocus*)
766 //cerr << "au plugin focus in\n";
767 //Keyboard::magic_widget_grab_focus ();
772 AUPluginUI::on_focus_out_event (GdkEventFocus*)
774 //cerr << "au plugin focus out\n";
775 //Keyboard::magic_widget_drop_focus ();