remove a bunch of uses of long (mostly replaced by int32_t)
[ardour.git] / libs / ardour / session_events.cc
1 /*
2     Copyright (C) 1999-2004 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cmath>
21 #include <unistd.h>
22
23 #include "ardour/timestamps.h"
24
25 #include "pbd/error.h"
26 #include "pbd/enumwriter.h"
27
28 #include "ardour/ardour.h"
29 #include "ardour/audio_diskstream.h"
30 #include "ardour/butler.h"
31 #include "ardour/debug.h"
32 #include "ardour/session_event.h"
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace PBD;
39
40 PerThreadPool* SessionEvent::pool;
41
42 void
43 SessionEvent::init_event_pool ()
44 {
45         pool = new PerThreadPool;
46 }
47
48 void
49 SessionEvent::create_per_thread_pool (const std::string& name, uint32_t nitems)
50 {
51         /* this is a per-thread call that simply creates a thread-private ptr to
52            a CrossThreadPool for use by this thread whenever events are allocated/released
53            from SessionEvent::pool()
54         */
55         pool->create_per_thread_pool (name, sizeof (SessionEvent), nitems);
56 }
57
58 void *
59 SessionEvent::operator new (size_t) 
60 {
61         CrossThreadPool* p = pool->per_thread_pool ();
62         SessionEvent* ev = static_cast<SessionEvent*> (p->alloc ());
63         ev->own_pool = p;
64         return ev;
65 }
66     
67 void 
68 SessionEvent::operator delete (void *ptr, size_t /*size*/) 
69 {
70         Pool* p = pool->per_thread_pool ();
71         SessionEvent* ev = static_cast<SessionEvent*> (ptr);
72
73         if (p == ev->own_pool) {
74                 p->release (ptr);
75         } else {
76                 ev->own_pool->push (ev);
77         }
78 }
79
80 void
81 SessionEventManager::add_event (nframes64_t frame, SessionEvent::Type type, nframes64_t target_frame)
82 {
83         SessionEvent* ev = new SessionEvent (type, SessionEvent::Add, frame, target_frame, 0);
84         queue_event (ev);
85 }
86
87 void
88 SessionEventManager::remove_event (nframes64_t frame, SessionEvent::Type type)
89 {
90         SessionEvent* ev = new SessionEvent (type, SessionEvent::Remove, frame, 0, 0);
91         queue_event (ev);
92 }
93
94 void
95 SessionEventManager::replace_event (SessionEvent::Type type, nframes64_t frame, nframes64_t target)
96 {
97         SessionEvent* ev = new SessionEvent (type, SessionEvent::Replace, frame, target, 0);
98         queue_event (ev);
99 }
100
101 void
102 SessionEventManager::clear_events (SessionEvent::Type type)
103 {
104         SessionEvent* ev = new SessionEvent (type, SessionEvent::Clear, 0, 0, 0);
105         queue_event (ev);
106 }
107
108
109 void
110 SessionEventManager::dump_events () const
111 {
112         cerr << "EVENT DUMP" << endl;
113         for (Events::const_iterator i = events.begin(); i != events.end(); ++i) {
114                 cerr << "\tat " << (*i)->action_frame << ' ' << (*i)->type << " target = " << (*i)->target_frame << endl;
115         }
116         cerr << "Next event: ";
117
118         if ((Events::const_iterator) next_event == events.end()) {
119                 cerr << "none" << endl;
120         } else {
121                 cerr << "at " << (*next_event)->action_frame << ' '
122                      << (*next_event)->type << " target = "
123                      << (*next_event)->target_frame << endl;
124         }
125         cerr << "Immediate events pending:\n";
126         for (Events::const_iterator i = immediate_events.begin(); i != immediate_events.end(); ++i) {
127                 cerr << "\tat " << (*i)->action_frame << ' ' << (*i)->type << " target = " << (*i)->target_frame << endl;
128         }
129         cerr << "END EVENT_DUMP" << endl;
130 }
131
132 void
133 SessionEventManager::merge_event (SessionEvent* ev)
134 {
135         switch (ev->action) {
136         case SessionEvent::Remove:
137                 _remove_event (ev);
138                 delete ev;
139                 return;
140
141         case SessionEvent::Replace:
142                 _replace_event (ev);
143                 return;
144
145         case SessionEvent::Clear:
146                 _clear_event_type (ev->type);
147                 delete ev;
148                 return;
149
150         case SessionEvent::Add:
151                 break;
152         }
153
154         /* try to handle immediate events right here */
155
156         if (ev->action_frame == 0) {
157                 process_event (ev);
158                 return;
159         }
160
161         switch (ev->type) {
162         case SessionEvent::AutoLoop:
163         case SessionEvent::StopOnce:
164                 _clear_event_type (ev->type);
165                 break;
166
167         default:
168                 for (Events::iterator i = events.begin(); i != events.end(); ++i) {
169                         if ((*i)->type == ev->type && (*i)->action_frame == ev->action_frame) {
170                           error << string_compose(_("Session: cannot have two events of type %1 at the same frame (%2)."),
171                                                   enum_2_string (ev->type), ev->action_frame) << endmsg;
172                                 return;
173                         }
174                 }
175         }
176
177         events.insert (events.begin(), ev);
178         events.sort (SessionEvent::compare);
179         next_event = events.begin();
180         set_next_event ();
181 }
182
183 /** @return true when @a ev is deleted. */
184 bool
185 SessionEventManager::_replace_event (SessionEvent* ev)
186 {
187         bool ret = false;
188         Events::iterator i;
189
190         /* private, used only for events that can only exist once in the queue */
191
192         for (i = events.begin(); i != events.end(); ++i) {
193                 if ((*i)->type == ev->type) {
194                         (*i)->action_frame = ev->action_frame;
195                         (*i)->target_frame = ev->target_frame;
196                         if ((*i) == ev) {
197                                 ret = true;
198                         }
199                         delete ev;
200                         break;
201                 }
202         }
203
204         if (i == events.end()) {
205                 events.insert (events.begin(), ev);
206         }
207
208         events.sort (SessionEvent::compare);
209         next_event = events.end();
210         set_next_event ();
211
212         return ret;
213 }
214
215 /** @return true when @a ev is deleted. */
216 bool
217 SessionEventManager::_remove_event (SessionEvent* ev)
218 {
219         bool ret = false;
220         Events::iterator i;
221
222         for (i = events.begin(); i != events.end(); ++i) {
223                 if ((*i)->type == ev->type && (*i)->action_frame == ev->action_frame) {
224                         if ((*i) == ev) {
225                                 ret = true;
226                         }
227
228                         delete *i;
229                         if (i == next_event) {
230                                 ++next_event;
231                         }
232                         events.erase (i);
233                         break;
234                 }
235         }
236
237         if (i != events.end()) {
238                 set_next_event ();
239         }
240
241         return ret;
242 }
243
244 void
245 SessionEventManager::_clear_event_type (SessionEvent::Type type)
246 {
247         Events::iterator i, tmp;
248
249         for (i = events.begin(); i != events.end(); ) {
250
251                 tmp = i;
252                 ++tmp;
253
254                 if ((*i)->type == type) {
255                         delete *i;
256                         if (i == next_event) {
257                                 ++next_event;
258                         }
259                         events.erase (i);
260                 }
261
262                 i = tmp;
263         }
264
265         for (i = immediate_events.begin(); i != immediate_events.end(); ) {
266
267                 tmp = i;
268                 ++tmp;
269
270                 if ((*i)->type == type) {
271                         delete *i;
272                         immediate_events.erase (i);
273                 }
274
275                 i = tmp;
276         }
277
278         set_next_event ();
279 }
280