1 /*
2  * Copyright (C) 2006-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2006 Hans Fugal <hans@fugal.net>
4  * Copyright (C) 2008-2011 David Robillard <d@drobilla.net>
5  * Copyright (C) 2008 Sakari Bergen <sakari.bergen@beatwaves.net>
6  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
7  * Copyright (C) 2015-2016 Nick Mainsbridge <mainsbridge@gmail.com>
8  * Copyright (C) 2016-2019 Robin Gareus <robin@gareus.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 #ifndef __ardour_location_h__
26 #define __ardour_location_h__
27 
28 #include <string>
29 #include <list>
30 #include <iostream>
31 #include <map>
32 
33 #include <sys/types.h>
34 
35 #include <glibmm/threads.h>
36 
37 #include "pbd/undo.h"
38 #include "pbd/stateful.h"
39 #include "pbd/statefuldestructible.h"
40 
41 #include "ardour/ardour.h"
42 #include "ardour/scene_change.h"
43 #include "ardour/session_handle.h"
44 
45 namespace ARDOUR {
46 
47 class SceneChange;
48 
49 /** Location on Timeline - abstract representation for Markers, Loop/Punch Ranges, CD-Markers etc. */
50 class LIBARDOUR_API Location : public SessionHandleRef, public PBD::StatefulDestructible
51 {
52 public:
53 	enum Flags {
54 		IsMark = 0x1,
55 		IsAutoPunch = 0x2,
56 		IsAutoLoop = 0x4,
57 		IsHidden = 0x8,
58 		IsCDMarker = 0x10,
59 		IsRangeMarker = 0x20,
60 		IsSessionRange = 0x40,
61 		IsSkip = 0x80,
62 		IsSkipping = 0x100, /* skipping is active (or not) */
63 		IsClockOrigin = 0x200,
64 		IsXrun = 0x400,
65 	};
66 
67 	Location (Session &);
68 	Location (Session &, samplepos_t, samplepos_t, const std::string &, Flags bits = Flags(0), const uint32_t sub_num = 0);
69 	Location (const Location& other);
70 	Location (Session &, const XMLNode&);
71 	Location* operator= (const Location& other);
72 
73 	bool operator==(const Location& other);
74 
locked()75 	bool locked() const { return _locked; }
76 	void lock ();
77 	void unlock ();
78 
timestamp()79 	int64_t timestamp() const { return _timestamp; };
start()80 	samplepos_t start() const { return _start; }
end()81 	samplepos_t end() const { return _end; }
length()82 	samplecnt_t length() const { return _end - _start; }
83 
84 	int set_start (samplepos_t s, bool force = false, bool allow_beat_recompute = true, const uint32_t sub_num = 0);
85 	int set_end (samplepos_t e, bool force = false, bool allow_beat_recompute = true, const uint32_t sub_num = 0);
86 	int set (samplepos_t start, samplepos_t end, bool allow_beat_recompute = true, const uint32_t sub_num = 0);
87 
88 	int move_to (samplepos_t pos, const uint32_t sub_num);
89 
name()90 	const std::string& name() const { return _name; }
91 	void set_name (const std::string &str);
92 
93 	void set_auto_punch (bool yn, void *src);
94 	void set_auto_loop (bool yn, void *src);
95 	void set_hidden (bool yn, void *src);
96 	void set_cd (bool yn, void *src);
97 	void set_is_range_marker (bool yn, void* src);
98 	void set_is_clock_origin (bool yn, void* src);
99 	void set_skip (bool yn);
100 	void set_skipping (bool yn);
101 
is_auto_punch()102 	bool is_auto_punch () const { return _flags & IsAutoPunch; }
is_auto_loop()103 	bool is_auto_loop () const { return _flags & IsAutoLoop; }
is_mark()104 	bool is_mark () const { return _flags & IsMark; }
is_hidden()105 	bool is_hidden () const { return _flags & IsHidden; }
is_cd_marker()106 	bool is_cd_marker () const { return _flags & IsCDMarker; }
is_session_range()107 	bool is_session_range () const { return _flags & IsSessionRange; }
is_range_marker()108 	bool is_range_marker() const { return _flags & IsRangeMarker; }
is_skip()109 	bool is_skip() const { return _flags & IsSkip; }
is_clock_origin()110 	bool is_clock_origin() const { return _flags & IsClockOrigin; }
is_skipping()111 	bool is_skipping() const { return (_flags & IsSkip) && (_flags & IsSkipping); }
is_xrun()112 	bool is_xrun() const { return _flags & IsXrun; }
matches(Flags f)113 	bool matches (Flags f) const { return _flags & f; }
114 
flags()115 	Flags flags () const { return _flags; }
116 
scene_change()117 	boost::shared_ptr<SceneChange> scene_change() const { return _scene_change; }
118 	void set_scene_change (boost::shared_ptr<SceneChange>);
119 
120 	/* these are static signals for objects that want to listen to all
121 	 * locations at once.
122 	 */
123 
124 	static PBD::Signal1<void,Location*> name_changed;
125 	static PBD::Signal1<void,Location*> end_changed;
126 	static PBD::Signal1<void,Location*> start_changed;
127 	static PBD::Signal1<void,Location*> flags_changed;
128 	static PBD::Signal1<void,Location*> lock_changed;
129 	static PBD::Signal1<void,Location*> position_lock_style_changed;
130 
131 	/* this is sent only when both start and end change at the same time */
132 	static PBD::Signal1<void,Location*> changed;
133 
134 	/* these are member signals for objects that care only about
135 	 * changes to this object
136 	 */
137 
138 	PBD::Signal0<void> Changed;
139 
140 	PBD::Signal0<void> NameChanged;
141 	PBD::Signal0<void> EndChanged;
142 	PBD::Signal0<void> StartChanged;
143 	PBD::Signal0<void> FlagsChanged;
144 	PBD::Signal0<void> LockChanged;
145 	PBD::Signal0<void> PositionLockStyleChanged;
146 
147 	/* CD Track / CD-Text info */
148 
149 	std::map<std::string, std::string> cd_info;
150 	XMLNode& cd_info_node (const std::string &, const std::string &);
151 
152 	XMLNode& get_state (void);
153 	int set_state (const XMLNode&, int version);
154 
position_lock_style()155 	PositionLockStyle position_lock_style() const { return _position_lock_style; }
156 	void set_position_lock_style (PositionLockStyle ps);
157 	void recompute_samples_from_beat ();
158 
159 	static PBD::Signal0<void> scene_changed; /* for use by backend scene change management, class level */
160 	PBD::Signal0<void> SceneChangeChanged;   /* for use by objects interested in this object */
161 
162 private:
163 	std::string        _name;
164 	samplepos_t        _start;
165 	double             _start_beat;
166 	samplepos_t        _end;
167 	double             _end_beat;
168 	Flags              _flags;
169 	bool               _locked;
170 	PositionLockStyle  _position_lock_style;
171 	boost::shared_ptr<SceneChange> _scene_change;
172 	int64_t            _timestamp;
173 
174 	void set_mark (bool yn);
175 	bool set_flag_internal (bool yn, Flags flag);
176 	void recompute_beat_from_samples (const uint32_t sub_num);
177 };
178 
179 /** A collection of session locations including unique dedicated locations (loop, punch, etc) */
180 class LIBARDOUR_API Locations : public SessionHandleRef, public PBD::StatefulDestructible
181 {
182 public:
183 	typedef std::list<Location *> LocationList;
184 
185 	Locations (Session &);
186 	~Locations ();
187 
list()188 	const LocationList& list () const { return locations; }
list()189 	LocationList list () { return locations; }
190 
191 	void add (Location *, bool make_current = false);
192 
193 	/** Add new range to the collection
194 	 *
195 	 * @param start start position
196 	 * @param end end position
197 	 *
198 	 * @return New location object
199 	 */
200 	Location* add_range (samplepos_t start, samplepos_t end);
201 
202 	void remove (Location *);
203 	bool clear ();
204 	bool clear_markers ();
205 	bool clear_xrun_markers ();
206 	bool clear_ranges ();
207 
208 	void ripple (samplepos_t at, samplecnt_t distance, bool include_locked, bool notify);
209 
210 	XMLNode& get_state (void);
211 	int set_state (const XMLNode&, int version);
212 	Location *get_location_by_id(PBD::ID);
213 
214 	Location* auto_loop_location () const;
215 	Location* auto_punch_location () const;
216 	Location* session_range_location() const;
217 	Location* clock_origin_location() const;
218 
219 	int next_available_name(std::string& result,std::string base);
220 	uint32_t num_range_markers() const;
221 
222 	int set_current (Location *, bool want_lock = true);
current()223 	Location *current () const { return current_location; }
224 
225 	Location* mark_at (samplepos_t, samplecnt_t slop = 0) const;
226 
227 	void set_clock_origin (Location*, void *src);
228 
229 	samplepos_t first_mark_before (samplepos_t, bool include_special_ranges = false);
230 	samplepos_t first_mark_after (samplepos_t, bool include_special_ranges = false);
231 
232 	void marks_either_side (samplepos_t const, samplepos_t &, samplepos_t &) const;
233 
234 	/** Return range with closest start pos to the where argument
235 	 *
236 	 * @param pos point to compare with start pos
237 	 * @param slop area around point to search for start pos
238 	 * @param incl (optional) look only for ranges that includes 'where' point
239 	 *
240 	 * @return Location object or nil
241 	 */
242 	Location* range_starts_at(samplepos_t pos, samplecnt_t slop = 0, bool incl = false) const;
243 
244 	void find_all_between (samplepos_t start, samplepos_t, LocationList&, Location::Flags);
245 
246 	PBD::Signal1<void,Location*> current_changed;
247 
248 	/* Objects that care about individual addition and removal of Locations should connect to added/removed.
249 	 * If an object additionally cares about potential mass clearance of Locations, they should connect to changed.
250 	 */
251 
252 	PBD::Signal1<void,Location*> added;
253 	PBD::Signal1<void,Location*> removed;
254 	PBD::Signal0<void> changed; /* emitted when any action that could have added/removed more than 1 location actually removed 1 or more */
255 
apply(T & obj,void (T::* method)(const LocationList &))256 	template<class T> void apply (T& obj, void (T::*method)(const LocationList&)) const {
257 		/* We don't want to hold the lock while the given method runs, so take a copy
258 		 * of the list and pass that instead.
259 		 */
260 		Locations::LocationList copy;
261 		{
262 			Glib::Threads::RWLock::ReaderLock lm (_lock);
263 			copy = locations;
264 		}
265 		(obj.*method)(copy);
266 	}
267 
268 private:
269 	LocationList locations;
270 	Location*    current_location;
271 	mutable Glib::Threads::RWLock _lock;
272 
273 	int set_current_unlocked (Location *);
274 	void location_changed (Location*);
275 	void listen_to (Location*);
276 };
277 
278 } // namespace ARDOUR
279 
280 #endif /* __ardour_location_h__ */
281