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