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"
26 #include "pbd/pthread_utils.h"
28 #include "ardour/debug.h"
29 #include "ardour/session_event.h"
34 using namespace ARDOUR;
37 PerThreadPool* SessionEvent::pool;
40 SessionEvent::init_event_pool ()
42 pool = new PerThreadPool;
46 SessionEvent::create_per_thread_pool (const std::string& name, uint32_t nitems)
48 /* this is a per-thread call that simply creates a thread-private ptr to
49 a CrossThreadPool for use by this thread whenever events are allocated/released
50 from SessionEvent::pool()
52 pool->create_per_thread_pool (name, sizeof (SessionEvent), nitems);
56 SessionEvent::operator new (size_t)
58 CrossThreadPool* p = pool->per_thread_pool ();
59 SessionEvent* ev = static_cast<SessionEvent*> (p->alloc ());
60 DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("%1 Allocating SessionEvent from %2 ev @ %3\n", pthread_name(), p->name(), ev));
62 if (DEBUG::SessionEvents & PBD::debug_bits) {
63 stacktrace (cerr, 40);
71 SessionEvent::operator delete (void *ptr, size_t /*size*/)
73 Pool* p = pool->per_thread_pool ();
74 SessionEvent* ev = static_cast<SessionEvent*> (ptr);
76 DEBUG_TRACE (DEBUG::SessionEvents, string_compose (
77 "%1 Deleting SessionEvent @ %2 ev thread pool = %3 ev pool = %4\n",
78 pthread_name(), ev, p->name(), ev->own_pool->name()
82 if (DEBUG::SessionEvents & PBD::debug_bits) {
83 stacktrace (cerr, 40);
87 if (p == ev->own_pool) {
90 ev->own_pool->push (ev);
95 SessionEventManager::add_event (framepos_t frame, SessionEvent::Type type, framepos_t target_frame)
97 SessionEvent* ev = new SessionEvent (type, SessionEvent::Add, frame, target_frame, 0);
102 SessionEventManager::remove_event (framepos_t frame, SessionEvent::Type type)
104 SessionEvent* ev = new SessionEvent (type, SessionEvent::Remove, frame, 0, 0);
109 SessionEventManager::replace_event (SessionEvent::Type type, framepos_t frame, framepos_t target)
111 SessionEvent* ev = new SessionEvent (type, SessionEvent::Replace, frame, target, 0);
116 SessionEventManager::clear_events (SessionEvent::Type type)
118 SessionEvent* ev = new SessionEvent (type, SessionEvent::Clear, 0, 0, 0);
124 SessionEventManager::dump_events () const
126 cerr << "EVENT DUMP" << endl;
127 for (Events::const_iterator i = events.begin(); i != events.end(); ++i) {
129 cerr << "\tat " << (*i)->action_frame << ' ' << enum_2_string ((*i)->type) << " target = " << (*i)->target_frame << endl;
131 cerr << "Next event: ";
133 if ((Events::const_iterator) next_event == events.end()) {
134 cerr << "none" << endl;
136 cerr << "at " << (*next_event)->action_frame << ' '
137 << enum_2_string ((*next_event)->type) << " target = "
138 << (*next_event)->target_frame << endl;
140 cerr << "Immediate events pending:\n";
141 for (Events::const_iterator i = immediate_events.begin(); i != immediate_events.end(); ++i) {
142 cerr << "\tat " << (*i)->action_frame << ' ' << enum_2_string((*i)->type) << " target = " << (*i)->target_frame << endl;
144 cerr << "END EVENT_DUMP" << endl;
148 SessionEventManager::merge_event (SessionEvent* ev)
150 switch (ev->action) {
151 case SessionEvent::Remove:
156 case SessionEvent::Replace:
160 case SessionEvent::Clear:
161 _clear_event_type (ev->type);
165 case SessionEvent::Add:
169 /* try to handle immediate events right here */
171 if (ev->action_frame == 0) {
177 case SessionEvent::AutoLoop:
178 case SessionEvent::AutoLoopDeclick:
179 case SessionEvent::StopOnce:
180 _clear_event_type (ev->type);
184 for (Events::iterator i = events.begin(); i != events.end(); ++i) {
185 if ((*i)->type == ev->type && (*i)->action_frame == ev->action_frame) {
186 error << string_compose(_("Session: cannot have two events of type %1 at the same frame (%2)."),
187 enum_2_string (ev->type), ev->action_frame) << endmsg;
193 events.insert (events.begin(), ev);
194 events.sort (SessionEvent::compare);
195 next_event = events.begin();
199 /** @return true when @a ev is deleted. */
201 SessionEventManager::_replace_event (SessionEvent* ev)
206 /* private, used only for events that can only exist once in the queue */
208 for (i = events.begin(); i != events.end(); ++i) {
209 if ((*i)->type == ev->type) {
210 (*i)->action_frame = ev->action_frame;
211 (*i)->target_frame = ev->target_frame;
220 if (i == events.end()) {
221 events.insert (events.begin(), ev);
224 events.sort (SessionEvent::compare);
225 next_event = events.end();
231 /** @return true when @a ev is deleted. */
233 SessionEventManager::_remove_event (SessionEvent* ev)
238 for (i = events.begin(); i != events.end(); ++i) {
239 if ((*i)->type == ev->type && (*i)->action_frame == ev->action_frame) {
245 if (i == next_event) {
248 i = events.erase (i);
253 if (i != events.end()) {
261 SessionEventManager::_clear_event_type (SessionEvent::Type type)
263 Events::iterator i, tmp;
265 for (i = events.begin(); i != events.end(); ) {
270 if ((*i)->type == type) {
272 if (i == next_event) {
281 for (i = immediate_events.begin(); i != immediate_events.end(); ) {
286 if ((*i)->type == type) {
288 immediate_events.erase (i);