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:NSViewBoundsDidChangeNotification
399 [[NSNotificationCenter defaultCenter] addObserver:_notify
400 selector:@selector(auViewResized:) name:NSViewFrameDidChangeNotification
403 // Get the size of the new AU View's frame
406 packFrame = [au_view frame];
407 prefwidth = packFrame.size.width;
408 prefheight = packFrame.size.height;
409 low_box.set_size_request (prefwidth, prefheight);
415 AUPluginUI::cocoa_view_resized ()
417 NSRect packFrame = [au_view frame];
418 prefwidth = packFrame.size.width;
419 prefheight = packFrame.size.height;
420 low_box.set_size_request (prefwidth, prefheight);
424 AUPluginUI::create_carbon_view ()
428 ControlRef root_control;
430 Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
432 OpenAComponent(editComponent, &editView);
434 error << _("AU Carbon view: cannot open AU Component") << endmsg;
438 Rect r = { 100, 100, 100, 100 };
439 WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
440 kWindowCompositingAttribute|
441 kWindowNoShadowAttribute|
442 kWindowNoTitleBarAttribute);
444 if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
445 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
446 CloseComponent (editView);
450 if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
451 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
452 DisposeWindow (carbon_window);
453 CloseComponent (editView);
458 Float32Point location = { 0.0, 0.0 };
459 Float32Point size = { 0.0, 0.0 } ;
461 if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
462 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
463 DisposeWindow (carbon_window);
464 CloseComponent (editView);
471 GetControlBounds(viewPane, &bounds);
472 size.x = bounds.right-bounds.left;
473 size.y = bounds.bottom-bounds.top;
475 prefwidth = (int) (size.x + 0.5);
476 prefheight = (int) (size.y + 0.5);
478 SizeWindow (carbon_window, prefwidth, prefheight, true);
479 low_box.set_size_request (prefwidth, prefheight);
483 error << _("AU Carbon GUI is not supported.") << endmsg;
489 AUPluginUI::get_nswindow ()
491 Gtk::Container* toplevel = get_toplevel();
493 if (!toplevel || !toplevel->is_toplevel()) {
494 error << _("AUPluginUI: no top level window!") << endmsg;
498 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
501 error << _("AUPluginUI: no top level window!") << endmsg;
509 AUPluginUI::activate ()
512 ActivateWindow (carbon_window, TRUE);
514 // [cocoa_parent makeKeyAndOrderFront:nil];
518 AUPluginUI::deactivate ()
521 ActivateWindow (carbon_window, FALSE);
526 AUPluginUI::parent_carbon_window ()
529 NSWindow* win = get_nswindow ();
536 Gtk::Container* toplevel = get_toplevel();
538 if (!toplevel || !toplevel->is_toplevel()) {
539 error << _("AUPluginUI: no top level window!") << endmsg;
543 toplevel->get_window()->get_root_origin (x, y);
545 /* compute how tall the title bar is, because we have to offset the position of the carbon window
549 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
550 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
552 int titlebar_height = wm_frame.size.height - content_frame.size.height;
554 int packing_extra = 6; // this is the total vertical packing in our top level window
556 MoveWindow (carbon_window, x, y + titlebar_height + top_box.get_height() + packing_extra, false);
557 ShowWindow (carbon_window);
559 // create the cocoa window for the carbon one and make it visible
560 cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
562 SetWindowActivationScope (carbon_window, kWindowActivationScopeNone);
564 _notify = [ [NotificationObject alloc] initWithPluginUI:this andCocoaParent:cocoa_parent andTopLevelParent:win ];
566 [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
575 AUPluginUI::parent_cocoa_window ()
577 NSWindow* win = get_nswindow ();
583 [win setAutodisplay:YES]; // turn of GTK stuff for this window
585 Gtk::Container* toplevel = get_toplevel();
587 if (!toplevel || !toplevel->is_toplevel()) {
588 error << _("AUPluginUI: no top level window!") << endmsg;
592 NSView* view = gdk_quartz_window_get_nsview (get_toplevel()->get_window()->gobj());
593 GtkRequisition a = top_box.size_request ();
595 /* move the au_view down so that it doesn't overlap the top_box contents */
597 NSPoint origin = { 0, a.height };
599 [au_view setFrameOrigin:origin];
600 [view addSubview:au_view];
606 dump_view_tree (NSView* view, int depth)
608 NSArray* subviews = [view subviews];
609 unsigned long cnt = [subviews count];
611 for (int d = 0; d < depth; d++) {
614 cerr << " view @ " << view << endl;
616 for (unsigned long i = 0; i < cnt; ++i) {
617 NSView* subview = [subviews objectAtIndex:i];
618 dump_view_tree (subview, depth+1);
623 AUPluginUI::forward_key_event (GdkEventKey* ev)
625 NSEvent* nsevent = gdk_quartz_event_get_nsevent ((GdkEvent*)ev);
627 if (au_view && nsevent) {
629 /* filter on nsevent type here because GDK massages FlagsChanged
630 messages into GDK_KEY_{PRESS,RELEASE} but Cocoa won't
631 handle a FlagsChanged message as a keyDown or keyUp
634 if ([nsevent type] == NSKeyDown) {
635 [[[au_view window] firstResponder] keyDown:nsevent];
636 } else if ([nsevent type] == NSKeyUp) {
637 [[[au_view window] firstResponder] keyUp:nsevent];
638 } else if ([nsevent type] == NSFlagsChanged) {
639 [[[au_view window] firstResponder] flagsChanged:nsevent];
645 AUPluginUI::on_realize ()
649 /* our windows should not have that resize indicator */
651 NSWindow* win = get_nswindow ();
653 [win setShowsResizeIndicator:NO];
658 AUPluginUI::lower_box_realized ()
661 parent_cocoa_window ();
662 } else if (carbon_window) {
663 parent_carbon_window ();
668 AUPluginUI::on_map_event (GdkEventAny*)
674 AUPluginUI::on_window_hide ()
678 HideWindow (carbon_window);
679 ActivateWindow (carbon_window, FALSE);
687 AUPluginUI::on_window_show (const string& /*title*/)
689 /* this is idempotent so just call it every time we show the window */
691 gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
697 ShowWindow (carbon_window);
698 ActivateWindow (carbon_window, TRUE);
706 AUPluginUI::start_updating (GdkEventAny*)
712 AUPluginUI::stop_updating (GdkEventAny*)
718 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
720 AUPluginUI* aup = new AUPluginUI (plugin_insert);
726 AUPluginUI::on_focus_in_event (GdkEventFocus*)
728 //cerr << "au plugin focus in\n";
729 //Keyboard::magic_widget_grab_focus ();
734 AUPluginUI::on_focus_out_event (GdkEventFocus*)
736 //cerr << "au plugin focus out\n";
737 //Keyboard::magic_widget_drop_focus ();