Move Diskstream ownership to Track, so that Session no longer holds lists of Diskstre...
[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
38             /* only one of each of these events can be queued at any one time */
39             
40             StopOnce,
41             AutoLoop
42     };
43     
44     enum Action {
45             Add,
46             Remove,
47             Replace,
48             Clear
49     };
50     
51     Type             type;
52     Action           action;
53     nframes64_t      action_frame;
54     nframes64_t      target_frame;
55     double           speed;
56     
57     union {
58         void*        ptr;
59         bool         yes_or_no;
60         nframes64_t  target2_frame;
61         Slave*       slave;
62         Route*       route;
63     };
64     
65     union {
66         bool second_yes_or_no;
67     };
68
69     /* 4 members to handle a multi-group event handled in RT context */
70
71     typedef boost::function<void (SessionEvent*)> RTeventCallback;
72
73     boost::shared_ptr<RouteList> routes;    /* apply to */
74     boost::function<void (void)> rt_slot;   /* what to call in RT context */
75     RTeventCallback              rt_return; /* called after rt_slot, with this event as an argument */
76     PBD::EventLoop*              event_loop;
77
78     std::list<AudioRange> audio_range;
79     std::list<MusicRange> music_range;
80     
81     boost::shared_ptr<Region> region;
82
83     SessionEvent (Type t, Action a, nframes_t when, nframes_t where, double spd, bool yn = false, bool yn2 = false)
84             : type (t)
85             , action (a)
86             , action_frame (when)
87             , target_frame (where)
88             , speed (spd)
89             , yes_or_no (yn)
90             , second_yes_or_no (yn2)
91             , event_loop (0) {}
92
93     void set_ptr (void* p) {
94             ptr = p;
95     }
96     
97     bool before (const SessionEvent& other) const {
98             return action_frame < other.action_frame;
99     }
100     
101     bool after (const SessionEvent& other) const {
102             return action_frame > other.action_frame;
103     }
104     
105     static bool compare (const SessionEvent *e1, const SessionEvent *e2) {
106             return e1->before (*e2);
107     }
108     
109     void* operator new (size_t);
110     void  operator delete (void *ptr, size_t /*size*/);
111     
112     static const nframes_t Immediate = 0;
113     
114     static void create_per_thread_pool (const std::string& n, unsigned long nitems);
115     static void init_event_pool ();
116
117 private:
118     static PerThreadPool* pool;
119     CrossThreadPool* own_pool;
120
121     friend class Butler;
122 };
123
124 class SessionEventManager {
125 public:
126         SessionEventManager () : pending_events (2048),
127                 auto_loop_event(0), punch_out_event(0), punch_in_event(0) {}
128         virtual ~SessionEventManager() {}
129
130         virtual void queue_event (SessionEvent *ev) = 0;
131         void clear_events (SessionEvent::Type type);
132
133 protected:
134         RingBuffer<SessionEvent*> pending_events;
135         typedef std::list<SessionEvent *> Events;
136         Events           events;
137         Events           immediate_events;
138         Events::iterator next_event;
139
140         /* there can only ever be one of each of these */
141
142         SessionEvent *auto_loop_event;
143         SessionEvent *punch_out_event;
144         SessionEvent *punch_in_event;
145
146         void dump_events () const;
147         void merge_event (SessionEvent*);
148         void replace_event (SessionEvent::Type, nframes64_t action_frame, nframes64_t target = 0);
149         bool _replace_event (SessionEvent*);
150         bool _remove_event (SessionEvent *);
151         void _clear_event_type (SessionEvent::Type);
152
153         void add_event (nframes64_t action_frame, SessionEvent::Type type, nframes64_t target_frame = 0);
154         void remove_event (nframes64_t frame, SessionEvent::Type type);
155
156         virtual void process_event(SessionEvent*) = 0;
157         virtual void set_next_event () = 0;
158 };
159
160 } /* namespace */
161
162 #endif /* __ardour_session_event_h__ */