catch jack shutdown (from server) and handle it better then we used to in terms of...
[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 */
19
20 #include <fstream>
21 #include <algorithm>
22 #include <string>
23 #include <cerrno>
24 #include <unistd.h>
25 #include <sstream>
26
27 #include <sigc++/bind.h>
28
29 #include <glibmm.h>
30
31 #include <pbd/xml++.h>
32 #include <pbd/enumwriter.h>
33
34 #include <ardour/redirect.h>
35 #include <ardour/session.h>
36 #include <ardour/utils.h>
37 #include <ardour/send.h>
38 #include <ardour/insert.h>
39
40 #include "i18n.h"
41
42 using namespace std;
43 using namespace ARDOUR;
44 using namespace PBD;
45
46 const string Redirect::state_node_name = "Redirect";
47 sigc::signal<void,Redirect*> Redirect::RedirectCreated;
48
49 Redirect::Redirect (Session& s, const string& name, Placement p,
50
51                     int input_min, int input_max, int output_min, int output_max)
52         : IO (s, name, input_min, input_max, output_min, output_max)
53 {
54         _placement = p;
55         _active = false;
56         _sort_key = 0;
57         _gui = 0;
58         _extra_xml = 0;
59 }
60
61 Redirect::~Redirect ()
62 {
63         notify_callbacks ();
64 }
65
66 boost::shared_ptr<Redirect>
67 Redirect::clone (boost::shared_ptr<const Redirect> other)
68 {
69         boost::shared_ptr<const Send> send;
70         boost::shared_ptr<const PortInsert> port_insert;
71         boost::shared_ptr<const PluginInsert> plugin_insert;
72
73         if ((send = boost::dynamic_pointer_cast<const Send>(other)) != 0) {
74                 return boost::shared_ptr<Redirect> (new Send (*send));
75         } else if ((port_insert = boost::dynamic_pointer_cast<const PortInsert>(other)) != 0) {
76                 return boost::shared_ptr<Redirect> (new PortInsert (*port_insert));
77         } else if ((plugin_insert = boost::dynamic_pointer_cast<const PluginInsert>(other)) != 0) {
78                 return boost::shared_ptr<Redirect> (new PluginInsert (*plugin_insert));
79         } else {
80                 fatal << _("programming error: unknown Redirect type in Redirect::Clone!\n")
81                       << endmsg;
82                 /*NOTREACHED*/
83         }
84         return boost::shared_ptr<Redirect>();
85 }
86
87 void
88 Redirect::set_sort_key (uint32_t key)
89 {
90         _sort_key = key;
91 }
92         
93 void
94 Redirect::set_placement (Placement p, void *src)
95 {
96         if (_placement != p) {
97                 _placement = p;
98                  placement_changed (this, src); /* EMIT SIGNAL */
99         }
100 }
101
102 /* NODE STRUCTURE 
103    
104     <Automation [optionally with visible="...." ]>
105        <parameter-N>
106          <AutomationList id=N>
107            <events>
108            X1 Y1
109            X2 Y2
110            ....
111            </events>
112        </parameter-N>
113     <Automation>
114 */
115
116 int
117 Redirect::set_automation_state (const XMLNode& node)
118 {
119         Glib::Mutex::Lock lm (_automation_lock);
120
121         parameter_automation.clear ();
122
123         XMLNodeList nlist = node.children();
124         XMLNodeIterator niter;
125
126         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
127                 uint32_t param;
128
129                 if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, &param) != 1) {
130                         error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
131                         continue;
132                 }
133
134                 AutomationList& al = automation_list (param);
135                 if (al.set_state (*(*niter)->children().front())) {
136                         goto bad;
137                 }
138         }
139
140         return 0;
141
142   bad:
143         error << string_compose(_("%1: cannot load automation data from XML"), _name) << endmsg;
144         parameter_automation.clear ();
145         return -1;
146 }
147
148 XMLNode&
149 Redirect::get_automation_state ()
150 {
151         Glib::Mutex::Lock lm (_automation_lock);
152         XMLNode* node = new XMLNode (X_("Automation"));
153         string fullpath;
154
155         if (parameter_automation.empty()) {
156                 return *node;
157         }
158
159         vector<AutomationList*>::iterator li;
160         uint32_t n;
161
162         for (n = 0, li = parameter_automation.begin(); li != parameter_automation.end(); ++li, ++n) {
163         
164                 if (*li) {
165                         XMLNode* child;
166                         
167                         char buf[64];
168                         stringstream str;
169                         snprintf (buf, sizeof (buf), "parameter-%" PRIu32, n);
170                         child = new XMLNode (buf);
171                         child->add_child_nocopy ((*li)->get_state ());
172                 }
173         }
174
175         return *node;
176 }
177
178 XMLNode&
179 Redirect::get_state (void)
180 {
181         return state (true);
182 }
183
184 XMLNode&
185 Redirect::state (bool full_state)
186 {
187         XMLNode* node = new XMLNode (state_node_name);
188         stringstream sstr;
189
190         node->add_property("active", active() ? "yes" : "no");  
191         node->add_property("placement", enum_2_string (_placement));
192         node->add_child_nocopy (IO::state (full_state));
193
194         if (_extra_xml){
195                 node->add_child_copy (*_extra_xml);
196         }
197         
198         if (full_state) {
199
200                 XMLNode& automation = get_automation_state(); 
201                 
202                 for (set<uint32_t>::iterator x = visible_parameter_automation.begin(); x != visible_parameter_automation.end(); ++x) {
203                         if (x != visible_parameter_automation.begin()) {
204                                 sstr << ' ';
205                         }
206                         sstr << *x;
207                 }
208
209                 automation.add_property ("visible", sstr.str());
210
211                 node->add_child_nocopy (automation);
212         }
213
214         return *node;
215 }
216
217
218 int
219 Redirect::set_state (const XMLNode& node)
220 {
221         const XMLProperty *prop;
222
223         if (node.name() != state_node_name) {
224                 error << string_compose(_("incorrect XML node \"%1\" passed to Redirect object"), node.name()) << endmsg;
225                 return -1;
226         }
227
228         XMLNodeList nlist = node.children();
229         XMLNodeIterator niter;
230         bool have_io = false;
231
232         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
233
234                 if ((*niter)->name() == IO::state_node_name) {
235
236                         IO::set_state (**niter);
237                         have_io = true;
238
239                 } else if ((*niter)->name() == X_("Automation")) {
240
241
242                         XMLProperty *prop;
243                         
244                         if ((prop = (*niter)->property ("path")) != 0) {
245                                 old_set_automation_state (*(*niter));
246                         } else {
247                                 set_automation_state (*(*niter));
248                         }
249
250                         if ((prop = (*niter)->property ("visible")) != 0) {
251                                 uint32_t what;
252                                 stringstream sstr;
253
254                                 visible_parameter_automation.clear ();
255                                 
256                                 sstr << prop->value();
257                                 while (1) {
258                                         sstr >> what;
259                                         if (sstr.fail()) {
260                                                 break;
261                                         }
262                                         mark_automation_visible (what, true);
263                                 }
264                         }
265
266                 } else if ((*niter)->name() == "extra") {
267                         _extra_xml = new XMLNode (*(*niter));
268                 }
269         }
270
271         if (!have_io) {
272                 error << _("XML node describing an IO is missing an IO node") << endmsg;
273                 return -1;
274         }
275
276         if ((prop = node.property ("active")) == 0) {
277                 error << _("XML node describing a redirect is missing the `active' field") << endmsg;
278                 return -1;
279         }
280
281         if (_active != (prop->value() == "yes")) {
282                 if (!(_session.state_of_the_state() & Session::Loading) || 
283                     !Session::get_disable_all_loaded_plugins()) {
284                         _active = !_active;
285                         active_changed (this, this); /* EMIT_SIGNAL */
286                 }
287         }
288         
289         if ((prop = node.property ("placement")) == 0) {
290                 error << _("XML node describing a redirect is missing the `placement' field") << endmsg;
291                 return -1;
292         }
293
294         /* hack to handle older sessions before we only used EnumWriter */
295
296         string pstr;
297
298         if (prop->value() == "pre") {
299                 pstr = "PreFader";
300         } else if (prop->value() == "post") {
301                 pstr = "PostFader";
302         } else {
303                 pstr = prop->value();
304         }
305
306         Placement p = Placement (string_2_enum (pstr, p));
307         set_placement (p, this);
308
309         return 0;
310 }
311
312 int
313 Redirect::old_set_automation_state (const XMLNode& node)
314 {
315         const XMLProperty *prop;
316                         
317         if ((prop = node.property ("path")) != 0) {
318                 load_automation (prop->value());
319         } else {
320                 warning << string_compose(_("%1: Automation node has no path property"), _name) << endmsg;
321         }
322         
323         if ((prop = node.property ("visible")) != 0) {
324                 uint32_t what;
325                 stringstream sstr;
326                 
327                 visible_parameter_automation.clear ();
328                 
329                 sstr << prop->value();
330                 while (1) {
331                         sstr >> what;
332                         if (sstr.fail()) {
333                                 break;
334                         }
335                         mark_automation_visible (what, true);
336                 }
337         }
338
339         return 0;
340 }
341
342 int
343 Redirect::load_automation (string path)
344 {
345         string fullpath;
346
347         if (path[0] == '/') { // legacy
348                 fullpath = path;
349         } else {
350                 fullpath = Glib::build_filename(_session.automation_dir(), path);
351         }
352         ifstream in (fullpath.c_str());
353
354         if (!in) {
355                 warning << string_compose(_("%1: cannot open %2 to load automation data (%3)"), _name, fullpath, strerror (errno)) << endmsg;
356                 return 1;
357         }
358
359         Glib::Mutex::Lock lm (_automation_lock);
360         set<uint32_t> tosave;
361         parameter_automation.clear ();
362
363         while (in) {
364                 double when;
365                 double value;
366                 uint32_t port;
367
368                 in >> port;     if (!in) break;
369                 in >> when;  if (!in) goto bad;
370                 in >> value; if (!in) goto bad;
371                 
372                 AutomationList& al = automation_list (port);
373                 al.add (when, value);
374                 tosave.insert (port);
375         }
376         
377         return 0;
378
379   bad:
380         error << string_compose(_("%1: cannot load automation data from %2"), _name, fullpath) << endmsg;
381         parameter_automation.clear ();
382         return -1;
383 }
384
385
386 void
387 Redirect::what_has_automation (set<uint32_t>& s) const
388 {
389         Glib::Mutex::Lock lm (_automation_lock);
390         vector<AutomationList*>::const_iterator li;
391         uint32_t n;
392
393         for (n = 0, li = parameter_automation.begin(); li != parameter_automation.end(); ++li, ++n) {
394                 if (*li) {
395                         s.insert  (n);
396                 }
397         }
398 }
399
400 void
401 Redirect::what_has_visible_automation (set<uint32_t>& s) const
402 {
403         Glib::Mutex::Lock lm (_automation_lock);
404         set<uint32_t>::const_iterator li;
405         
406         for (li = visible_parameter_automation.begin(); li != visible_parameter_automation.end(); ++li) {
407                 s.insert  (*li);
408         }
409 }
410
411 AutomationList&
412 Redirect::automation_list (uint32_t parameter)
413 {
414         AutomationList* al = parameter_automation[parameter];
415         
416         if (al == 0) {
417                 al = parameter_automation[parameter] = new AutomationList (default_parameter_value (parameter));
418                 /* let derived classes do whatever they need with this */
419                 automation_list_creation_callback (parameter, *al);
420         }
421
422         return *al;
423 }
424
425 string
426 Redirect::describe_parameter (uint32_t which)
427 {
428         /* derived classes will override this */
429         return "";
430 }
431
432 void
433 Redirect::can_automate (uint32_t what)
434 {
435         can_automate_list.insert (what);
436 }
437
438 void
439 Redirect::mark_automation_visible (uint32_t what, bool yn)
440 {
441         if (yn) {
442                 visible_parameter_automation.insert (what);
443         } else {
444                 set<uint32_t>::iterator i;
445
446                 if ((i = visible_parameter_automation.find (what)) != visible_parameter_automation.end()) {
447                         visible_parameter_automation.erase (i);
448                 }
449         }
450 }
451
452 bool
453 Redirect::find_next_event (nframes_t now, nframes_t end, ControlEvent& next_event) const
454 {
455         vector<AutomationList*>::const_iterator li;     
456         AutomationList::TimeComparator cmp;
457
458         next_event.when = max_frames;
459
460         for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
461
462                 const AutomationList* alist = *li;
463                 
464                 if (!alist) {
465                         continue;
466                 }
467                 
468                 AutomationList::const_iterator i;
469                 ControlEvent cp (now, 0.0f);
470                 
471                 for (i = lower_bound (alist->const_begin(), alist->const_end(), &cp, cmp); i != alist->const_end() && (*i)->when < end; ++i) {
472                         if ((*i)->when > now) {
473                                 break; 
474                         }
475                 }
476                 
477                 if (i != alist->const_end() && (*i)->when < end) {
478                         
479                         if ((*i)->when < next_event.when) {
480                                 next_event.when = (*i)->when;
481                         }
482                 }
483         }
484
485         return next_event.when != max_frames;
486 }
487
488 void
489 Redirect::set_active (bool yn, void* src)
490 {
491         _active = yn; 
492         active_changed (this, src); 
493         _session.set_dirty ();
494 }
495