put man pages here at top level
[ardour2.git] / gtk2_ardour / au_pluginui.mm
bloba46021f66fd04cd952133b0f65ea18c61ef16a89
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 AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
49         : PlugUIBase (insert)
50         , automation_mode_label (_("Automation"))
51         , preset_label (_("Presets"))
52         
54         if (automation_mode_strings.empty()) {
55                 automation_mode_strings = I18N (_automation_mode_strings);
56         }
57         
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 ();
64         }
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);
90         set_spacing (6);
91         pack_start (top_box, false, false);
92         pack_start (low_box, false, false);
94         preset_label.show ();
95         preset_combo.show ();
96         automation_mode_label.show ();
97         automation_mode_selector.show ();
98         bypass_button.show ();
99         top_box.show ();
100         low_box.show ();
102         _activating_from_app = false;
103         cocoa_parent = 0;
104         cocoa_window = 0;
105         au_view = 0;
106         packView = 0;
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 ();
114         } else {
115                 create_cocoa_view ();
116         }
118         low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
121 AUPluginUI::~AUPluginUI ()
123         if (cocoa_parent) {
124                 NSWindow* win = get_nswindow();
125                 RemoveEventHandler(carbon_event_handler);
126                 [win removeChildWindow:cocoa_parent];
127         } else if (carbon_window) {
128                 /* never parented */
129                 DisposeWindow (carbon_window);
130         }
132         if (packView && packView != au_view) {
133                 [packView release];
134         }
137 bool
138 AUPluginUI::test_carbon_view_support ()
140         bool ret = false;
141         
142         carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
143         carbon_descriptor.componentSubType = 'gnrc';
144         carbon_descriptor.componentManufacturer = 'appl';
145         carbon_descriptor.componentFlags = 0;
146         carbon_descriptor.componentFlagsMask = 0;
147         
148         OSStatus err;
150         // ask the AU for its first editor component
151         UInt32 propertySize;
152         err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
153         if (!err) {
154                 int nEditors = propertySize / sizeof(ComponentDescription);
155                 ComponentDescription *editors = new ComponentDescription[nEditors];
156                 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
157                 if (!err) {
158                         // just pick the first one for now
159                         carbon_descriptor = editors[0];
160                         ret = true;
161                 }
162                 delete[] editors;
163         }
165         return ret;
167         
168 bool
169 AUPluginUI::test_cocoa_view_support ()
171         UInt32 dataSize   = 0;
172         Boolean isWritable = 0;
173         OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
174                                                 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
175                                                 0, &dataSize, &isWritable);
176         
177         return dataSize > 0 && err == noErr;
180 bool
181 AUPluginUI::plugin_class_valid (Class pluginClass)
183         if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
184                 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
185                    [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
186                                 return true;
187                 }
188         }
189         return false;
193 AUPluginUI::create_cocoa_view ()
195         BOOL wasAbleToLoadCustomView = NO;
196         AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
197         UInt32               numberOfClasses = 0;
198         UInt32     dataSize;
199         Boolean    isWritable;
200         NSString*           factoryClassName = 0;
201         NSURL*              CocoaViewBundlePath;
203         OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
204                                                     kAudioUnitProperty_CocoaUI,
205                                                     kAudioUnitScope_Global, 
206                                                     0,
207                                                     &dataSize,
208                                                     &isWritable );
210         numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
211         
212         // Does view have custom Cocoa UI?
213         
214         if ((result == noErr) && (numberOfClasses > 0) ) {
215                 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
216                 if(AudioUnitGetProperty(*au->get_au(),
217                                         kAudioUnitProperty_CocoaUI,
218                                         kAudioUnitScope_Global,
219                                         0,
220                                         cocoaViewInfo,
221                                         &dataSize) == noErr) {
223                         CocoaViewBundlePath     = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
224                         
225                         // we only take the first view in this example.
226                         factoryClassName        = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
228                 } else {
230                         if (cocoaViewInfo != NULL) {
231                                 free (cocoaViewInfo);
232                                 cocoaViewInfo = NULL;
233                         }
234                 }
235         }
237         NSRect crect = { { 0, 0 }, { 1, 1} };
239         // [A] Show custom UI if view has it
241         if (CocoaViewBundlePath && factoryClassName) {
242                 NSBundle *viewBundle    = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
243                 if (viewBundle == nil) {
244                         error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
245                         return -1;
246                 } else {
247                         Class factoryClass = [viewBundle classNamed:factoryClassName];
248                         if (!factoryClass) {
249                                 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
250                                 return -1;
251                         }
252                         
253                         // make sure 'factoryClass' implements the AUCocoaUIBase protocol
254                         if (!plugin_class_valid (factoryClass)) {
255                                 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
256                                 return -1;
257                         }
258                         // make a factory
259                         id factoryInstance = [[[factoryClass alloc] init] autorelease];
260                         if (factoryInstance == nil) {
261                                 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
262                                 return -1;
263                         }
265                         // make a view
266                         au_view = [factoryInstance uiViewForAudioUnit:*au->get_au() withSize:crect.size];
267                         
268                         // cleanup
269                         [CocoaViewBundlePath release];
270                         if (cocoaViewInfo) {
271                                 UInt32 i;
272                                 for (i = 0; i < numberOfClasses; i++)
273                                         CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
274                                 
275                                 free (cocoaViewInfo);
276                         }
277                         wasAbleToLoadCustomView = YES;
278                 }
279         }
281         if (!wasAbleToLoadCustomView) {
282                 // load generic Cocoa view
283                 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
284                 [(AUGenericView *)au_view setShowsExpertParameters:YES];
285         }
287         NSRect packFrame;
289         // Get the size of the new AU View's frame 
290         packFrame = [au_view frame];
292         packFrame.origin.x = 0;
293         packFrame.origin.y = 0;
295         if (packFrame.size.width > 500 || packFrame.size.height > 500) {
296                 
297                 /* its too big - use a scrollview */
299                 NSRect frameRect = [[cocoa_window contentView] frame];
300                 scroll_view = [[[NSScrollView alloc] initWithFrame:frameRect] autorelease];
301                 [scroll_view setDrawsBackground:NO];
302                 [scroll_view setHasHorizontalScroller:YES];
303                 [scroll_view setHasVerticalScroller:YES];
305                 packFrame.size = [NSScrollView  frameSizeForContentSize:packFrame.size
306                                     hasHorizontalScroller:[scroll_view hasHorizontalScroller]
307                                     hasVerticalScroller:[scroll_view hasVerticalScroller]
308                                     borderType:[scroll_view borderType]];
309                 
310                 // Create a new frame with same origin as current
311                 // frame but size equal to the size of the new view
312                 NSRect newFrame;
313                 newFrame.origin = [scroll_view frame].origin;
314                 newFrame.size = packFrame.size;
315                 
316                 // Set the new frame and document views on the scroll view
317                 [scroll_view setFrame:newFrame];
318                 [scroll_view setDocumentView:au_view];
319                 
320                 packView = scroll_view;
322         } else {
324                 packView = au_view;
325         }
327         prefwidth = packFrame.size.width;
328         prefheight = packFrame.size.height;
330         return 0;
334 AUPluginUI::create_carbon_view ()
336         OSStatus err;
337         ControlRef root_control;
339         Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
340         
341         OpenAComponent(editComponent, &editView);
342         if (!editView) {
343                 error << _("AU Carbon view: cannot open AU Component") << endmsg;
344                 return -1;
345         }
346         
347         Rect r = { 100, 100, 100, 100 };
348         WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
349                                                   kWindowCompositingAttribute|
350                                                   kWindowNoShadowAttribute|
351                                                   kWindowNoTitleBarAttribute);
353         if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
354                 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
355                 return -1;
356         }
357         
358         if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
359                 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
360                 return -1;
361         }
363         ControlRef viewPane;
364         Float32Point location  = { 0.0, 0.0 };
365         Float32Point size = { 0.0, 0.0 } ;
367         if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
368                 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
369                 return -1;
370         }
372         // resize window
374         Rect bounds;
375         GetControlBounds(viewPane, &bounds);
376         size.x = bounds.right-bounds.left;
377         size.y = bounds.bottom-bounds.top;
379         prefwidth = (int) (size.x + 0.5);
380         prefheight = (int) (size.y + 0.5);
382         SizeWindow (carbon_window, prefwidth, prefheight,  true);
383         low_box.set_size_request (prefwidth, prefheight);
385         return 0;
388 NSWindow*
389 AUPluginUI::get_nswindow ()
391         Gtk::Container* toplevel = get_toplevel();
393         if (!toplevel || !toplevel->is_toplevel()) {
394                 error << _("AUPluginUI: no top level window!") << endmsg;
395                 return 0;
396         }
398         NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
400         if (!true_parent) {
401                 error << _("AUPluginUI: no top level window!") << endmsg;
402                 return 0;
403         }
405         return true_parent;
408 void
409 AUPluginUI::activate ()
411         if (carbon_window && cocoa_parent) {
412                 cerr << "APP activated, activate carbon window " << insert->name() << endl;
413                 _activating_from_app = true;
414                 ActivateWindow (carbon_window, TRUE);
415                 _activating_from_app = false;
416                 [cocoa_parent makeKeyAndOrderFront:nil];
417         } 
420 void
421 AUPluginUI::deactivate ()
423         return;
424         cerr << "APP DEactivated, for " << insert->name() << endl;
425         _activating_from_app = true;
426         ActivateWindow (carbon_window, FALSE);
427         _activating_from_app = false;
431 OSStatus 
432 _carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData) 
434         return ((AUPluginUI*)userData)->carbon_event (nextHandlerRef, event);
437 OSStatus 
438 AUPluginUI::carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event)
440         cerr << "CARBON EVENT\n";
442         UInt32 eventKind = GetEventKind(event);
443         ClickActivationResult howToHandleClick;
444         NSWindow* win = get_nswindow ();
446         cerr << "window " << win << " carbon event type " << eventKind << endl;
448         switch (eventKind) {
449         case kEventWindowHandleActivate:
450                 cerr << "carbon window for " << insert->name() << " activated\n";
451                 if (_activating_from_app) {
452                         cerr << "app activation, ignore window activation\n";
453                         return noErr;
454                 }
455                 [win makeMainWindow];
456                 return eventNotHandledErr;
457                 break;
459         case kEventWindowHandleDeactivate:
460                 cerr << "carbon window for " << insert->name() << " would have been deactivated\n";
461                 // never deactivate the carbon window
462                 return noErr;
463                 break;
464                 
465         case kEventWindowGetClickActivation:
466                 cerr << "carbon window CLICK activated\n";
467                 [win makeKeyAndOrderFront:nil];
468                 howToHandleClick = kActivateAndHandleClick;
469                 SetEventParameter(event, kEventParamClickActivation, typeClickActivationResult, 
470                                   sizeof(ClickActivationResult), &howToHandleClick);
471                 break;
472         }
474         return noErr;
478 AUPluginUI::parent_carbon_window ()
480         NSWindow* win = get_nswindow ();
481         int x, y;
483         if (!win) {
484                 return -1;
485         }
487         Gtk::Container* toplevel = get_toplevel();
489         if (!toplevel || !toplevel->is_toplevel()) {
490                 error << _("AUPluginUI: no top level window!") << endmsg;
491                 return -1;
492         }
493         
494         toplevel->get_window()->get_root_origin (x, y);
496         /* compute how tall the title bar is, because we have to offset the position of the carbon window
497            by that much.
498         */
500         NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
501         NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
503         int titlebar_height = wm_frame.size.height - content_frame.size.height;
505         int packing_extra = 6; // this is the total vertical packing in our top level window
507         MoveWindow (carbon_window, x, y + titlebar_height + top_box.get_height() + packing_extra, false);
508         ShowWindow (carbon_window);
510         // create the cocoa window for the carbon one and make it visible
511         cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
513         EventTypeSpec   windowEventTypes[] = {
514                 {kEventClassWindow, kEventWindowGetClickActivation },
515                 {kEventClassWindow, kEventWindowHandleDeactivate }
516         };
517         
518         EventHandlerUPP   ehUPP = NewEventHandlerUPP(_carbon_event);
519         OSStatus result = InstallWindowEventHandler (carbon_window, ehUPP, 
520                                                      sizeof(windowEventTypes) / sizeof(EventTypeSpec), 
521                                                      windowEventTypes, this, &carbon_event_handler);
522         if (result != noErr) {
523                 return -1;
524         }
526         [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
528         return 0;
529 }       
532 AUPluginUI::parent_cocoa_window ()
534         NSWindow* win = get_nswindow ();
535         NSRect packFrame;
537         if (!win) {
538                 return -1;
539         }
541         [win setAutodisplay:YES]; // turn of GTK stuff for this window
543         Gtk::Container* toplevel = get_toplevel();
545         if (!toplevel || !toplevel->is_toplevel()) {
546                 error << _("AUPluginUI: no top level window!") << endmsg;
547                 return -1;
548         }
549         
550         // Get the size of the new AU View's frame 
551         packFrame = [au_view frame];
553         NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
554         
556         [view setFrame:packFrame];
557         [view addSubview:packView]; 
559         low_box.set_size_request (packFrame.size.width, packFrame.size.height);
561         return 0;
564 void
565 AUPluginUI::on_realize ()
567         VBox::on_realize ();
569         /* our windows should not have that resize indicator */
571         NSWindow* win = get_nswindow ();
572         if (win) {
573                 [win setShowsResizeIndicator:NO];
574         }
577 void
578 AUPluginUI::lower_box_realized ()
580         if (au_view) {
581                 parent_cocoa_window ();
582         } else if (carbon_window) {
583                 parent_carbon_window ();
584         }
587 void
588 AUPluginUI::on_hide ()
590         // VBox::on_hide ();
591         cerr << "AU plugin window hidden\n";
594 bool
595 AUPluginUI::on_map_event (GdkEventAny* ev)
597         return false;
600 void
601 AUPluginUI::on_show ()
603         cerr << "AU plugin window shown\n";
605         VBox::on_show ();
607         gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
609         if (au_view) {
610                 show_all ();
611         } else if (carbon_window) {
612                 [cocoa_parent setIsVisible:YES];
613                 ShowWindow (carbon_window);
614         }
617 bool
618 AUPluginUI::start_updating (GdkEventAny* any)
620         return false;
623 bool
624 AUPluginUI::stop_updating (GdkEventAny* any)
626         return false;
629 PlugUIBase*
630 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
632         AUPluginUI* aup = new AUPluginUI (plugin_insert);
633         (*box) = aup;
634         return aup;
637 bool
638 AUPluginUI::on_focus_in_event (GdkEventFocus* ev)
640         //cerr << "au plugin focus in\n";
641         //Keyboard::magic_widget_grab_focus ();
642         return false;
645 bool
646 AUPluginUI::on_focus_out_event (GdkEventFocus* ev)
648         //cerr << "au plugin focus out\n";
649         //Keyboard::magic_widget_drop_focus ();
650         return false;