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