2 Copyright (C) 2000-2007 Paul Davis
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.
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.
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.
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 <pbd/error.h>
36 #include <pbd/statefuldestructible.h>
37 #include <pbd/failed_constructor.h>
40 using namespace ARDOUR;
44 void Session::register_with_memento_command_factory(PBD::ID id, PBD::StatefulThingWithGoingAway *ptr)
50 Session::memento_command_factory(XMLNode *n)
53 XMLNode *before = 0, *after = 0;
57 id = PBD::ID(n->property("obj-id")->value());
59 /* get before/after */
61 if (n->name() == "MementoCommand") {
62 before = new XMLNode(*n->children().front());
63 after = new XMLNode(*n->children().back());
65 } else if (n->name() == "MementoUndoCommand") {
66 before = new XMLNode(*n->children().front());
68 } else if (n->name() == "MementoRedoCommand") {
69 after = new XMLNode(*n->children().front());
71 } else if (n->name() == "PlaylistCommand") {
72 before = new XMLNode(*n->children().front());
73 after = new XMLNode(*n->children().back());
78 error << _("Tried to reconstitute a MementoCommand with no contents, failing. id=") << id.to_s() << endmsg;
83 string obj_T = n->property ("type-name")->value();
84 if (obj_T == typeid (AudioRegion).name() || obj_T == typeid (MidiRegion).name() || obj_T == typeid (Region).name()) {
85 if (regions.count(id)) {
86 return new MementoCommand<Region>(*regions[id], before, after);
88 } else if (obj_T == typeid (AudioSource).name() || obj_T == typeid (MidiSource).name()) {
89 if (sources.count(id))
90 return new MementoCommand<Source>(*sources[id], before, after);
91 } else if (obj_T == typeid (Location).name()) {
92 Location* loc = _locations.get_location_by_id(id);
94 return new MementoCommand<Location>(*loc, before, after);
96 } else if (obj_T == typeid (Locations).name()) {
97 return new MementoCommand<Locations>(_locations, before, after);
98 } else if (obj_T == typeid (TempoMap).name()) {
99 return new MementoCommand<TempoMap>(*_tempo_map, before, after);
100 } else if (obj_T == typeid (Playlist).name() || obj_T == typeid (AudioPlaylist).name() || obj_T == typeid (MidiPlaylist).name()) {
101 if (boost::shared_ptr<Playlist> pl = playlist_by_name(child->property("name")->value())) {
102 return new MementoCommand<Playlist>(*(pl.get()), before, after);
104 } else if (obj_T == typeid (Route).name() || obj_T == typeid (AudioTrack).name() || obj_T == typeid(MidiTrack).name()) {
105 return new MementoCommand<Route>(*route_by_id(id), before, after);
106 } else if (obj_T == typeid (Evoral::Curve).name() || obj_T == typeid (AutomationList).name()) {
107 std::map<PBD::ID, AutomationList*>::iterator i = automation_lists.find(id);
108 if (i != automation_lists.end()) {
109 return new MementoCommand<AutomationList>(*i->second, before, after);
111 } else if (registry.count(id)) { // For Editor and AutomationLine which are off-limits here
112 return new MementoCommand<PBD::StatefulThingWithGoingAway>(*registry[id], before, after);
116 error << string_compose (_("could not reconstitute MementoCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s()) << endmsg;
122 Session::global_state_command_factory (const XMLNode& node)
124 const XMLProperty* prop;
125 Command* command = 0;
127 if ((prop = node.property ("type")) == 0) {
128 error << _("GlobalRouteStateCommand has no \"type\" node, ignoring") << endmsg;
134 if (prop->value() == "solo") {
135 command = new GlobalSoloStateCommand (*this, node);
136 } else if (prop->value() == "mute") {
137 command = new GlobalMuteStateCommand (*this, node);
138 } else if (prop->value() == "rec-enable") {
139 command = new GlobalRecordEnableStateCommand (*this, node);
140 } else if (prop->value() == "metering") {
141 command = new GlobalMeteringStateCommand (*this, node);
143 error << string_compose (_("unknown type of GlobalRouteStateCommand (%1), ignored"), prop->value()) << endmsg;
147 catch (failed_constructor& err) {
154 Session::GlobalRouteStateCommand::GlobalRouteStateCommand (Session& s, void* p)
159 Session::GlobalRouteStateCommand::GlobalRouteStateCommand (Session& s, const XMLNode& node)
160 : sess (s), src (this)
162 if (set_state (node)) {
163 throw failed_constructor ();
168 Session::GlobalRouteStateCommand::set_state (const XMLNode& node)
170 GlobalRouteBooleanState states;
172 const XMLProperty* prop;
174 XMLNodeConstIterator niter;
180 for (loop = 0; loop < 2; ++loop) {
190 if ((child = node.child (str)) == 0) {
191 warning << string_compose (_("global route state command has no \"%1\" node, ignoring entire command"), str) << endmsg;
195 nlist = child->children();
197 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
199 RouteBooleanState rbs;
200 boost::shared_ptr<Route> route;
203 prop = (*niter)->property ("id");
206 if ((route = sess.route_by_id (id)) == 0) {
207 warning << string_compose (_("cannot find track/bus \"%1\" while rebuilding a global route state command, ignored"), id.to_s()) << endmsg;
211 rbs.first = boost::weak_ptr<Route> (route);
213 prop = (*niter)->property ("yn");
214 rbs.second = (prop->value() == "1");
217 after.push_back (rbs);
219 before.push_back (rbs);
228 Session::GlobalRouteStateCommand::get_state ()
230 XMLNode* node = new XMLNode (X_("GlobalRouteStateCommand"));
231 XMLNode* nbefore = new XMLNode (X_("before"));
232 XMLNode* nafter = new XMLNode (X_("after"));
234 for (Session::GlobalRouteBooleanState::iterator x = before.begin(); x != before.end(); ++x) {
235 XMLNode* child = new XMLNode ("s");
236 boost::shared_ptr<Route> r = x->first.lock();
239 child->add_property (X_("id"), r->id().to_s());
240 child->add_property (X_("yn"), (x->second ? "1" : "0"));
241 nbefore->add_child_nocopy (*child);
245 for (Session::GlobalRouteBooleanState::iterator x = after.begin(); x != after.end(); ++x) {
246 XMLNode* child = new XMLNode ("s");
247 boost::shared_ptr<Route> r = x->first.lock();
250 child->add_property (X_("id"), r->id().to_s());
251 child->add_property (X_("yn"), (x->second ? "1" : "0"));
252 nafter->add_child_nocopy (*child);
256 node->add_child_nocopy (*nbefore);
257 node->add_child_nocopy (*nafter);
264 Session::GlobalSoloStateCommand::GlobalSoloStateCommand(Session &sess, void *src)
265 : GlobalRouteStateCommand (sess, src)
267 after = before = sess.get_global_route_boolean(&Route::soloed);
270 Session::GlobalSoloStateCommand::GlobalSoloStateCommand (Session& sess, const XMLNode& node)
271 : Session::GlobalRouteStateCommand (sess, node)
276 Session::GlobalSoloStateCommand::mark()
278 after = sess.get_global_route_boolean(&Route::soloed);
282 Session::GlobalSoloStateCommand::operator()()
284 sess.set_global_solo(after, src);
288 Session::GlobalSoloStateCommand::undo()
290 sess.set_global_solo(before, src);
294 Session::GlobalSoloStateCommand::get_state()
296 XMLNode& node = GlobalRouteStateCommand::get_state();
297 node.add_property ("type", "solo");
302 Session::GlobalMuteStateCommand::GlobalMuteStateCommand(Session &sess, void *src)
303 : GlobalRouteStateCommand (sess, src)
305 after = before = sess.get_global_route_boolean(&Route::muted);
308 Session::GlobalMuteStateCommand::GlobalMuteStateCommand (Session& sess, const XMLNode& node)
309 : Session::GlobalRouteStateCommand (sess, node)
314 Session::GlobalMuteStateCommand::mark()
316 after = sess.get_global_route_boolean(&Route::muted);
320 Session::GlobalMuteStateCommand::operator()()
322 sess.set_global_mute(after, src);
326 Session::GlobalMuteStateCommand::undo()
328 sess.set_global_mute(before, src);
332 Session::GlobalMuteStateCommand::get_state()
334 XMLNode& node = GlobalRouteStateCommand::get_state();
335 node.add_property ("type", "mute");
340 Session::GlobalRecordEnableStateCommand::GlobalRecordEnableStateCommand(Session &sess, void *src)
341 : GlobalRouteStateCommand (sess, src)
343 after = before = sess.get_global_route_boolean(&Route::record_enabled);
346 Session::GlobalRecordEnableStateCommand::GlobalRecordEnableStateCommand (Session& sess, const XMLNode& node)
347 : Session::GlobalRouteStateCommand (sess, node)
352 Session::GlobalRecordEnableStateCommand::mark()
354 after = sess.get_global_route_boolean(&Route::record_enabled);
358 Session::GlobalRecordEnableStateCommand::operator()()
360 sess.set_global_record_enable(after, src);
364 Session::GlobalRecordEnableStateCommand::undo()
366 sess.set_global_record_enable(before, src);
370 Session::GlobalRecordEnableStateCommand::get_state()
372 XMLNode& node = GlobalRouteStateCommand::get_state();
373 node.add_property ("type", "rec-enable");
378 Session::GlobalMeteringStateCommand::GlobalMeteringStateCommand(Session &s, void *p)
381 after = before = sess.get_global_route_metering();
384 Session::GlobalMeteringStateCommand::GlobalMeteringStateCommand (Session& s, const XMLNode& node)
385 : sess (s), src (this)
387 if (set_state (node)) {
388 throw failed_constructor();
393 Session::GlobalMeteringStateCommand::mark()
395 after = sess.get_global_route_metering();
399 Session::GlobalMeteringStateCommand::operator()()
401 sess.set_global_route_metering(after, src);
405 Session::GlobalMeteringStateCommand::undo()
407 sess.set_global_route_metering(before, src);
411 Session::GlobalMeteringStateCommand::get_state()
413 XMLNode* node = new XMLNode (X_("GlobalRouteStateCommand"));
414 XMLNode* nbefore = new XMLNode (X_("before"));
415 XMLNode* nafter = new XMLNode (X_("after"));
417 for (Session::GlobalRouteMeterState::iterator x = before.begin(); x != before.end(); ++x) {
418 XMLNode* child = new XMLNode ("s");
419 boost::shared_ptr<Route> r = x->first.lock();
422 child->add_property (X_("id"), r->id().to_s());
424 const char* meterstr = 0;
428 meterstr = X_("input");
431 meterstr = X_("pre");
434 meterstr = X_("post");
437 fatal << string_compose (_("programming error: %1") , "no meter state in Session::GlobalMeteringStateCommand::get_state") << endmsg;
440 child->add_property (X_("meter"), meterstr);
441 nbefore->add_child_nocopy (*child);
445 for (Session::GlobalRouteMeterState::iterator x = after.begin(); x != after.end(); ++x) {
446 XMLNode* child = new XMLNode ("s");
447 boost::shared_ptr<Route> r = x->first.lock();
450 child->add_property (X_("id"), r->id().to_s());
452 const char* meterstr;
456 meterstr = X_("input");
459 meterstr = X_("pre");
462 meterstr = X_("post");
464 default: meterstr = "";
467 child->add_property (X_("meter"), meterstr);
468 nafter->add_child_nocopy (*child);
472 node->add_child_nocopy (*nbefore);
473 node->add_child_nocopy (*nafter);
475 node->add_property ("type", "metering");
481 Session::GlobalMeteringStateCommand::set_state (const XMLNode& node)
483 GlobalRouteBooleanState states;
485 const XMLProperty* prop;
487 XMLNodeConstIterator niter;
493 for (loop = 0; loop < 2; ++loop) {
503 if ((child = node.child (str)) == 0) {
504 warning << string_compose (_("global route meter state command has no \"%1\" node, ignoring entire command"), str) << endmsg;
508 nlist = child->children();
510 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
513 boost::shared_ptr<Route> route;
516 prop = (*niter)->property ("id");
519 if ((route = sess.route_by_id (id)) == 0) {
520 warning << string_compose (_("cannot find track/bus \"%1\" while rebuilding a global route state command, ignored"), id.to_s()) << endmsg;
524 rms.first = boost::weak_ptr<Route> (route);
526 prop = (*niter)->property ("meter");
528 if (prop->value() == X_("pre")) {
529 rms.second = MeterPreFader;
530 } else if (prop->value() == X_("post")) {
531 rms.second = MeterPostFader;
533 rms.second = MeterInput;
537 after.push_back (rms);
539 before.push_back (rms);