2 Copyright (C) 1999-2004 Paul Davis
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.
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.
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.
23 #include "ardour/timestamps.h"
25 #include "pbd/error.h"
26 #include "pbd/enumwriter.h"
27 #include "pbd/stacktrace.h"
29 #include "ardour/ardour.h"
30 #include "ardour/audio_diskstream.h"
31 #include "ardour/butler.h"
32 #include "ardour/debug.h"
33 #include "ardour/session_event.h"
38 using namespace ARDOUR;
41 PerThreadPool* SessionEvent::pool;
44 SessionEvent::init_event_pool ()
46 pool = new PerThreadPool;
50 SessionEvent::create_per_thread_pool (const std::string& name, uint32_t nitems)
52 /* this is a per-thread call that simply creates a thread-private ptr to
53 a CrossThreadPool for use by this thread whenever events are allocated/released
54 from SessionEvent::pool()
56 pool->create_per_thread_pool (name, sizeof (SessionEvent), nitems);
60 SessionEvent::operator new (size_t)
62 CrossThreadPool* p = pool->per_thread_pool ();
63 SessionEvent* ev = static_cast<SessionEvent*> (p->alloc ());
64 cerr << pthread_self() << " Allocating SessionEvent from " << p->name() << " ev @ " << ev << endl;
70 SessionEvent::operator delete (void *ptr, size_t /*size*/)
72 Pool* p = pool->per_thread_pool ();
73 SessionEvent* ev = static_cast<SessionEvent*> (ptr);
75 cerr << pthread_self() << " Deleting SessionEvent @ " << ev << " thread pool = " << p->name() << " ev pool = " << ev->own_pool->name() << endl;
76 stacktrace (cerr, 20);
77 if (p == ev->own_pool) {
80 ev->own_pool->push (ev);
85 SessionEventManager::add_event (framepos_t frame, SessionEvent::Type type, framepos_t target_frame)
87 SessionEvent* ev = new SessionEvent (type, SessionEvent::Add, frame, target_frame, 0);
92 SessionEventManager::remove_event (framepos_t frame, SessionEvent::Type type)
94 SessionEvent* ev = new SessionEvent (type, SessionEvent::Remove, frame, 0, 0);
99 SessionEventManager::replace_event (SessionEvent::Type type, framepos_t frame, framepos_t target)
101 SessionEvent* ev = new SessionEvent (type, SessionEvent::Replace, frame, target, 0);
106 SessionEventManager::clear_events (SessionEvent::Type type)
108 SessionEvent* ev = new SessionEvent (type, SessionEvent::Clear, 0, 0, 0);
114 SessionEventManager::dump_events () const
116 cerr << "EVENT DUMP" << endl;
117 for (Events::const_iterator i = events.begin(); i != events.end(); ++i) {
118 cerr << "\tat " << (*i)->action_frame << ' ' << (*i)->type << " target = " << (*i)->target_frame << endl;
120 cerr << "Next event: ";
122 if ((Events::const_iterator) next_event == events.end()) {
123 cerr << "none" << endl;
125 cerr << "at " << (*next_event)->action_frame << ' '
126 << (*next_event)->type << " target = "
127 << (*next_event)->target_frame << endl;
129 cerr << "Immediate events pending:\n";
130 for (Events::const_iterator i = immediate_events.begin(); i != immediate_events.end(); ++i) {
131 cerr << "\tat " << (*i)->action_frame << ' ' << (*i)->type << " target = " << (*i)->target_frame << endl;
133 cerr << "END EVENT_DUMP" << endl;
137 SessionEventManager::merge_event (SessionEvent* ev)
139 switch (ev->action) {
140 case SessionEvent::Remove:
145 case SessionEvent::Replace:
149 case SessionEvent::Clear:
150 _clear_event_type (ev->type);
154 case SessionEvent::Add:
158 /* try to handle immediate events right here */
160 if (ev->action_frame == 0) {
166 case SessionEvent::AutoLoop:
167 case SessionEvent::StopOnce:
168 _clear_event_type (ev->type);
172 for (Events::iterator i = events.begin(); i != events.end(); ++i) {
173 if ((*i)->type == ev->type && (*i)->action_frame == ev->action_frame) {
174 error << string_compose(_("Session: cannot have two events of type %1 at the same frame (%2)."),
175 enum_2_string (ev->type), ev->action_frame) << endmsg;
181 events.insert (events.begin(), ev);
182 events.sort (SessionEvent::compare);
183 next_event = events.begin();
187 /** @return true when @a ev is deleted. */
189 SessionEventManager::_replace_event (SessionEvent* ev)
194 /* private, used only for events that can only exist once in the queue */
196 for (i = events.begin(); i != events.end(); ++i) {
197 if ((*i)->type == ev->type) {
198 (*i)->action_frame = ev->action_frame;
199 (*i)->target_frame = ev->target_frame;
208 if (i == events.end()) {
209 events.insert (events.begin(), ev);
212 events.sort (SessionEvent::compare);
213 next_event = events.end();
219 /** @return true when @a ev is deleted. */
221 SessionEventManager::_remove_event (SessionEvent* ev)
226 for (i = events.begin(); i != events.end(); ++i) {
227 if ((*i)->type == ev->type && (*i)->action_frame == ev->action_frame) {
233 if (i == next_event) {
241 if (i != events.end()) {
249 SessionEventManager::_clear_event_type (SessionEvent::Type type)
251 Events::iterator i, tmp;
253 for (i = events.begin(); i != events.end(); ) {
258 if ((*i)->type == type) {
260 if (i == next_event) {
269 for (i = immediate_events.begin(); i != immediate_events.end(); ) {
274 if ((*i)->type == type) {
276 immediate_events.erase (i);