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->pack_start (preset_label, false, false, 10);
71 smaller_hbox->pack_start (preset_combo, false, false);
72 smaller_hbox->pack_start (save_button, false, false);
73 smaller_hbox->pack_start (automation_mode_label, false, false);
74 smaller_hbox->pack_start (automation_mode_selector, false, false);
75 smaller_hbox->pack_start (bypass_button, false, true);
77 VBox* v1_box = manage (new VBox);
78 VBox* v2_box = manage (new VBox);
80 v1_box->pack_start (*smaller_hbox, false, true);
81 v2_box->pack_start (focus_button, false, true);
83 top_box.set_homogeneous (false);
84 top_box.set_spacing (6);
85 top_box.set_border_width (6);
87 top_box.pack_end (*v2_box, false, false);
88 top_box.pack_end (*v1_box, false, false);
91 pack_start (top_box, false, false);
92 pack_start (low_box, false, false);
96 automation_mode_label.show ();
97 automation_mode_selector.show ();
98 bypass_button.show ();
102 _activating_from_app = false;
108 /* prefer cocoa, fall back to cocoa, but use carbon if its there */
110 if (test_cocoa_view_support()) {
111 create_cocoa_view ();
112 } else if (test_carbon_view_support()) {
113 create_carbon_view ();
115 create_cocoa_view ();
118 low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
121 AUPluginUI::~AUPluginUI ()
124 NSWindow* win = get_nswindow();
125 RemoveEventHandler(carbon_event_handler);
126 [win removeChildWindow:cocoa_parent];
127 } else if (carbon_window) {
129 DisposeWindow (carbon_window);
133 /* remove whatever we packed into low_box so that GTK doesn't
137 [packView removeFromSuperview];
142 AUPluginUI::test_carbon_view_support ()
146 carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
147 carbon_descriptor.componentSubType = 'gnrc';
148 carbon_descriptor.componentManufacturer = 'appl';
149 carbon_descriptor.componentFlags = 0;
150 carbon_descriptor.componentFlagsMask = 0;
154 // ask the AU for its first editor component
156 err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
158 int nEditors = propertySize / sizeof(ComponentDescription);
159 ComponentDescription *editors = new ComponentDescription[nEditors];
160 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
162 // just pick the first one for now
163 carbon_descriptor = editors[0];
173 AUPluginUI::test_cocoa_view_support ()
176 Boolean isWritable = 0;
177 OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
178 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
179 0, &dataSize, &isWritable);
181 return dataSize > 0 && err == noErr;
185 AUPluginUI::plugin_class_valid (Class pluginClass)
187 if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
188 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
189 [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
197 AUPluginUI::create_cocoa_view ()
199 BOOL wasAbleToLoadCustomView = NO;
200 AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
201 UInt32 numberOfClasses = 0;
204 NSString* factoryClassName = 0;
205 NSURL* CocoaViewBundlePath;
207 OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
208 kAudioUnitProperty_CocoaUI,
209 kAudioUnitScope_Global,
214 numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
216 // Does view have custom Cocoa UI?
218 if ((result == noErr) && (numberOfClasses > 0) ) {
219 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
220 if(AudioUnitGetProperty(*au->get_au(),
221 kAudioUnitProperty_CocoaUI,
222 kAudioUnitScope_Global,
225 &dataSize) == noErr) {
227 CocoaViewBundlePath = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
229 // we only take the first view in this example.
230 factoryClassName = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
234 if (cocoaViewInfo != NULL) {
235 free (cocoaViewInfo);
236 cocoaViewInfo = NULL;
241 NSRect crect = { { 0, 0 }, { 1, 1} };
243 // [A] Show custom UI if view has it
245 if (CocoaViewBundlePath && factoryClassName) {
246 NSBundle *viewBundle = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
247 if (viewBundle == nil) {
248 error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
251 Class factoryClass = [viewBundle classNamed:factoryClassName];
253 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
257 // make sure 'factoryClass' implements the AUCocoaUIBase protocol
258 if (!plugin_class_valid (factoryClass)) {
259 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
263 id factoryInstance = [[[factoryClass alloc] init] autorelease];
264 if (factoryInstance == nil) {
265 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
270 au_view = [factoryInstance uiViewForAudioUnit:*au->get_au() withSize:crect.size];
273 [CocoaViewBundlePath release];
276 for (i = 0; i < numberOfClasses; i++)
277 CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
279 free (cocoaViewInfo);
281 wasAbleToLoadCustomView = YES;
285 if (!wasAbleToLoadCustomView) {
286 // load generic Cocoa view
287 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
288 [(AUGenericView *)au_view setShowsExpertParameters:YES];
293 // Get the size of the new AU View's frame
295 packFrame = [au_view frame];
296 prefwidth = packFrame.size.width;
297 prefheight = packFrame.size.height;
303 AUPluginUI::create_carbon_view ()
306 ControlRef root_control;
308 Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
310 OpenAComponent(editComponent, &editView);
312 error << _("AU Carbon view: cannot open AU Component") << endmsg;
316 Rect r = { 100, 100, 100, 100 };
317 WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
318 kWindowCompositingAttribute|
319 kWindowNoShadowAttribute|
320 kWindowNoTitleBarAttribute);
322 if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
323 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
327 if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
328 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
333 Float32Point location = { 0.0, 0.0 };
334 Float32Point size = { 0.0, 0.0 } ;
336 if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
337 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
344 GetControlBounds(viewPane, &bounds);
345 size.x = bounds.right-bounds.left;
346 size.y = bounds.bottom-bounds.top;
348 prefwidth = (int) (size.x + 0.5);
349 prefheight = (int) (size.y + 0.5);
351 SizeWindow (carbon_window, prefwidth, prefheight, true);
352 low_box.set_size_request (prefwidth, prefheight);
358 AUPluginUI::get_nswindow ()
360 Gtk::Container* toplevel = get_toplevel();
362 if (!toplevel || !toplevel->is_toplevel()) {
363 error << _("AUPluginUI: no top level window!") << endmsg;
367 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
370 error << _("AUPluginUI: no top level window!") << endmsg;
378 AUPluginUI::activate ()
380 if (carbon_window && cocoa_parent) {
381 cerr << "APP activated, activate carbon window " << insert->name() << endl;
382 _activating_from_app = true;
383 ActivateWindow (carbon_window, TRUE);
384 _activating_from_app = false;
385 [cocoa_parent makeKeyAndOrderFront:nil];
390 AUPluginUI::deactivate ()
393 cerr << "APP DEactivated, for " << insert->name() << endl;
394 _activating_from_app = true;
395 ActivateWindow (carbon_window, FALSE);
396 _activating_from_app = false;
401 _carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData)
403 return ((AUPluginUI*)userData)->carbon_event (nextHandlerRef, event);
407 AUPluginUI::carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event)
409 cerr << "CARBON EVENT\n";
411 UInt32 eventKind = GetEventKind(event);
412 ClickActivationResult howToHandleClick;
413 NSWindow* win = get_nswindow ();
415 cerr << "window " << win << " carbon event type " << eventKind << endl;
418 case kEventWindowHandleActivate:
419 cerr << "carbon window for " << insert->name() << " activated\n";
420 if (_activating_from_app) {
421 cerr << "app activation, ignore window activation\n";
424 [win makeMainWindow];
425 return eventNotHandledErr;
428 case kEventWindowHandleDeactivate:
429 cerr << "carbon window for " << insert->name() << " would have been deactivated\n";
430 // never deactivate the carbon window
434 case kEventWindowGetClickActivation:
435 cerr << "carbon window CLICK activated\n";
436 [win makeKeyAndOrderFront:nil];
437 howToHandleClick = kActivateAndHandleClick;
438 SetEventParameter(event, kEventParamClickActivation, typeClickActivationResult,
439 sizeof(ClickActivationResult), &howToHandleClick);
447 AUPluginUI::parent_carbon_window ()
449 NSWindow* win = get_nswindow ();
456 Gtk::Container* toplevel = get_toplevel();
458 if (!toplevel || !toplevel->is_toplevel()) {
459 error << _("AUPluginUI: no top level window!") << endmsg;
463 toplevel->get_window()->get_root_origin (x, y);
465 /* compute how tall the title bar is, because we have to offset the position of the carbon window
469 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
470 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
472 int titlebar_height = wm_frame.size.height - content_frame.size.height;
474 int packing_extra = 6; // this is the total vertical packing in our top level window
476 MoveWindow (carbon_window, x, y + titlebar_height + top_box.get_height() + packing_extra, false);
477 ShowWindow (carbon_window);
479 // create the cocoa window for the carbon one and make it visible
480 cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
482 EventTypeSpec windowEventTypes[] = {
483 {kEventClassWindow, kEventWindowGetClickActivation },
484 {kEventClassWindow, kEventWindowHandleDeactivate }
487 EventHandlerUPP ehUPP = NewEventHandlerUPP(_carbon_event);
488 OSStatus result = InstallWindowEventHandler (carbon_window, ehUPP,
489 sizeof(windowEventTypes) / sizeof(EventTypeSpec),
490 windowEventTypes, this, &carbon_event_handler);
491 if (result != noErr) {
495 [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
501 AUPluginUI::parent_cocoa_window ()
503 NSWindow* win = get_nswindow ();
510 [win setAutodisplay:YES]; // turn of GTK stuff for this window
512 Gtk::Container* toplevel = get_toplevel();
514 if (!toplevel || !toplevel->is_toplevel()) {
515 error << _("AUPluginUI: no top level window!") << endmsg;
519 // Get the size of the new AU View's frame
520 packFrame = [au_view frame];
522 NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
525 [view setFrame:packFrame];
526 [view addSubview:packView];
528 low_box.set_size_request (packFrame.size.width, packFrame.size.height);
534 AUPluginUI::on_realize ()
538 /* our windows should not have that resize indicator */
540 NSWindow* win = get_nswindow ();
542 [win setShowsResizeIndicator:NO];
547 AUPluginUI::lower_box_realized ()
550 parent_cocoa_window ();
551 } else if (carbon_window) {
552 parent_carbon_window ();
557 AUPluginUI::on_hide ()
560 cerr << "AU plugin window hidden\n";
564 AUPluginUI::on_map_event (GdkEventAny* ev)
570 AUPluginUI::on_show ()
572 cerr << "AU plugin window shown\n";
576 gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
580 } else if (carbon_window) {
581 [cocoa_parent setIsVisible:YES];
582 ShowWindow (carbon_window);
587 AUPluginUI::start_updating (GdkEventAny* any)
593 AUPluginUI::stop_updating (GdkEventAny* any)
599 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
601 AUPluginUI* aup = new AUPluginUI (plugin_insert);
607 AUPluginUI::on_focus_in_event (GdkEventFocus* ev)
609 //cerr << "au plugin focus in\n";
610 //Keyboard::magic_widget_grab_focus ();
615 AUPluginUI::on_focus_out_event (GdkEventFocus* ev)
617 //cerr << "au plugin focus out\n";
618 //Keyboard::magic_widget_drop_focus ();