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