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