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;
88 /* prefer cocoa, fall back to cocoa, but use carbon if its there */
90 if (test_cocoa_view_support()) {
92 } else if (test_carbon_view_support()) {
93 create_carbon_view ();
98 low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
101 AUPluginUI::~AUPluginUI ()
104 NSWindow* win = get_nswindow();
105 RemoveEventHandler(carbon_event_handler);
106 [win removeChildWindow:cocoa_parent];
107 } else if (carbon_window) {
109 DisposeWindow (carbon_window);
112 if (packView && packView != au_view) {
118 AUPluginUI::test_carbon_view_support ()
122 carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
123 carbon_descriptor.componentSubType = 'gnrc';
124 carbon_descriptor.componentManufacturer = 'appl';
125 carbon_descriptor.componentFlags = 0;
126 carbon_descriptor.componentFlagsMask = 0;
130 // ask the AU for its first editor component
132 err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
134 int nEditors = propertySize / sizeof(ComponentDescription);
135 ComponentDescription *editors = new ComponentDescription[nEditors];
136 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
138 // just pick the first one for now
139 carbon_descriptor = editors[0];
149 AUPluginUI::test_cocoa_view_support ()
152 Boolean isWritable = 0;
153 OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
154 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
155 0, &dataSize, &isWritable);
157 return dataSize > 0 && err == noErr;
161 AUPluginUI::plugin_class_valid (Class pluginClass)
163 if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
164 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
165 [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
173 AUPluginUI::create_cocoa_view ()
175 BOOL wasAbleToLoadCustomView = NO;
176 AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
177 UInt32 numberOfClasses = 0;
180 NSString* factoryClassName = 0;
181 NSURL* CocoaViewBundlePath;
183 OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
184 kAudioUnitProperty_CocoaUI,
185 kAudioUnitScope_Global,
190 numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
192 // Does view have custom Cocoa UI?
194 if ((result == noErr) && (numberOfClasses > 0) ) {
195 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
196 if(AudioUnitGetProperty(*au->get_au(),
197 kAudioUnitProperty_CocoaUI,
198 kAudioUnitScope_Global,
201 &dataSize) == noErr) {
203 CocoaViewBundlePath = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
205 // we only take the first view in this example.
206 factoryClassName = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
210 if (cocoaViewInfo != NULL) {
211 free (cocoaViewInfo);
212 cocoaViewInfo = NULL;
217 NSRect crect = { { 0, 0 }, { 1, 1} };
219 // [A] Show custom UI if view has it
221 if (CocoaViewBundlePath && factoryClassName) {
222 NSBundle *viewBundle = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
223 if (viewBundle == nil) {
224 error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
227 Class factoryClass = [viewBundle classNamed:factoryClassName];
229 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
233 // make sure 'factoryClass' implements the AUCocoaUIBase protocol
234 if (!plugin_class_valid (factoryClass)) {
235 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
239 id factoryInstance = [[[factoryClass alloc] init] autorelease];
240 if (factoryInstance == nil) {
241 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
246 au_view = [factoryInstance uiViewForAudioUnit:*au->get_au() withSize:crect.size];
249 [CocoaViewBundlePath release];
252 for (i = 0; i < numberOfClasses; i++)
253 CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
255 free (cocoaViewInfo);
257 wasAbleToLoadCustomView = YES;
261 if (!wasAbleToLoadCustomView) {
262 // load generic Cocoa view
263 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
264 [(AUGenericView *)au_view setShowsExpertParameters:YES];
269 // Get the size of the new AU View's frame
270 packFrame = [au_view frame];
272 packFrame.origin.x = 0;
273 packFrame.origin.y = 0;
275 if (packFrame.size.width > 500 || packFrame.size.height > 500) {
277 /* its too big - use a scrollview */
279 NSRect frameRect = [[cocoa_window contentView] frame];
280 scroll_view = [[[NSScrollView alloc] initWithFrame:frameRect] autorelease];
281 [scroll_view setDrawsBackground:NO];
282 [scroll_view setHasHorizontalScroller:YES];
283 [scroll_view setHasVerticalScroller:YES];
285 packFrame.size = [NSScrollView frameSizeForContentSize:packFrame.size
286 hasHorizontalScroller:[scroll_view hasHorizontalScroller]
287 hasVerticalScroller:[scroll_view hasVerticalScroller]
288 borderType:[scroll_view borderType]];
290 // Create a new frame with same origin as current
291 // frame but size equal to the size of the new view
293 newFrame.origin = [scroll_view frame].origin;
294 newFrame.size = packFrame.size;
296 // Set the new frame and document views on the scroll view
297 [scroll_view setFrame:newFrame];
298 [scroll_view setDocumentView:au_view];
300 packView = scroll_view;
307 prefwidth = packFrame.size.width;
308 prefheight = packFrame.size.height;
314 AUPluginUI::create_carbon_view ()
317 ControlRef root_control;
319 Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
321 OpenAComponent(editComponent, &editView);
323 error << _("AU Carbon view: cannot open AU Component") << endmsg;
327 Rect r = { 100, 100, 100, 100 };
328 WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
329 kWindowCompositingAttribute|
330 kWindowNoShadowAttribute|
331 kWindowNoTitleBarAttribute);
333 if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
334 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
338 if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
339 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
344 Float32Point location = { 0.0, 0.0 };
345 Float32Point size = { 0.0, 0.0 } ;
347 if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
348 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
355 GetControlBounds(viewPane, &bounds);
356 size.x = bounds.right-bounds.left;
357 size.y = bounds.bottom-bounds.top;
359 prefwidth = (int) (size.x + 0.5);
360 prefheight = (int) (size.y + 0.5);
362 SizeWindow (carbon_window, prefwidth, prefheight, true);
363 low_box.set_size_request (prefwidth, prefheight);
369 AUPluginUI::get_nswindow ()
371 Gtk::Container* toplevel = get_toplevel();
373 if (!toplevel || !toplevel->is_toplevel()) {
374 error << _("AUPluginUI: no top level window!") << endmsg;
378 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
381 error << _("AUPluginUI: no top level window!") << endmsg;
389 AUPluginUI::activate ()
391 if (carbon_window && cocoa_parent) {
392 cerr << "APP activated, activate carbon window " << insert->name() << endl;
393 _activating_from_app = true;
394 ActivateWindow (carbon_window, TRUE);
395 _activating_from_app = false;
396 [cocoa_parent makeKeyAndOrderFront:nil];
401 AUPluginUI::deactivate ()
404 cerr << "APP DEactivated, for " << insert->name() << endl;
405 _activating_from_app = true;
406 ActivateWindow (carbon_window, FALSE);
407 _activating_from_app = false;
412 _carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData)
414 return ((AUPluginUI*)userData)->carbon_event (nextHandlerRef, event);
418 AUPluginUI::carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event)
420 cerr << "CARBON EVENT\n";
422 UInt32 eventKind = GetEventKind(event);
423 ClickActivationResult howToHandleClick;
424 NSWindow* win = get_nswindow ();
426 cerr << "window " << win << " carbon event type " << eventKind << endl;
429 case kEventWindowHandleActivate:
430 cerr << "carbon window for " << insert->name() << " activated\n";
431 if (_activating_from_app) {
432 cerr << "app activation, ignore window activation\n";
435 [win makeMainWindow];
436 return eventNotHandledErr;
439 case kEventWindowHandleDeactivate:
440 cerr << "carbon window for " << insert->name() << " would have been deactivated\n";
441 // never deactivate the carbon window
445 case kEventWindowGetClickActivation:
446 cerr << "carbon window CLICK activated\n";
447 [win makeKeyAndOrderFront:nil];
448 howToHandleClick = kActivateAndHandleClick;
449 SetEventParameter(event, kEventParamClickActivation, typeClickActivationResult,
450 sizeof(ClickActivationResult), &howToHandleClick);
458 AUPluginUI::parent_carbon_window ()
460 NSWindow* win = get_nswindow ();
467 Gtk::Container* toplevel = get_toplevel();
469 if (!toplevel || !toplevel->is_toplevel()) {
470 error << _("AUPluginUI: no top level window!") << endmsg;
474 toplevel->get_window()->get_root_origin (x, y);
476 /* compute how tall the title bar is, because we have to offset the position of the carbon window
480 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
481 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
483 int titlebar_height = wm_frame.size.height - content_frame.size.height;
485 int packing_extra = 6; // this is the total vertical packing in our top level window
487 MoveWindow (carbon_window, x, y + titlebar_height + top_box.get_height() + packing_extra, false);
488 ShowWindow (carbon_window);
490 // create the cocoa window for the carbon one and make it visible
491 cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
493 EventTypeSpec windowEventTypes[] = {
494 {kEventClassWindow, kEventWindowGetClickActivation },
495 {kEventClassWindow, kEventWindowHandleDeactivate }
498 EventHandlerUPP ehUPP = NewEventHandlerUPP(_carbon_event);
499 OSStatus result = InstallWindowEventHandler (carbon_window, ehUPP,
500 sizeof(windowEventTypes) / sizeof(EventTypeSpec),
501 windowEventTypes, this, &carbon_event_handler);
502 if (result != noErr) {
506 [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
512 AUPluginUI::parent_cocoa_window ()
514 NSWindow* win = get_nswindow ();
521 Gtk::Container* toplevel = get_toplevel();
523 if (!toplevel || !toplevel->is_toplevel()) {
524 error << _("AUPluginUI: no top level window!") << endmsg;
528 // Get the size of the new AU View's frame
529 packFrame = [au_view frame];
531 NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
533 [view setFrame:packFrame];
534 [view addSubview:packView];
536 low_box.set_size_request (packFrame.size.width, packFrame.size.height);
542 AUPluginUI::on_realize ()
546 /* our windows should not have that resize indicator */
548 NSWindow* win = get_nswindow ();
550 [win setShowsResizeIndicator:NO];
555 AUPluginUI::lower_box_realized ()
558 parent_cocoa_window ();
559 } else if (carbon_window) {
560 parent_carbon_window ();
565 AUPluginUI::on_hide ()
568 cerr << "AU plugin window hidden\n";
572 AUPluginUI::on_map_event (GdkEventAny* ev)
578 AUPluginUI::on_show ()
580 cerr << "AU plugin window shown\n";
584 gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
588 } else if (carbon_window) {
589 [cocoa_parent setIsVisible:YES];
590 ShowWindow (carbon_window);
595 AUPluginUI::start_updating (GdkEventAny* any)
601 AUPluginUI::stop_updating (GdkEventAny* any)
607 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
609 AUPluginUI* aup = new AUPluginUI (plugin_insert);
615 AUPluginUI::on_focus_in_event (GdkEventFocus* ev)
617 cerr << "au plugin focus in\n";
622 AUPluginUI::on_focus_out_event (GdkEventFocus* ev)
624 cerr << "au plugin focus out\n";