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 "pbd/error.h"
24 #include "pbd/enumwriter.h"
25 #include "pbd/stacktrace.h"
27 #include "ardour/debug.h"
28 #include "ardour/session_event.h"
33 using namespace ARDOUR;
36 PerThreadPool* SessionEvent::pool;
39 SessionEvent::init_event_pool ()
41 pool = new PerThreadPool;
45 SessionEvent::create_per_thread_pool (const std::string& name, uint32_t nitems)
47 /* this is a per-thread call that simply creates a thread-private ptr to
48 a CrossThreadPool for use by this thread whenever events are allocated/released
49 from SessionEvent::pool()
51 pool->create_per_thread_pool (name, sizeof (SessionEvent), nitems);
55 SessionEvent::operator new (size_t)
57 CrossThreadPool* p = pool->per_thread_pool ();
58 SessionEvent* ev = static_cast<SessionEvent*> (p->alloc ());
59 DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("%1 Allocating SessionEvent from %2 ev @ %3\n", pthread_self(), p->name(), ev));
61 if (DEBUG::SessionEvents & PBD::debug_bits) {
62 stacktrace (cerr, 40);
70 SessionEvent::operator delete (void *ptr, size_t /*size*/)
72 Pool* p = pool->per_thread_pool ();
73 SessionEvent* ev = static_cast<SessionEvent*> (ptr);
75 DEBUG_TRACE (DEBUG::SessionEvents, string_compose (
76 "%1 Deleting SessionEvent @ %2 ev thread pool = %3 ev pool = %4\n",
77 pthread_self(), ev, p->name(), ev->own_pool->name()
81 if (DEBUG::SessionEvents & PBD::debug_bits) {
82 stacktrace (cerr, 40);
86 if (p == ev->own_pool) {
89 ev->own_pool->push (ev);
94 SessionEventManager::add_event (framepos_t frame, SessionEvent::Type type, framepos_t target_frame)
96 SessionEvent* ev = new SessionEvent (type, SessionEvent::Add, frame, target_frame, 0);
101 SessionEventManager::remove_event (framepos_t frame, SessionEvent::Type type)
103 SessionEvent* ev = new SessionEvent (type, SessionEvent::Remove, frame, 0, 0);
108 SessionEventManager::replace_event (SessionEvent::Type type, framepos_t frame, framepos_t target)
110 SessionEvent* ev = new SessionEvent (type, SessionEvent::Replace, frame, target, 0);
115 SessionEventManager::clear_events (SessionEvent::Type type)
117 SessionEvent* ev = new SessionEvent (type, SessionEvent::Clear, 0, 0, 0);
123 SessionEventManager::dump_events () const
125 cerr << "EVENT DUMP" << endl;
126 for (Events::const_iterator i = events.begin(); i != events.end(); ++i) {
127 cerr << "\tat " << (*i)->action_frame << ' ' << (*i)->type << " target = " << (*i)->target_frame << endl;
129 cerr << "Next event: ";
131 if ((Events::const_iterator) next_event == events.end()) {
132 cerr << "none" << endl;
134 cerr << "at " << (*next_event)->action_frame << ' '
135 << (*next_event)->type << " target = "
136 << (*next_event)->target_frame << endl;
138 cerr << "Immediate events pending:\n";
139 for (Events::const_iterator i = immediate_events.begin(); i != immediate_events.end(); ++i) {
140 cerr << "\tat " << (*i)->action_frame << ' ' << (*i)->type << " target = " << (*i)->target_frame << endl;
142 cerr << "END EVENT_DUMP" << endl;
146 SessionEventManager::merge_event (SessionEvent* ev)
148 switch (ev->action) {
149 case SessionEvent::Remove:
154 case SessionEvent::Replace:
158 case SessionEvent::Clear:
159 _clear_event_type (ev->type);
163 case SessionEvent::Add:
167 /* try to handle immediate events right here */
169 if (ev->action_frame == 0) {
175 case SessionEvent::AutoLoop:
176 case SessionEvent::AutoLoopDeclick:
177 case SessionEvent::StopOnce:
178 _clear_event_type (ev->type);
182 for (Events::iterator i = events.begin(); i != events.end(); ++i) {
183 if ((*i)->type == ev->type && (*i)->action_frame == ev->action_frame) {
184 error << string_compose(_("Session: cannot have two events of type %1 at the same frame (%2)."),
185 enum_2_string (ev->type), ev->action_frame) << endmsg;
191 events.insert (events.begin(), ev);
192 events.sort (SessionEvent::compare);
193 next_event = events.begin();
197 /** @return true when @a ev is deleted. */
199 SessionEventManager::_replace_event (SessionEvent* ev)
204 /* private, used only for events that can only exist once in the queue */
206 for (i = events.begin(); i != events.end(); ++i) {
207 if ((*i)->type == ev->type) {
208 (*i)->action_frame = ev->action_frame;
209 (*i)->target_frame = ev->target_frame;
218 if (i == events.end()) {
219 events.insert (events.begin(), ev);
222 events.sort (SessionEvent::compare);
223 next_event = events.end();
229 /** @return true when @a ev is deleted. */
231 SessionEventManager::_remove_event (SessionEvent* ev)
236 for (i = events.begin(); i != events.end(); ++i) {
237 if ((*i)->type == ev->type && (*i)->action_frame == ev->action_frame) {
243 if (i == next_event) {
251 if (i != events.end()) {
259 SessionEventManager::_clear_event_type (SessionEvent::Type type)
261 Events::iterator i, tmp;
263 for (i = events.begin(); i != events.end(); ) {
268 if ((*i)->type == type) {
270 if (i == next_event) {
279 for (i = immediate_events.begin(); i != immediate_events.end(); ) {
284 if ((*i)->type == type) {
286 immediate_events.erase (i);