Use a shared_ptr for SessionPlaylists so that it can be explicitly destroyed in ...
[ardour.git] / libs / ardour / session_command.cc
1 /*
2     Copyright (C) 2000-2007 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 "ardour/session.h"
21 #include "ardour/route.h"
22 #include "pbd/memento_command.h"
23 #include "ardour/diskstream.h"
24 #include "ardour/playlist.h"
25 #include "ardour/audioplaylist.h"
26 #include "ardour/audio_track.h"
27 #include "ardour/midi_playlist.h"
28 #include "ardour/midi_track.h"
29 #include "ardour/tempo.h"
30 #include "ardour/audiosource.h"
31 #include "ardour/audioregion.h"
32 #include "ardour/midi_source.h"
33 #include "ardour/midi_region.h"
34 #include "ardour/session_playlists.h"
35 #include "pbd/error.h"
36 #include "pbd/id.h"
37 #include "pbd/statefuldestructible.h"
38 #include "pbd/failed_constructor.h"
39 #include "evoral/Curve.hpp"
40
41 using namespace PBD;
42 using namespace ARDOUR;
43
44 #include "i18n.h"
45
46 void Session::register_with_memento_command_factory(PBD::ID id, PBD::StatefulThingWithGoingAway *ptr)
47 {
48     registry[id] = ptr;
49 }
50
51 Command *
52 Session::memento_command_factory(XMLNode *n)
53 {
54     PBD::ID id;
55     XMLNode *before = 0, *after = 0;
56     XMLNode *child = 0;
57
58     /* get id */
59     id = PBD::ID(n->property("obj-id")->value());
60
61     /* get before/after */
62
63     if (n->name() == "MementoCommand") {
64             before = new XMLNode(*n->children().front());
65             after = new XMLNode(*n->children().back());
66             child = before;
67     } else if (n->name() == "MementoUndoCommand") {
68             before = new XMLNode(*n->children().front());
69             child = before;
70     } else if (n->name() == "MementoRedoCommand") {
71             after = new XMLNode(*n->children().front());
72             child = after;
73     } else if (n->name() == "PlaylistCommand") {
74             before = new XMLNode(*n->children().front());
75             after = new XMLNode(*n->children().back());
76             child = before;
77     }
78
79     if (!child) {
80         error << _("Tried to reconstitute a MementoCommand with no contents, failing. id=") << id.to_s() << endmsg;
81         return 0;
82     }
83
84     /* create command */
85     string obj_T = n->property ("type-name")->value();
86     if (obj_T == typeid (AudioRegion).name() || obj_T == typeid (MidiRegion).name() || obj_T == typeid (Region).name()) {
87             if (regions.count(id)) {
88                     return new MementoCommand<Region>(*regions[id], before, after);
89             }
90     } else if (obj_T == typeid (AudioSource).name() || obj_T == typeid (MidiSource).name()) {
91             if (sources.count(id))
92                     return new MementoCommand<Source>(*sources[id], before, after);
93     } else if (obj_T == typeid (Location).name()) {
94             Location* loc = _locations.get_location_by_id(id);
95             if (loc) {
96                     return new MementoCommand<Location>(*loc, before, after);
97             }
98     } else if (obj_T == typeid (Locations).name()) {
99             return new MementoCommand<Locations>(_locations, before, after);
100     } else if (obj_T == typeid (TempoMap).name()) {
101             return new MementoCommand<TempoMap>(*_tempo_map, before, after);
102     } else if (obj_T == typeid (Playlist).name() || obj_T == typeid (AudioPlaylist).name() || obj_T == typeid (MidiPlaylist).name()) {
103             if (boost::shared_ptr<Playlist> pl = playlists->by_name(child->property("name")->value())) {
104                     return new MementoCommand<Playlist>(*(pl.get()), before, after);
105             }
106     } else if (obj_T == typeid (Route).name() || obj_T == typeid (AudioTrack).name() || obj_T == typeid(MidiTrack).name()) {
107                 if (boost::shared_ptr<Route> r = route_by_id(id)) {
108                         return new MementoCommand<Route>(*r, before, after);
109                 } else {
110                         error << string_compose (X_("Route %1 not found in session"), id) << endmsg;
111                 }
112     } else if (obj_T == typeid (Evoral::Curve).name() || obj_T == typeid (AutomationList).name()) {
113                 std::map<PBD::ID, AutomationList*>::iterator i = automation_lists.find(id);
114                 if (i != automation_lists.end()) {
115                     return new MementoCommand<AutomationList>(*i->second, before, after);
116                 }
117     } else if (registry.count(id)) { // For Editor and AutomationLine which are off-limits here
118             return new MementoCommand<PBD::StatefulThingWithGoingAway>(*registry[id], before, after);
119     }
120
121     /* we failed */
122     error << string_compose (_("could not reconstitute MementoCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s()) << endmsg;
123
124     return 0 ;
125 }
126
127 Command *
128 Session::global_state_command_factory (const XMLNode& node)
129 {
130         const XMLProperty* prop;
131         Command* command = 0;
132
133         if ((prop = node.property ("type")) == 0) {
134                 error << _("GlobalRouteStateCommand has no \"type\" node, ignoring") << endmsg;
135                 return 0;
136         }
137
138         try {
139
140                 if (prop->value() == "solo") {
141                         command = new GlobalSoloStateCommand (*this, node);
142                 } else if (prop->value() == "mute") {
143                         command = new GlobalMuteStateCommand (*this, node);
144                 } else if (prop->value() == "rec-enable") {
145                         command = new GlobalRecordEnableStateCommand (*this, node);
146                 } else if (prop->value() == "metering") {
147                         command = new GlobalMeteringStateCommand (*this, node);
148                 } else {
149                         error << string_compose (_("unknown type of GlobalRouteStateCommand (%1), ignored"), prop->value()) << endmsg;
150                 }
151         }
152
153         catch (failed_constructor& err) {
154                 return 0;
155         }
156
157         return command;
158 }
159
160 Session::GlobalRouteStateCommand::GlobalRouteStateCommand (Session& s, void* p)
161         : sess (s), src (p)
162 {
163 }
164
165 Session::GlobalRouteStateCommand::GlobalRouteStateCommand (Session& s, const XMLNode& node)
166         : sess (s), src (this)
167 {
168         if (set_state (node, Stateful::loading_state_version)) {
169                 throw failed_constructor ();
170         }
171 }
172
173 int
174 Session::GlobalRouteStateCommand::set_state (const XMLNode& node, int /*version*/)
175 {
176         GlobalRouteBooleanState states;
177         XMLNodeList nlist;
178         const XMLProperty* prop;
179         XMLNode* child;
180         XMLNodeConstIterator niter;
181         int loop;
182
183         before.clear ();
184         after.clear ();
185
186         for (loop = 0; loop < 2; ++loop) {
187
188                 const char *str;
189
190                 if (loop) {
191                         str = "after";
192                 } else {
193                         str = "before";
194                 }
195
196                 if ((child = node.child (str)) == 0) {
197                         warning << string_compose (_("global route state command has no \"%1\" node, ignoring entire command"), str) << endmsg;
198                         return -1;
199                 }
200
201                 nlist = child->children();
202
203                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
204
205                         RouteBooleanState rbs;
206                         boost::shared_ptr<Route> route;
207                         ID id;
208
209                         prop = (*niter)->property ("id");
210                         id = prop->value ();
211
212                         if ((route = sess.route_by_id (id)) == 0) {
213                                 warning << string_compose (_("cannot find track/bus \"%1\" while rebuilding a global route state command, ignored"), id.to_s()) << endmsg;
214                                 continue;
215                         }
216
217                         rbs.first = boost::weak_ptr<Route> (route);
218
219                         prop = (*niter)->property ("yn");
220                         rbs.second = (prop->value() == "1");
221
222                         if (loop) {
223                                 after.push_back (rbs);
224                         } else {
225                                 before.push_back (rbs);
226                         }
227                 }
228         }
229
230         return 0;
231 }
232
233 XMLNode&
234 Session::GlobalRouteStateCommand::get_state ()
235 {
236         XMLNode* node = new XMLNode (X_("GlobalRouteStateCommand"));
237         XMLNode* nbefore = new XMLNode (X_("before"));
238         XMLNode* nafter = new XMLNode (X_("after"));
239
240         for (Session::GlobalRouteBooleanState::iterator x = before.begin(); x != before.end(); ++x) {
241                 XMLNode* child = new XMLNode ("s");
242                 boost::shared_ptr<Route> r = x->first.lock();
243
244                 if (r) {
245                         child->add_property (X_("id"), r->id().to_s());
246                         child->add_property (X_("yn"), (x->second ? "1" : "0"));
247                         nbefore->add_child_nocopy (*child);
248                 }
249         }
250
251         for (Session::GlobalRouteBooleanState::iterator x = after.begin(); x != after.end(); ++x) {
252                 XMLNode* child = new XMLNode ("s");
253                 boost::shared_ptr<Route> r = x->first.lock();
254
255                 if (r) {
256                         child->add_property (X_("id"), r->id().to_s());
257                         child->add_property (X_("yn"), (x->second ? "1" : "0"));
258                         nafter->add_child_nocopy (*child);
259                 }
260         }
261
262         node->add_child_nocopy (*nbefore);
263         node->add_child_nocopy (*nafter);
264
265         return *node;
266 }
267
268 // solo
269
270 Session::GlobalSoloStateCommand::GlobalSoloStateCommand(Session &sess, void *src)
271         : GlobalRouteStateCommand (sess, src)
272 {
273     after = before = sess.get_global_route_boolean(&Route::soloed);
274 }
275
276 Session::GlobalSoloStateCommand::GlobalSoloStateCommand (Session& sess, const XMLNode& node)
277         : Session::GlobalRouteStateCommand (sess, node)
278 {
279 }
280
281 void
282 Session::GlobalSoloStateCommand::mark()
283 {
284     after = sess.get_global_route_boolean(&Route::soloed);
285 }
286
287 void
288 Session::GlobalSoloStateCommand::operator()()
289 {
290     sess.set_global_solo(after, src);
291 }
292
293 void
294 Session::GlobalSoloStateCommand::undo()
295 {
296     sess.set_global_solo(before, src);
297 }
298
299 XMLNode&
300 Session::GlobalSoloStateCommand::get_state()
301 {
302         XMLNode& node = GlobalRouteStateCommand::get_state();
303         node.add_property ("type", "solo");
304         return node;
305 }
306
307 // mute
308 Session::GlobalMuteStateCommand::GlobalMuteStateCommand(Session &sess, void *src)
309         : GlobalRouteStateCommand (sess, src)
310 {
311     after = before = sess.get_global_route_boolean(&Route::muted);
312 }
313
314 Session::GlobalMuteStateCommand::GlobalMuteStateCommand (Session& sess, const XMLNode& node)
315         : Session::GlobalRouteStateCommand (sess, node)
316 {
317 }
318
319 void
320 Session::GlobalMuteStateCommand::mark()
321 {
322         after = sess.get_global_route_boolean(&Route::muted);
323 }
324
325 void
326 Session::GlobalMuteStateCommand::operator()()
327 {
328         sess.set_global_mute(after, src);
329 }
330
331 void
332 Session::GlobalMuteStateCommand::undo()
333 {
334         sess.set_global_mute(before, src);
335 }
336
337 XMLNode&
338 Session::GlobalMuteStateCommand::get_state()
339 {
340         XMLNode& node = GlobalRouteStateCommand::get_state();
341         node.add_property ("type", "mute");
342         return node;
343 }
344
345 // record enable
346 Session::GlobalRecordEnableStateCommand::GlobalRecordEnableStateCommand(Session &sess, void *src)
347         : GlobalRouteStateCommand (sess, src)
348 {
349         after = before = sess.get_global_route_boolean(&Route::record_enabled);
350 }
351
352 Session::GlobalRecordEnableStateCommand::GlobalRecordEnableStateCommand (Session& sess, const XMLNode& node)
353         : Session::GlobalRouteStateCommand (sess, node)
354 {
355 }
356
357 void
358 Session::GlobalRecordEnableStateCommand::mark()
359 {
360         after = sess.get_global_route_boolean(&Route::record_enabled);
361 }
362
363 void
364 Session::GlobalRecordEnableStateCommand::operator()()
365 {
366         sess.set_global_record_enable(after, src);
367 }
368
369 void
370 Session::GlobalRecordEnableStateCommand::undo()
371 {
372         sess.set_global_record_enable(before, src);
373 }
374
375 XMLNode&
376 Session::GlobalRecordEnableStateCommand::get_state()
377 {
378         XMLNode& node = GlobalRouteStateCommand::get_state();
379         node.add_property ("type", "rec-enable");
380         return node;
381 }
382
383 // metering
384 Session::GlobalMeteringStateCommand::GlobalMeteringStateCommand(Session &s, void *p)
385         : sess (s), src (p)
386 {
387         after = before = sess.get_global_route_metering();
388 }
389
390 Session::GlobalMeteringStateCommand::GlobalMeteringStateCommand (Session& s, const XMLNode& node)
391         : sess (s), src (this)
392 {
393         if (set_state (node, Stateful::loading_state_version)) {
394                 throw failed_constructor();
395         }
396 }
397
398 void
399 Session::GlobalMeteringStateCommand::mark()
400 {
401         after = sess.get_global_route_metering();
402 }
403
404 void
405 Session::GlobalMeteringStateCommand::operator()()
406 {
407         sess.set_global_route_metering(after, src);
408 }
409
410 void
411 Session::GlobalMeteringStateCommand::undo()
412 {
413         sess.set_global_route_metering(before, src);
414 }
415
416 XMLNode&
417 Session::GlobalMeteringStateCommand::get_state()
418 {
419         XMLNode* node = new XMLNode (X_("GlobalRouteStateCommand"));
420         XMLNode* nbefore = new XMLNode (X_("before"));
421         XMLNode* nafter = new XMLNode (X_("after"));
422
423         for (Session::GlobalRouteMeterState::iterator x = before.begin(); x != before.end(); ++x) {
424                 XMLNode* child = new XMLNode ("s");
425                 boost::shared_ptr<Route> r = x->first.lock();
426
427                 if (r) {
428                         child->add_property (X_("id"), r->id().to_s());
429
430                         const char* meterstr = 0;
431
432                         switch (x->second) {
433                         case MeterInput:
434                                 meterstr = X_("input");
435                                 break;
436                         case MeterPreFader:
437                                 meterstr = X_("pre");
438                                 break;
439                         case MeterPostFader:
440                                 meterstr = X_("post");
441                                 break;
442                         default:
443                                 fatal << string_compose (_("programming error: %1") , "no meter state in Session::GlobalMeteringStateCommand::get_state") << endmsg;
444                         }
445
446                         child->add_property (X_("meter"), meterstr);
447                         nbefore->add_child_nocopy (*child);
448                 }
449         }
450
451         for (Session::GlobalRouteMeterState::iterator x = after.begin(); x != after.end(); ++x) {
452                 XMLNode* child = new XMLNode ("s");
453                 boost::shared_ptr<Route> r = x->first.lock();
454
455                 if (r) {
456                         child->add_property (X_("id"), r->id().to_s());
457
458                         const char* meterstr;
459
460                         switch (x->second) {
461                         case MeterInput:
462                                 meterstr = X_("input");
463                                 break;
464                         case MeterPreFader:
465                                 meterstr = X_("pre");
466                                 break;
467                         case MeterPostFader:
468                                 meterstr = X_("post");
469                                 break;
470                         default: meterstr = "";
471                         }
472
473                         child->add_property (X_("meter"), meterstr);
474                         nafter->add_child_nocopy (*child);
475                 }
476         }
477
478         node->add_child_nocopy (*nbefore);
479         node->add_child_nocopy (*nafter);
480
481         node->add_property ("type", "metering");
482
483         return *node;
484 }
485
486 int
487 Session::GlobalMeteringStateCommand::set_state (const XMLNode& node, int /*version*/)
488 {
489         GlobalRouteBooleanState states;
490         XMLNodeList nlist;
491         const XMLProperty* prop;
492         XMLNode* child;
493         XMLNodeConstIterator niter;
494         int loop;
495
496         before.clear ();
497         after.clear ();
498
499         for (loop = 0; loop < 2; ++loop) {
500
501                 const char *str;
502
503                 if (loop) {
504                         str = "after";
505                 } else {
506                         str = "before";
507                 }
508
509                 if ((child = node.child (str)) == 0) {
510                         warning << string_compose (_("global route meter state command has no \"%1\" node, ignoring entire command"), str) << endmsg;
511                         return -1;
512                 }
513
514                 nlist = child->children();
515
516                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
517
518                         RouteMeterState rms;
519                         boost::shared_ptr<Route> route;
520                         ID id;
521
522                         prop = (*niter)->property ("id");
523                         id = prop->value ();
524
525                         if ((route = sess.route_by_id (id)) == 0) {
526                                 warning << string_compose (_("cannot find track/bus \"%1\" while rebuilding a global route state command, ignored"), id.to_s()) << endmsg;
527                                 continue;
528                         }
529
530                         rms.first = boost::weak_ptr<Route> (route);
531
532                         prop = (*niter)->property ("meter");
533
534                         if (prop->value() == X_("pre")) {
535                                 rms.second = MeterPreFader;
536                         } else if (prop->value() == X_("post")) {
537                                 rms.second = MeterPostFader;
538                         } else {
539                                 rms.second = MeterInput;
540                         }
541
542                         if (loop) {
543                                 after.push_back (rms);
544                         } else {
545                                 before.push_back (rms);
546                         }
547                 }
548         }
549
550         return 0;
551 }