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;
62 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
78 - (void)cocoaParentActivationHandler:(NSNotification *)notification
80 NSWindow* notification_window = (NSWindow *)[notification object];
82 if (top_level_parent == notification_window || cocoa_parent == notification_window) {
83 if ([notification_window isMainWindow]) {
84 plugin_ui->activate();
86 plugin_ui->deactivate();
91 - (void)cocoaParentBecameKeyHandler:(NSNotification *)notification
93 NSWindow* notification_window = (NSWindow *)[notification object];
95 if (top_level_parent == notification_window || cocoa_parent == notification_window) {
96 if ([notification_window isKeyWindow]) {
97 plugin_ui->activate();
99 plugin_ui->deactivate();
104 - (void)auViewResized:(NSNotification *)notification;
107 plugin_ui->cocoa_view_resized();
112 AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
113 : PlugUIBase (insert)
114 , automation_mode_label (_("Automation"))
115 , preset_label (_("Presets"))
118 if (automation_mode_strings.empty()) {
119 automation_mode_strings = I18N (_automation_mode_strings);
122 set_popdown_strings (automation_mode_selector, automation_mode_strings);
123 automation_mode_selector.set_active_text (automation_mode_strings.front());
125 if ((au = boost::dynamic_pointer_cast<AUPlugin> (insert->plugin())) == 0) {
126 error << _("unknown type of editor-supplying plugin (note: no AudioUnit support in this version of ardour)") << endmsg;
127 throw failed_constructor ();
130 /* stuff some stuff into the top of the window */
132 HBox* smaller_hbox = manage (new HBox);
134 smaller_hbox->set_spacing (6);
135 smaller_hbox->pack_start (preset_label, false, false, 4);
136 smaller_hbox->pack_start (_preset_combo, false, false);
137 smaller_hbox->pack_start (save_button, false, false);
139 /* one day these might be useful with an AU plugin, but not yet */
140 smaller_hbox->pack_start (automation_mode_label, false, false);
141 smaller_hbox->pack_start (automation_mode_selector, false, false);
143 smaller_hbox->pack_start (bypass_button, false, true);
145 VBox* v1_box = manage (new VBox);
146 VBox* v2_box = manage (new VBox);
148 v1_box->pack_start (*smaller_hbox, false, true);
149 v2_box->pack_start (focus_button, false, true);
151 top_box.set_homogeneous (false);
152 top_box.set_spacing (6);
153 top_box.set_border_width (6);
155 top_box.pack_end (*v2_box, false, false);
156 top_box.pack_end (*v1_box, false, false);
159 pack_start (top_box, false, false);
160 pack_start (low_box, false, false);
162 preset_label.show ();
163 _preset_combo.show ();
164 automation_mode_label.show ();
165 automation_mode_selector.show ();
166 bypass_button.show ();
170 _activating_from_app = false;
178 /* prefer cocoa, fall back to cocoa, but use carbon if its there */
180 if (test_cocoa_view_support()) {
181 create_cocoa_view ();
183 } else if (test_carbon_view_support()) {
184 create_carbon_view ();
187 create_cocoa_view ();
190 low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
193 AUPluginUI::~AUPluginUI ()
196 NSWindow* win = get_nswindow();
197 [[NSNotificationCenter defaultCenter] removeObserver:_notify];
198 [win removeChildWindow:cocoa_parent];
204 /* not parented, just overlaid on top of our window */
205 DisposeWindow (carbon_window);
210 CloseComponent (editView);
214 /* remove whatever we packed into low_box so that GTK doesn't
218 [au_view removeFromSuperview];
223 AUPluginUI::test_carbon_view_support ()
227 carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
228 carbon_descriptor.componentSubType = 'gnrc';
229 carbon_descriptor.componentManufacturer = 'appl';
230 carbon_descriptor.componentFlags = 0;
231 carbon_descriptor.componentFlagsMask = 0;
235 // ask the AU for its first editor component
237 err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
239 int nEditors = propertySize / sizeof(ComponentDescription);
240 ComponentDescription *editors = new ComponentDescription[nEditors];
241 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
243 // just pick the first one for now
244 carbon_descriptor = editors[0];
254 AUPluginUI::test_cocoa_view_support ()
257 Boolean isWritable = 0;
258 OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
259 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
260 0, &dataSize, &isWritable);
262 return dataSize > 0 && err == noErr;
266 AUPluginUI::plugin_class_valid (Class pluginClass)
268 if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
269 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
270 [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
278 AUPluginUI::create_cocoa_view ()
280 BOOL wasAbleToLoadCustomView = NO;
281 AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
282 UInt32 numberOfClasses = 0;
285 NSString* factoryClassName = 0;
286 NSURL* CocoaViewBundlePath = NULL;
288 OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
289 kAudioUnitProperty_CocoaUI,
290 kAudioUnitScope_Global,
295 numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
297 // Does view have custom Cocoa UI?
299 if ((result == noErr) && (numberOfClasses > 0) ) {
301 DEBUG_TRACE(DEBUG::AudioUnits,
302 string_compose ( "based on %1, there are %2 cocoa UI classes\n", dataSize, numberOfClasses));
304 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
305 if(AudioUnitGetProperty(*au->get_au(),
306 kAudioUnitProperty_CocoaUI,
307 kAudioUnitScope_Global,
310 &dataSize) == noErr) {
312 CocoaViewBundlePath = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
314 // we only take the first view in this example.
315 factoryClassName = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
317 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("the factory name is %1 bundle is %2\n",
318 factoryClassName, CocoaViewBundlePath));
322 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("No cocoaUI property cocoaViewInfo = %1\n", cocoaViewInfo));
324 if (cocoaViewInfo != NULL) {
325 free (cocoaViewInfo);
326 cocoaViewInfo = NULL;
331 NSRect crect = { { 0, 0 }, { 1, 1} };
333 // [A] Show custom UI if view has it
335 if (CocoaViewBundlePath && factoryClassName) {
336 NSBundle *viewBundle = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
338 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("tried to create bundle, result = %1\n", viewBundle));
340 if (viewBundle == nil) {
341 error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
344 Class factoryClass = [viewBundle classNamed:factoryClassName];
345 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("tried to create factory class, result = %1\n", factoryClass));
347 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
351 // make sure 'factoryClass' implements the AUCocoaUIBase protocol
352 if (!plugin_class_valid (factoryClass)) {
353 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
357 id factoryInstance = [[[factoryClass alloc] init] autorelease];
358 if (factoryInstance == nil) {
359 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
363 DEBUG_TRACE (DEBUG::AudioUnits, "got a factory instance\n");
366 au_view = [factoryInstance uiViewForAudioUnit:*au->get_au() withSize:crect.size];
368 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("view created @ %1\n", au_view));
371 [CocoaViewBundlePath release];
374 for (i = 0; i < numberOfClasses; i++)
375 CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
377 free (cocoaViewInfo);
379 wasAbleToLoadCustomView = YES;
383 if (!wasAbleToLoadCustomView) {
384 // load generic Cocoa view
385 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("Loading generic view using %1 -> %2\n", au,
387 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
388 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("view created @ %1\n", au_view));
389 [(AUGenericView *)au_view setShowsExpertParameters:YES];
392 // watch for size changes of the view
394 [[NSNotificationCenter defaultCenter] addObserver:_notify
395 selector:@selector(auViewResized:) name:NSWindowDidResizeNotification
398 // Get the size of the new AU View's frame
401 packFrame = [au_view frame];
402 prefwidth = packFrame.size.width;
403 prefheight = packFrame.size.height;
404 low_box.set_size_request (prefwidth, prefheight);
410 AUPluginUI::cocoa_view_resized ()
412 NSRect packFrame = [au_view frame];
416 AUPluginUI::create_carbon_view ()
420 ControlRef root_control;
422 Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
424 OpenAComponent(editComponent, &editView);
426 error << _("AU Carbon view: cannot open AU Component") << endmsg;
430 Rect r = { 100, 100, 100, 100 };
431 WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
432 kWindowCompositingAttribute|
433 kWindowNoShadowAttribute|
434 kWindowNoTitleBarAttribute);
436 if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
437 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
438 CloseComponent (editView);
442 if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
443 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
444 DisposeWindow (carbon_window);
445 CloseComponent (editView);
450 Float32Point location = { 0.0, 0.0 };
451 Float32Point size = { 0.0, 0.0 } ;
453 if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
454 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
455 DisposeWindow (carbon_window);
456 CloseComponent (editView);
463 GetControlBounds(viewPane, &bounds);
464 size.x = bounds.right-bounds.left;
465 size.y = bounds.bottom-bounds.top;
467 prefwidth = (int) (size.x + 0.5);
468 prefheight = (int) (size.y + 0.5);
470 SizeWindow (carbon_window, prefwidth, prefheight, true);
471 low_box.set_size_request (prefwidth, prefheight);
475 error << _("AU Carbon GUI is not supported.") << endmsg;
481 AUPluginUI::get_nswindow ()
483 Gtk::Container* toplevel = get_toplevel();
485 if (!toplevel || !toplevel->is_toplevel()) {
486 error << _("AUPluginUI: no top level window!") << endmsg;
490 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
493 error << _("AUPluginUI: no top level window!") << endmsg;
501 AUPluginUI::activate ()
504 ActivateWindow (carbon_window, TRUE);
506 // [cocoa_parent makeKeyAndOrderFront:nil];
510 AUPluginUI::deactivate ()
513 ActivateWindow (carbon_window, FALSE);
518 AUPluginUI::parent_carbon_window ()
521 NSWindow* win = get_nswindow ();
528 Gtk::Container* toplevel = get_toplevel();
530 if (!toplevel || !toplevel->is_toplevel()) {
531 error << _("AUPluginUI: no top level window!") << endmsg;
535 toplevel->get_window()->get_root_origin (x, y);
537 /* compute how tall the title bar is, because we have to offset the position of the carbon window
541 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
542 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
544 int titlebar_height = wm_frame.size.height - content_frame.size.height;
546 int packing_extra = 6; // this is the total vertical packing in our top level window
548 MoveWindow (carbon_window, x, y + titlebar_height + top_box.get_height() + packing_extra, false);
549 ShowWindow (carbon_window);
551 // create the cocoa window for the carbon one and make it visible
552 cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
554 SetWindowActivationScope (carbon_window, kWindowActivationScopeNone);
556 _notify = [ [NotificationObject alloc] initWithPluginUI:this andCocoaParent:cocoa_parent andTopLevelParent:win ];
558 [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
567 AUPluginUI::parent_cocoa_window ()
569 NSWindow* win = get_nswindow ();
575 [win setAutodisplay:YES]; // turn of GTK stuff for this window
577 Gtk::Container* toplevel = get_toplevel();
579 if (!toplevel || !toplevel->is_toplevel()) {
580 error << _("AUPluginUI: no top level window!") << endmsg;
584 NSView* view = gdk_quartz_window_get_nsview (get_toplevel()->get_window()->gobj());
585 GtkRequisition a = top_box.size_request ();
587 /* move the au_view down so that it doesn't overlap the top_box contents */
589 NSPoint origin = { 0, a.height };
591 [au_view setFrameOrigin:origin];
592 [view addSubview:au_view];
598 dump_view_tree (NSView* view, int depth)
600 NSArray* subviews = [view subviews];
601 unsigned long cnt = [subviews count];
603 for (int d = 0; d < depth; d++) {
606 cerr << " view @ " << view << endl;
608 for (unsigned long i = 0; i < cnt; ++i) {
609 NSView* subview = [subviews objectAtIndex:i];
610 dump_view_tree (subview, depth+1);
615 AUPluginUI::forward_key_event (GdkEventKey* ev)
617 NSEvent* nsevent = gdk_quartz_event_get_nsevent ((GdkEvent*)ev);
619 if (au_view && nsevent) {
621 /* filter on nsevent type here because GDK massages FlagsChanged
622 messages into GDK_KEY_{PRESS,RELEASE} but Cocoa won't
623 handle a FlagsChanged message as a keyDown or keyUp
626 if ([nsevent type] == NSKeyDown) {
627 [[[au_view window] firstResponder] keyDown:nsevent];
628 } else if ([nsevent type] == NSKeyUp) {
629 [[[au_view window] firstResponder] keyUp:nsevent];
630 } else if ([nsevent type] == NSFlagsChanged) {
631 [[[au_view window] firstResponder] flagsChanged:nsevent];
637 AUPluginUI::on_realize ()
641 /* our windows should not have that resize indicator */
643 NSWindow* win = get_nswindow ();
645 [win setShowsResizeIndicator:NO];
650 AUPluginUI::lower_box_realized ()
653 parent_cocoa_window ();
654 } else if (carbon_window) {
655 parent_carbon_window ();
660 AUPluginUI::on_map_event (GdkEventAny*)
666 AUPluginUI::on_window_hide ()
670 HideWindow (carbon_window);
671 ActivateWindow (carbon_window, FALSE);
679 AUPluginUI::on_window_show (const string& /*title*/)
681 /* this is idempotent so just call it every time we show the window */
683 gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
689 ShowWindow (carbon_window);
690 ActivateWindow (carbon_window, TRUE);
698 AUPluginUI::start_updating (GdkEventAny*)
704 AUPluginUI::stop_updating (GdkEventAny*)
710 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
712 AUPluginUI* aup = new AUPluginUI (plugin_insert);
718 AUPluginUI::on_focus_in_event (GdkEventFocus*)
720 //cerr << "au plugin focus in\n";
721 //Keyboard::magic_widget_grab_focus ();
726 AUPluginUI::on_focus_out_event (GdkEventFocus*)
728 //cerr << "au plugin focus out\n";
729 //Keyboard::magic_widget_drop_focus ();