2 #include <ardour/audio_unit.h>
3 #include <ardour/insert.h>
5 #undef check // stupid gtk, stupid apple
7 #include <gtkmm/button.h>
8 #include <gdk/gdkquartz.h>
10 #include <gtkmm2ext/utils.h>
12 #include "au_pluginui.h"
13 #include "gui_thread.h"
15 #include <appleutility/CAAudioUnit.h>
16 #include <appleutility/CAComponent.h>
18 #import <AudioUnit/AUCocoaUIView.h>
19 #import <CoreAudioKit/AUGenericView.h>
23 using namespace ARDOUR;
25 using namespace Gtkmm2ext;
30 vector<string> AUPluginUI::automation_mode_strings;
32 static const gchar* _automation_mode_strings[] = {
40 AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
42 , automation_mode_label (_("Automation"))
43 , preset_label (_("Presets"))
46 if (automation_mode_strings.empty()) {
47 automation_mode_strings = I18N (_automation_mode_strings);
50 set_popdown_strings (automation_mode_selector, automation_mode_strings);
51 automation_mode_selector.set_active_text (automation_mode_strings.front());
53 if ((au = boost::dynamic_pointer_cast<AUPlugin> (insert->plugin())) == 0) {
54 error << _("unknown type of editor-supplying plugin (note: no AudioUnit support in this version of ardour)") << endmsg;
55 throw failed_constructor ();
58 /* stuff some stuff into the top of the window */
60 top_box.set_spacing (6);
61 top_box.set_border_width (6);
63 top_box.pack_end (bypass_button, false, true);
64 top_box.pack_end (automation_mode_selector, false, false);
65 top_box.pack_end (automation_mode_label, false, false);
66 top_box.pack_end (save_button, false, false);
67 top_box.pack_end (preset_combo, false, false);
68 top_box.pack_end (preset_label, false, false);
71 pack_start (top_box, false, false);
72 pack_start (low_box, false, false);
76 automation_mode_label.show ();
77 automation_mode_selector.show ();
78 bypass_button.show ();
82 _activating_from_app = false;
87 /* prefer cocoa, fall back to cocoa, but use carbon if its there */
89 if (test_cocoa_view_support()) {
91 } else if (test_carbon_view_support()) {
92 create_carbon_view ();
97 low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
100 AUPluginUI::~AUPluginUI ()
103 NSWindow* win = get_nswindow();
104 RemoveEventHandler(carbon_event_handler);
105 [win removeChildWindow:cocoa_parent];
106 } else if (carbon_window) {
108 DisposeWindow (carbon_window);
114 AUPluginUI::test_carbon_view_support ()
118 carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
119 carbon_descriptor.componentSubType = 'gnrc';
120 carbon_descriptor.componentManufacturer = 'appl';
121 carbon_descriptor.componentFlags = 0;
122 carbon_descriptor.componentFlagsMask = 0;
126 // ask the AU for its first editor component
128 err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
130 int nEditors = propertySize / sizeof(ComponentDescription);
131 ComponentDescription *editors = new ComponentDescription[nEditors];
132 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
134 // just pick the first one for now
135 carbon_descriptor = editors[0];
145 AUPluginUI::test_cocoa_view_support ()
148 Boolean isWritable = 0;
149 OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
150 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
151 0, &dataSize, &isWritable);
153 return dataSize > 0 && err == noErr;
157 AUPluginUI::plugin_class_valid (Class pluginClass)
159 if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
160 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
161 [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
169 AUPluginUI::create_cocoa_view ()
171 BOOL wasAbleToLoadCustomView = NO;
172 AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
173 UInt32 numberOfClasses = 0;
176 NSString* factoryClassName = 0;
177 NSURL* CocoaViewBundlePath;
179 OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
180 kAudioUnitProperty_CocoaUI,
181 kAudioUnitScope_Global,
186 numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
188 // Does view have custom Cocoa UI?
190 if ((result == noErr) && (numberOfClasses > 0) ) {
191 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
192 if(AudioUnitGetProperty(*au->get_au(),
193 kAudioUnitProperty_CocoaUI,
194 kAudioUnitScope_Global,
197 &dataSize) == noErr) {
199 CocoaViewBundlePath = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
201 // we only take the first view in this example.
202 factoryClassName = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
206 if (cocoaViewInfo != NULL) {
207 free (cocoaViewInfo);
208 cocoaViewInfo = NULL;
213 NSRect crect = { { 0, 0 }, { 1, 1} };
215 // [A] Show custom UI if view has it
217 if (CocoaViewBundlePath && factoryClassName) {
218 NSBundle *viewBundle = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
219 if (viewBundle == nil) {
220 error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
223 Class factoryClass = [viewBundle classNamed:factoryClassName];
225 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
229 // make sure 'factoryClass' implements the AUCocoaUIBase protocol
230 if (!plugin_class_valid (factoryClass)) {
231 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
235 id factoryInstance = [[[factoryClass alloc] init] autorelease];
236 if (factoryInstance == nil) {
237 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
242 au_view = [factoryInstance uiViewForAudioUnit:*au->get_au() withSize:crect.size];
245 [CocoaViewBundlePath release];
248 for (i = 0; i < numberOfClasses; i++)
249 CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
251 free (cocoaViewInfo);
253 wasAbleToLoadCustomView = YES;
257 if (!wasAbleToLoadCustomView) {
258 // load generic Cocoa view
259 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
260 [(AUGenericView *)au_view setShowsExpertParameters:YES];
267 AUPluginUI::create_carbon_view ()
270 ControlRef root_control;
272 Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
274 OpenAComponent(editComponent, &editView);
276 error << _("AU Carbon view: cannot open AU Component") << endmsg;
280 Rect r = { 100, 100, 100, 100 };
281 WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
282 kWindowCompositingAttribute|
283 kWindowNoShadowAttribute|
284 kWindowNoTitleBarAttribute);
286 if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
287 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
291 if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
292 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
297 Float32Point location = { 0.0, 0.0 };
298 Float32Point size = { 0.0, 0.0 } ;
300 if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
301 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
308 GetControlBounds(viewPane, &bounds);
309 size.x = bounds.right-bounds.left;
310 size.y = bounds.bottom-bounds.top;
312 prefwidth = (int) (size.x + 0.5);
313 prefheight = (int) (size.y + 0.5);
315 SizeWindow (carbon_window, prefwidth, prefheight, true);
316 low_box.set_size_request (prefwidth, prefheight);
322 AUPluginUI::get_nswindow ()
324 Gtk::Container* toplevel = get_toplevel();
326 if (!toplevel || !toplevel->is_toplevel()) {
327 error << _("AUPluginUI: no top level window!") << endmsg;
331 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
334 error << _("AUPluginUI: no top level window!") << endmsg;
342 AUPluginUI::activate ()
344 cerr << "AUPluginUI:: activate!\n";
346 if (carbon_window && cocoa_parent) {
347 cerr << "APP activated, activate carbon window " << insert->name() << endl;
348 _activating_from_app = true;
349 ActivateWindow (carbon_window, TRUE);
350 _activating_from_app = false;
351 [cocoa_parent makeKeyAndOrderFront:nil];
356 AUPluginUI::deactivate ()
359 cerr << "APP DEactivated, for " << insert->name() << endl;
360 _activating_from_app = true;
361 ActivateWindow (carbon_window, FALSE);
362 _activating_from_app = false;
367 _carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData)
369 return ((AUPluginUI*)userData)->carbon_event (nextHandlerRef, event);
373 AUPluginUI::carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event)
375 cerr << "CARBON EVENT\n";
377 UInt32 eventKind = GetEventKind(event);
378 ClickActivationResult howToHandleClick;
379 NSWindow* win = get_nswindow ();
381 cerr << "window " << win << " carbon event type " << eventKind << endl;
384 case kEventWindowHandleActivate:
385 cerr << "carbon window for " << insert->name() << " activated\n";
386 if (_activating_from_app) {
387 cerr << "app activation, ignore window activation\n";
390 [win makeMainWindow];
391 return eventNotHandledErr;
394 case kEventWindowHandleDeactivate:
395 cerr << "carbon window for " << insert->name() << " deactivated\n";
396 // never deactivate the carbon window
400 case kEventWindowGetClickActivation:
401 cerr << "carbon window CLICK activated\n";
402 [win makeKeyAndOrderFront:nil];
403 howToHandleClick = kActivateAndHandleClick;
404 SetEventParameter(event, kEventParamClickActivation, typeClickActivationResult,
405 sizeof(ClickActivationResult), &howToHandleClick);
413 AUPluginUI::parent_carbon_window ()
415 NSWindow* win = get_nswindow ();
422 Gtk::Container* toplevel = get_toplevel();
424 if (!toplevel || !toplevel->is_toplevel()) {
425 error << _("AUPluginUI: no top level window!") << endmsg;
429 toplevel->get_window()->get_root_origin (x, y);
431 /* compute how tall the title bar is, because we have to offset the position of the carbon window
435 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
436 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
438 int titlebar_height = wm_frame.size.height - content_frame.size.height;
440 int packing_extra = 6; // this is the total vertical packing in our top level window
442 MoveWindow (carbon_window, x, y + titlebar_height + top_box.get_height() + packing_extra, false);
443 ShowWindow (carbon_window);
445 // create the cocoa window for the carbon one and make it visible
446 cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
448 EventTypeSpec windowEventTypes[] = {
449 {kEventClassWindow, kEventWindowGetClickActivation },
450 {kEventClassWindow, kEventWindowHandleDeactivate }
453 EventHandlerUPP ehUPP = NewEventHandlerUPP(_carbon_event);
454 OSStatus result = InstallWindowEventHandler (carbon_window, ehUPP,
455 sizeof(windowEventTypes) / sizeof(EventTypeSpec),
456 windowEventTypes, this, &carbon_event_handler);
457 if (result != noErr) {
461 [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
467 AUPluginUI::parent_cocoa_window ()
469 NSWindow* win = get_nswindow ();
470 NSView* packView = 0;
477 Gtk::Container* toplevel = get_toplevel();
479 if (!toplevel || !toplevel->is_toplevel()) {
480 error << _("AUPluginUI: no top level window!") << endmsg;
484 // Get the size of the new AU View's frame
485 packFrame = [au_view frame];
486 packFrame.origin.x = 0;
487 packFrame.origin.y = 0;
489 if (packFrame.size.width > 500 || packFrame.size.height > 500) {
491 /* its too big - use a scrollview */
493 NSRect frameRect = [[cocoa_window contentView] frame];
494 scroll_view = [[[NSScrollView alloc] initWithFrame:frameRect] autorelease];
495 [scroll_view setDrawsBackground:NO];
496 [scroll_view setHasHorizontalScroller:YES];
497 [scroll_view setHasVerticalScroller:YES];
499 packFrame.size = [NSScrollView frameSizeForContentSize:packFrame.size
500 hasHorizontalScroller:[scroll_view hasHorizontalScroller]
501 hasVerticalScroller:[scroll_view hasVerticalScroller]
502 borderType:[scroll_view borderType]];
504 // Create a new frame with same origin as current
505 // frame but size equal to the size of the new view
507 newFrame.origin = [scroll_view frame].origin;
508 newFrame.size = packFrame.size;
510 // Set the new frame and document views on the scroll view
511 [scroll_view setFrame:newFrame];
512 [scroll_view setDocumentView:au_view];
514 packView = scroll_view;
521 NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
523 [view setFrame:packFrame];
524 [view addSubview:packView];
526 low_box.set_size_request (packFrame.size.width, packFrame.size.height);
532 AUPluginUI::on_realize ()
536 /* our windows should not have that resize indicator */
538 NSWindow* win = get_nswindow ();
540 [win setShowsResizeIndicator:NO];
545 AUPluginUI::lower_box_realized ()
548 parent_cocoa_window ();
549 } else if (carbon_window) {
550 parent_carbon_window ();
555 AUPluginUI::on_hide ()
558 cerr << "AU plugin window hidden\n";
562 AUPluginUI::on_map_event (GdkEventAny* ev)
564 cerr << "AU plugin map event\n";
568 // move top level GTK window to the correct level
569 // to keep the stack together and not be sliceable
571 NSWindow* win = get_nswindow ();
572 // [win setLevel:NSFloatingWindowLevel];
579 AUPluginUI::on_show ()
581 cerr << "AU plugin window shown\n";
585 gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
589 } else if (carbon_window) {
590 [cocoa_parent setIsVisible:YES];
591 ShowWindow (carbon_window);
596 AUPluginUI::start_updating (GdkEventAny* any)
602 AUPluginUI::stop_updating (GdkEventAny* any)
608 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
610 AUPluginUI* aup = new AUPluginUI (plugin_insert);
616 AUPluginUI::on_focus_in_event (GdkEventFocus* ev)
618 cerr << "au plugin focus in\n";
623 AUPluginUI::on_focus_out_event (GdkEventFocus* ev)
625 cerr << "au plugin focus out\n";