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());
79 error << _("Tried to reconstitute a MementoCommand with no contents, failing. id=") << id.to_s() << endmsg;
84 string obj_T = n->property ("type_name")->value();
85 if (obj_T == typeid (AudioRegion).name() || obj_T == typeid (MidiRegion).name() || obj_T == typeid (Region).name()) {
86 if (regions.count(id)) {
87 return new MementoCommand<Region>(*regions[id], before, after);
89 } else if (obj_T == typeid (AudioSource).name() || obj_T == typeid (MidiSource).name()) {
90 if (sources.count(id))
91 return new MementoCommand<Source>(*sources[id], before, after);
92 } else if (obj_T == typeid (Location).name()) {
93 return new MementoCommand<Location>(*_locations.get_location_by_id(id), before, after);
94 } else if (obj_T == typeid (Locations).name()) {
95 return new MementoCommand<Locations>(_locations, before, after);
96 } else if (obj_T == typeid (TempoMap).name()) {
97 return new MementoCommand<TempoMap>(*_tempo_map, before, after);
98 } else if (obj_T == typeid (Playlist).name() || obj_T == typeid (AudioPlaylist).name() || obj_T == typeid (MidiPlaylist).name()) {
99 if (boost::shared_ptr<Playlist> pl = playlist_by_name(child->property("name")->value())) {
100 return new MementoCommand<Playlist>(*(pl.get()), before, after);
102 } else if (obj_T == typeid (Route).name() || obj_T == typeid (AudioTrack).name() || obj_T == typeid(MidiTrack).name()) {
103 return new MementoCommand<Route>(*route_by_id(id), before, after);
104 } else if (obj_T == typeid (Curve).name() || obj_T == typeid (AutomationList).name()) {
105 if (automation_lists.count(id))
106 return new MementoCommand<AutomationList>(*automation_lists[id], before, after);
107 } else if (registry.count(id)) { // For Editor and AutomationLine which are off-limits here
108 return new MementoCommand<PBD::StatefulThingWithGoingAway>(*registry[id], before, after);
112 error << string_compose (_("could not reconstitute MementoCommand from XMLNode. object type = %1 id = %2"), obj_T, id.to_s()) << endmsg;
118 Session::global_state_command_factory (const XMLNode& node)
120 const XMLProperty* prop;
121 Command* command = 0;
123 if ((prop = node.property ("type")) == 0) {
124 error << _("GlobalRouteStateCommand has no \"type\" node, ignoring") << endmsg;
130 if (prop->value() == "solo") {
131 command = new GlobalSoloStateCommand (*this, node);
132 } else if (prop->value() == "mute") {
133 command = new GlobalMuteStateCommand (*this, node);
134 } else if (prop->value() == "rec-enable") {
135 command = new GlobalRecordEnableStateCommand (*this, node);
136 } else if (prop->value() == "metering") {
137 command = new GlobalMeteringStateCommand (*this, node);
139 error << string_compose (_("unknown type of GlobalRouteStateCommand (%1), ignored"), prop->value()) << endmsg;
143 catch (failed_constructor& err) {
150 Session::GlobalRouteStateCommand::GlobalRouteStateCommand (Session& s, void* p)
155 Session::GlobalRouteStateCommand::GlobalRouteStateCommand (Session& s, const XMLNode& node)
156 : sess (s), src (this)
158 if (set_state (node)) {
159 throw failed_constructor ();
164 Session::GlobalRouteStateCommand::set_state (const XMLNode& node)
166 GlobalRouteBooleanState states;
168 const XMLProperty* prop;
170 XMLNodeConstIterator niter;
176 for (loop = 0; loop < 2; ++loop) {
186 if ((child = node.child (str)) == 0) {
187 warning << string_compose (_("global route state command has no \"%1\" node, ignoring entire command"), str) << endmsg;
191 nlist = child->children();
193 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
195 RouteBooleanState rbs;
196 boost::shared_ptr<Route> route;
199 prop = (*niter)->property ("id");
202 if ((route = sess.route_by_id (id)) == 0) {
203 warning << string_compose (_("cannot find track/bus \"%1\" while rebuilding a global route state command, ignored"), id.to_s()) << endmsg;
207 rbs.first = boost::weak_ptr<Route> (route);
209 prop = (*niter)->property ("yn");
210 rbs.second = (prop->value() == "1");
213 after.push_back (rbs);
215 before.push_back (rbs);
224 Session::GlobalRouteStateCommand::get_state ()
226 XMLNode* node = new XMLNode (X_("GlobalRouteStateCommand"));
227 XMLNode* nbefore = new XMLNode (X_("before"));
228 XMLNode* nafter = new XMLNode (X_("after"));
230 for (Session::GlobalRouteBooleanState::iterator x = before.begin(); x != before.end(); ++x) {
231 XMLNode* child = new XMLNode ("s");
232 boost::shared_ptr<Route> r = x->first.lock();
235 child->add_property (X_("id"), r->id().to_s());
236 child->add_property (X_("yn"), (x->second ? "1" : "0"));
237 nbefore->add_child_nocopy (*child);
241 for (Session::GlobalRouteBooleanState::iterator x = after.begin(); x != after.end(); ++x) {
242 XMLNode* child = new XMLNode ("s");
243 boost::shared_ptr<Route> r = x->first.lock();
246 child->add_property (X_("id"), r->id().to_s());
247 child->add_property (X_("yn"), (x->second ? "1" : "0"));
248 nafter->add_child_nocopy (*child);
252 node->add_child_nocopy (*nbefore);
253 node->add_child_nocopy (*nafter);
260 Session::GlobalSoloStateCommand::GlobalSoloStateCommand(Session &sess, void *src)
261 : GlobalRouteStateCommand (sess, src)
263 after = before = sess.get_global_route_boolean(&Route::soloed);
266 Session::GlobalSoloStateCommand::GlobalSoloStateCommand (Session& sess, const XMLNode& node)
267 : Session::GlobalRouteStateCommand (sess, node)
272 Session::GlobalSoloStateCommand::mark()
274 after = sess.get_global_route_boolean(&Route::soloed);
278 Session::GlobalSoloStateCommand::operator()()
280 sess.set_global_solo(after, src);
284 Session::GlobalSoloStateCommand::undo()
286 sess.set_global_solo(before, src);
290 Session::GlobalSoloStateCommand::get_state()
292 XMLNode& node = GlobalRouteStateCommand::get_state();
293 node.add_property ("type", "solo");
298 Session::GlobalMuteStateCommand::GlobalMuteStateCommand(Session &sess, void *src)
299 : GlobalRouteStateCommand (sess, src)
301 after = before = sess.get_global_route_boolean(&Route::muted);
304 Session::GlobalMuteStateCommand::GlobalMuteStateCommand (Session& sess, const XMLNode& node)
305 : Session::GlobalRouteStateCommand (sess, node)
310 Session::GlobalMuteStateCommand::mark()
312 after = sess.get_global_route_boolean(&Route::muted);
316 Session::GlobalMuteStateCommand::operator()()
318 sess.set_global_mute(after, src);
322 Session::GlobalMuteStateCommand::undo()
324 sess.set_global_mute(before, src);
328 Session::GlobalMuteStateCommand::get_state()
330 XMLNode& node = GlobalRouteStateCommand::get_state();
331 node.add_property ("type", "mute");
336 Session::GlobalRecordEnableStateCommand::GlobalRecordEnableStateCommand(Session &sess, void *src)
337 : GlobalRouteStateCommand (sess, src)
339 after = before = sess.get_global_route_boolean(&Route::record_enabled);
342 Session::GlobalRecordEnableStateCommand::GlobalRecordEnableStateCommand (Session& sess, const XMLNode& node)
343 : Session::GlobalRouteStateCommand (sess, node)
348 Session::GlobalRecordEnableStateCommand::mark()
350 after = sess.get_global_route_boolean(&Route::record_enabled);
354 Session::GlobalRecordEnableStateCommand::operator()()
356 sess.set_global_record_enable(after, src);
360 Session::GlobalRecordEnableStateCommand::undo()
362 sess.set_global_record_enable(before, src);
366 Session::GlobalRecordEnableStateCommand::get_state()
368 XMLNode& node = GlobalRouteStateCommand::get_state();
369 node.add_property ("type", "rec-enable");
374 Session::GlobalMeteringStateCommand::GlobalMeteringStateCommand(Session &s, void *p)
377 after = before = sess.get_global_route_metering();
380 Session::GlobalMeteringStateCommand::GlobalMeteringStateCommand (Session& s, const XMLNode& node)
381 : sess (s), src (this)
383 if (set_state (node)) {
384 throw failed_constructor();
389 Session::GlobalMeteringStateCommand::mark()
391 after = sess.get_global_route_metering();
395 Session::GlobalMeteringStateCommand::operator()()
397 sess.set_global_route_metering(after, src);
401 Session::GlobalMeteringStateCommand::undo()
403 sess.set_global_route_metering(before, src);
407 Session::GlobalMeteringStateCommand::get_state()
409 XMLNode* node = new XMLNode (X_("GlobalRouteStateCommand"));
410 XMLNode* nbefore = new XMLNode (X_("before"));
411 XMLNode* nafter = new XMLNode (X_("after"));
413 for (Session::GlobalRouteMeterState::iterator x = before.begin(); x != before.end(); ++x) {
414 XMLNode* child = new XMLNode ("s");
415 boost::shared_ptr<Route> r = x->first.lock();
418 child->add_property (X_("id"), r->id().to_s());
420 const char* meterstr = 0;
424 meterstr = X_("input");
427 meterstr = X_("pre");
430 meterstr = X_("post");
433 fatal << string_compose (_("programming error: %1") , "no meter state in Session::GlobalMeteringStateCommand::get_state") << endmsg;
436 child->add_property (X_("meter"), meterstr);
437 nbefore->add_child_nocopy (*child);
441 for (Session::GlobalRouteMeterState::iterator x = after.begin(); x != after.end(); ++x) {
442 XMLNode* child = new XMLNode ("s");
443 boost::shared_ptr<Route> r = x->first.lock();
446 child->add_property (X_("id"), r->id().to_s());
448 const char* meterstr;
452 meterstr = X_("input");
455 meterstr = X_("pre");
458 meterstr = X_("post");
460 default: meterstr = "";
463 child->add_property (X_("meter"), meterstr);
464 nafter->add_child_nocopy (*child);
468 node->add_child_nocopy (*nbefore);
469 node->add_child_nocopy (*nafter);
471 node->add_property ("type", "metering");
477 Session::GlobalMeteringStateCommand::set_state (const XMLNode& node)
479 GlobalRouteBooleanState states;
481 const XMLProperty* prop;
483 XMLNodeConstIterator niter;
489 for (loop = 0; loop < 2; ++loop) {
499 if ((child = node.child (str)) == 0) {
500 warning << string_compose (_("global route meter state command has no \"%1\" node, ignoring entire command"), str) << endmsg;
504 nlist = child->children();
506 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
509 boost::shared_ptr<Route> route;
512 prop = (*niter)->property ("id");
515 if ((route = sess.route_by_id (id)) == 0) {
516 warning << string_compose (_("cannot find track/bus \"%1\" while rebuilding a global route state command, ignored"), id.to_s()) << endmsg;
520 rms.first = boost::weak_ptr<Route> (route);
522 prop = (*niter)->property ("meter");
524 if (prop->value() == X_("pre")) {
525 rms.second = MeterPreFader;
526 } else if (prop->value() == X_("post")) {
527 rms.second = MeterPostFader;
529 rms.second = MeterInput;
533 after.push_back (rms);
535 before.push_back (rms);