2 #define Marker FuckYouAppleAndYourLackOfNameSpaces
5 #include <ardour/audio_unit.h>
6 #include <ardour/insert.h>
8 #undef check // stupid gtk, stupid apple
10 #include <gtkmm/button.h>
11 #include <gdk/gdkquartz.h>
13 #include <gtkmm2ext/utils.h>
15 #include "au_pluginui.h"
16 #include "gui_thread.h"
18 #include <appleutility/CAAudioUnit.h>
19 #include <appleutility/CAComponent.h>
21 #import <AudioUnit/AUCocoaUIView.h>
22 #import <CoreAudioKit/AUGenericView.h>
28 #include "public_editor.h"
31 using namespace ARDOUR;
33 using namespace Gtkmm2ext;
38 vector<string> AUPluginUI::automation_mode_strings;
40 static const gchar* _automation_mode_strings[] = {
48 @implementation NotificationObject
50 - (NotificationObject*) initWithPluginUI: (AUPluginUI*) apluginui andCocoaParent: (NSWindow*) cp andTopLevelParent: (NSWindow*) tlp
52 self = [ super init ];
55 plugin_ui = apluginui;
57 top_level_parent = tlp;
59 [[NSNotificationCenter defaultCenter] addObserver:self
60 selector:@selector(cocoaParentActivationHandler:)
61 name:NSWindowDidBecomeMainNotification
64 [[NSNotificationCenter defaultCenter] addObserver:self
65 selector:@selector(cocoaParentBecameKeyHandler:)
66 name:NSWindowDidBecomeKeyNotification
73 - (void)cocoaParentActivationHandler:(NSNotification *)notification
75 NSWindow* notification_window = (NSWindow *)[notification object];
77 if (top_level_parent == notification_window || cocoa_parent == notification_window) {
78 if ([notification_window isMainWindow]) {
79 plugin_ui->activate();
81 plugin_ui->deactivate();
86 - (void)cocoaParentBecameKeyHandler:(NSNotification *)notification
88 NSWindow* notification_window = (NSWindow *)[notification object];
90 cerr << "KeyNotification Handler\n";
92 if (top_level_parent == notification_window || cocoa_parent == notification_window) {
93 cerr << "\tKeyHandler, top level parent is key: " << ([top_level_parent isKeyWindow] ? "yes" : "no") << endl;
94 cerr << "\tKeyHandler, cocoa parent is key: " << ([cocoa_parent isKeyWindow] ? "yes" : "no") << endl;
96 if ([notification_window isKeyWindow]) {
97 cerr << "\t\tActivating plugin UI\n";
98 plugin_ui->activate();
100 cerr << "\t\tDeActivating plugin UI\n";
101 plugin_ui->deactivate();
104 cerr << "\tsome other window become Key (" << notification_window << ") CP is " << cocoa_parent << "\n";
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);
138 smaller_hbox->pack_start (automation_mode_label, false, false);
139 smaller_hbox->pack_start (automation_mode_selector, false, false);
140 smaller_hbox->pack_start (bypass_button, false, true);
142 VBox* v1_box = manage (new VBox);
143 VBox* v2_box = manage (new VBox);
145 v1_box->pack_start (*smaller_hbox, false, true);
146 v2_box->pack_start (focus_button, false, true);
148 top_box.set_homogeneous (false);
149 top_box.set_spacing (6);
150 top_box.set_border_width (6);
152 top_box.pack_end (*v2_box, false, false);
153 top_box.pack_end (*v1_box, false, false);
156 pack_start (top_box, false, false);
157 pack_start (low_box, false, false);
159 preset_label.show ();
160 preset_combo.show ();
161 automation_mode_label.show ();
162 automation_mode_selector.show ();
163 bypass_button.show ();
167 _activating_from_app = false;
175 /* prefer cocoa, fall back to cocoa, but use carbon if its there */
177 if (test_cocoa_view_support()) {
178 create_cocoa_view ();
179 } else if (test_carbon_view_support()) {
180 create_carbon_view ();
182 create_cocoa_view ();
185 low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
188 AUPluginUI::~AUPluginUI ()
191 NSWindow* win = get_nswindow();
192 [[NSNotificationCenter defaultCenter] removeObserver:_notify];
193 [win removeChildWindow:cocoa_parent];
195 } else if (carbon_window) {
196 /* not parented, just overlaid on top of our window */
197 DisposeWindow (carbon_window);
201 CloseComponent (editView);
205 /* remove whatever we packed into low_box so that GTK doesn't
209 [packView removeFromSuperview];
214 AUPluginUI::test_carbon_view_support ()
218 carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
219 carbon_descriptor.componentSubType = 'gnrc';
220 carbon_descriptor.componentManufacturer = 'appl';
221 carbon_descriptor.componentFlags = 0;
222 carbon_descriptor.componentFlagsMask = 0;
226 // ask the AU for its first editor component
228 err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
230 int nEditors = propertySize / sizeof(ComponentDescription);
231 ComponentDescription *editors = new ComponentDescription[nEditors];
232 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
234 // just pick the first one for now
235 carbon_descriptor = editors[0];
245 AUPluginUI::test_cocoa_view_support ()
248 Boolean isWritable = 0;
249 OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
250 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
251 0, &dataSize, &isWritable);
253 return dataSize > 0 && err == noErr;
257 AUPluginUI::plugin_class_valid (Class pluginClass)
259 if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
260 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
261 [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
269 AUPluginUI::create_cocoa_view ()
271 BOOL wasAbleToLoadCustomView = NO;
272 AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
273 UInt32 numberOfClasses = 0;
276 NSString* factoryClassName = 0;
277 NSURL* CocoaViewBundlePath;
279 OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
280 kAudioUnitProperty_CocoaUI,
281 kAudioUnitScope_Global,
286 numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
288 // Does view have custom Cocoa UI?
290 if ((result == noErr) && (numberOfClasses > 0) ) {
291 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
292 if(AudioUnitGetProperty(*au->get_au(),
293 kAudioUnitProperty_CocoaUI,
294 kAudioUnitScope_Global,
297 &dataSize) == noErr) {
299 CocoaViewBundlePath = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
301 // we only take the first view in this example.
302 factoryClassName = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
306 if (cocoaViewInfo != NULL) {
307 free (cocoaViewInfo);
308 cocoaViewInfo = NULL;
313 NSRect crect = { { 0, 0 }, { 1, 1} };
315 // [A] Show custom UI if view has it
317 if (CocoaViewBundlePath && factoryClassName) {
318 NSBundle *viewBundle = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
319 if (viewBundle == nil) {
320 error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
323 Class factoryClass = [viewBundle classNamed:factoryClassName];
325 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
329 // make sure 'factoryClass' implements the AUCocoaUIBase protocol
330 if (!plugin_class_valid (factoryClass)) {
331 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
335 id factoryInstance = [[[factoryClass alloc] init] autorelease];
336 if (factoryInstance == nil) {
337 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
342 au_view = [factoryInstance uiViewForAudioUnit:*au->get_au() withSize:crect.size];
345 [CocoaViewBundlePath release];
348 for (i = 0; i < numberOfClasses; i++)
349 CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
351 free (cocoaViewInfo);
353 wasAbleToLoadCustomView = YES;
357 if (!wasAbleToLoadCustomView) {
358 // load generic Cocoa view
359 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
360 [(AUGenericView *)au_view setShowsExpertParameters:YES];
365 // Get the size of the new AU View's frame
368 packFrame = [au_view frame];
369 prefwidth = packFrame.size.width;
370 prefheight = packFrame.size.height;
376 AUPluginUI::create_carbon_view ()
379 ControlRef root_control;
381 Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
383 OpenAComponent(editComponent, &editView);
385 error << _("AU Carbon view: cannot open AU Component") << endmsg;
389 Rect r = { 100, 100, 100, 100 };
390 WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
391 kWindowCompositingAttribute|
392 kWindowNoShadowAttribute|
393 kWindowNoTitleBarAttribute);
395 if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
396 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
397 CloseComponent (editView);
401 if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
402 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
403 DisposeWindow (carbon_window);
404 CloseComponent (editView);
409 Float32Point location = { 0.0, 0.0 };
410 Float32Point size = { 0.0, 0.0 } ;
412 if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
413 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
414 DisposeWindow (carbon_window);
415 CloseComponent (editView);
422 GetControlBounds(viewPane, &bounds);
423 size.x = bounds.right-bounds.left;
424 size.y = bounds.bottom-bounds.top;
426 prefwidth = (int) (size.x + 0.5);
427 prefheight = (int) (size.y + 0.5);
429 SizeWindow (carbon_window, prefwidth, prefheight, true);
430 low_box.set_size_request (prefwidth, prefheight);
436 AUPluginUI::get_nswindow ()
438 Gtk::Container* toplevel = get_toplevel();
440 if (!toplevel || !toplevel->is_toplevel()) {
441 error << _("AUPluginUI: no top level window!") << endmsg;
445 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
448 error << _("AUPluginUI: no top level window!") << endmsg;
456 AUPluginUI::activate ()
458 ActivateWindow (carbon_window, TRUE);
459 // [cocoa_parent makeKeyAndOrderFront:nil];
463 AUPluginUI::deactivate ()
465 ActivateWindow (carbon_window, FALSE);
469 AUPluginUI::parent_carbon_window ()
471 NSWindow* win = get_nswindow ();
478 Gtk::Container* toplevel = get_toplevel();
480 if (!toplevel || !toplevel->is_toplevel()) {
481 error << _("AUPluginUI: no top level window!") << endmsg;
485 toplevel->get_window()->get_root_origin (x, y);
487 /* compute how tall the title bar is, because we have to offset the position of the carbon window
491 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
492 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
494 int titlebar_height = wm_frame.size.height - content_frame.size.height;
496 int packing_extra = 6; // this is the total vertical packing in our top level window
498 MoveWindow (carbon_window, x, y + titlebar_height + top_box.get_height() + packing_extra, false);
499 ShowWindow (carbon_window);
501 // create the cocoa window for the carbon one and make it visible
502 cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
504 SetWindowActivationScope (carbon_window, kWindowActivationScopeNone);
506 _notify = [ [NotificationObject alloc] initWithPluginUI:this andCocoaParent:cocoa_parent andTopLevelParent:win ];
508 [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
514 AUPluginUI::parent_cocoa_window ()
516 NSWindow* win = get_nswindow ();
523 [win setAutodisplay:YES]; // turn of GTK stuff for this window
525 Gtk::Container* toplevel = get_toplevel();
527 if (!toplevel || !toplevel->is_toplevel()) {
528 error << _("AUPluginUI: no top level window!") << endmsg;
532 // Get the size of the new AU View's frame
533 packFrame = [au_view frame];
535 NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
537 [view setFrame:packFrame];
538 [view addSubview:packView];
540 low_box.set_size_request (packFrame.size.width, packFrame.size.height);
546 AUPluginUI::on_realize ()
550 /* our windows should not have that resize indicator */
552 NSWindow* win = get_nswindow ();
554 [win setShowsResizeIndicator:NO];
559 AUPluginUI::lower_box_realized ()
562 parent_cocoa_window ();
563 } else if (carbon_window) {
564 parent_carbon_window ();
569 AUPluginUI::on_map_event (GdkEventAny* ev)
575 AUPluginUI::on_window_hide ()
578 HideWindow (carbon_window);
579 ActivateWindow (carbon_window, FALSE);
586 AUPluginUI::on_window_show (const Glib::ustring& title)
588 /* this is idempotent so just call it every time we show the window */
590 gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
595 ShowWindow (carbon_window);
596 ActivateWindow (carbon_window, TRUE);
603 AUPluginUI::start_updating (GdkEventAny* any)
609 AUPluginUI::stop_updating (GdkEventAny* any)
615 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
617 AUPluginUI* aup = new AUPluginUI (plugin_insert);
623 AUPluginUI::on_focus_in_event (GdkEventFocus* ev)
625 //cerr << "au plugin focus in\n";
626 //Keyboard::magic_widget_grab_focus ();
631 AUPluginUI::on_focus_out_event (GdkEventFocus* ev)
633 //cerr << "au plugin focus out\n";
634 //Keyboard::magic_widget_drop_focus ();