Remove random access interface from MidiBuffer, towards killing fixed/limited event...
[ardour.git] / gtk2_ardour / au_pluginui.cc
1 /*
2     Copyright (C) 2006 Paul Davis 
3         Written by Taybin Rutkin
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <ardour/audio_unit.h>
22 #include <ardour/processor.h>
23
24 #include <gtkmm2ext/doi.h>
25
26 #include "au_pluginui.h"
27 #include "gui_thread.h"
28
29 #include <appleutility/CAAudioUnit.h>
30 #include <appleutility/CAComponent.h>
31
32 #include <AudioUnit/AudioUnit.h>
33
34 #include "i18n.h"
35
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
40 {
41         if ((au = boost::dynamic_pointer_cast<AUPlugin> (insert->plugin())) == 0) {
42                 error << _("unknown type of editor-supplying plugin (note: no AudioUnit support in this version of ardour)") << endmsg;
43                 throw failed_constructor ();
44         }
45
46         OSStatus err = noErr;
47         
48         CAComponentDescription desc;
49         Component carbonViewComponent = NULL;
50         AudioUnitCarbonView carbonView = NULL;
51         
52         GetComponentInfo(au->get_comp()->Comp(), &desc, 0, 0, 0);
53         carbonViewComponent = get_carbon_view_component(desc.componentSubType);
54         err = OpenAComponent(carbonViewComponent, &carbonView);
55         
56         Rect rec;
57         rec.top = 0;
58         rec.left = 0;
59         rec.bottom = 400;
60         rec.right = 500;
61         
62         ProcessSerialNumber ourPSN;
63         
64         /* Here we will set the MacOSX native section of the process to the foreground for putting up this
65      * dialog box.  First step is to get our process serial number.  We do this by calling 
66      * GetCurrentProcess. 
67      * First Argument: On success this PSN will be our PSN on return.
68      * Return Value: A Macintosh error indicating success or failure.
69      */
70     err = GetCurrentProcess(&ourPSN);
71     
72     //If no error then set this process to be frontmost.
73     if (err == noErr) {
74         /* Calling SetFrontProcess to make us frontmost.
75          * First Argument: The Process Serial Number of the process we want to make frontmost.  Here
76          *    of course we pass our process serial number
77          * Return Value: An error value indicating success or failure.  We just ignore the return
78          *    value here.
79          */
80         (void)SetFrontProcess(&ourPSN);
81     } else {
82                 error << "couldn't get current process" << endmsg;
83         }
84         
85         err = CreateNewWindow (kDocumentWindowClass, kWindowStandardFloatingAttributes, &rec, &wr);
86         
87         ComponentResult auResult;
88         ControlRef rootControl = NULL;
89         GetRootControl(wr, &rootControl);
90         
91         int width = 500;
92         int height = 400;
93         Float32Point location = {30, 30};
94         Float32Point size = {width, height};
95         ControlRef audioUnitControl = NULL;
96         
97         auResult = AudioUnitCarbonViewCreate(carbonView,
98                                              au->get_au()->AU(),
99                                              wr,
100                                              rootControl,
101                                              &location,
102                                              &size,
103                                              &audioUnitControl);    
104         
105         ShowWindow (wr);
106         BringToFront (wr);
107 //      AudioUnitCarbonViewSetEventListener(carbonView, EventListener, this);
108 #if 0
109         set_name ("PluginEditor");
110         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
111
112         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window*> (this)));
113 #endif  
114
115         insert->GoingAway.connect (mem_fun(*this, &AUPluginUI::plugin_going_away));
116         
117         info << "AUPluginUI created" << endmsg;
118 }
119
120 AUPluginUI::~AUPluginUI ()
121 {
122         // nothing to do here - plugin destructor destroys the GUI
123 }
124
125 void
126 AUPluginUI::plugin_going_away (ARDOUR::IOProcessor* ignored)
127 {
128         ENSURE_GUI_THREAD(bind (mem_fun(*this, &AUPluginUI::plugin_going_away), ignored));
129
130         delete_when_idle (this);
131 }
132
133 Component
134 AUPluginUI::get_carbon_view_component(OSType subtype)
135 {
136         ComponentDescription desc;
137         Component component;
138         
139         desc.componentType = kAudioUnitCarbonViewComponentType; // 'auvw'
140         desc.componentSubType = subtype;
141         desc.componentManufacturer = 0;
142         desc.componentFlags = 0;
143         desc.componentFlagsMask = 0;
144         
145         // First see if we can find a carbon view designed specifically for this
146         // plug-in:
147         
148         component = FindNextComponent(NULL, &desc);
149         if (component)
150            return component;
151         
152         // If not, grab the generic carbon view, which will create a GUI for
153         // any Audio Unit.
154         
155         desc.componentSubType = kAUCarbonViewSubType_Generic;
156         component = FindNextComponent(NULL, &desc);
157         
158         return component;
159 }
160