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) {
128 cerr << "\tat " << (*i)->action_frame << ' ' << (*i)->type << " target = " << (*i)->target_frame << endl;
130 cerr << "Next event: ";
132 if ((Events::const_iterator) next_event == events.end()) {
133 cerr << "none" << endl;
135 cerr << "at " << (*next_event)->action_frame << ' '
136 << (*next_event)->type << " target = "
137 << (*next_event)->target_frame << endl;
139 cerr << "Immediate events pending:\n";
140 for (Events::const_iterator i = immediate_events.begin(); i != immediate_events.end(); ++i) {
141 cerr << "\tat " << (*i)->action_frame << ' ' << (*i)->type << " target = " << (*i)->target_frame << endl;
143 cerr << "END EVENT_DUMP" << endl;
147 SessionEventManager::merge_event (SessionEvent* ev)
149 switch (ev->action) {
150 case SessionEvent::Remove:
155 case SessionEvent::Replace:
159 case SessionEvent::Clear:
160 _clear_event_type (ev->type);
164 case SessionEvent::Add:
168 /* try to handle immediate events right here */
170 if (ev->action_frame == 0) {
176 case SessionEvent::AutoLoop:
177 case SessionEvent::AutoLoopDeclick:
178 case SessionEvent::StopOnce:
179 _clear_event_type (ev->type);
183 for (Events::iterator i = events.begin(); i != events.end(); ++i) {
184 if ((*i)->type == ev->type && (*i)->action_frame == ev->action_frame) {
185 error << string_compose(_("Session: cannot have two events of type %1 at the same frame (%2)."),
186 enum_2_string (ev->type), ev->action_frame) << endmsg;
192 events.insert (events.begin(), ev);
193 events.sort (SessionEvent::compare);
194 next_event = events.begin();
198 /** @return true when @a ev is deleted. */
200 SessionEventManager::_replace_event (SessionEvent* ev)
205 /* private, used only for events that can only exist once in the queue */
207 for (i = events.begin(); i != events.end(); ++i) {
208 if ((*i)->type == ev->type) {
209 (*i)->action_frame = ev->action_frame;
210 (*i)->target_frame = ev->target_frame;
219 if (i == events.end()) {
220 events.insert (events.begin(), ev);
223 events.sort (SessionEvent::compare);
224 next_event = events.end();
230 /** @return true when @a ev is deleted. */
232 SessionEventManager::_remove_event (SessionEvent* ev)
237 for (i = events.begin(); i != events.end(); ++i) {
238 if ((*i)->type == ev->type && (*i)->action_frame == ev->action_frame) {
244 if (i == next_event) {
252 if (i != events.end()) {
260 SessionEventManager::_clear_event_type (SessionEvent::Type type)
262 Events::iterator i, tmp;
264 for (i = events.begin(); i != events.end(); ) {
269 if ((*i)->type == type) {
271 if (i == next_event) {
280 for (i = immediate_events.begin(); i != immediate_events.end(); ) {
285 if ((*i)->type == type) {
287 immediate_events.erase (i);