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 DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("%1 Allocating SessionEvent from %2 ev @ %3\n", pthread_self(), p->name(), ev));
66 if (DEBUG::SessionEvents & PBD::debug_bits) {
67 stacktrace (cerr, 40);
75 SessionEvent::operator delete (void *ptr, size_t /*size*/)
77 Pool* p = pool->per_thread_pool ();
78 SessionEvent* ev = static_cast<SessionEvent*> (ptr);
80 DEBUG_TRACE (DEBUG::SessionEvents, string_compose (
81 "%1 Deleting SessionEvent @ %2 ev thread pool = %3 ev pool = %4\n",
82 pthread_self(), ev, p->name(), ev->own_pool->name()
86 if (DEBUG::SessionEvents & PBD::debug_bits) {
87 stacktrace (cerr, 40);
91 if (p == ev->own_pool) {
94 ev->own_pool->push (ev);
99 SessionEventManager::add_event (framepos_t frame, SessionEvent::Type type, framepos_t target_frame)
101 SessionEvent* ev = new SessionEvent (type, SessionEvent::Add, frame, target_frame, 0);
106 SessionEventManager::remove_event (framepos_t frame, SessionEvent::Type type)
108 SessionEvent* ev = new SessionEvent (type, SessionEvent::Remove, frame, 0, 0);
113 SessionEventManager::replace_event (SessionEvent::Type type, framepos_t frame, framepos_t target)
115 SessionEvent* ev = new SessionEvent (type, SessionEvent::Replace, frame, target, 0);
120 SessionEventManager::clear_events (SessionEvent::Type type)
122 SessionEvent* ev = new SessionEvent (type, SessionEvent::Clear, 0, 0, 0);
128 SessionEventManager::dump_events () const
130 cerr << "EVENT DUMP" << endl;
131 for (Events::const_iterator i = events.begin(); i != events.end(); ++i) {
132 cerr << "\tat " << (*i)->action_frame << ' ' << (*i)->type << " target = " << (*i)->target_frame << endl;
134 cerr << "Next event: ";
136 if ((Events::const_iterator) next_event == events.end()) {
137 cerr << "none" << endl;
139 cerr << "at " << (*next_event)->action_frame << ' '
140 << (*next_event)->type << " target = "
141 << (*next_event)->target_frame << endl;
143 cerr << "Immediate events pending:\n";
144 for (Events::const_iterator i = immediate_events.begin(); i != immediate_events.end(); ++i) {
145 cerr << "\tat " << (*i)->action_frame << ' ' << (*i)->type << " target = " << (*i)->target_frame << endl;
147 cerr << "END EVENT_DUMP" << endl;
151 SessionEventManager::merge_event (SessionEvent* ev)
153 switch (ev->action) {
154 case SessionEvent::Remove:
159 case SessionEvent::Replace:
163 case SessionEvent::Clear:
164 _clear_event_type (ev->type);
168 case SessionEvent::Add:
172 /* try to handle immediate events right here */
174 if (ev->action_frame == 0) {
180 case SessionEvent::AutoLoop:
181 case SessionEvent::StopOnce:
182 _clear_event_type (ev->type);
186 for (Events::iterator i = events.begin(); i != events.end(); ++i) {
187 if ((*i)->type == ev->type && (*i)->action_frame == ev->action_frame) {
188 error << string_compose(_("Session: cannot have two events of type %1 at the same frame (%2)."),
189 enum_2_string (ev->type), ev->action_frame) << endmsg;
195 events.insert (events.begin(), ev);
196 events.sort (SessionEvent::compare);
197 next_event = events.begin();
201 /** @return true when @a ev is deleted. */
203 SessionEventManager::_replace_event (SessionEvent* ev)
208 /* private, used only for events that can only exist once in the queue */
210 for (i = events.begin(); i != events.end(); ++i) {
211 if ((*i)->type == ev->type) {
212 (*i)->action_frame = ev->action_frame;
213 (*i)->target_frame = ev->target_frame;
222 if (i == events.end()) {
223 events.insert (events.begin(), ev);
226 events.sort (SessionEvent::compare);
227 next_event = events.end();
233 /** @return true when @a ev is deleted. */
235 SessionEventManager::_remove_event (SessionEvent* ev)
240 for (i = events.begin(); i != events.end(); ++i) {
241 if ((*i)->type == ev->type && (*i)->action_frame == ev->action_frame) {
247 if (i == next_event) {
255 if (i != events.end()) {
263 SessionEventManager::_clear_event_type (SessionEvent::Type type)
265 Events::iterator i, tmp;
267 for (i = events.begin(); i != events.end(); ) {
272 if ((*i)->type == type) {
274 if (i == next_event) {
283 for (i = immediate_events.begin(); i != immediate_events.end(); ) {
288 if ((*i)->type == type) {
290 immediate_events.erase (i);