Update libardour GPL boilerplate and (C) from git log
[ardour.git] / libs / ardour / ardour / session_event.h
1 /*
2  * Copyright (C) 2009-2018 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
4  * Copyright (C) 2010-2012 David Robillard <d@drobilla.net>
5  * Copyright (C) 2015-2018 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifndef __ardour_session_event_h__
23 #define __ardour_session_event_h__
24
25 #include <list>
26 #include <boost/function.hpp>
27 #include <boost/shared_ptr.hpp>
28
29 #include "pbd/pool.h"
30 #include "pbd/ringbuffer.h"
31 #include "pbd/event_loop.h"
32
33 #include "ardour/libardour_visibility.h"
34 #include "ardour/types.h"
35
36 namespace ARDOUR {
37
38 class TransportMaster;
39 class Region;
40
41 class LIBARDOUR_API SessionEvent {
42 public:
43         enum Type {
44                 SetTransportSpeed,
45                 Locate,
46                 LocateRoll,
47                 LocateRollLocate,
48                 SetLoop,
49                 PunchIn,
50                 PunchOut,
51                 RangeStop,
52                 RangeLocate,
53                 Overwrite,
54                 Audition,
55                 SetPlayAudioRange,
56                 CancelPlayAudioRange,
57                 RealTimeOperation,
58                 AdjustPlaybackBuffering,
59                 AdjustCaptureBuffering,
60                 SetTimecodeTransmission,
61                 Skip,
62                 SetTransportMaster,
63
64                 /* only one of each of these events can be queued at any one time */
65
66                 StopOnce,
67                 AutoLoop,
68         };
69
70         enum Action {
71                 Add,
72                 Remove,
73                 Replace,
74                 Clear
75         };
76
77         Type       type;
78         Action     action;
79         samplepos_t action_sample;
80         samplepos_t target_sample;
81         double     speed;
82
83         union {
84                 void*            ptr;
85                 bool             yes_or_no;
86                 samplepos_t      target2_sample;
87                 Route*           route;
88         };
89
90         union {
91                 bool second_yes_or_no;
92                 double control_value;
93         };
94
95         union {
96                 bool third_yes_or_no;
97         };
98
99         /* 5 members to handle a multi-group event handled in RT context */
100
101         typedef boost::function<void (SessionEvent*)> RTeventCallback;
102
103         boost::shared_ptr<ControlList> controls; /* apply to */
104         boost::shared_ptr<RouteList> routes;     /* apply to */
105         boost::function<void (void)> rt_slot;    /* what to call in RT context */
106         RTeventCallback              rt_return;  /* called after rt_slot, with this event as an argument */
107         PBD::EventLoop*              event_loop;
108
109         std::list<AudioRange> audio_range;
110         std::list<MusicRange> music_range;
111
112         boost::shared_ptr<Region> region;
113         boost::shared_ptr<TransportMaster> transport_master;
114
115         SessionEvent (Type t, Action a, samplepos_t when, samplepos_t where, double spd, bool yn = false, bool yn2 = false, bool yn3 = false);
116
117         void set_ptr (void* p) {
118                 ptr = p;
119         }
120
121         bool before (const SessionEvent& other) const {
122                 return action_sample < other.action_sample;
123         }
124
125         bool after (const SessionEvent& other) const {
126                 return action_sample > other.action_sample;
127         }
128
129         static bool compare (const SessionEvent *e1, const SessionEvent *e2) {
130                 return e1->before (*e2);
131         }
132
133         void* operator new (size_t);
134         void  operator delete (void *ptr, size_t /*size*/);
135
136         static const samplepos_t Immediate = -1;
137
138         static bool has_per_thread_pool ();
139         static void create_per_thread_pool (const std::string& n, uint32_t nitems);
140         static void init_event_pool ();
141
142         CrossThreadPool* event_pool() const { return own_pool; }
143
144 private:
145         static PerThreadPool* pool;
146         CrossThreadPool* own_pool;
147
148         friend class Butler;
149 };
150
151 class SessionEventManager {
152 public:
153         SessionEventManager () : pending_events (2048),
154                                  auto_loop_event(0), punch_out_event(0), punch_in_event(0) {}
155         virtual ~SessionEventManager() {}
156
157         virtual void queue_event (SessionEvent *ev) = 0;
158         void clear_events (SessionEvent::Type type);
159         void clear_events (SessionEvent::Type type, boost::function<void (void)> after);
160
161 protected:
162         PBD::RingBuffer<SessionEvent*> pending_events;
163         typedef std::list<SessionEvent *> Events;
164         Events           events;
165         Events           immediate_events;
166         Events::iterator next_event;
167
168         Glib::Threads::Mutex rb_write_lock;
169
170         /* there can only ever be one of each of these */
171
172         SessionEvent *auto_loop_event;
173         SessionEvent *punch_out_event;
174         SessionEvent *punch_in_event;
175
176         void dump_events () const;
177         void merge_event (SessionEvent*);
178         void replace_event (SessionEvent::Type, samplepos_t action_sample, samplepos_t target = 0);
179         bool _replace_event (SessionEvent*);
180         bool _remove_event (SessionEvent *);
181         void _clear_event_type (SessionEvent::Type);
182
183         void add_event (samplepos_t action_sample, SessionEvent::Type type, samplepos_t target_sample = 0);
184         void remove_event (samplepos_t sample, SessionEvent::Type type);
185
186         virtual void process_event(SessionEvent*) = 0;
187         virtual void set_next_event () = 0;
188 };
189
190 } /* namespace */
191
192 #endif /* __ardour_session_event_h__ */