3f7fc6c092219861f4ccfd4da7b3055f1e158485
[ardour.git] / libs / ardour / ardour / session_event.h
1 /*
2     Copyright (C) 2012 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_session_event_h__
21 #define __ardour_session_event_h__
22
23 #include <list>
24 #include <boost/function.hpp>
25 #include <boost/shared_ptr.hpp>
26
27 #include "pbd/pool.h"
28 #include "pbd/ringbuffer.h"
29 #include "pbd/event_loop.h"
30
31 #include "ardour/libardour_visibility.h"
32 #include "ardour/types.h"
33
34 namespace ARDOUR {
35
36 class Slave;
37 class Region;
38
39 class LIBARDOUR_API SessionEvent {
40 public:
41         enum Type {
42                 SetTransportSpeed,
43                 SetTrackSpeed,
44                 Locate,
45                 LocateRoll,
46                 LocateRollLocate,
47                 SetLoop,
48                 PunchIn,
49                 PunchOut,
50                 RangeStop,
51                 RangeLocate,
52                 Overwrite,
53                 SetSyncSource,
54                 Audition,
55                 InputConfigurationChange,
56                 SetPlayAudioRange,
57                 CancelPlayAudioRange,
58                 RealTimeOperation,
59                 AdjustPlaybackBuffering,
60                 AdjustCaptureBuffering,
61                 SetTimecodeTransmission,
62                 Skip,
63
64                 /* only one of each of these events can be queued at any one time */
65
66                 StopOnce,
67                 AutoLoop,
68                 AutoLoopDeclick,
69         };
70
71         enum Action {
72                 Add,
73                 Remove,
74                 Replace,
75                 Clear
76         };
77
78         Type       type;
79         Action     action;
80         framepos_t action_frame;
81         framepos_t target_frame;
82         double     speed;
83
84         union {
85                 void*        ptr;
86                 bool         yes_or_no;
87                 framepos_t  target2_frame;
88                 Slave*       slave;
89                 Route*       route;
90         };
91
92         union {
93                 bool second_yes_or_no;
94         };
95
96         union {
97                 bool third_yes_or_no;
98         };
99
100         /* 4 members to handle a multi-group event handled in RT context */
101
102         typedef boost::function<void (SessionEvent*)> RTeventCallback;
103
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
114         SessionEvent (Type t, Action a, framepos_t when, framepos_t where, double spd, bool yn = false, bool yn2 = false, bool yn3 = false)
115                 : type (t)
116                 , action (a)
117                 , action_frame (when)
118                 , target_frame (where)
119                 , speed (spd)
120                 , yes_or_no (yn)
121                 , second_yes_or_no (yn2)
122                 , third_yes_or_no (yn3)
123                 , event_loop (0) {}
124
125         void set_ptr (void* p) {
126                 ptr = p;
127         }
128
129         bool before (const SessionEvent& other) const {
130                 return action_frame < other.action_frame;
131         }
132
133         bool after (const SessionEvent& other) const {
134                 return action_frame > other.action_frame;
135         }
136
137         static bool compare (const SessionEvent *e1, const SessionEvent *e2) {
138                 return e1->before (*e2);
139         }
140
141         void* operator new (size_t);
142         void  operator delete (void *ptr, size_t /*size*/);
143
144         static const framepos_t Immediate = -1;
145
146         static void create_per_thread_pool (const std::string& n, uint32_t nitems);
147         static void init_event_pool ();
148
149 private:
150         static PerThreadPool* pool;
151         CrossThreadPool* own_pool;
152
153         friend class Butler;
154 };
155
156 class SessionEventManager {
157 public:
158         SessionEventManager () : pending_events (2048),
159                                  auto_loop_event(0), punch_out_event(0), punch_in_event(0) {}
160         virtual ~SessionEventManager() {}
161
162         virtual void queue_event (SessionEvent *ev) = 0;
163         void clear_events (SessionEvent::Type type);
164
165 protected:
166         RingBuffer<SessionEvent*> pending_events;
167         typedef std::list<SessionEvent *> Events;
168         Events           events;
169         Events           immediate_events;
170         Events::iterator next_event;
171
172         /* there can only ever be one of each of these */
173
174         SessionEvent *auto_loop_event;
175         SessionEvent *punch_out_event;
176         SessionEvent *punch_in_event;
177
178         void dump_events () const;
179         void merge_event (SessionEvent*);
180         void replace_event (SessionEvent::Type, framepos_t action_frame, framepos_t target = 0);
181         bool _replace_event (SessionEvent*);
182         bool _remove_event (SessionEvent *);
183         void _clear_event_type (SessionEvent::Type);
184
185         void add_event (framepos_t action_frame, SessionEvent::Type type, framepos_t target_frame = 0);
186         void remove_event (framepos_t frame, SessionEvent::Type type);
187
188         virtual void process_event(SessionEvent*) = 0;
189         virtual void set_next_event () = 0;
190 };
191
192 } /* namespace */
193
194 #endif /* __ardour_session_event_h__ */