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 "pbd/pthread_utils.h"
23 #include "midi++/manager.h"
24 #include "midi++/port.h"
26 #include "ardour/debug.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/midi_ui.h"
29 #include "ardour/session.h"
30 #include "ardour/session_event.h"
31 #include "ardour/types.h"
34 using namespace ARDOUR;
39 BaseUI::RequestType MidiControlUI::PortChange = BaseUI::new_request_type();
41 #include "pbd/abstract_ui.cc" /* instantiate the template */
43 MidiControlUI::MidiControlUI (Session& s)
44 : AbstractUI<MidiUIRequest> (_("midiui"))
47 MIDI::Manager::instance()->PortsChanged.connect (sigc::mem_fun (*this, &MidiControlUI::change_midi_ports));
50 MidiControlUI::~MidiControlUI ()
56 MidiControlUI::do_request (MidiUIRequest* req)
58 if (req->type == PortChange) {
60 /* restart event loop with new ports */
61 DEBUG_TRACE (DEBUG::MidiIO, "reset ports\n");
64 } else if (req->type == CallSlot) {
68 } else if (req->type == Quit) {
75 MidiControlUI::change_midi_ports ()
77 MidiUIRequest* req = get_request (PortChange);
85 MidiControlUI::midi_input_handler (IOCondition ioc, MIDI::Port* port)
93 if (port->must_drain_selectable()) {
94 CrossThreadChannel::drain (port->selectable());
97 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", port->name()));
98 nframes64_t now = _session.engine().frame_time();
106 MidiControlUI::clear_ports ()
108 for (PortSources::iterator i = port_sources.begin(); i != port_sources.end(); ++i) {
109 g_source_destroy (*i);
113 port_sources.clear ();
117 MidiControlUI::reset_ports ()
121 MIDI::Manager::PortList plist = MIDI::Manager::instance()->get_midi_ports ();
123 for (MIDI::Manager::PortList::iterator i = plist.begin(); i != plist.end(); ++i) {
125 if ((fd = (*i)->selectable ()) >= 0) {
126 Glib::RefPtr<IOSource> psrc = IOSource::create (fd, IO_IN|IO_HUP|IO_ERR);
128 psrc->connect (sigc::bind (sigc::mem_fun (*this, &MidiControlUI::midi_input_handler), (*i)));
129 psrc->attach (_main_loop->get_context());
131 // glibmm hack: for now, store only the GSource*
133 port_sources.push_back (psrc->gobj());
134 g_source_ref (psrc->gobj());
140 MidiControlUI::thread_init ()
142 struct sched_param rtparam;
144 PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("MIDI"), 2048);
145 SessionEvent::create_per_thread_pool (X_("MIDI I/O"), 128);
147 memset (&rtparam, 0, sizeof (rtparam));
148 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
150 if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
151 // do we care? not particularly.