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 AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
50 , automation_mode_label (_("Automation"))
51 , preset_label (_("Presets"))
54 if (automation_mode_strings.empty()) {
55 automation_mode_strings = I18N (_automation_mode_strings);
58 set_popdown_strings (automation_mode_selector, automation_mode_strings);
59 automation_mode_selector.set_active_text (automation_mode_strings.front());
61 if ((au = boost::dynamic_pointer_cast<AUPlugin> (insert->plugin())) == 0) {
62 error << _("unknown type of editor-supplying plugin (note: no AudioUnit support in this version of ardour)") << endmsg;
63 throw failed_constructor ();
66 /* stuff some stuff into the top of the window */
68 HBox* smaller_hbox = manage (new HBox);
70 smaller_hbox->set_spacing (6);
71 smaller_hbox->pack_start (preset_label, false, false, 4);
72 smaller_hbox->pack_start (preset_combo, false, false);
73 smaller_hbox->pack_start (save_button, false, false);
74 smaller_hbox->pack_start (automation_mode_label, false, false);
75 smaller_hbox->pack_start (automation_mode_selector, false, false);
76 smaller_hbox->pack_start (bypass_button, false, true);
78 VBox* v1_box = manage (new VBox);
79 VBox* v2_box = manage (new VBox);
81 v1_box->pack_start (*smaller_hbox, false, true);
82 v2_box->pack_start (focus_button, false, true);
84 top_box.set_homogeneous (false);
85 top_box.set_spacing (6);
86 top_box.set_border_width (6);
88 top_box.pack_end (*v2_box, false, false);
89 top_box.pack_end (*v1_box, false, false);
92 pack_start (top_box, false, false);
93 pack_start (low_box, false, false);
97 automation_mode_label.show ();
98 automation_mode_selector.show ();
99 bypass_button.show ();
103 _activating_from_app = false;
110 /* prefer cocoa, fall back to cocoa, but use carbon if its there */
112 if (test_cocoa_view_support()) {
113 create_cocoa_view ();
114 } else if (test_carbon_view_support()) {
115 create_carbon_view ();
117 create_cocoa_view ();
120 low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
123 AUPluginUI::~AUPluginUI ()
126 NSWindow* win = get_nswindow();
127 RemoveEventHandler(carbon_event_handler);
128 [win removeChildWindow:cocoa_parent];
129 } else if (carbon_window) {
130 /* not parented, just overlaid on top of our window */
131 DisposeWindow (carbon_window);
135 CloseComponent (editView);
139 /* remove whatever we packed into low_box so that GTK doesn't
143 [packView removeFromSuperview];
148 AUPluginUI::test_carbon_view_support ()
152 carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
153 carbon_descriptor.componentSubType = 'gnrc';
154 carbon_descriptor.componentManufacturer = 'appl';
155 carbon_descriptor.componentFlags = 0;
156 carbon_descriptor.componentFlagsMask = 0;
160 // ask the AU for its first editor component
162 err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
164 int nEditors = propertySize / sizeof(ComponentDescription);
165 ComponentDescription *editors = new ComponentDescription[nEditors];
166 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
168 // just pick the first one for now
169 carbon_descriptor = editors[0];
179 AUPluginUI::test_cocoa_view_support ()
182 Boolean isWritable = 0;
183 OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
184 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
185 0, &dataSize, &isWritable);
187 return dataSize > 0 && err == noErr;
191 AUPluginUI::plugin_class_valid (Class pluginClass)
193 if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
194 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
195 [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
203 AUPluginUI::create_cocoa_view ()
205 BOOL wasAbleToLoadCustomView = NO;
206 AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
207 UInt32 numberOfClasses = 0;
210 NSString* factoryClassName = 0;
211 NSURL* CocoaViewBundlePath;
213 OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
214 kAudioUnitProperty_CocoaUI,
215 kAudioUnitScope_Global,
220 numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
222 // Does view have custom Cocoa UI?
224 if ((result == noErr) && (numberOfClasses > 0) ) {
225 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
226 if(AudioUnitGetProperty(*au->get_au(),
227 kAudioUnitProperty_CocoaUI,
228 kAudioUnitScope_Global,
231 &dataSize) == noErr) {
233 CocoaViewBundlePath = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
235 // we only take the first view in this example.
236 factoryClassName = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
240 if (cocoaViewInfo != NULL) {
241 free (cocoaViewInfo);
242 cocoaViewInfo = NULL;
247 NSRect crect = { { 0, 0 }, { 1, 1} };
249 // [A] Show custom UI if view has it
251 if (CocoaViewBundlePath && factoryClassName) {
252 NSBundle *viewBundle = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
253 if (viewBundle == nil) {
254 error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
257 Class factoryClass = [viewBundle classNamed:factoryClassName];
259 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
263 // make sure 'factoryClass' implements the AUCocoaUIBase protocol
264 if (!plugin_class_valid (factoryClass)) {
265 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
269 id factoryInstance = [[[factoryClass alloc] init] autorelease];
270 if (factoryInstance == nil) {
271 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
276 au_view = [factoryInstance uiViewForAudioUnit:*au->get_au() withSize:crect.size];
279 [CocoaViewBundlePath release];
282 for (i = 0; i < numberOfClasses; i++)
283 CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
285 free (cocoaViewInfo);
287 wasAbleToLoadCustomView = YES;
291 if (!wasAbleToLoadCustomView) {
292 // load generic Cocoa view
293 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
294 [(AUGenericView *)au_view setShowsExpertParameters:YES];
299 // Get the size of the new AU View's frame
302 packFrame = [au_view frame];
303 prefwidth = packFrame.size.width;
304 prefheight = packFrame.size.height;
310 AUPluginUI::create_carbon_view ()
313 ControlRef root_control;
315 Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
317 OpenAComponent(editComponent, &editView);
319 error << _("AU Carbon view: cannot open AU Component") << endmsg;
323 Rect r = { 100, 100, 100, 100 };
324 WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
325 kWindowCompositingAttribute|
326 kWindowNoShadowAttribute|
327 kWindowNoTitleBarAttribute);
329 if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
330 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
331 CloseComponent (editView);
335 if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
336 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
337 DisposeWindow (carbon_window);
338 CloseComponent (editView);
343 Float32Point location = { 0.0, 0.0 };
344 Float32Point size = { 0.0, 0.0 } ;
346 if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
347 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
348 DisposeWindow (carbon_window);
349 CloseComponent (editView);
356 GetControlBounds(viewPane, &bounds);
357 size.x = bounds.right-bounds.left;
358 size.y = bounds.bottom-bounds.top;
360 prefwidth = (int) (size.x + 0.5);
361 prefheight = (int) (size.y + 0.5);
363 SizeWindow (carbon_window, prefwidth, prefheight, true);
364 low_box.set_size_request (prefwidth, prefheight);
370 AUPluginUI::get_nswindow ()
372 Gtk::Container* toplevel = get_toplevel();
374 if (!toplevel || !toplevel->is_toplevel()) {
375 error << _("AUPluginUI: no top level window!") << endmsg;
379 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
382 error << _("AUPluginUI: no top level window!") << endmsg;
390 AUPluginUI::activate ()
392 if (carbon_window && cocoa_parent) {
393 cerr << "APP activated, activate carbon window " << insert->name() << endl;
394 _activating_from_app = true;
395 ActivateWindow (carbon_window, TRUE);
396 _activating_from_app = false;
397 [cocoa_parent makeKeyAndOrderFront:nil];
402 AUPluginUI::deactivate ()
405 cerr << "APP DEactivated, for " << insert->name() << endl;
406 _activating_from_app = true;
407 ActivateWindow (carbon_window, FALSE);
408 _activating_from_app = false;
413 _carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData)
415 return ((AUPluginUI*)userData)->carbon_event (nextHandlerRef, event);
419 AUPluginUI::carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event)
421 cerr << "CARBON EVENT\n";
423 UInt32 eventKind = GetEventKind(event);
424 ClickActivationResult howToHandleClick;
425 NSWindow* win = get_nswindow ();
427 cerr << "window " << win << " carbon event type " << eventKind << endl;
430 case kEventWindowHandleActivate:
431 cerr << "carbon window for " << insert->name() << " activated\n";
432 if (_activating_from_app) {
433 cerr << "app activation, ignore window activation\n";
436 [win makeMainWindow];
437 return eventNotHandledErr;
440 case kEventWindowHandleDeactivate:
441 cerr << "carbon window for " << insert->name() << " would have been deactivated\n";
442 // never deactivate the carbon window
446 case kEventWindowGetClickActivation:
447 cerr << "carbon window CLICK activated\n";
448 [win makeKeyAndOrderFront:nil];
449 howToHandleClick = kActivateAndHandleClick;
450 SetEventParameter(event, kEventParamClickActivation, typeClickActivationResult,
451 sizeof(ClickActivationResult), &howToHandleClick);
459 AUPluginUI::parent_carbon_window ()
461 NSWindow* win = get_nswindow ();
468 Gtk::Container* toplevel = get_toplevel();
470 if (!toplevel || !toplevel->is_toplevel()) {
471 error << _("AUPluginUI: no top level window!") << endmsg;
475 toplevel->get_window()->get_root_origin (x, y);
477 /* compute how tall the title bar is, because we have to offset the position of the carbon window
481 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
482 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
484 int titlebar_height = wm_frame.size.height - content_frame.size.height;
486 int packing_extra = 6; // this is the total vertical packing in our top level window
488 MoveWindow (carbon_window, x, y + titlebar_height + top_box.get_height() + packing_extra, false);
489 ShowWindow (carbon_window);
491 // create the cocoa window for the carbon one and make it visible
492 cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
494 EventTypeSpec windowEventTypes[] = {
495 {kEventClassWindow, kEventWindowGetClickActivation },
496 {kEventClassWindow, kEventWindowHandleDeactivate }
499 EventHandlerUPP ehUPP = NewEventHandlerUPP(_carbon_event);
500 OSStatus result = InstallWindowEventHandler (carbon_window, ehUPP,
501 sizeof(windowEventTypes) / sizeof(EventTypeSpec),
502 windowEventTypes, this, &carbon_event_handler);
503 if (result != noErr) {
507 [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
513 AUPluginUI::parent_cocoa_window ()
515 NSWindow* win = get_nswindow ();
522 [win setAutodisplay:YES]; // turn of GTK stuff for this window
524 Gtk::Container* toplevel = get_toplevel();
526 if (!toplevel || !toplevel->is_toplevel()) {
527 error << _("AUPluginUI: no top level window!") << endmsg;
531 // Get the size of the new AU View's frame
532 packFrame = [au_view frame];
534 NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
536 [view setFrame:packFrame];
537 [view addSubview:packView];
539 low_box.set_size_request (packFrame.size.width, packFrame.size.height);
545 AUPluginUI::on_realize ()
549 /* our windows should not have that resize indicator */
551 NSWindow* win = get_nswindow ();
553 [win setShowsResizeIndicator:NO];
558 AUPluginUI::lower_box_realized ()
561 parent_cocoa_window ();
562 } else if (carbon_window) {
563 parent_carbon_window ();
568 AUPluginUI::on_hide ()
571 cerr << "AU plugin window hidden\n";
575 AUPluginUI::on_map_event (GdkEventAny* ev)
581 AUPluginUI::on_show ()
583 cerr << "AU plugin window shown\n";
587 gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
591 } else if (carbon_window) {
592 [cocoa_parent setIsVisible:YES];
593 ShowWindow (carbon_window);
598 AUPluginUI::start_updating (GdkEventAny* any)
604 AUPluginUI::stop_updating (GdkEventAny* any)
610 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
612 AUPluginUI* aup = new AUPluginUI (plugin_insert);
618 AUPluginUI::on_focus_in_event (GdkEventFocus* ev)
620 //cerr << "au plugin focus in\n";
621 //Keyboard::magic_widget_grab_focus ();
626 AUPluginUI::on_focus_out_event (GdkEventFocus* ev)
628 //cerr << "au plugin focus out\n";
629 //Keyboard::magic_widget_drop_focus ();