some strategic documentation
[ardour.git] / libs / ardour / ardour / location.h
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 */
19
20 #ifndef __ardour_location_h__
21 #define __ardour_location_h__
22
23 #include <string>
24 #include <list>
25 #include <iostream>
26 #include <map>
27
28 #include <sys/types.h>
29
30 #include <glibmm/threads.h>
31
32 #include "pbd/undo.h"
33 #include "pbd/stateful.h"
34 #include "pbd/statefuldestructible.h"
35
36 #include "ardour/ardour.h"
37 #include "ardour/scene_change.h"
38 #include "ardour/session_handle.h"
39
40 namespace ARDOUR {
41
42 class SceneChange;
43
44 /** Location on Timeline - abstract representation for Markers, Loop/Punch Ranges, CD-Markers etc. */
45 class LIBARDOUR_API Location : public SessionHandleRef, public PBD::StatefulDestructible
46 {
47   public:
48         enum Flags {
49                 IsMark = 0x1,
50                 IsAutoPunch = 0x2,
51                 IsAutoLoop = 0x4,
52                 IsHidden = 0x8,
53                 IsCDMarker = 0x10,
54                 IsRangeMarker = 0x20,
55                 IsSessionRange = 0x40,
56                 IsSkip = 0x80,
57                 IsSkipping = 0x100, /* skipping is active (or not) */
58         };
59
60         Location (Session &);
61         Location (Session &, framepos_t, framepos_t, const std::string &, Flags bits = Flags(0));
62         Location (const Location& other);
63         Location (Session &, const XMLNode&);
64         Location* operator= (const Location& other);
65
66         bool operator==(const Location& other);
67
68         bool locked() const { return _locked; }
69         void lock ();
70         void unlock ();
71
72         framepos_t start() const  { return _start; }
73         framepos_t end() const { return _end; }
74         framecnt_t length() const { return _end - _start; }
75
76         int set_start (framepos_t s, bool force = false, bool allow_bbt_recompute = true);
77         int set_end (framepos_t e, bool force = false, bool allow_bbt_recompute = true);
78         int set (framepos_t start, framepos_t end, bool allow_bbt_recompute = true);
79
80         int move_to (framepos_t pos);
81
82         const std::string& name() const { return _name; }
83         void set_name (const std::string &str);
84
85         void set_auto_punch (bool yn, void *src);
86         void set_auto_loop (bool yn, void *src);
87         void set_hidden (bool yn, void *src);
88         void set_cd (bool yn, void *src);
89         void set_is_range_marker (bool yn, void* src);
90         void set_skip (bool yn);
91         void set_skipping (bool yn);
92
93         bool is_auto_punch () const { return _flags & IsAutoPunch; }
94         bool is_auto_loop () const { return _flags & IsAutoLoop; }
95         bool is_mark () const { return _flags & IsMark; }
96         bool is_hidden () const { return _flags & IsHidden; }
97         bool is_cd_marker () const { return _flags & IsCDMarker; }
98         bool is_session_range () const { return _flags & IsSessionRange; }
99         bool is_range_marker() const { return _flags & IsRangeMarker; }
100         bool is_skip() const { return _flags & IsSkip; }
101         bool is_skipping() const { return (_flags & IsSkip) && (_flags & IsSkipping); }
102         bool matches (Flags f) const { return _flags & f; }
103
104         Flags flags () const { return _flags; }
105
106         boost::shared_ptr<SceneChange> scene_change() const { return _scene_change; }
107         void set_scene_change (boost::shared_ptr<SceneChange>);
108
109         /* these are static signals for objects that want to listen to all
110            locations at once.
111         */
112
113         static PBD::Signal1<void,Location*> name_changed;
114         static PBD::Signal1<void,Location*> end_changed;
115         static PBD::Signal1<void,Location*> start_changed;
116         static PBD::Signal1<void,Location*> flags_changed;
117         static PBD::Signal1<void,Location*> lock_changed;
118         static PBD::Signal1<void,Location*> position_lock_style_changed;
119
120         /* this is sent only when both start and end change at the same time */
121         static PBD::Signal1<void,Location*> changed;
122
123         /* these are member signals for objects that care only about
124            changes to this object
125         */
126
127         PBD::Signal0<void> Changed;
128
129         PBD::Signal0<void> NameChanged;
130         PBD::Signal0<void> EndChanged;
131         PBD::Signal0<void> StartChanged;
132         PBD::Signal0<void> FlagsChanged;
133         PBD::Signal0<void> LockChanged;
134         PBD::Signal0<void> PositionLockStyleChanged;
135
136         /* CD Track / CD-Text info */
137
138         std::map<std::string, std::string> cd_info;
139         XMLNode& cd_info_node (const std::string &, const std::string &);
140
141         XMLNode& get_state (void);
142         int set_state (const XMLNode&, int version);
143
144         PositionLockStyle position_lock_style() const { return _position_lock_style; }
145         void set_position_lock_style (PositionLockStyle ps);
146         void recompute_frames_from_bbt ();
147
148         static PBD::Signal0<void> scene_changed; /* for use by backend scene change management, class level */
149         PBD::Signal0<void> SceneChangeChanged;   /* for use by objects interested in this object */
150
151   private:
152         std::string        _name;
153         framepos_t         _start;
154         Timecode::BBT_Time _bbt_start;
155         framepos_t         _end;
156         Timecode::BBT_Time _bbt_end;
157         Flags              _flags;
158         bool               _locked;
159         PositionLockStyle  _position_lock_style;
160         boost::shared_ptr<SceneChange> _scene_change;
161
162         void set_mark (bool yn);
163         bool set_flag_internal (bool yn, Flags flag);
164         void recompute_bbt_from_frames ();
165 };
166
167 /** A collection of session locations including unique dedicated locations (loop, punch, etc) */
168 class LIBARDOUR_API Locations : public SessionHandleRef, public PBD::StatefulDestructible
169 {
170   public:
171         typedef std::list<Location *> LocationList;
172
173         Locations (Session &);
174         ~Locations ();
175
176         const LocationList& list() { return locations; }
177
178         void add (Location *, bool make_current = false);
179         void remove (Location *);
180         void clear ();
181         void clear_markers ();
182         void clear_ranges ();
183
184         XMLNode& get_state (void);
185         int set_state (const XMLNode&, int version);
186         Location *get_location_by_id(PBD::ID);
187
188         Location* auto_loop_location () const;
189         Location* auto_punch_location () const;
190         Location* session_range_location() const;
191
192         int next_available_name(std::string& result,std::string base);
193         uint32_t num_range_markers() const;
194
195         int set_current (Location *, bool want_lock = true);
196         Location *current () const { return current_location; }
197
198         Location* mark_at (framepos_t, framecnt_t slop = 0) const;
199
200         framepos_t first_mark_before (framepos_t, bool include_special_ranges = false);
201         framepos_t first_mark_after (framepos_t, bool include_special_ranges = false);
202
203         void marks_either_side (framepos_t const, framepos_t &, framepos_t &) const;
204
205         void find_all_between (framepos_t start, framepos_t, LocationList&, Location::Flags);
206
207         PBD::Signal1<void,Location*> current_changed;
208
209         /* Objects that care about individual addition and removal of Locations should connect to added/removed.
210            If an object additionally cares about potential mass clearance of Locations, they should connect to changed.
211         */
212
213         PBD::Signal1<void,Location*> added;
214         PBD::Signal1<void,Location*> removed;
215         PBD::Signal0<void> changed; /* emitted when any action that could have added/removed more than 1 location actually removed 1 or more */
216
217         template<class T> void apply (T& obj, void (T::*method)(const LocationList&)) const {
218                 /* We don't want to hold the lock while the given method runs, so take a copy
219                    of the list and pass that instead.
220                 */
221                 Locations::LocationList copy;
222                 {
223                         Glib::Threads::Mutex::Lock lm (lock);
224                         copy = locations;
225                 }
226                 (obj.*method)(copy);
227         }
228
229   private:
230
231         LocationList         locations;
232         Location            *current_location;
233         mutable Glib::Threads::Mutex  lock;
234
235         int set_current_unlocked (Location *);
236         void location_changed (Location*);
237         void listen_to (Location*);
238 };
239
240 } // namespace ARDOUR
241
242 #endif /* __ardour_location_h__ */