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_box, 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;
177 /* prefer cocoa, fall back to cocoa, but use carbon if its there */
179 if (test_cocoa_view_support()) {
180 create_cocoa_view ();
181 } else if (test_carbon_view_support()) {
182 create_carbon_view ();
184 create_cocoa_view ();
187 low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
190 AUPluginUI::~AUPluginUI ()
193 NSWindow* win = get_nswindow();
194 [[NSNotificationCenter defaultCenter] removeObserver:_notify];
195 [win removeChildWindow:cocoa_parent];
200 /* not parented, just overlaid on top of our window */
201 DisposeWindow (carbon_window);
205 CloseComponent (editView);
209 /* remove whatever we packed into low_box so that GTK doesn't
213 [au_view removeFromSuperview];
218 AUPluginUI::test_carbon_view_support ()
222 carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
223 carbon_descriptor.componentSubType = 'gnrc';
224 carbon_descriptor.componentManufacturer = 'appl';
225 carbon_descriptor.componentFlags = 0;
226 carbon_descriptor.componentFlagsMask = 0;
230 // ask the AU for its first editor component
232 err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
234 int nEditors = propertySize / sizeof(ComponentDescription);
235 ComponentDescription *editors = new ComponentDescription[nEditors];
236 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
238 // just pick the first one for now
239 carbon_descriptor = editors[0];
249 AUPluginUI::test_cocoa_view_support ()
252 Boolean isWritable = 0;
253 OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
254 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
255 0, &dataSize, &isWritable);
257 return dataSize > 0 && err == noErr;
261 AUPluginUI::plugin_class_valid (Class pluginClass)
263 if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
264 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
265 [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
273 AUPluginUI::create_cocoa_view ()
275 BOOL wasAbleToLoadCustomView = NO;
276 AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
277 UInt32 numberOfClasses = 0;
280 NSString* factoryClassName = 0;
281 NSURL* CocoaViewBundlePath = NULL;
283 OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
284 kAudioUnitProperty_CocoaUI,
285 kAudioUnitScope_Global,
290 numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
292 // Does view have custom Cocoa UI?
294 if ((result == noErr) && (numberOfClasses > 0) ) {
296 DEBUG_TRACE(DEBUG::AudioUnits,
297 string_compose ( "based on %1, there are %2 cocoa UI classes\n", dataSize, numberOfClasses));
299 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
300 if(AudioUnitGetProperty(*au->get_au(),
301 kAudioUnitProperty_CocoaUI,
302 kAudioUnitScope_Global,
305 &dataSize) == noErr) {
307 CocoaViewBundlePath = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
309 // we only take the first view in this example.
310 factoryClassName = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
312 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("the factory name is %1 bundle is %2\n",
313 factoryClassName, CocoaViewBundlePath));
317 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("No cocoaUI property cocoaViewInfo = %1\n", cocoaViewInfo));
319 if (cocoaViewInfo != NULL) {
320 free (cocoaViewInfo);
321 cocoaViewInfo = NULL;
326 NSRect crect = { { 0, 0 }, { 1, 1} };
328 // [A] Show custom UI if view has it
330 if (CocoaViewBundlePath && factoryClassName) {
331 NSBundle *viewBundle = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
333 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("tried to create bundle, result = %1\n", viewBundle));
335 if (viewBundle == nil) {
336 error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
339 Class factoryClass = [viewBundle classNamed:factoryClassName];
340 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("tried to create factory class, result = %1\n", factoryClass));
342 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
346 // make sure 'factoryClass' implements the AUCocoaUIBase protocol
347 if (!plugin_class_valid (factoryClass)) {
348 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
352 id factoryInstance = [[[factoryClass alloc] init] autorelease];
353 if (factoryInstance == nil) {
354 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
358 DEBUG_TRACE (DEBUG::AudioUnits, "got a factory instance\n");
361 au_view = [factoryInstance uiViewForAudioUnit:*au->get_au() withSize:crect.size];
363 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("view created @ %1\n", au_view));
366 [CocoaViewBundlePath release];
369 for (i = 0; i < numberOfClasses; i++)
370 CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
372 free (cocoaViewInfo);
374 wasAbleToLoadCustomView = YES;
378 if (!wasAbleToLoadCustomView) {
379 // load generic Cocoa view
380 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("Loading generic view using %1 -> %2\n", au,
382 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
383 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("view created @ %1\n", au_view));
384 [(AUGenericView *)au_view setShowsExpertParameters:YES];
387 // watch for size changes of the view
389 [[NSNotificationCenter defaultCenter] addObserver:_notify
390 selector:@selector(auViewResized:) name:NSWindowDidResizeNotification
393 // Get the size of the new AU View's frame
396 packFrame = [au_view frame];
397 prefwidth = packFrame.size.width;
398 prefheight = packFrame.size.height;
399 low_box.set_size_request (prefwidth, prefheight);
405 AUPluginUI::cocoa_view_resized ()
407 NSRect packFrame = [au_view frame];
411 AUPluginUI::create_carbon_view ()
414 ControlRef root_control;
416 Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
418 OpenAComponent(editComponent, &editView);
420 error << _("AU Carbon view: cannot open AU Component") << endmsg;
424 Rect r = { 100, 100, 100, 100 };
425 WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
426 kWindowCompositingAttribute|
427 kWindowNoShadowAttribute|
428 kWindowNoTitleBarAttribute);
430 if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
431 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
432 CloseComponent (editView);
436 if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
437 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
438 DisposeWindow (carbon_window);
439 CloseComponent (editView);
444 Float32Point location = { 0.0, 0.0 };
445 Float32Point size = { 0.0, 0.0 } ;
447 if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
448 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
449 DisposeWindow (carbon_window);
450 CloseComponent (editView);
457 GetControlBounds(viewPane, &bounds);
458 size.x = bounds.right-bounds.left;
459 size.y = bounds.bottom-bounds.top;
461 prefwidth = (int) (size.x + 0.5);
462 prefheight = (int) (size.y + 0.5);
464 SizeWindow (carbon_window, prefwidth, prefheight, true);
465 low_box.set_size_request (prefwidth, prefheight);
471 AUPluginUI::get_nswindow ()
473 Gtk::Container* toplevel = get_toplevel();
475 if (!toplevel || !toplevel->is_toplevel()) {
476 error << _("AUPluginUI: no top level window!") << endmsg;
480 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
483 error << _("AUPluginUI: no top level window!") << endmsg;
491 AUPluginUI::activate ()
493 ActivateWindow (carbon_window, TRUE);
494 // [cocoa_parent makeKeyAndOrderFront:nil];
498 AUPluginUI::deactivate ()
500 ActivateWindow (carbon_window, FALSE);
504 AUPluginUI::parent_carbon_window ()
506 NSWindow* win = get_nswindow ();
513 Gtk::Container* toplevel = get_toplevel();
515 if (!toplevel || !toplevel->is_toplevel()) {
516 error << _("AUPluginUI: no top level window!") << endmsg;
520 toplevel->get_window()->get_root_origin (x, y);
522 /* compute how tall the title bar is, because we have to offset the position of the carbon window
526 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
527 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
529 int titlebar_height = wm_frame.size.height - content_frame.size.height;
531 int packing_extra = 6; // this is the total vertical packing in our top level window
533 MoveWindow (carbon_window, x, y + titlebar_height + top_box.get_height() + packing_extra, false);
534 ShowWindow (carbon_window);
536 // create the cocoa window for the carbon one and make it visible
537 cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
539 SetWindowActivationScope (carbon_window, kWindowActivationScopeNone);
541 _notify = [ [NotificationObject alloc] initWithPluginUI:this andCocoaParent:cocoa_parent andTopLevelParent:win ];
543 [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
549 AUPluginUI::parent_cocoa_window ()
551 NSWindow* win = get_nswindow ();
557 [win setAutodisplay:YES]; // turn of GTK stuff for this window
559 Gtk::Container* toplevel = get_toplevel();
561 if (!toplevel || !toplevel->is_toplevel()) {
562 error << _("AUPluginUI: no top level window!") << endmsg;
566 NSView* view = gdk_quartz_window_get_nsview (get_toplevel()->get_window()->gobj());
567 GtkRequisition a = top_box.size_request ();
569 /* move the au_view down so that it doesn't overlap the top_box contents */
571 NSPoint origin = { 0, a.height };
573 [au_view setFrameOrigin:origin];
574 [view addSubview:au_view];
580 dump_view_tree (NSView* view, int depth)
582 NSArray* subviews = [view subviews];
583 unsigned long cnt = [subviews count];
585 for (int d = 0; d < depth; d++) {
588 cerr << " view @ " << view << endl;
590 for (unsigned long i = 0; i < cnt; ++i) {
591 NSView* subview = [subviews objectAtIndex:i];
592 dump_view_tree (subview, depth+1);
597 AUPluginUI::forward_key_event (GdkEventKey* ev)
599 NSEvent* nsevent = gdk_quartz_event_get_nsevent ((GdkEvent*)ev);
601 if (au_view && nsevent) {
603 /* filter on nsevent type here because GDK massages FlagsChanged
604 messages into GDK_KEY_{PRESS,RELEASE} but Cocoa won't
605 handle a FlagsChanged message as a keyDown or keyUp
608 if ([nsevent type] == NSKeyDown) {
609 [[[au_view window] firstResponder] keyDown:nsevent];
610 } else if ([nsevent type] == NSKeyUp) {
611 [[[au_view window] firstResponder] keyUp:nsevent];
612 } else if ([nsevent type] == NSFlagsChanged) {
613 [[[au_view window] firstResponder] flagsChanged:nsevent];
619 AUPluginUI::on_realize ()
623 /* our windows should not have that resize indicator */
625 NSWindow* win = get_nswindow ();
627 [win setShowsResizeIndicator:NO];
632 AUPluginUI::lower_box_realized ()
635 parent_cocoa_window ();
636 } else if (carbon_window) {
637 parent_carbon_window ();
642 AUPluginUI::on_map_event (GdkEventAny*)
648 AUPluginUI::on_window_hide ()
651 HideWindow (carbon_window);
652 ActivateWindow (carbon_window, FALSE);
659 AUPluginUI::on_window_show (const string& /*title*/)
661 /* this is idempotent so just call it every time we show the window */
663 gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
668 ShowWindow (carbon_window);
669 ActivateWindow (carbon_window, TRUE);
676 AUPluginUI::start_updating (GdkEventAny*)
682 AUPluginUI::stop_updating (GdkEventAny*)
688 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
690 AUPluginUI* aup = new AUPluginUI (plugin_insert);
696 AUPluginUI::on_focus_in_event (GdkEventFocus*)
698 //cerr << "au plugin focus in\n";
699 //Keyboard::magic_widget_grab_focus ();
704 AUPluginUI::on_focus_out_event (GdkEventFocus*)
706 //cerr << "au plugin focus out\n";
707 //Keyboard::magic_widget_drop_focus ();