first pass at end-to-end RT operation request (GUI->session->RT thread->GUI), just...
[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             SetRecordEnable,
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         RouteList* routes;
68     };
69
70     sigc::slot<void>               rt_slot;    /* what to call in RT context */
71     sigc::slot<void,SessionEvent*> rt_return;  /* called after rt_slot, with this event as an argument */
72
73     std::list<AudioRange> audio_range;
74     std::list<MusicRange> music_range;
75     
76     boost::shared_ptr<Region> region;
77
78     SessionEvent (Type t, Action a, nframes_t when, nframes_t where, double spd, bool yn = false, bool yn2 = false)
79             : type (t)
80             , action (a)
81             , action_frame (when)
82             , target_frame (where)
83             , speed (spd)
84             , yes_or_no (yn)
85             , second_yes_or_no (yn2) {}
86
87     void set_ptr (void* p) {
88             ptr = p;
89     }
90     
91     bool before (const SessionEvent& other) const {
92             return action_frame < other.action_frame;
93     }
94     
95     bool after (const SessionEvent& other) const {
96             return action_frame > other.action_frame;
97     }
98     
99     static bool compare (const SessionEvent *e1, const SessionEvent *e2) {
100             return e1->before (*e2);
101     }
102     
103     void* operator new (size_t);
104     void  operator delete (void *ptr, size_t /*size*/);
105     
106     static const nframes_t Immediate = 0;
107     
108     static void create_per_thread_pool (const std::string& n, unsigned long nitems);
109     static void init_event_pool ();
110
111 private:
112     static PerThreadPool* pool;
113     CrossThreadPool* own_pool;
114 };
115
116 class SessionEventManager {
117    public:
118         SessionEventManager () : pending_events (2048){}
119         virtual ~SessionEventManager() {}
120
121         void add_event (nframes64_t action_frame, SessionEvent::Type type, nframes64_t target_frame = 0);
122         void remove_event (nframes64_t frame, SessionEvent::Type type);
123         void clear_events (SessionEvent::Type type);
124     
125         
126   protected:
127         RingBuffer<SessionEvent*> pending_events;
128         typedef std::list<SessionEvent *> Events;
129         Events           events;
130         Events           immediate_events;
131         Events::iterator next_event;
132
133         /* there can only ever be one of each of these */
134
135         SessionEvent *auto_loop_event;
136         SessionEvent *punch_out_event;
137         SessionEvent *punch_in_event;
138     
139         void dump_events () const;
140         void merge_event (SessionEvent*);
141         void replace_event (SessionEvent::Type, nframes64_t action_frame, nframes64_t target = 0);
142         bool _replace_event (SessionEvent*);
143         bool _remove_event (SessionEvent *);
144         void _clear_event_type (SessionEvent::Type);
145
146         virtual void process_event(SessionEvent*) = 0;
147         virtual void queue_event (SessionEvent *ev) = 0; 
148         virtual void set_next_event () = 0;
149 };
150
151 } /* namespace */
152
153 #endif /* __ardour_session_event_h__ */