OSC: a strip controlling a send should deactivate other controls
[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                 IsClockOrigin = 0x200,
59         };
60
61         Location (Session &);
62         Location (Session &, samplepos_t, samplepos_t, const std::string &, Flags bits = Flags(0), const uint32_t sub_num = 0);
63         Location (const Location& other);
64         Location (Session &, const XMLNode&);
65         Location* operator= (const Location& other);
66
67         bool operator==(const Location& other);
68
69         bool locked() const { return _locked; }
70         void lock ();
71         void unlock ();
72
73         samplepos_t start() const  { return _start; }
74         samplepos_t end() const { return _end; }
75         samplecnt_t length() const { return _end - _start; }
76
77         int set_start (samplepos_t s, bool force = false, bool allow_beat_recompute = true, const uint32_t sub_num = 0);
78         int set_end (samplepos_t e, bool force = false, bool allow_beat_recompute = true, const uint32_t sub_num = 0);
79         int set (samplepos_t start, samplepos_t end, bool allow_beat_recompute = true, const uint32_t sub_num = 0);
80
81         int move_to (samplepos_t pos, const uint32_t sub_num);
82
83         const std::string& name() const { return _name; }
84         void set_name (const std::string &str);
85
86         void set_auto_punch (bool yn, void *src);
87         void set_auto_loop (bool yn, void *src);
88         void set_hidden (bool yn, void *src);
89         void set_cd (bool yn, void *src);
90         void set_is_range_marker (bool yn, void* src);
91         void set_is_clock_origin (bool yn, void* src);
92         void set_skip (bool yn);
93         void set_skipping (bool yn);
94
95         bool is_auto_punch () const { return _flags & IsAutoPunch; }
96         bool is_auto_loop () const { return _flags & IsAutoLoop; }
97         bool is_mark () const { return _flags & IsMark; }
98         bool is_hidden () const { return _flags & IsHidden; }
99         bool is_cd_marker () const { return _flags & IsCDMarker; }
100         bool is_session_range () const { return _flags & IsSessionRange; }
101         bool is_range_marker() const { return _flags & IsRangeMarker; }
102         bool is_skip() const { return _flags & IsSkip; }
103         bool is_clock_origin() const { return _flags & IsClockOrigin; }
104         bool is_skipping() const { return (_flags & IsSkip) && (_flags & IsSkipping); }
105         bool matches (Flags f) const { return _flags & f; }
106
107         Flags flags () const { return _flags; }
108
109         boost::shared_ptr<SceneChange> scene_change() const { return _scene_change; }
110         void set_scene_change (boost::shared_ptr<SceneChange>);
111
112         /* these are static signals for objects that want to listen to all
113            locations at once.
114         */
115
116         static PBD::Signal1<void,Location*> name_changed;
117         static PBD::Signal1<void,Location*> end_changed;
118         static PBD::Signal1<void,Location*> start_changed;
119         static PBD::Signal1<void,Location*> flags_changed;
120         static PBD::Signal1<void,Location*> lock_changed;
121         static PBD::Signal1<void,Location*> position_lock_style_changed;
122
123         /* this is sent only when both start and end change at the same time */
124         static PBD::Signal1<void,Location*> changed;
125
126         /* these are member signals for objects that care only about
127            changes to this object
128         */
129
130         PBD::Signal0<void> Changed;
131
132         PBD::Signal0<void> NameChanged;
133         PBD::Signal0<void> EndChanged;
134         PBD::Signal0<void> StartChanged;
135         PBD::Signal0<void> FlagsChanged;
136         PBD::Signal0<void> LockChanged;
137         PBD::Signal0<void> PositionLockStyleChanged;
138
139         /* CD Track / CD-Text info */
140
141         std::map<std::string, std::string> cd_info;
142         XMLNode& cd_info_node (const std::string &, const std::string &);
143
144         XMLNode& get_state (void);
145         int set_state (const XMLNode&, int version);
146
147         PositionLockStyle position_lock_style() const { return _position_lock_style; }
148         void set_position_lock_style (PositionLockStyle ps);
149         void recompute_samples_from_beat ();
150
151         static PBD::Signal0<void> scene_changed; /* for use by backend scene change management, class level */
152         PBD::Signal0<void> SceneChangeChanged;   /* for use by objects interested in this object */
153
154   private:
155         std::string        _name;
156         samplepos_t         _start;
157         double             _start_beat;
158         samplepos_t         _end;
159         double             _end_beat;
160         Flags              _flags;
161         bool               _locked;
162         PositionLockStyle  _position_lock_style;
163         boost::shared_ptr<SceneChange> _scene_change;
164
165         void set_mark (bool yn);
166         bool set_flag_internal (bool yn, Flags flag);
167         void recompute_beat_from_samples (const uint32_t sub_num);
168 };
169
170 /** A collection of session locations including unique dedicated locations (loop, punch, etc) */
171 class LIBARDOUR_API Locations : public SessionHandleRef, public PBD::StatefulDestructible
172 {
173   public:
174         typedef std::list<Location *> LocationList;
175
176         Locations (Session &);
177         ~Locations ();
178
179         const LocationList& list () const { return locations; }
180         LocationList list () { return locations; }
181
182         void add (Location *, bool make_current = false);
183         void remove (Location *);
184         void clear ();
185         void clear_markers ();
186         void clear_ranges ();
187
188         XMLNode& get_state (void);
189         int set_state (const XMLNode&, int version);
190         Location *get_location_by_id(PBD::ID);
191
192         Location* auto_loop_location () const;
193         Location* auto_punch_location () const;
194         Location* session_range_location() const;
195         Location* clock_origin_location() const;
196
197         int next_available_name(std::string& result,std::string base);
198         uint32_t num_range_markers() const;
199
200         int set_current (Location *, bool want_lock = true);
201         Location *current () const { return current_location; }
202
203         Location* mark_at (samplepos_t, samplecnt_t slop = 0) const;
204
205         void set_clock_origin (Location*, void *src);
206
207         samplepos_t first_mark_before (samplepos_t, bool include_special_ranges = false);
208         samplepos_t first_mark_after (samplepos_t, bool include_special_ranges = false);
209
210         void marks_either_side (samplepos_t const, samplepos_t &, samplepos_t &) const;
211
212         void find_all_between (samplepos_t start, samplepos_t, LocationList&, Location::Flags);
213
214         PBD::Signal1<void,Location*> current_changed;
215
216         /* Objects that care about individual addition and removal of Locations should connect to added/removed.
217            If an object additionally cares about potential mass clearance of Locations, they should connect to changed.
218         */
219
220         PBD::Signal1<void,Location*> added;
221         PBD::Signal1<void,Location*> removed;
222         PBD::Signal0<void> changed; /* emitted when any action that could have added/removed more than 1 location actually removed 1 or more */
223
224         template<class T> void apply (T& obj, void (T::*method)(const LocationList&)) const {
225                 /* We don't want to hold the lock while the given method runs, so take a copy
226                    of the list and pass that instead.
227                 */
228                 Locations::LocationList copy;
229                 {
230                         Glib::Threads::Mutex::Lock lm (lock);
231                         copy = locations;
232                 }
233                 (obj.*method)(copy);
234         }
235
236   private:
237
238         LocationList         locations;
239         Location            *current_location;
240         mutable Glib::Threads::Mutex  lock;
241
242         int set_current_unlocked (Location *);
243         void location_changed (Location*);
244         void listen_to (Location*);
245 };
246
247 } // namespace ARDOUR
248
249 #endif /* __ardour_location_h__ */