2 Copyright (C) 2009 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.
21 #include <sigc++/signal.h>
23 #include "pbd/pthread_utils.h"
25 #include "ardour/async_midi_port.h"
26 #include "ardour/debug.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/midi_port.h"
29 #include "ardour/midiport_manager.h"
30 #include "ardour/midi_ui.h"
31 #include "ardour/session.h"
32 #include "ardour/session_event.h"
33 #include "ardour/types.h"
36 using namespace ARDOUR;
42 BaseUI::RequestType MidiControlUI::PortChange = BaseUI::new_request_type();
43 MidiControlUI* MidiControlUI::_instance = 0;
45 #include "pbd/abstract_ui.cc" /* instantiate the template */
47 MidiControlUI::MidiControlUI (Session& s)
48 : AbstractUI<MidiUIRequest> (X_("midiui"))
54 MidiControlUI::~MidiControlUI ()
61 MidiControlUI::do_request (MidiUIRequest* req)
63 if (req->type == PortChange) {
65 /* restart event loop with new ports */
66 DEBUG_TRACE (DEBUG::MidiIO, "reset ports\n");
69 } else if (req->type == CallSlot) {
72 if (getenv ("DEBUG_THREADED_SIGNALS")) {
73 cerr << "MIDI UI calls a slot\n";
79 } else if (req->type == Quit) {
86 MidiControlUI::midi_input_handler (IOCondition ioc, AsyncMIDIPort* port)
88 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("something happend on %1\n", ((ARDOUR::Port*)port)->name()));
96 CrossThreadChannel::drain (port->selectable());
98 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", ((ARDOUR::Port*)port)->name()));
99 framepos_t now = _session.engine().sample_time();
107 MidiControlUI::clear_ports ()
109 for (PortSources::iterator i = port_sources.begin(); i != port_sources.end(); ++i) {
110 g_source_destroy (*i);
114 port_sources.clear ();
118 MidiControlUI::reset_ports ()
120 if (port_sources.empty()) {
121 AsyncMIDIPort* async = dynamic_cast<AsyncMIDIPort*> (AudioEngine::instance()->midi_input_port());
129 if ((fd = async->selectable ()) >= 0) {
130 Glib::RefPtr<IOSource> psrc = IOSource::create (fd, IO_IN|IO_HUP|IO_ERR);
132 psrc->connect (sigc::bind (sigc::mem_fun (this, &MidiControlUI::midi_input_handler), async));
133 psrc->attach (_main_loop->get_context());
135 // glibmm hack: for now, store only the GSource*
137 port_sources.push_back (psrc->gobj());
138 g_source_ref (psrc->gobj());
144 MidiControlUI::thread_init ()
146 struct sched_param rtparam;
148 pthread_set_name (X_("midiUI"));
150 PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("MIDI"), 2048);
151 SessionEvent::create_per_thread_pool (X_("MIDI I/O"), 128);
153 memset (&rtparam, 0, sizeof (rtparam));
154 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
156 if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
157 // do we care? not particularly.