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>
30 using namespace ARDOUR;
32 using namespace Gtkmm2ext;
37 vector<string> AUPluginUI::automation_mode_strings;
39 static const gchar* _automation_mode_strings[] = {
47 AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
49 , automation_mode_label (_("Automation"))
50 , preset_label (_("Presets"))
53 if (automation_mode_strings.empty()) {
54 automation_mode_strings = I18N (_automation_mode_strings);
57 set_popdown_strings (automation_mode_selector, automation_mode_strings);
58 automation_mode_selector.set_active_text (automation_mode_strings.front());
60 if ((au = boost::dynamic_pointer_cast<AUPlugin> (insert->plugin())) == 0) {
61 error << _("unknown type of editor-supplying plugin (note: no AudioUnit support in this version of ardour)") << endmsg;
62 throw failed_constructor ();
65 /* stuff some stuff into the top of the window */
67 top_box.set_spacing (6);
68 top_box.set_border_width (6);
70 top_box.pack_end (focus_button, false, true);
71 top_box.pack_end (bypass_button, false, true);
72 top_box.pack_end (automation_mode_selector, false, false);
73 top_box.pack_end (automation_mode_label, false, false);
74 top_box.pack_end (save_button, false, false);
75 top_box.pack_end (preset_combo, false, false);
76 top_box.pack_end (preset_label, false, false);
79 pack_start (top_box, false, false);
80 pack_start (low_box, false, false);
84 automation_mode_label.show ();
85 automation_mode_selector.show ();
86 bypass_button.show ();
90 _activating_from_app = false;
96 /* prefer cocoa, fall back to cocoa, but use carbon if its there */
98 if (test_cocoa_view_support()) {
100 } else if (test_carbon_view_support()) {
101 create_carbon_view ();
103 create_cocoa_view ();
106 low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
109 AUPluginUI::~AUPluginUI ()
112 NSWindow* win = get_nswindow();
113 RemoveEventHandler(carbon_event_handler);
114 [win removeChildWindow:cocoa_parent];
115 } else if (carbon_window) {
117 DisposeWindow (carbon_window);
120 if (packView && packView != au_view) {
126 AUPluginUI::test_carbon_view_support ()
130 carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
131 carbon_descriptor.componentSubType = 'gnrc';
132 carbon_descriptor.componentManufacturer = 'appl';
133 carbon_descriptor.componentFlags = 0;
134 carbon_descriptor.componentFlagsMask = 0;
138 // ask the AU for its first editor component
140 err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
142 int nEditors = propertySize / sizeof(ComponentDescription);
143 ComponentDescription *editors = new ComponentDescription[nEditors];
144 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
146 // just pick the first one for now
147 carbon_descriptor = editors[0];
157 AUPluginUI::test_cocoa_view_support ()
160 Boolean isWritable = 0;
161 OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
162 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
163 0, &dataSize, &isWritable);
165 return dataSize > 0 && err == noErr;
169 AUPluginUI::plugin_class_valid (Class pluginClass)
171 if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
172 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
173 [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
181 AUPluginUI::create_cocoa_view ()
183 BOOL wasAbleToLoadCustomView = NO;
184 AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
185 UInt32 numberOfClasses = 0;
188 NSString* factoryClassName = 0;
189 NSURL* CocoaViewBundlePath;
191 OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
192 kAudioUnitProperty_CocoaUI,
193 kAudioUnitScope_Global,
198 numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
200 // Does view have custom Cocoa UI?
202 if ((result == noErr) && (numberOfClasses > 0) ) {
203 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
204 if(AudioUnitGetProperty(*au->get_au(),
205 kAudioUnitProperty_CocoaUI,
206 kAudioUnitScope_Global,
209 &dataSize) == noErr) {
211 CocoaViewBundlePath = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
213 // we only take the first view in this example.
214 factoryClassName = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
218 if (cocoaViewInfo != NULL) {
219 free (cocoaViewInfo);
220 cocoaViewInfo = NULL;
225 NSRect crect = { { 0, 0 }, { 1, 1} };
227 // [A] Show custom UI if view has it
229 if (CocoaViewBundlePath && factoryClassName) {
230 NSBundle *viewBundle = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
231 if (viewBundle == nil) {
232 error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
235 Class factoryClass = [viewBundle classNamed:factoryClassName];
237 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
241 // make sure 'factoryClass' implements the AUCocoaUIBase protocol
242 if (!plugin_class_valid (factoryClass)) {
243 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
247 id factoryInstance = [[[factoryClass alloc] init] autorelease];
248 if (factoryInstance == nil) {
249 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
254 au_view = [factoryInstance uiViewForAudioUnit:*au->get_au() withSize:crect.size];
257 [CocoaViewBundlePath release];
260 for (i = 0; i < numberOfClasses; i++)
261 CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
263 free (cocoaViewInfo);
265 wasAbleToLoadCustomView = YES;
269 if (!wasAbleToLoadCustomView) {
270 // load generic Cocoa view
271 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
272 [(AUGenericView *)au_view setShowsExpertParameters:YES];
277 // Get the size of the new AU View's frame
278 packFrame = [au_view frame];
280 packFrame.origin.x = 0;
281 packFrame.origin.y = 0;
283 if (packFrame.size.width > 500 || packFrame.size.height > 500) {
285 /* its too big - use a scrollview */
287 NSRect frameRect = [[cocoa_window contentView] frame];
288 scroll_view = [[[NSScrollView alloc] initWithFrame:frameRect] autorelease];
289 [scroll_view setDrawsBackground:NO];
290 [scroll_view setHasHorizontalScroller:YES];
291 [scroll_view setHasVerticalScroller:YES];
293 packFrame.size = [NSScrollView frameSizeForContentSize:packFrame.size
294 hasHorizontalScroller:[scroll_view hasHorizontalScroller]
295 hasVerticalScroller:[scroll_view hasVerticalScroller]
296 borderType:[scroll_view borderType]];
298 // Create a new frame with same origin as current
299 // frame but size equal to the size of the new view
301 newFrame.origin = [scroll_view frame].origin;
302 newFrame.size = packFrame.size;
304 // Set the new frame and document views on the scroll view
305 [scroll_view setFrame:newFrame];
306 [scroll_view setDocumentView:au_view];
308 packView = scroll_view;
315 prefwidth = packFrame.size.width;
316 prefheight = packFrame.size.height;
322 AUPluginUI::create_carbon_view ()
325 ControlRef root_control;
327 Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
329 OpenAComponent(editComponent, &editView);
331 error << _("AU Carbon view: cannot open AU Component") << endmsg;
335 Rect r = { 100, 100, 100, 100 };
336 WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
337 kWindowCompositingAttribute|
338 kWindowNoShadowAttribute|
339 kWindowNoTitleBarAttribute);
341 if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
342 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
346 if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
347 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
352 Float32Point location = { 0.0, 0.0 };
353 Float32Point size = { 0.0, 0.0 } ;
355 if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
356 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
363 GetControlBounds(viewPane, &bounds);
364 size.x = bounds.right-bounds.left;
365 size.y = bounds.bottom-bounds.top;
367 prefwidth = (int) (size.x + 0.5);
368 prefheight = (int) (size.y + 0.5);
370 SizeWindow (carbon_window, prefwidth, prefheight, true);
371 low_box.set_size_request (prefwidth, prefheight);
377 AUPluginUI::get_nswindow ()
379 Gtk::Container* toplevel = get_toplevel();
381 if (!toplevel || !toplevel->is_toplevel()) {
382 error << _("AUPluginUI: no top level window!") << endmsg;
386 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
389 error << _("AUPluginUI: no top level window!") << endmsg;
397 AUPluginUI::activate ()
399 if (carbon_window && cocoa_parent) {
400 cerr << "APP activated, activate carbon window " << insert->name() << endl;
401 _activating_from_app = true;
402 ActivateWindow (carbon_window, TRUE);
403 _activating_from_app = false;
404 [cocoa_parent makeKeyAndOrderFront:nil];
409 AUPluginUI::deactivate ()
412 cerr << "APP DEactivated, for " << insert->name() << endl;
413 _activating_from_app = true;
414 ActivateWindow (carbon_window, FALSE);
415 _activating_from_app = false;
420 _carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData)
422 return ((AUPluginUI*)userData)->carbon_event (nextHandlerRef, event);
426 AUPluginUI::carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event)
428 cerr << "CARBON EVENT\n";
430 UInt32 eventKind = GetEventKind(event);
431 ClickActivationResult howToHandleClick;
432 NSWindow* win = get_nswindow ();
434 cerr << "window " << win << " carbon event type " << eventKind << endl;
437 case kEventWindowHandleActivate:
438 cerr << "carbon window for " << insert->name() << " activated\n";
439 if (_activating_from_app) {
440 cerr << "app activation, ignore window activation\n";
443 [win makeMainWindow];
444 return eventNotHandledErr;
447 case kEventWindowHandleDeactivate:
448 cerr << "carbon window for " << insert->name() << " would have been deactivated\n";
449 // never deactivate the carbon window
453 case kEventWindowGetClickActivation:
454 cerr << "carbon window CLICK activated\n";
455 [win makeKeyAndOrderFront:nil];
456 howToHandleClick = kActivateAndHandleClick;
457 SetEventParameter(event, kEventParamClickActivation, typeClickActivationResult,
458 sizeof(ClickActivationResult), &howToHandleClick);
466 AUPluginUI::parent_carbon_window ()
468 NSWindow* win = get_nswindow ();
475 Gtk::Container* toplevel = get_toplevel();
477 if (!toplevel || !toplevel->is_toplevel()) {
478 error << _("AUPluginUI: no top level window!") << endmsg;
482 toplevel->get_window()->get_root_origin (x, y);
484 /* compute how tall the title bar is, because we have to offset the position of the carbon window
488 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
489 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
491 int titlebar_height = wm_frame.size.height - content_frame.size.height;
493 int packing_extra = 6; // this is the total vertical packing in our top level window
495 MoveWindow (carbon_window, x, y + titlebar_height + top_box.get_height() + packing_extra, false);
496 ShowWindow (carbon_window);
498 // create the cocoa window for the carbon one and make it visible
499 cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
501 EventTypeSpec windowEventTypes[] = {
502 {kEventClassWindow, kEventWindowGetClickActivation },
503 {kEventClassWindow, kEventWindowHandleDeactivate }
506 EventHandlerUPP ehUPP = NewEventHandlerUPP(_carbon_event);
507 OSStatus result = InstallWindowEventHandler (carbon_window, ehUPP,
508 sizeof(windowEventTypes) / sizeof(EventTypeSpec),
509 windowEventTypes, this, &carbon_event_handler);
510 if (result != noErr) {
514 [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
520 AUPluginUI::parent_cocoa_window ()
522 NSWindow* win = get_nswindow ();
529 [win setAutodisplay:YES]; // turn of GTK stuff for this window
531 Gtk::Container* toplevel = get_toplevel();
533 if (!toplevel || !toplevel->is_toplevel()) {
534 error << _("AUPluginUI: no top level window!") << endmsg;
538 // Get the size of the new AU View's frame
539 packFrame = [au_view frame];
541 NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
544 [view setFrame:packFrame];
545 [view addSubview:packView];
547 low_box.set_size_request (packFrame.size.width, packFrame.size.height);
553 AUPluginUI::on_realize ()
557 /* our windows should not have that resize indicator */
559 NSWindow* win = get_nswindow ();
561 [win setShowsResizeIndicator:NO];
566 AUPluginUI::lower_box_realized ()
569 parent_cocoa_window ();
570 } else if (carbon_window) {
571 parent_carbon_window ();
576 AUPluginUI::on_hide ()
579 cerr << "AU plugin window hidden\n";
583 AUPluginUI::on_map_event (GdkEventAny* ev)
589 AUPluginUI::on_show ()
591 cerr << "AU plugin window shown\n";
595 gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
599 } else if (carbon_window) {
600 [cocoa_parent setIsVisible:YES];
601 ShowWindow (carbon_window);
606 AUPluginUI::start_updating (GdkEventAny* any)
612 AUPluginUI::stop_updating (GdkEventAny* any)
618 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
620 AUPluginUI* aup = new AUPluginUI (plugin_insert);
626 AUPluginUI::on_focus_in_event (GdkEventFocus* ev)
628 //cerr << "au plugin focus in\n";
629 //Keyboard::magic_widget_grab_focus ();
634 AUPluginUI::on_focus_out_event (GdkEventFocus* ev)
636 //cerr << "au plugin focus out\n";
637 //Keyboard::magic_widget_drop_focus ();