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