39a83cb6dedb3e4dafad2862c3c22e81c6a5e375
[ardour.git] / libs / ardour / source.cc
1 /*
2     Copyright (C) 2000 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     $Id$
19 */
20
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <poll.h>
25 #include <float.h>
26 #include <cerrno>
27 #include <ctime>
28 #include <cmath>
29 #include <iomanip>
30 #include <algorithm>
31
32 #include <glibmm/thread.h>
33 #include <pbd/xml++.h>
34 #include <pbd/pthread_utils.h>
35
36 #include <ardour/source.h>
37 #include <ardour/playlist.h>
38
39 #include "i18n.h"
40
41 using std::min;
42 using std::max;
43
44 using namespace ARDOUR;
45
46 Source::Source (Session& s, string name)
47         : _session (s)
48 {
49         _name = name;
50         _timestamp = 0;
51         _in_use = 0;
52 }
53
54 Source::Source (Session& s, const XMLNode& node) 
55         : _session (s)
56 {
57         _timestamp = 0;
58         _in_use = 0;
59
60         if (set_state (node)) {
61                 throw failed_constructor();
62         }
63 }
64
65 Source::~Source ()
66 {
67         notify_callbacks ();
68 }
69
70 XMLNode&
71 Source::get_state ()
72 {
73         XMLNode *node = new XMLNode ("Source");
74         char buf[64];
75
76         node->add_property ("name", _name);
77         _id.print (buf, sizeof (buf));
78         node->add_property ("id", buf);
79
80         if (_timestamp != 0) {
81                 snprintf (buf, sizeof (buf), "%ld", _timestamp);
82                 node->add_property ("timestamp", buf);
83         }
84
85         return *node;
86 }
87
88 int
89 Source::set_state (const XMLNode& node)
90 {
91         const XMLProperty* prop;
92
93         if ((prop = node.property ("name")) != 0) {
94                 _name = prop->value();
95         } else {
96                 return -1;
97         }
98         
99         if ((prop = node.property ("id")) != 0) {
100                 _id = prop->value ();
101         } else {
102                 return -1;
103         }
104
105         if ((prop = node.property ("timestamp")) != 0) {
106                 sscanf (prop->value().c_str(), "%ld", &_timestamp);
107         }
108
109         return 0;
110 }
111
112 void
113 Source::add_playlist (boost::shared_ptr<Playlist> pl)
114 {
115         _playlists.insert (pl);
116         pl->GoingAway.connect (bind (mem_fun (*this, &Source::remove_playlist), boost::weak_ptr<Playlist> (pl)));
117 }
118
119 void
120 Source::remove_playlist (boost::weak_ptr<Playlist> wpl)
121 {
122         boost::shared_ptr<Playlist> pl (wpl.lock());
123
124         if (!pl) {
125                 return;
126         }
127
128         std::set<boost::shared_ptr<Playlist> >::iterator x;
129
130         if ((x = _playlists.find (pl)) != _playlists.end()) {
131                 _playlists.erase (x);
132         }
133 }
134
135 uint32_t
136 Source::used () const
137 {
138         return _playlists.size();
139 }