fix SVN merge SNAFU causing double deletion
[ardour.git] / libs / ardour / redirect.cc
1 /*
2     Copyright (C) 2001 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include <fstream>
22 #include <algorithm>
23 #include <string>
24 #include <cerrno>
25 #include <unistd.h>
26 #include <sstream>
27
28 #include <sigc++/bind.h>
29
30 #include <pbd/xml++.h>
31
32 #include <ardour/redirect.h>
33 #include <ardour/session.h>
34 #include <ardour/utils.h>
35 #include <ardour/send.h>
36 #include <ardour/insert.h>
37
38 #include "i18n.h"
39
40 using namespace std;
41 using namespace ARDOUR;
42 using namespace PBD;
43
44 const string Redirect::state_node_name = "Redirect";
45 sigc::signal<void,Redirect*> Redirect::RedirectCreated;
46
47 Redirect::Redirect (Session& s, const string& name, Placement p,
48
49                     int input_min, int input_max, int output_min, int output_max)
50         : IO (s, name, input_min, input_max, output_min, output_max)
51 {
52         _placement = p;
53         _active = false;
54         _sort_key = 0;
55         _gui = 0;
56         _extra_xml = 0;
57 }
58
59 Redirect::~Redirect ()
60 {
61 }
62
63 boost::shared_ptr<Redirect>
64 Redirect::clone (boost::shared_ptr<const Redirect> other)
65 {
66         boost::shared_ptr<const Send> send;
67         boost::shared_ptr<const PortInsert> port_insert;
68         boost::shared_ptr<const PluginInsert> plugin_insert;
69
70         if ((send = boost::dynamic_pointer_cast<const Send>(other)) != 0) {
71                 return boost::shared_ptr<Redirect> (new Send (*send));
72         } else if ((port_insert = boost::dynamic_pointer_cast<const PortInsert>(other)) != 0) {
73                 return boost::shared_ptr<Redirect> (new PortInsert (*port_insert));
74         } else if ((plugin_insert = boost::dynamic_pointer_cast<const PluginInsert>(other)) != 0) {
75                 return boost::shared_ptr<Redirect> (new PluginInsert (*plugin_insert));
76         } else {
77                 fatal << _("programming error: unknown Redirect type in Redirect::Clone!\n")
78                       << endmsg;
79                 /*NOTREACHED*/
80         }
81         return boost::shared_ptr<Redirect>();
82 }
83
84 void
85 Redirect::set_sort_key (uint32_t key)
86 {
87         _sort_key = key;
88 }
89         
90 void
91 Redirect::set_placement (Placement p, void *src)
92 {
93         if (_placement != p) {
94                 _placement = p;
95                  placement_changed (this, src); /* EMIT SIGNAL */
96         }
97 }
98
99 void
100 Redirect::set_placement (const string& str, void *src)
101 {
102         if (str == _("pre")) {
103                 set_placement (PreFader, this);
104         } else if (str == _("post")) {
105                 set_placement (PostFader, this);
106         } else {
107                 error << string_compose(_("Redirect: unknown placement string \"%1\" (ignored)"), str) << endmsg;
108         }
109 }
110
111 int
112 Redirect::set_automation_state (const XMLNode& node)
113 {
114         /* NODE STRUCTURE 
115
116            <Automation [optionally with visible="...." ]>
117              <parameter-N>
118                <events>
119                X1 Y1
120                X2 Y2
121                ....
122                </events>
123              </parameter-N>
124             <Automation>
125  
126         */
127
128         Glib::Mutex::Lock lm (_automation_lock);
129
130         parameter_automation.clear ();
131
132         XMLNodeList nlist = node.children();
133         XMLNodeIterator niter;
134
135         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
136                 uint32_t param;
137
138                 if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, &param) != 1) {
139                         error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
140                         continue;
141                 }
142
143                 AutomationList& al = automation_list (param);
144                 if (al.set_state (*(*niter)->children().front())) {
145                         goto bad;
146                 }
147         }
148
149         return 0;
150
151   bad:
152         error << string_compose(_("%1: cannot load automation data from XML"), _name) << endmsg;
153         parameter_automation.clear ();
154         return -1;
155 }
156
157 XMLNode&
158 Redirect::get_automation_state ()
159 {
160         /* NODE STRUCTURE 
161
162            <Automation [optionally with visible="...." ]>
163              <parameter-N>
164                <events>
165                X1 Y1
166                X2 Y2
167                ....
168                </events>
169              </parameter-N>
170             <Automation>
171  
172         */
173
174         Glib::Mutex::Lock lm (_automation_lock);
175         XMLNode* node = new XMLNode (X_("Automation"));
176         string fullpath;
177
178         if (parameter_automation.empty()) {
179                 return *node;
180         }
181
182         map<uint32_t,AutomationList*>::iterator li;
183         
184         for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
185         
186                 XMLNode* child;
187                 
188                 char buf[64];
189                 stringstream str;
190                 snprintf (buf, sizeof (buf), "parameter-%" PRIu32, li->first);
191                 child = new XMLNode (buf);
192                 child->add_child_nocopy (li->second->get_state ());
193         }
194
195         return *node;
196 }
197
198 XMLNode&
199 Redirect::get_state (void)
200 {
201         return state (true);
202 }
203
204 XMLNode&
205 Redirect::state (bool full_state)
206 {
207         XMLNode* node = new XMLNode (state_node_name);
208         stringstream sstr;
209
210         node->add_property("active", active() ? "yes" : "no");  
211         node->add_property("placement", placement_as_string (placement()));
212         node->add_child_nocopy (IO::state (full_state));
213
214         if (_extra_xml){
215                 node->add_child_copy (*_extra_xml);
216         }
217         
218         if (full_state) {
219
220                 /* NODE STRUCTURE 
221                    
222                 <Automation [optionally with visible="...." ]>
223                    <parameter-N>
224                      <events>
225                      X1 Y1
226                      X2 Y2
227                      ....
228                      </events>
229                   </parameter-N>
230                 <Automation>
231  
232                 */
233
234                 XMLNode& automation = get_automation_state(); 
235                 
236                 for (set<uint32_t>::iterator x = visible_parameter_automation.begin(); x != visible_parameter_automation.end(); ++x) {
237                         if (x != visible_parameter_automation.begin()) {
238                                 sstr << ' ';
239                         }
240                         sstr << *x;
241                 }
242
243                 automation.add_property ("visible", sstr.str());
244
245                 node->add_child_nocopy (automation);
246         }
247
248         return *node;
249 }
250
251 void
252 Redirect::what_has_automation (set<uint32_t>& s) const
253 {
254         Glib::Mutex::Lock lm (_automation_lock);
255         map<uint32_t,AutomationList*>::const_iterator li;
256         
257         for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
258                 s.insert  ((*li).first);
259         }
260 }
261
262 void
263 Redirect::what_has_visible_automation (set<uint32_t>& s) const
264 {
265         Glib::Mutex::Lock lm (_automation_lock);
266         set<uint32_t>::const_iterator li;
267         
268         for (li = visible_parameter_automation.begin(); li != visible_parameter_automation.end(); ++li) {
269                 s.insert  (*li);
270         }
271 }
272
273 int
274 Redirect::set_state (const XMLNode& node)
275 {
276         const XMLProperty *prop;
277
278         if (node.name() != state_node_name) {
279                 error << string_compose(_("incorrect XML node \"%1\" passed to Redirect object"), node.name()) << endmsg;
280                 return -1;
281         }
282
283         XMLNodeList nlist = node.children();
284         XMLNodeIterator niter;
285         bool have_io = false;
286
287         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
288
289                 if ((*niter)->name() == IO::state_node_name) {
290
291                         IO::set_state (**niter);
292                         have_io = true;
293
294                 } else if ((*niter)->name() == "Automation") {
295
296                         XMLProperty *prop;
297                         
298                         if ((prop = (*niter)->property ("path")) != 0) {
299                                 warning << string_compose (_("old automation data found for %1, ignored"), _name) << endmsg;
300                         } else {
301                                 set_automation_state (*(*niter));
302                         }
303
304                         if ((prop = (*niter)->property ("visible")) != 0) {
305                                 uint32_t what;
306                                 stringstream sstr;
307
308                                 visible_parameter_automation.clear ();
309                                 
310                                 sstr << prop->value();
311                                 while (1) {
312                                         sstr >> what;
313                                         if (sstr.fail()) {
314                                                 break;
315                                         }
316                                         mark_automation_visible (what, true);
317                                 }
318                         }
319
320                 } else if ((*niter)->name() == "extra") {
321                         _extra_xml = new XMLNode (*(*niter));
322                 }
323         }
324
325         if (!have_io) {
326                 error << _("XML node describing an IO is missing an IO node") << endmsg;
327                 return -1;
328         }
329
330         if ((prop = node.property ("active")) == 0) {
331                 error << _("XML node describing a redirect is missing the `active' field") << endmsg;
332                 return -1;
333         }
334
335         if (_active != (prop->value() == "yes")) {
336                 _active = !_active;
337                 active_changed (this, this); /* EMIT_SIGNAL */
338         }
339         
340         if ((prop = node.property ("placement")) == 0) {
341                 error << _("XML node describing a redirect is missing the `placement' field") << endmsg;
342                 return -1;
343         }
344
345         set_placement (prop->value(), this);
346
347         return 0;
348 }
349
350 AutomationList&
351 Redirect::automation_list (uint32_t parameter)
352 {
353         AutomationList* al = parameter_automation[parameter];
354
355         if (al == 0) {
356                 al = parameter_automation[parameter] = new AutomationList (default_parameter_value (parameter));
357                 /* let derived classes do whatever they need with this */
358                 automation_list_creation_callback (parameter, *al);
359         }
360
361         return *al;
362 }
363
364 string
365 Redirect::describe_parameter (uint32_t which)
366 {
367         /* derived classes will override this */
368         return "";
369 }
370
371 void
372 Redirect::can_automate (uint32_t what)
373 {
374         can_automate_list.insert (what);
375 }
376
377 void
378 Redirect::mark_automation_visible (uint32_t what, bool yn)
379 {
380         if (yn) {
381                 visible_parameter_automation.insert (what);
382         } else {
383                 set<uint32_t>::iterator i;
384
385                 if ((i = visible_parameter_automation.find (what)) != visible_parameter_automation.end()) {
386                         visible_parameter_automation.erase (i);
387                 }
388         }
389 }
390
391 bool
392 Redirect::find_next_event (nframes_t now, nframes_t end, ControlEvent& next_event) const
393 {
394         map<uint32_t,AutomationList*>::const_iterator li;       
395         AutomationList::TimeComparator cmp;
396
397         next_event.when = max_frames;
398         
399         for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
400                 
401                 AutomationList::const_iterator i;
402                 const AutomationList& alist (*((*li).second));
403                 ControlEvent cp (now, 0.0f);
404                 
405                 for (i = lower_bound (alist.const_begin(), alist.const_end(), &cp, cmp); i != alist.const_end() && (*i)->when < end; ++i) {
406                         if ((*i)->when > now) {
407                                 break; 
408                         }
409                 }
410                 
411                 if (i != alist.const_end() && (*i)->when < end) {
412                         
413                         if ((*i)->when < next_event.when) {
414                                 next_event.when = (*i)->when;
415                         }
416                 }
417         }
418
419         return next_event.when != max_frames;
420 }
421
422 void
423 Redirect::set_active (bool yn, void* src)
424 {
425         _active = yn; 
426         active_changed (this, src); 
427         _session.set_dirty ();
428 }
429