fix import/embed with "sequence files" option
[ardour2.git] / gtk2_ardour / au_pluginui.mm
blob08cd3bf09ebabb6a30e9ee42ef124bb60e92891d
1 #undef  Marker
2 #define Marker FuckYouAppleAndYourLackOfNameSpaces
4 #include <pbd/error.h>
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>
24 #undef Marker
26 #include "keyboard.h"
27 #include "utils.h"
28 #include "public_editor.h"
29 #include "i18n.h"
31 using namespace ARDOUR;
32 using namespace Gtk;
33 using namespace Gtkmm2ext;
34 using namespace sigc;
35 using namespace std;
36 using namespace PBD;
38 vector<string> AUPluginUI::automation_mode_strings;
40 static const gchar* _automation_mode_strings[] = {
41         X_("Manual"),
42         X_("Play"),
43         X_("Write"),
44         X_("Touch"),
45         0
48 @implementation NotificationObject
50 - (NotificationObject*) initWithPluginUI: (AUPluginUI*) apluginui andCocoaParent: (NSWindow*) cp andTopLevelParent: (NSWindow*) tlp
52         self = [ super init ];
54         if (self) {
55                 plugin_ui = apluginui;
56                 cocoa_parent = cp;
57                 top_level_parent = tlp;
59                 [[NSNotificationCenter defaultCenter] addObserver:self
60                  selector:@selector(cocoaParentActivationHandler:)
61                  name:NSWindowDidBecomeMainNotification
62                  object:nil];
64                 [[NSNotificationCenter defaultCenter] addObserver:self
65                  selector:@selector(cocoaParentBecameKeyHandler:)
66                  name:NSWindowDidBecomeKeyNotification
67                  object:nil];
68         }
70         return self;
72                 
73 - (void)cocoaParentActivationHandler:(NSNotification *)notification
75         NSWindow* notification_window = (NSWindow *)[notification object];
77         if (top_level_parent == notification_window || cocoa_parent == notification_window) {
78                 if ([notification_window isMainWindow]) {
79                         plugin_ui->activate();
80                 } else {
81                         plugin_ui->deactivate();
82                 }
83         } 
86 - (void)cocoaParentBecameKeyHandler:(NSNotification *)notification
88         NSWindow* notification_window = (NSWindow *)[notification object];
90         cerr << "KeyNotification Handler\n";
92         if (top_level_parent == notification_window || cocoa_parent == notification_window) {
93                 cerr << "\tKeyHandler, top level parent is key: " <<  ([top_level_parent isKeyWindow] ? "yes" : "no") << endl;
94                 cerr << "\tKeyHandler, cocoa parent is key: " <<  ([cocoa_parent isKeyWindow] ? "yes" : "no") << endl;
95                 
96                 if ([notification_window isKeyWindow]) {
97                         cerr << "\t\tActivating plugin UI\n";
98                         plugin_ui->activate();
99                 } else {
100                         cerr << "\t\tDeActivating plugin UI\n";
101                         plugin_ui->deactivate();
102                 }
103         } else {
104                 cerr << "\tsome other window become Key (" << notification_window << ") CP is " << cocoa_parent << "\n";
105         }
110 @end
112 AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
113         : PlugUIBase (insert)
114         , automation_mode_label (_("Automation"))
115         , preset_label (_("Presets"))
116         
118         if (automation_mode_strings.empty()) {
119                 automation_mode_strings = I18N (_automation_mode_strings);
120         }
121         
122         set_popdown_strings (automation_mode_selector, automation_mode_strings);
123         automation_mode_selector.set_active_text (automation_mode_strings.front());
125         if ((au = boost::dynamic_pointer_cast<AUPlugin> (insert->plugin())) == 0) {
126                 error << _("unknown type of editor-supplying plugin (note: no AudioUnit support in this version of ardour)") << endmsg;
127                 throw failed_constructor ();
128         }
130         /* stuff some stuff into the top of the window */
132         HBox* smaller_hbox = manage (new HBox);
134         smaller_hbox->set_spacing (6);
135         smaller_hbox->pack_start (preset_label, false, false, 4);
136         smaller_hbox->pack_start (preset_combo, false, false);
137         smaller_hbox->pack_start (save_button, false, false);
138         smaller_hbox->pack_start (automation_mode_label, false, false);
139         smaller_hbox->pack_start (automation_mode_selector, false, false);
140         smaller_hbox->pack_start (bypass_button, false, true);
142         VBox* v1_box = manage (new VBox);
143         VBox* v2_box = manage (new VBox);
145         v1_box->pack_start (*smaller_hbox, false, true);
146         v2_box->pack_start (focus_button, false, true);
148         top_box.set_homogeneous (false);
149         top_box.set_spacing (6);
150         top_box.set_border_width (6);
152         top_box.pack_end (*v2_box, false, false);
153         top_box.pack_end (*v1_box, false, false);
155         set_spacing (6);
156         pack_start (top_box, false, false);
157         pack_start (low_box, false, false);
159         preset_label.show ();
160         preset_combo.show ();
161         automation_mode_label.show ();
162         automation_mode_selector.show ();
163         bypass_button.show ();
164         top_box.show ();
165         low_box.show ();
167         _activating_from_app = false;
168         cocoa_parent = 0;
169         _notify = 0;
170         cocoa_window = 0;
171         au_view = 0;
172         packView = 0;
173         editView = 0;
175         /* prefer cocoa, fall back to cocoa, but use carbon if its there */
177         if (test_cocoa_view_support()) {
178                 create_cocoa_view ();
179         } else if (test_carbon_view_support()) {
180                 create_carbon_view ();
181         } else {
182                 create_cocoa_view ();
183         }
185         low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
188 AUPluginUI::~AUPluginUI ()
190         if (cocoa_parent) {
191                 NSWindow* win = get_nswindow();
192                 [[NSNotificationCenter defaultCenter] removeObserver:_notify];
193                 [win removeChildWindow:cocoa_parent];
195         } else if (carbon_window) {
196                 /* not parented, just overlaid on top of our window */
197                 DisposeWindow (carbon_window);
198         }
200         if (editView) {
201                 CloseComponent (editView);
202         }
204         if (packView) {
205                 /* remove whatever we packed into low_box so that GTK doesn't
206                    mess with it.
207                 */
209                 [packView removeFromSuperview];
210         }
213 bool
214 AUPluginUI::test_carbon_view_support ()
216         bool ret = false;
217         
218         carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
219         carbon_descriptor.componentSubType = 'gnrc';
220         carbon_descriptor.componentManufacturer = 'appl';
221         carbon_descriptor.componentFlags = 0;
222         carbon_descriptor.componentFlagsMask = 0;
223         
224         OSStatus err;
226         // ask the AU for its first editor component
227         UInt32 propertySize;
228         err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
229         if (!err) {
230                 int nEditors = propertySize / sizeof(ComponentDescription);
231                 ComponentDescription *editors = new ComponentDescription[nEditors];
232                 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
233                 if (!err) {
234                         // just pick the first one for now
235                         carbon_descriptor = editors[0];
236                         ret = true;
237                 }
238                 delete[] editors;
239         }
241         return ret;
243         
244 bool
245 AUPluginUI::test_cocoa_view_support ()
247         UInt32 dataSize   = 0;
248         Boolean isWritable = 0;
249         OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
250                                                 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
251                                                 0, &dataSize, &isWritable);
252         
253         return dataSize > 0 && err == noErr;
256 bool
257 AUPluginUI::plugin_class_valid (Class pluginClass)
259         if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
260                 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
261                    [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
262                                 return true;
263                 }
264         }
265         return false;
269 AUPluginUI::create_cocoa_view ()
271         BOOL wasAbleToLoadCustomView = NO;
272         AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
273         UInt32               numberOfClasses = 0;
274         UInt32     dataSize;
275         Boolean    isWritable;
276         NSString*           factoryClassName = 0;
277         NSURL*              CocoaViewBundlePath;
279         OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
280                                                     kAudioUnitProperty_CocoaUI,
281                                                     kAudioUnitScope_Global, 
282                                                     0,
283                                                     &dataSize,
284                                                     &isWritable );
286         numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
287         
288         // Does view have custom Cocoa UI?
289         
290         if ((result == noErr) && (numberOfClasses > 0) ) {
291                 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
292                 if(AudioUnitGetProperty(*au->get_au(),
293                                         kAudioUnitProperty_CocoaUI,
294                                         kAudioUnitScope_Global,
295                                         0,
296                                         cocoaViewInfo,
297                                         &dataSize) == noErr) {
299                         CocoaViewBundlePath     = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
300                         
301                         // we only take the first view in this example.
302                         factoryClassName        = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
304                 } else {
306                         if (cocoaViewInfo != NULL) {
307                                 free (cocoaViewInfo);
308                                 cocoaViewInfo = NULL;
309                         }
310                 }
311         }
313         NSRect crect = { { 0, 0 }, { 1, 1} };
315         // [A] Show custom UI if view has it
317         if (CocoaViewBundlePath && factoryClassName) {
318                 NSBundle *viewBundle    = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
319                 if (viewBundle == nil) {
320                         error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
321                         return -1;
322                 } else {
323                         Class factoryClass = [viewBundle classNamed:factoryClassName];
324                         if (!factoryClass) {
325                                 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
326                                 return -1;
327                         }
328                         
329                         // make sure 'factoryClass' implements the AUCocoaUIBase protocol
330                         if (!plugin_class_valid (factoryClass)) {
331                                 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
332                                 return -1;
333                         }
334                         // make a factory
335                         id factoryInstance = [[[factoryClass alloc] init] autorelease];
336                         if (factoryInstance == nil) {
337                                 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
338                                 return -1;
339                         }
341                         // make a view
342                         au_view = [factoryInstance uiViewForAudioUnit:*au->get_au() withSize:crect.size];
343                         
344                         // cleanup
345                         [CocoaViewBundlePath release];
346                         if (cocoaViewInfo) {
347                                 UInt32 i;
348                                 for (i = 0; i < numberOfClasses; i++)
349                                         CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
350                                 
351                                 free (cocoaViewInfo);
352                         }
353                         wasAbleToLoadCustomView = YES;
354                 }
355         }
357         if (!wasAbleToLoadCustomView) {
358                 // load generic Cocoa view
359                 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
360                 [(AUGenericView *)au_view setShowsExpertParameters:YES];
361         }
363         packView = au_view;
365         // Get the size of the new AU View's frame 
366         
367         NSRect packFrame;
368         packFrame = [au_view frame];
369         prefwidth = packFrame.size.width;
370         prefheight = packFrame.size.height;
372         return 0;
376 AUPluginUI::create_carbon_view ()
378         OSStatus err;
379         ControlRef root_control;
381         Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
382         
383         OpenAComponent(editComponent, &editView);
384         if (!editView) {
385                 error << _("AU Carbon view: cannot open AU Component") << endmsg;
386                 return -1;
387         }
388         
389         Rect r = { 100, 100, 100, 100 };
390         WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
391                                                   kWindowCompositingAttribute|
392                                                   kWindowNoShadowAttribute|
393                                                   kWindowNoTitleBarAttribute);
395         if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
396                 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
397                 CloseComponent (editView);
398                 return -1;
399         }
400         
401         if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
402                 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
403                 DisposeWindow (carbon_window);
404                 CloseComponent (editView);
405                 return -1;
406         }
408         ControlRef viewPane;
409         Float32Point location  = { 0.0, 0.0 };
410         Float32Point size = { 0.0, 0.0 } ;
412         if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
413                 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
414                 DisposeWindow (carbon_window);
415                 CloseComponent (editView);
416                 return -1;
417         }
419         // resize window
421         Rect bounds;
422         GetControlBounds(viewPane, &bounds);
423         size.x = bounds.right-bounds.left;
424         size.y = bounds.bottom-bounds.top;
426         prefwidth = (int) (size.x + 0.5);
427         prefheight = (int) (size.y + 0.5);
429         SizeWindow (carbon_window, prefwidth, prefheight,  true);
430         low_box.set_size_request (prefwidth, prefheight);
432         return 0;
435 NSWindow*
436 AUPluginUI::get_nswindow ()
438         Gtk::Container* toplevel = get_toplevel();
440         if (!toplevel || !toplevel->is_toplevel()) {
441                 error << _("AUPluginUI: no top level window!") << endmsg;
442                 return 0;
443         }
445         NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
447         if (!true_parent) {
448                 error << _("AUPluginUI: no top level window!") << endmsg;
449                 return 0;
450         }
452         return true_parent;
455 void
456 AUPluginUI::activate ()
458         ActivateWindow (carbon_window, TRUE);
459         // [cocoa_parent makeKeyAndOrderFront:nil];
462 void
463 AUPluginUI::deactivate ()
465         ActivateWindow (carbon_window, FALSE);
469 AUPluginUI::parent_carbon_window ()
471         NSWindow* win = get_nswindow ();
472         int x, y;
474         if (!win) {
475                 return -1;
476         }
478         Gtk::Container* toplevel = get_toplevel();
480         if (!toplevel || !toplevel->is_toplevel()) {
481                 error << _("AUPluginUI: no top level window!") << endmsg;
482                 return -1;
483         }
484         
485         toplevel->get_window()->get_root_origin (x, y);
487         /* compute how tall the title bar is, because we have to offset the position of the carbon window
488            by that much.
489         */
491         NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
492         NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
494         int titlebar_height = wm_frame.size.height - content_frame.size.height;
496         int packing_extra = 6; // this is the total vertical packing in our top level window
498         MoveWindow (carbon_window, x, y + titlebar_height + top_box.get_height() + packing_extra, false);
499         ShowWindow (carbon_window);
501         // create the cocoa window for the carbon one and make it visible
502         cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
504         SetWindowActivationScope (carbon_window, kWindowActivationScopeNone);
506         _notify = [ [NotificationObject alloc] initWithPluginUI:this andCocoaParent:cocoa_parent andTopLevelParent:win ]; 
508         [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
510         return 0;
511 }       
514 AUPluginUI::parent_cocoa_window ()
516         NSWindow* win = get_nswindow ();
517         NSRect packFrame;
519         if (!win) {
520                 return -1;
521         }
523         [win setAutodisplay:YES]; // turn of GTK stuff for this window
525         Gtk::Container* toplevel = get_toplevel();
527         if (!toplevel || !toplevel->is_toplevel()) {
528                 error << _("AUPluginUI: no top level window!") << endmsg;
529                 return -1;
530         }
531         
532         // Get the size of the new AU View's frame 
533         packFrame = [au_view frame];
535         NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
537         [view setFrame:packFrame];
538         [view addSubview:packView]; 
540         low_box.set_size_request (packFrame.size.width, packFrame.size.height);
542         return 0;
545 void
546 AUPluginUI::on_realize ()
548         VBox::on_realize ();
550         /* our windows should not have that resize indicator */
552         NSWindow* win = get_nswindow ();
553         if (win) {
554                 [win setShowsResizeIndicator:NO];
555         }
558 void
559 AUPluginUI::lower_box_realized ()
561         if (au_view) {
562                 parent_cocoa_window ();
563         } else if (carbon_window) {
564                 parent_carbon_window ();
565         }
568 bool
569 AUPluginUI::on_map_event (GdkEventAny* ev)
571         return false;
574 void
575 AUPluginUI::on_window_hide ()
577         if (carbon_window) {
578                 HideWindow (carbon_window);
579                 ActivateWindow (carbon_window, FALSE);
580         }
582         hide_all ();
585 bool
586 AUPluginUI::on_window_show (const Glib::ustring& title)
588         /* this is idempotent so just call it every time we show the window */
590         gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
592         show_all ();
594         if (carbon_window) {
595                 ShowWindow (carbon_window);
596                 ActivateWindow (carbon_window, TRUE);
597         }
599         return true;
602 bool
603 AUPluginUI::start_updating (GdkEventAny* any)
605         return false;
608 bool
609 AUPluginUI::stop_updating (GdkEventAny* any)
611         return false;
614 PlugUIBase*
615 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
617         AUPluginUI* aup = new AUPluginUI (plugin_insert);
618         (*box) = aup;
619         return aup;
622 bool
623 AUPluginUI::on_focus_in_event (GdkEventFocus* ev)
625         //cerr << "au plugin focus in\n";
626         //Keyboard::magic_widget_grab_focus ();
627         return false;
630 bool
631 AUPluginUI::on_focus_out_event (GdkEventFocus* ev)
633         //cerr << "au plugin focus out\n";
634         //Keyboard::magic_widget_drop_focus ();
635         return false;