1 #include <gtkmm/stock.h>
4 #define Marker FuckYouAppleAndYourLackOfNameSpaces
6 #include "pbd/convert.h"
9 #include "ardour/audio_unit.h"
10 #include "ardour/debug.h"
11 #include "ardour/plugin_insert.h"
13 #undef check // stupid gtk, stupid apple
15 #include <gtkmm/button.h>
16 #include <gdk/gdkquartz.h>
18 #include <gtkmm2ext/utils.h>
20 #include "au_pluginui.h"
21 #include "gui_thread.h"
23 #include "appleutility/CAAudioUnit.h"
24 #include "appleutility/CAComponent.h"
26 #import <AudioUnit/AUCocoaUIView.h>
27 #import <CoreAudioKit/AUGenericView.h>
33 #include "public_editor.h"
36 using namespace ARDOUR;
38 using namespace Gtkmm2ext;
43 vector<string> AUPluginUI::automation_mode_strings;
45 static const gchar* _automation_mode_strings[] = {
53 @implementation NotificationObject
55 - (NotificationObject*) initWithPluginUI: (AUPluginUI*) apluginui andCocoaParent: (NSWindow*) cp andTopLevelParent: (NSWindow*) tlp
57 self = [ super init ];
60 plugin_ui = apluginui;
61 top_level_parent = tlp;
66 [[NSNotificationCenter defaultCenter] addObserver:self
67 selector:@selector(cocoaParentActivationHandler:)
68 name:NSWindowDidBecomeMainNotification
71 [[NSNotificationCenter defaultCenter] addObserver:self
72 selector:@selector(cocoaParentBecameKeyHandler:)
73 name:NSWindowDidBecomeKeyNotification
81 - (void)cocoaParentActivationHandler:(NSNotification *)notification
83 NSWindow* notification_window = (NSWindow *)[notification object];
85 if (top_level_parent == notification_window || cocoa_parent == notification_window) {
86 if ([notification_window isMainWindow]) {
87 plugin_ui->activate();
89 plugin_ui->deactivate();
94 - (void)cocoaParentBecameKeyHandler:(NSNotification *)notification
96 NSWindow* notification_window = (NSWindow *)[notification object];
98 if (top_level_parent == notification_window || cocoa_parent == notification_window) {
99 if ([notification_window isKeyWindow]) {
100 plugin_ui->activate();
102 plugin_ui->deactivate();
107 - (void)auViewResized:(NSNotification *)notification;
109 (void) notification; // stop complaints about unusued argument
110 plugin_ui->cocoa_view_resized();
115 AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
116 : PlugUIBase (insert)
117 , automation_mode_label (_("Automation"))
118 , preset_label (_("Presets"))
121 if (automation_mode_strings.empty()) {
122 automation_mode_strings = I18N (_automation_mode_strings);
125 set_popdown_strings (automation_mode_selector, automation_mode_strings);
126 automation_mode_selector.set_active_text (automation_mode_strings.front());
128 if ((au = boost::dynamic_pointer_cast<AUPlugin> (insert->plugin())) == 0) {
129 error << _("unknown type of editor-supplying plugin (note: no AudioUnit support in this version of ardour)") << endmsg;
130 throw failed_constructor ();
133 /* stuff some stuff into the top of the window */
135 HBox* smaller_hbox = manage (new HBox);
137 smaller_hbox->set_spacing (6);
138 smaller_hbox->pack_start (preset_label, false, false, 4);
139 smaller_hbox->pack_start (_preset_combo, false, false);
140 smaller_hbox->pack_start (save_button, false, false);
142 /* one day these might be useful with an AU plugin, but not yet */
143 smaller_hbox->pack_start (automation_mode_label, false, false);
144 smaller_hbox->pack_start (automation_mode_selector, false, false);
146 smaller_hbox->pack_start (bypass_button, false, true);
148 VBox* v1_box = manage (new VBox);
149 VBox* v2_box = manage (new VBox);
151 v1_box->pack_start (*smaller_hbox, false, true);
152 v2_box->pack_start (focus_button, false, true);
154 top_box.set_homogeneous (false);
155 top_box.set_spacing (6);
156 top_box.set_border_width (6);
158 top_box.pack_end (*v2_box, false, false);
159 top_box.pack_end (*v1_box, false, false);
162 pack_start (top_box, false, false);
163 pack_start (low_box, false, false);
165 preset_label.show ();
166 _preset_combo.show ();
167 automation_mode_label.show ();
168 automation_mode_selector.show ();
169 bypass_button.show ();
173 _activating_from_app = false;
181 /* prefer cocoa, fall back to cocoa, but use carbon if its there */
183 if (test_cocoa_view_support()) {
184 create_cocoa_view ();
186 } else if (test_carbon_view_support()) {
187 create_carbon_view ();
190 create_cocoa_view ();
193 low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
196 AUPluginUI::~AUPluginUI ()
199 [[NSNotificationCenter defaultCenter] removeObserver:_notify];
203 NSWindow* win = get_nswindow();
204 [win removeChildWindow:cocoa_parent];
209 /* not parented, just overlaid on top of our window */
210 DisposeWindow (carbon_window);
215 CloseComponent (editView);
219 /* remove whatever we packed into low_box so that GTK doesn't
223 [au_view removeFromSuperview];
228 AUPluginUI::test_carbon_view_support ()
232 carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
233 carbon_descriptor.componentSubType = 'gnrc';
234 carbon_descriptor.componentManufacturer = 'appl';
235 carbon_descriptor.componentFlags = 0;
236 carbon_descriptor.componentFlagsMask = 0;
240 // ask the AU for its first editor component
242 err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
244 int nEditors = propertySize / sizeof(ComponentDescription);
245 ComponentDescription *editors = new ComponentDescription[nEditors];
246 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
248 // just pick the first one for now
249 carbon_descriptor = editors[0];
259 AUPluginUI::test_cocoa_view_support ()
262 Boolean isWritable = 0;
263 OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
264 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
265 0, &dataSize, &isWritable);
267 return dataSize > 0 && err == noErr;
271 AUPluginUI::plugin_class_valid (Class pluginClass)
273 if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
274 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
275 [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
283 AUPluginUI::create_cocoa_view ()
285 BOOL wasAbleToLoadCustomView = NO;
286 AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
287 UInt32 numberOfClasses = 0;
290 NSString* factoryClassName = 0;
291 NSURL* CocoaViewBundlePath = NULL;
293 OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
294 kAudioUnitProperty_CocoaUI,
295 kAudioUnitScope_Global,
300 numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
302 // Does view have custom Cocoa UI?
304 if ((result == noErr) && (numberOfClasses > 0) ) {
306 DEBUG_TRACE(DEBUG::AudioUnits,
307 string_compose ( "based on %1, there are %2 cocoa UI classes\n", dataSize, numberOfClasses));
309 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
311 if(AudioUnitGetProperty(*au->get_au(),
312 kAudioUnitProperty_CocoaUI,
313 kAudioUnitScope_Global,
316 &dataSize) == noErr) {
318 CocoaViewBundlePath = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
320 // we only take the first view in this example.
321 factoryClassName = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
323 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("the factory name is %1 bundle is %2\n",
324 [factoryClassName UTF8String], CocoaViewBundlePath));
328 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("No cocoaUI property cocoaViewInfo = %1\n", cocoaViewInfo));
330 if (cocoaViewInfo != NULL) {
331 free (cocoaViewInfo);
332 cocoaViewInfo = NULL;
337 // [A] Show custom UI if view has it
339 if (CocoaViewBundlePath && factoryClassName) {
340 NSBundle *viewBundle = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
342 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("tried to create bundle, result = %1\n", viewBundle));
344 if (viewBundle == nil) {
345 error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
348 Class factoryClass = [viewBundle classNamed:factoryClassName];
349 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("tried to create factory class, result = %1\n", factoryClass));
351 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
355 // make sure 'factoryClass' implements the AUCocoaUIBase protocol
356 if (!plugin_class_valid (factoryClass)) {
357 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
361 id factory = [[[factoryClass alloc] init] autorelease];
362 if (factory == nil) {
363 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
367 DEBUG_TRACE (DEBUG::AudioUnits, "got a factory instance\n");
370 au_view = [factory uiViewForAudioUnit:*au->get_au() withSize:NSZeroSize];
372 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("view created @ %1\n", au_view));
375 [CocoaViewBundlePath release];
378 for (i = 0; i < numberOfClasses; i++)
379 CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
381 free (cocoaViewInfo);
383 wasAbleToLoadCustomView = YES;
387 if (!wasAbleToLoadCustomView) {
388 // load generic Cocoa view
389 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("Loading generic view using %1 -> %2\n", au,
391 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
392 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("view created @ %1\n", au_view));
393 [(AUGenericView *)au_view setShowsExpertParameters:YES];
396 // Get the initial size of the new AU View's frame
398 NSRect rect = [au_view frame];
399 low_box.set_size_request (rect.size.width, rect.size.height);
405 AUPluginUI::cocoa_view_resized ()
407 GtkRequisition topsize = top_box.size_request ();
408 NSWindow* window = get_nswindow ();
409 NSSize oldContentSize= [window contentRectForFrameRect:[window frame]].size;
410 NSSize newContentSize= [au_view frame].size;
411 NSRect windowFrame= [window frame];
413 oldContentSize.height -= topsize.height;
415 float dy = oldContentSize.height - newContentSize.height;
416 float dx = oldContentSize.width - newContentSize.width;
418 windowFrame.origin.y += dy;
419 windowFrame.origin.x += dx;
420 windowFrame.size.height -= dy;
421 windowFrame.size.width -= dx;
423 [[NSNotificationCenter defaultCenter] removeObserver:_notify
424 name:NSViewFrameDidChangeNotification
427 NSUInteger old_auto_resize = [au_view autoresizingMask];
429 [au_view setAutoresizingMask:NSViewNotSizable];
430 [window setFrame:windowFrame display:YES];
431 [au_view setAutoresizingMask:old_auto_resize];
433 [[NSNotificationCenter defaultCenter] addObserver:_notify
434 selector:@selector(auViewResized:) name:NSViewFrameDidChangeNotification
439 AUPluginUI::create_carbon_view ()
443 ControlRef root_control;
445 Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
447 OpenAComponent(editComponent, &editView);
449 error << _("AU Carbon view: cannot open AU Component") << endmsg;
453 Rect r = { 100, 100, 100, 100 };
454 WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
455 kWindowCompositingAttribute|
456 kWindowNoShadowAttribute|
457 kWindowNoTitleBarAttribute);
459 if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
460 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
461 CloseComponent (editView);
465 if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
466 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
467 DisposeWindow (carbon_window);
468 CloseComponent (editView);
473 Float32Point location = { 0.0, 0.0 };
474 Float32Point size = { 0.0, 0.0 } ;
476 if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
477 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
478 DisposeWindow (carbon_window);
479 CloseComponent (editView);
486 GetControlBounds(viewPane, &bounds);
487 size.x = bounds.right-bounds.left;
488 size.y = bounds.bottom-bounds.top;
490 prefwidth = (int) (size.x + 0.5);
491 prefheight = (int) (size.y + 0.5);
493 SizeWindow (carbon_window, prefwidth, prefheight, true);
494 low_box.set_size_request (prefwidth, prefheight);
498 error << _("AU Carbon GUI is not supported.") << endmsg;
504 AUPluginUI::get_nswindow ()
506 Gtk::Container* toplevel = get_toplevel();
508 if (!toplevel || !toplevel->is_toplevel()) {
509 error << _("AUPluginUI: no top level window!") << endmsg;
513 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
516 error << _("AUPluginUI: no top level window!") << endmsg;
524 AUPluginUI::activate ()
527 ActivateWindow (carbon_window, TRUE);
529 // [cocoa_parent makeKeyAndOrderFront:nil];
533 AUPluginUI::deactivate ()
536 ActivateWindow (carbon_window, FALSE);
541 AUPluginUI::parent_carbon_window ()
544 NSWindow* win = get_nswindow ();
545 Rect windowStructureBoundsRect;
551 Gtk::Container* toplevel = get_toplevel();
553 if (!toplevel || !toplevel->is_toplevel()) {
554 error << _("AUPluginUI: no top level window!") << endmsg;
558 /* figure out where the cocoa parent window is in carbon-coordinate space, which
559 differs from both cocoa-coordinate space and GTK-coordinate space
562 GetWindowBounds((WindowRef) [win windowRef], kWindowStructureRgn, &windowStructureBoundsRect);
564 /* compute how tall the title bar is, because we have to offset the position of the carbon window
568 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
569 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
571 int titlebar_height = wm_frame.size.height - content_frame.size.height;
573 int packing_extra = 6; // this is the total vertical packing in our top level window
575 /* move into position, based on parent window position */
576 MoveWindow (carbon_window,
577 windowStructureBoundsRect.left,
578 windowStructureBoundsRect.top + titlebar_height + top_box.get_height() + packing_extra,
580 ShowWindow (carbon_window);
582 // create the cocoa window for the carbon one and make it visible
583 cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
585 SetWindowActivationScope (carbon_window, kWindowActivationScopeNone);
587 _notify = [ [NotificationObject alloc] initWithPluginUI:this andCocoaParent:cocoa_parent andTopLevelParent:win ];
589 [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
598 AUPluginUI::parent_cocoa_window ()
600 NSWindow* win = get_nswindow ();
606 [win setAutodisplay:YES]; // turn of GTK stuff for this window
608 Gtk::Container* toplevel = get_toplevel();
610 if (!toplevel || !toplevel->is_toplevel()) {
611 error << _("AUPluginUI: no top level window!") << endmsg;
615 NSView* view = gdk_quartz_window_get_nsview (get_toplevel()->get_window()->gobj());
616 GtkRequisition a = top_box.size_request ();
618 /* move the au_view down so that it doesn't overlap the top_box contents */
620 NSPoint origin = { 0, a.height };
622 [au_view setFrameOrigin:origin];
623 [view addSubview:au_view positioned:NSWindowBelow relativeTo:nil];
625 // watch for size changes of the view
627 _notify = [ [NotificationObject alloc] initWithPluginUI:this andCocoaParent:nil andTopLevelParent:win ];
629 [[NSNotificationCenter defaultCenter] addObserver:_notify
630 selector:@selector(auViewResized:) name:NSViewFrameDidChangeNotification
637 dump_view_tree (NSView* view, int depth)
639 NSArray* subviews = [view subviews];
640 unsigned long cnt = [subviews count];
642 for (int d = 0; d < depth; d++) {
645 cerr << " view @ " << view << endl;
647 for (unsigned long i = 0; i < cnt; ++i) {
648 NSView* subview = [subviews objectAtIndex:i];
649 dump_view_tree (subview, depth+1);
654 AUPluginUI::forward_key_event (GdkEventKey* ev)
656 NSEvent* nsevent = gdk_quartz_event_get_nsevent ((GdkEvent*)ev);
658 if (au_view && nsevent) {
660 /* filter on nsevent type here because GDK massages FlagsChanged
661 messages into GDK_KEY_{PRESS,RELEASE} but Cocoa won't
662 handle a FlagsChanged message as a keyDown or keyUp
665 if ([nsevent type] == NSKeyDown) {
666 [[[au_view window] firstResponder] keyDown:nsevent];
667 } else if ([nsevent type] == NSKeyUp) {
668 [[[au_view window] firstResponder] keyUp:nsevent];
669 } else if ([nsevent type] == NSFlagsChanged) {
670 [[[au_view window] firstResponder] flagsChanged:nsevent];
676 AUPluginUI::on_realize ()
680 /* our windows should not have that resize indicator */
682 NSWindow* win = get_nswindow ();
684 [win setShowsResizeIndicator:NO];
689 AUPluginUI::lower_box_realized ()
692 parent_cocoa_window ();
693 } else if (carbon_window) {
694 parent_carbon_window ();
699 AUPluginUI::on_map_event (GdkEventAny*)
705 AUPluginUI::on_window_hide ()
709 HideWindow (carbon_window);
710 ActivateWindow (carbon_window, FALSE);
718 AUPluginUI::on_window_show (const string& /*title*/)
720 /* this is idempotent so just call it every time we show the window */
722 gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
728 ShowWindow (carbon_window);
729 ActivateWindow (carbon_window, TRUE);
737 AUPluginUI::start_updating (GdkEventAny*)
743 AUPluginUI::stop_updating (GdkEventAny*)
749 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
751 AUPluginUI* aup = new AUPluginUI (plugin_insert);
757 AUPluginUI::on_focus_in_event (GdkEventFocus*)
759 //cerr << "au plugin focus in\n";
760 //Keyboard::magic_widget_grab_focus ();
765 AUPluginUI::on_focus_out_event (GdkEventFocus*)
767 //cerr << "au plugin focus out\n";
768 //Keyboard::magic_widget_drop_focus ();